source: trunk/makeBat.py @ 4213

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

multiple small changes to allow docs build under 3.x

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