source: trunk/makeBat.py @ 3929

Last change on this file since 3929 was 3929, checked in by vondreele, 2 years ago

makeBat now recognizes old GSAS-II shortcuts & allows alternative name or Cancel (no new shortcut)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 7.2 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 3929 2019-04-24 14:18:45Z vondreele $"
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
23try:
24    import _winreg as winreg
25except ImportError:
26    import winreg
27
28Script = '''@echo ========================================================================
29@echo                General Structure Analysis System-II
30@echo              by Robert B. Von Dreele and Brian H. Toby
31@echo                Argonne National Laboratory(C), 2010
32@echo  This product includes software developed by the UChicago Argonne, LLC,
33@echo             as Operator of Argonne National Laboratory.
34@echo                            Please cite:
35@echo      B.H. Toby and R.B. Von Dreele, J. Appl. Cryst. 46, 544-549 (2013)
36@echo                   for small angle use also cite:
37@echo      R.B. Von Dreele, J. Appl. Cryst. 47, 1784-9 (2014)
38@echo                   for DIFFaX use also cite:
39@echo      M.M.J. Treacy, J.M. Newsam and M.W. Deem,
40@echo                   Proc. Roy. Soc. Lond. 433A, 499-520 (1991)
41@echo ========================================================================
42@
43{:s}{:s} {:s} "%~1"
44@REM To keep the window from disappearing with any error messages
45pause
46
47'''
48
49if __name__ == '__main__':
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(gsaspath)[0],'Scripts','activate')
69    print(activate)
70    if os.path.exists(activate):
71        print('adding activate to .bat file')
72        activate = "call "+os.path.realpath(activate) + '\n'
73    else:
74        print('activate not found')
75        activate = ''
76    pexe = pythonexe
77    if ' ' in pythonexe: pexe = '"'+pythonexe+'"'
78    G2s = G2script
79    if ' ' in G2s: G2s = '"'+G2script+'"'
80    # is mingw-w64\bin present? If so add it to path.
81    #d = os.path.split(pexe)[0]
82    #mdir = os.path.join(d,'Library','mingw-w64','bin')
83    #if os.path.exists(mdir):
84    #    fp.write('@path={};%path%\n'.format(mdir))
85    fp.write(Script.format(activate,pexe,G2s))
86    fp.close()
87    print('\nCreated GSAS-II batch file RunGSASII.bat in '+gsaspath)
88   
89    new = False
90    try:
91        oldgpx = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project')
92        oldopen = winreg.OpenKey(oldgpx,r'shell\open\command')
93    except FileNotFoundError:
94        new = True
95    if not new:
96        try:
97            oldBat = winreg.QueryValue(oldopen,None).split()[0]
98            if oldBat != G2bat:
99                dlg = wx.MessageDialog(None,'gpx files already assigned in registry to: \n'+oldBat+'\n Replace with: '+G2bat+'?','GSAS-II gpx in use', 
100                        wx.YES_NO | wx.ICON_QUESTION)
101                if dlg.ShowModal() == wx.ID_YES:
102                    new = True
103                dlg.Destroy()
104        finally:
105            pass
106    if new:
107        # Associate a script and icon with .gpx files
108        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\.gpx')
109        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II.project')
110        winreg.CloseKey(gpxkey)
111        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project')
112        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II project')
113        iconkey = winreg.CreateKey(gpxkey, 'DefaultIcon')
114        winreg.SetValue(iconkey, None, winreg.REG_SZ, G2icon)
115        openkey = winreg.CreateKey(gpxkey, r'shell\open\command')
116        winreg.SetValue(openkey, None, winreg.REG_SZ, G2bat+' "%1"')
117        winreg.CloseKey(iconkey)
118        winreg.CloseKey(openkey)
119        winreg.CloseKey(gpxkey)
120        print('Assigned icon and batch file to .gpx files in registery')
121    else:
122        print('old assignment of icon and batch file in registery is retained')
123
124    try:
125        import win32com.shell.shell, win32com.shell.shellcon
126        win32com.shell.shell.SHChangeNotify(
127            win32com.shell.shellcon.SHCNE_ASSOCCHANGED, 0, None, None)
128    except ImportError:
129        print('Module pywin32 not present, login again to see file types properly')
130    except:
131        print('Unexpected error on explorer refresh. Please report:')
132        import traceback
133        print(traceback.format_exc())
134
135    # make a desktop shortcut to GSAS-II
136    try:
137        import win32com.shell.shell, win32com.shell.shellcon, win32com.client
138        desktop = win32com.shell.shell.SHGetFolderPath(
139            0, win32com.shell.shellcon.CSIDL_DESKTOP, None, 0)
140        shortbase = "GSAS-II.lnk"
141        shortcut = os.path.join(desktop, shortbase)
142        save = True
143        if win32com.shell.shell.SHGetFileInfo(shortcut,0,0)[0]:
144            print('GSAS-II shortcut exists!')
145            dlg = wx.FileDialog(None, 'Choose new GSAS-II shortcut name',  desktop, shortbase,
146                wildcard='GSAS-II shortcut (*.lnk)|*.lnk',style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
147            try:
148                if dlg.ShowModal() == wx.ID_OK:
149                    shortcut = dlg.GetPath()
150                else:
151                    save = False
152            finally:
153                dlg.Destroy()
154        if save:
155            shell = win32com.client.Dispatch('WScript.Shell')
156            shobj = shell.CreateShortCut(shortcut)
157            shobj.Targetpath = G2bat
158            #shobj.WorkingDirectory = wDir # could specify a default project location here
159            shobj.IconLocation = G2icon
160            shobj.save()
161            print('Created shortcut to start GSAS-II on desktop')
162        else:
163            print('No shortcut for this GSAS-II created on desktop')
164    except ImportError:
165        print('Module pywin32 not present, will not make desktop shortcut')
166    except:
167        print('Unexpected error making desktop shortcut. Please report:')
168        import traceback
169        print(traceback.format_exc())
170   
Note: See TracBrowser for help on using the repository browser.