1 | ''' |
---|
2 | *makeBat: Create GSAS-II Batch File* |
---|
3 | ==================================== |
---|
4 | |
---|
5 | This script creates a file named ``RunGSASII.bat`` and a desktop shortcut to that file. |
---|
6 | It registers the filetype .gpx so that the GSAS-II project files exhibit the |
---|
7 | GSAS-II icon and so that double-clicking on them opens them in GSAS-II. |
---|
8 | |
---|
9 | Run this script with no arguments; the path to the ``GSASII.py`` file |
---|
10 | is assumed to be the the same as the path to the ``makeBat.py`` file |
---|
11 | and the path to Python is determined from the version of Python |
---|
12 | used to run this script. |
---|
13 | |
---|
14 | ''' |
---|
15 | # creates Windows files to aid in running GSAS-II |
---|
16 | # creates RunGSASII.bat and a desktop shortcut to that file |
---|
17 | # registers the filetype .gpx so that the GSAS-II project files exhibit the |
---|
18 | # GSAS-II icon and so that double-clicking on them opens them in GSAS-II |
---|
19 | import os, sys |
---|
20 | import datetime |
---|
21 | try: |
---|
22 | import _winreg as winreg |
---|
23 | except ImportError: |
---|
24 | import winreg |
---|
25 | |
---|
26 | Script = '''@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 |
---|
43 | pause |
---|
44 | |
---|
45 | ''' |
---|
46 | |
---|
47 | if __name__ == '__main__': |
---|
48 | gsaspath = os.path.split(sys.argv[0])[0] |
---|
49 | if not gsaspath: gsaspath = os.path.curdir |
---|
50 | gsaspath = os.path.abspath(os.path.expanduser(gsaspath)) |
---|
51 | G2script = os.path.join(gsaspath,'GSASII.py') |
---|
52 | G2bat = os.path.join(gsaspath,'RunGSASII.bat') |
---|
53 | G2icon = os.path.join(gsaspath,'gsas2.ico') |
---|
54 | pythonexe = os.path.realpath(sys.executable) |
---|
55 | # Bob reports a problem using pythonw.exe w/Canopy on Windows, so change that if used |
---|
56 | if pythonexe.lower().endswith('pythonw.exe'): |
---|
57 | print(" using python.exe rather than "+pythonexe) |
---|
58 | pythonexe = os.path.join(os.path.split(pythonexe)[0],'python.exe') |
---|
59 | print(" now pythonexe="+pythonexe) |
---|
60 | # create a GSAS-II script |
---|
61 | fp = open(os.path.join(G2bat),'w') |
---|
62 | fp.write("@REM created by run of bootstrap.py on {:%d %b %Y %H:%M}\n".format( |
---|
63 | datetime.datetime.now())) |
---|
64 | activate = "call "+os.path.join(os.path.split(gsas2path)[0],'Scripts','activate') |
---|
65 | print(activate) |
---|
66 | if os.path.exists(activate): |
---|
67 | print('adding activate to .bat file') |
---|
68 | activate = os.path.realpath(activate) + '\n' |
---|
69 | else: |
---|
70 | print('activate not found') |
---|
71 | activate = '' |
---|
72 | pexe = pythonexe |
---|
73 | if ' ' in pythonexe: pexe = '"'+pythonexe+'"' |
---|
74 | G2s = G2script |
---|
75 | if ' ' in G2s: G2s = '"'+G2script+'"' |
---|
76 | # is mingw-w64\bin present? If so add it to path. |
---|
77 | #d = os.path.split(pexe)[0] |
---|
78 | #mdir = os.path.join(d,'Library','mingw-w64','bin') |
---|
79 | #if os.path.exists(mdir): |
---|
80 | # fp.write('@path={};%path%\n'.format(mdir)) |
---|
81 | fp.write(Script.format(activate,pexe,G2s)) |
---|
82 | fp.close() |
---|
83 | print('\nCreated GSAS-II batch file RunGSASII.bat in '+gsaspath) |
---|
84 | |
---|
85 | # Associate a script and icon with .gpx files |
---|
86 | #gpxkey = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, '.gpx') |
---|
87 | gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\.gpx') |
---|
88 | winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II.project') |
---|
89 | winreg.CloseKey(gpxkey) |
---|
90 | gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project') |
---|
91 | winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II project') |
---|
92 | iconkey = winreg.CreateKey(gpxkey, 'DefaultIcon') |
---|
93 | winreg.SetValue(iconkey, None, winreg.REG_SZ, G2icon) |
---|
94 | openkey = winreg.CreateKey(gpxkey, r'shell\open\command') |
---|
95 | winreg.SetValue(openkey, None, winreg.REG_SZ, G2bat+' "%1"') |
---|
96 | winreg.CloseKey(iconkey) |
---|
97 | winreg.CloseKey(openkey) |
---|
98 | winreg.CloseKey(gpxkey) |
---|
99 | print('Assigned icon and batch file to .gpx files') |
---|
100 | |
---|
101 | try: |
---|
102 | import win32com.shell.shell, win32com.shell.shellcon |
---|
103 | win32com.shell.shell.SHChangeNotify( |
---|
104 | win32com.shell.shellcon.SHCNE_ASSOCCHANGED, 0, None, None) |
---|
105 | except ImportError: |
---|
106 | print('Module pywin32 not present, login again to see file types properly') |
---|
107 | except: |
---|
108 | print('Unexpected error on explorer refresh. Please report:') |
---|
109 | import traceback |
---|
110 | print(traceback.format_exc()) |
---|
111 | |
---|
112 | # make a desktop shortcut to GSAS-II |
---|
113 | try: |
---|
114 | import win32com.shell.shell, win32com.shell.shellcon, win32com.client |
---|
115 | desktop = win32com.shell.shell.SHGetFolderPath( |
---|
116 | 0, win32com.shell.shellcon.CSIDL_DESKTOP, None, 0) |
---|
117 | shortcut = os.path.join(desktop, "GSAS-II.lnk") |
---|
118 | shell = win32com.client.Dispatch('WScript.Shell') |
---|
119 | shobj = shell.CreateShortCut(shortcut) |
---|
120 | shobj.Targetpath = G2bat |
---|
121 | #shobj.WorkingDirectory = wDir # could specify a default project location here |
---|
122 | shobj.IconLocation = G2icon |
---|
123 | shobj.save() |
---|
124 | print('Created shortcut to start GSAS-II on desktop') |
---|
125 | except ImportError: |
---|
126 | print('Module pywin32 not present, will not make desktop shortcut') |
---|
127 | except: |
---|
128 | print('Unexpected error making desktop shortcut. Please report:') |
---|
129 | import traceback |
---|
130 | print(traceback.format_exc()) |
---|
131 | |
---|