source: trunk/makeBat.py @ 3927

Last change on this file since 3927 was 3927, checked in by vondreele, 3 years ago

modified makeBat to check about old .gpx registry entries
add options for rotating the selected view axis down onto plane; currently hidden from users

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 6.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 3927 2019-04-23 20:26:36Z 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 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_CLASSES_ROOT, '.gpx')
109        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\.gpx')
110        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II.project')
111        winreg.CloseKey(gpxkey)
112        gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project')
113        winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II project')
114        iconkey = winreg.CreateKey(gpxkey, 'DefaultIcon')
115        winreg.SetValue(iconkey, None, winreg.REG_SZ, G2icon)
116        openkey = winreg.CreateKey(gpxkey, r'shell\open\command')
117        winreg.SetValue(openkey, None, winreg.REG_SZ, G2bat+' "%1"')
118        winreg.CloseKey(iconkey)
119        winreg.CloseKey(openkey)
120        winreg.CloseKey(gpxkey)
121        print('Assigned icon and batch file to .gpx files')
122    else:
123        print('old assignment of icon and batch file is retained')
124
125    try:
126        import win32com.shell.shell, win32com.shell.shellcon
127        win32com.shell.shell.SHChangeNotify(
128            win32com.shell.shellcon.SHCNE_ASSOCCHANGED, 0, None, None)
129    except ImportError:
130        print('Module pywin32 not present, login again to see file types properly')
131    except:
132        print('Unexpected error on explorer refresh. Please report:')
133        import traceback
134        print(traceback.format_exc())
135
136    # make a desktop shortcut to GSAS-II
137    try:
138        import win32com.shell.shell, win32com.shell.shellcon, win32com.client
139        desktop = win32com.shell.shell.SHGetFolderPath(
140            0, win32com.shell.shellcon.CSIDL_DESKTOP, None, 0)
141        shortcut = os.path.join(desktop, "GSAS-II.lnk")
142        shell = win32com.client.Dispatch('WScript.Shell')
143        shobj = shell.CreateShortCut(shortcut)
144        shobj.Targetpath = G2bat
145        #shobj.WorkingDirectory = wDir # could specify a default project location here
146        shobj.IconLocation = G2icon
147        shobj.save()
148        print('Created shortcut to start GSAS-II on desktop')
149    except ImportError:
150        print('Module pywin32 not present, will not make desktop shortcut')
151    except:
152        print('Unexpected error making desktop shortcut. Please report:')
153        import traceback
154        print(traceback.format_exc())
155   
Note: See TracBrowser for help on using the repository browser.