source: trunk/makeBat.py @ 3939

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

check if old GSASII.bat file in registry exists; if not change registry to new one
fix crash on opening plot window after refinement

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 7.4 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 3939 2019-04-30 13:21:22Z 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(pythonexe)[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        oldBat = winreg.QueryValue(oldopen,None).split()[0]
94        os.stat(oldBat)     #check if it is still around
95    except FileNotFoundError:
96        if oldBat:
97            print('old '+oldBat+ 'not found; registry entry will be made for new one')
98        else:
99            new = True
100    if not new:
101        try:
102            if oldBat != G2bat:
103                dlg = wx.MessageDialog(None,'gpx files already assigned in registry to: \n'+oldBat+'\n Replace with: '+G2bat+'?','GSAS-II gpx in use', 
104                        wx.YES_NO | wx.ICON_QUESTION)
105                if dlg.ShowModal() == wx.ID_YES:
106                    new = True
107                dlg.Destroy()
108        finally:
109            pass
110    if new:
111        # Associate a script and icon with .gpx files
112        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\.gpx')
113        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II.project')
114        winreg.CloseKey(gpxkey)
115        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project')
116        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II project')
117        iconkey = winreg.CreateKey(gpxkey, 'DefaultIcon')
118        winreg.SetValue(iconkey, None, winreg.REG_SZ, G2icon)
119        openkey = winreg.CreateKey(gpxkey, r'shell\open\command')
120        winreg.SetValue(openkey, None, winreg.REG_SZ, G2bat+' "%1"')
121        winreg.CloseKey(iconkey)
122        winreg.CloseKey(openkey)
123        winreg.CloseKey(gpxkey)
124        print('Assigned icon and batch file to .gpx files in registery')
125    else:
126        print('old assignment of icon and batch file in registery is retained')
127
128    try:
129        import win32com.shell.shell, win32com.shell.shellcon
130        win32com.shell.shell.SHChangeNotify(
131            win32com.shell.shellcon.SHCNE_ASSOCCHANGED, 0, None, None)
132    except ImportError:
133        print('Module pywin32 not present, login again to see file types properly')
134    except:
135        print('Unexpected error on explorer refresh. Please report:')
136        import traceback
137        print(traceback.format_exc())
138
139    # make a desktop shortcut to GSAS-II
140    try:
141        import win32com.shell.shell, win32com.shell.shellcon, win32com.client
142        desktop = win32com.shell.shell.SHGetFolderPath(
143            0, win32com.shell.shellcon.CSIDL_DESKTOP, None, 0)
144        shortbase = "GSAS-II.lnk"
145        shortcut = os.path.join(desktop, shortbase)
146        save = True
147        if win32com.shell.shell.SHGetFileInfo(shortcut,0,0)[0]:
148            print('GSAS-II shortcut exists!')
149            dlg = wx.FileDialog(None, 'Choose new GSAS-II shortcut name',  desktop, shortbase,
150                wildcard='GSAS-II shortcut (*.lnk)|*.lnk',style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
151            try:
152                if dlg.ShowModal() == wx.ID_OK:
153                    shortcut = dlg.GetPath()
154                else:
155                    save = False
156            finally:
157                dlg.Destroy()
158        if save:
159            shell = win32com.client.Dispatch('WScript.Shell')
160            shobj = shell.CreateShortCut(shortcut)
161            shobj.Targetpath = G2bat
162            #shobj.WorkingDirectory = wDir # could specify a default project location here
163            shobj.IconLocation = G2icon
164            shobj.save()
165            print('Created shortcut to start GSAS-II on desktop')
166        else:
167            print('No shortcut for this GSAS-II created on desktop')
168    except ImportError:
169        print('Module pywin32 not present, will not make desktop shortcut')
170    except:
171        print('Unexpected error making desktop shortcut. Please report:')
172        import traceback
173        print(traceback.format_exc())
174   
Note: See TracBrowser for help on using the repository browser.