source: trunk/makeBat.py @ 4230

Last change on this file since 4230 was 4230, checked in by toby, 22 months ago

more work on gsas2full build

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 7.8 KB
Line 
1'''
2*makeBat: Create GSAS-II Batch File*
3====================================
4
5This script creates a file named ``RunGSASII.bat`` and a desktop shortcut to that file.
6It registers the filetype .gpx so that the GSAS-II project files exhibit the
7GSAS-II icon and so that double-clicking on them opens them in GSAS-II.
8
9Run this script with no arguments; the path to the ``GSASII.py`` file
10is assumed to be the the same as the path to the ``makeBat.py`` file
11and the path to Python is determined from the version of Python
12used to run this script.
13
14'''
15version = "$Id: makeBat.py 4230 2020-01-03 18:19:24Z toby $"
16# creates Windows files to aid in running GSAS-II
17#   creates RunGSASII.bat and a desktop shortcut to that file
18#   registers the filetype .gpx so that the GSAS-II project files exhibit the
19#     GSAS-II icon and so that double-clicking on them opens them in GSAS-II
20#
21import os, sys
22import datetime
23import wx
24
25Script = '''@echo ========================================================================
26@echo                General Structure Analysis System-II
27@echo              by Robert B. Von Dreele and Brian H. Toby
28@echo                Argonne National Laboratory(C), 2010
29@echo  This product includes software developed by the UChicago Argonne, LLC,
30@echo             as Operator of Argonne National Laboratory.
31@echo                            Please cite:
32@echo      B.H. Toby and R.B. Von Dreele, J. Appl. Cryst. 46, 544-549 (2013)
33@echo                   for small angle use also cite:
34@echo      R.B. Von Dreele, J. Appl. Cryst. 47, 1784-9 (2014)
35@echo                   for DIFFaX use also cite:
36@echo      M.M.J. Treacy, J.M. Newsam and M.W. Deem,
37@echo                   Proc. Roy. Soc. Lond. 433A, 499-520 (1991)
38@echo ========================================================================
39@
40{:s}{:s} {:s} "%~1"
41@REM To keep the window from disappearing with any error messages
42pause
43
44'''
45
46if __name__ == '__main__':
47    try:
48        import _winreg as winreg
49    except ImportError:
50        import winreg
51    app = None # delay starting wx until we need it. Likely not needed.
52    gsaspath = os.path.split(sys.argv[0])[0]
53    if not gsaspath: gsaspath = os.path.curdir
54    gsaspath = os.path.abspath(os.path.expanduser(gsaspath))
55    G2script = os.path.join(gsaspath,'GSASII.py')
56    G2bat = os.path.join(gsaspath,'RunGSASII.bat')
57    G2icon = os.path.join(gsaspath,'gsas2.ico')
58    pythonexe = os.path.realpath(sys.executable)
59    # Bob reports a problem using pythonw.exe w/Canopy on Windows, so change that if used
60    if pythonexe.lower().endswith('pythonw.exe'):
61        print("  using python.exe rather than "+pythonexe)
62        pythonexe = os.path.join(os.path.split(pythonexe)[0],'python.exe')
63        print("  now pythonexe="+pythonexe)
64    # create a GSAS-II script
65    fp = open(os.path.join(G2bat),'w')
66    fp.write("@REM created by run of bootstrap.py on {:%d %b %Y %H:%M}\n".format(
67        datetime.datetime.now()))
68    activate = os.path.join(os.path.split(pythonexe)[0],'Scripts','activate')
69    print("Looking for",activate)
70    if os.path.exists(activate):
71        activate = os.path.realpath(activate)
72        if ' ' in activate:
73            activate = 'call "'+ activate + '"\n'
74        else:
75            activate = 'call '+ activate + '\n'
76        print('adding activate to .bat file ({})'.format(activate))
77    else:
78        print('Anaconda activate not found')
79        activate = ''
80    pexe = pythonexe
81    if ' ' in pythonexe: pexe = '"'+pythonexe+'"'
82    G2s = G2script
83    if ' ' in G2s: G2s = '"'+G2script+'"'
84    # is mingw-w64\bin present? If so add it to path.
85    #d = os.path.split(pexe)[0]
86    #mdir = os.path.join(d,'Library','mingw-w64','bin')
87    #if os.path.exists(mdir):
88    #    fp.write('@path={};%path%\n'.format(mdir))
89    fp.write(Script.format(activate,pexe,G2s))
90    fp.close()
91    print('\nCreated GSAS-II batch file RunGSASII.bat in '+gsaspath)
92   
93    new = False
94    oldBat = ''
95    # this code does not appear to work properly when paths have spaces
96    try:
97        oldgpx = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project')
98        oldopen = winreg.OpenKey(oldgpx,r'shell\open\command')
99        oldBat = winreg.QueryValue(oldopen,None).split()[0]
100        os.stat(oldBat)     #check if it is still around
101    except FileNotFoundError:
102        if oldBat:
103            print('old '+oldBat+ 'not found; registry entry will be made for new one')
104        else:
105            new = True
106    if not new:
107        try:
108            if oldBat != G2bat:
109                if app is None:
110                    app = wx.App()
111                    app.MainLoop()
112                dlg = wx.MessageDialog(None,'gpx files already assigned in registry to: \n'+oldBat+'\n Replace with: '+G2bat+'?','GSAS-II gpx in use', 
113                        wx.YES_NO | wx.ICON_QUESTION)
114                if dlg.ShowModal() == wx.ID_YES:
115                    new = True
116                dlg.Destroy()
117        finally:
118            pass
119    if new:
120        # Associate a script and icon with .gpx files
121        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\.gpx')
122        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II.project')
123        winreg.CloseKey(gpxkey)
124        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project')
125        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II project')
126        iconkey = winreg.CreateKey(gpxkey, 'DefaultIcon')
127        winreg.SetValue(iconkey, None, winreg.REG_SZ, G2icon)
128        openkey = winreg.CreateKey(gpxkey, r'shell\open\command')
129        winreg.SetValue(openkey, None, winreg.REG_SZ, G2bat+' "%1"')
130        winreg.CloseKey(iconkey)
131        winreg.CloseKey(openkey)
132        winreg.CloseKey(gpxkey)
133        print('Assigned icon and batch file to .gpx files in registery')
134    else:
135        print('old assignment of icon and batch file in registery is retained')
136
137    try:
138        import win32com.shell.shell, win32com.shell.shellcon
139        win32com.shell.shell.SHChangeNotify(
140            win32com.shell.shellcon.SHCNE_ASSOCCHANGED, 0, None, None)
141    except ImportError:
142        print('Module pywin32 not present, login again to see file types properly')
143    except:
144        print('Unexpected error on explorer refresh. Please report:')
145        import traceback
146        print(traceback.format_exc())
147
148    # make a desktop shortcut to GSAS-II
149    try:
150        import win32com.shell.shell, win32com.shell.shellcon, win32com.client
151        desktop = win32com.shell.shell.SHGetFolderPath(
152            0, win32com.shell.shellcon.CSIDL_DESKTOP, None, 0)
153        shortbase = "GSAS-II.lnk"
154        shortcut = os.path.join(desktop, shortbase)
155        save = True
156        if win32com.shell.shell.SHGetFileInfo(shortcut,0,0)[0]:
157            print('GSAS-II shortcut exists!')
158            if app is None:
159                app = wx.App()
160                app.MainLoop()
161            dlg = wx.FileDialog(None, 'Choose new GSAS-II shortcut name',  desktop, shortbase,
162                wildcard='GSAS-II shortcut (*.lnk)|*.lnk',style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
163            try:
164                if dlg.ShowModal() == wx.ID_OK:
165                    shortcut = dlg.GetPath()
166                else:
167                    save = False
168            finally:
169                dlg.Destroy()
170        if save:
171            shell = win32com.client.Dispatch('WScript.Shell')
172            shobj = shell.CreateShortCut(shortcut)
173            shobj.Targetpath = G2bat
174            #shobj.WorkingDirectory = wDir # could specify a default project location here
175            shobj.IconLocation = G2icon
176            shobj.save()
177            print('Created shortcut to start GSAS-II on desktop')
178        else:
179            print('No shortcut for this GSAS-II created on desktop')
180    except ImportError:
181        print('Module pywin32 not present, will not make desktop shortcut')
182    except:
183        print('Unexpected error making desktop shortcut. Please report:')
184        import traceback
185        print(traceback.format_exc())
186   
Note: See TracBrowser for help on using the repository browser.