source: trunk/makeBat.py @ 4225

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

Revise windows batch file creation for paths with spaces

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 7.5 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 4225 2020-01-02 20:59:18Z 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
20import os, sys
21import datetime
22import wx
23
24Script = '''@echo ========================================================================
25@echo                General Structure Analysis System-II
26@echo              by Robert B. Von Dreele and Brian H. Toby
27@echo                Argonne National Laboratory(C), 2010
28@echo  This product includes software developed by the UChicago Argonne, LLC,
29@echo             as Operator of Argonne National Laboratory.
30@echo                            Please cite:
31@echo      B.H. Toby and R.B. Von Dreele, J. Appl. Cryst. 46, 544-549 (2013)
32@echo                   for small angle use also cite:
33@echo      R.B. Von Dreele, J. Appl. Cryst. 47, 1784-9 (2014)
34@echo                   for DIFFaX use also cite:
35@echo      M.M.J. Treacy, J.M. Newsam and M.W. Deem,
36@echo                   Proc. Roy. Soc. Lond. 433A, 499-520 (1991)
37@echo ========================================================================
38@
39{:s}{:s} {:s} "%~1"
40@REM To keep the window from disappearing with any error messages
41pause
42
43'''
44
45if __name__ == '__main__':
46    try:
47        import _winreg as winreg
48    except ImportError:
49        import winreg
50    app = wx.App()
51    app.MainLoop()
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    try:
96        oldgpx = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project')
97        oldopen = winreg.OpenKey(oldgpx,r'shell\open\command')
98        oldBat = winreg.QueryValue(oldopen,None).split()[0]
99        os.stat(oldBat)     #check if it is still around
100    except FileNotFoundError:
101        if oldBat:
102            print('old '+oldBat+ 'not found; registry entry will be made for new one')
103        else:
104            new = True
105    if not new:
106        try:
107            if oldBat != G2bat:
108                dlg = wx.MessageDialog(None,'gpx files already assigned in registry to: \n'+oldBat+'\n Replace with: '+G2bat+'?','GSAS-II gpx in use', 
109                        wx.YES_NO | wx.ICON_QUESTION)
110                if dlg.ShowModal() == wx.ID_YES:
111                    new = True
112                dlg.Destroy()
113        finally:
114            pass
115    if new:
116        # Associate a script and icon with .gpx files
117        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\.gpx')
118        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II.project')
119        winreg.CloseKey(gpxkey)
120        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project')
121        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II project')
122        iconkey = winreg.CreateKey(gpxkey, 'DefaultIcon')
123        winreg.SetValue(iconkey, None, winreg.REG_SZ, G2icon)
124        openkey = winreg.CreateKey(gpxkey, r'shell\open\command')
125        winreg.SetValue(openkey, None, winreg.REG_SZ, G2bat+' "%1"')
126        winreg.CloseKey(iconkey)
127        winreg.CloseKey(openkey)
128        winreg.CloseKey(gpxkey)
129        print('Assigned icon and batch file to .gpx files in registery')
130    else:
131        print('old assignment of icon and batch file in registery is retained')
132
133    try:
134        import win32com.shell.shell, win32com.shell.shellcon
135        win32com.shell.shell.SHChangeNotify(
136            win32com.shell.shellcon.SHCNE_ASSOCCHANGED, 0, None, None)
137    except ImportError:
138        print('Module pywin32 not present, login again to see file types properly')
139    except:
140        print('Unexpected error on explorer refresh. Please report:')
141        import traceback
142        print(traceback.format_exc())
143
144    # make a desktop shortcut to GSAS-II
145    try:
146        import win32com.shell.shell, win32com.shell.shellcon, win32com.client
147        desktop = win32com.shell.shell.SHGetFolderPath(
148            0, win32com.shell.shellcon.CSIDL_DESKTOP, None, 0)
149        shortbase = "GSAS-II.lnk"
150        shortcut = os.path.join(desktop, shortbase)
151        save = True
152        if win32com.shell.shell.SHGetFileInfo(shortcut,0,0)[0]:
153            print('GSAS-II shortcut exists!')
154            dlg = wx.FileDialog(None, 'Choose new GSAS-II shortcut name',  desktop, shortbase,
155                wildcard='GSAS-II shortcut (*.lnk)|*.lnk',style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
156            try:
157                if dlg.ShowModal() == wx.ID_OK:
158                    shortcut = dlg.GetPath()
159                else:
160                    save = False
161            finally:
162                dlg.Destroy()
163        if save:
164            shell = win32com.client.Dispatch('WScript.Shell')
165            shobj = shell.CreateShortCut(shortcut)
166            shobj.Targetpath = G2bat
167            #shobj.WorkingDirectory = wDir # could specify a default project location here
168            shobj.IconLocation = G2icon
169            shobj.save()
170            print('Created shortcut to start GSAS-II on desktop')
171        else:
172            print('No shortcut for this GSAS-II created on desktop')
173    except ImportError:
174        print('Module pywin32 not present, will not make desktop shortcut')
175    except:
176        print('Unexpected error making desktop shortcut. Please report:')
177        import traceback
178        print(traceback.format_exc())
179   
Note: See TracBrowser for help on using the repository browser.