source: trunk/makeBat.py

Last change on this file was 4298, checked in by toby, 20 months ago

put data items into legend, but make that optional; fix publication plot to work independent of legend status; offer publication plot with sqrt(I)

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