blob: e49c77329b7361122df038524643199bc2b96847 [file] [log] [blame]
Jack Jansend8eb8a71995-08-09 15:16:58 +00001# This python script creates Finder aliases for all the
2# dynamically-loaded modules that "live in" in a single
3# shared library.
Jack Jansend8eb8a71995-08-09 15:16:58 +00004#
Jack Jansene3c90a41996-08-28 14:19:53 +00005# This is sort-of a merger between Jack's MkPluginAliases
6# and Guido's mkaliases.
7#
8# Jack Jansen, CWI, August 1996
Jack Jansend8eb8a71995-08-09 15:16:58 +00009
Jack Jansend8eb8a71995-08-09 15:16:58 +000010import sys
Jack Jansene3c90a41996-08-28 14:19:53 +000011import os
Jack Jansenbc128731995-09-24 21:06:50 +000012import macfs
Jack Jansend0240831997-09-08 13:16:29 +000013import MacOS
Jack Jansen09dcff71997-08-27 14:11:15 +000014verbose=0
Jack Jansend8eb8a71995-08-09 15:16:58 +000015
Jack Jansend0240831997-09-08 13:16:29 +000016SPLASH_LOCATE=512
17SPLASH_REMOVE=513
18SPLASH_CFM68K=514
19SPLASH_PPC=515
20SPLASH_NUMPY=516
Jack Jansen52b5b022000-10-13 23:33:34 +000021ALERT_NONBOOT=517
22ALERT_NONBOOT_COPY=1
23ALERT_NONBOOT_ALIAS=2
Jack Jansend0240831997-09-08 13:16:29 +000024
Jack Jansen25b361f1996-08-20 16:35:30 +000025ppc_goals = [
Jack Jansend8eb8a71995-08-09 15:16:58 +000026]
27
Jack Jansene3c90a41996-08-28 14:19:53 +000028def gotopluginfolder():
29 """Go to the plugin folder, assuming we are somewhere in the Python tree"""
30 import os
31
Just van Rossum786cb111999-01-30 17:46:34 +000032 while not os.path.isdir(":Mac:PlugIns"):
Jack Jansene3c90a41996-08-28 14:19:53 +000033 os.chdir("::")
Just van Rossum786cb111999-01-30 17:46:34 +000034 os.chdir(":Mac:PlugIns")
Jack Jansen09dcff71997-08-27 14:11:15 +000035 if verbose: print "current directory is", os.getcwd()
Jack Jansene3c90a41996-08-28 14:19:53 +000036
37def loadtoolboxmodules():
38 """Attempt to load the Res module"""
39 try:
40 import Res
41 except ImportError, arg:
42 err1 = arg
43 pass
44 else:
Jack Jansen09dcff71997-08-27 14:11:15 +000045 if verbose: print 'imported Res the standard way.'
Jack Jansene3c90a41996-08-28 14:19:53 +000046 return
47
Jack Jansen3112bc12001-02-14 17:04:51 +000048 # We cannot import it. Try a manual load
Jack Jansene3c90a41996-08-28 14:19:53 +000049 try:
50 dummy = imp.load_dynamic('Res', 'toolboxmodules.ppc.slb')
51 except ImportError, arg:
52 err3 = arg
53 pass
54 else:
Jack Jansen09dcff71997-08-27 14:11:15 +000055 if verbose: print 'Loaded Res from toolboxmodules.ppc.slb.'
Jack Jansene3c90a41996-08-28 14:19:53 +000056 return
57
58 # Tough luck....
59 print "I cannot import the Res module, nor load it from either of"
60 print "toolboxmodules shared libraries. The errors encountered were:"
61 print "import Res:", err1
Jack Jansene3c90a41996-08-28 14:19:53 +000062 print "load from toolboxmodules.ppc.slb:", err3
63 sys.exit(1)
64
Jack Jansend09deac1996-10-22 15:32:06 +000065def getextensiondirfile(fname):
66 import macfs
67 import MACFS
68 vrefnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk, MACFS.kExtensionFolderType, 0)
69 fss = macfs.FSSpec((vrefnum, dirid, fname))
70 return fss.as_pathname()
71
72def mkcorealias(src, altsrc):
73 import string
74 import macostools
75 version = string.split(sys.version)[0]
76 dst = getextensiondirfile(src+ ' ' + version)
Just van Rossum786cb111999-01-30 17:46:34 +000077 if not os.path.exists(os.path.join(sys.exec_prefix, src)):
78 if not os.path.exists(os.path.join(sys.exec_prefix, altsrc)):
Jack Jansen09dcff71997-08-27 14:11:15 +000079 if verbose: print '*', src, 'not found'
Jack Jansend09deac1996-10-22 15:32:06 +000080 return 0
81 src = altsrc
82 try:
83 os.unlink(dst)
84 except os.error:
85 pass
Jack Jansen52b5b022000-10-13 23:33:34 +000086 do_copy = 0
87 if macfs.FSSpec(sys.exec_prefix).as_tuple()[0] != -1: # XXXX
88 try:
89 import Dlg
90 rv = Dlg.CautionAlert(ALERT_NONBOOT, None)
91 if rv == ALERT_NONBOOT_COPY:
92 do_copy = 1
93 except ImportError:
94 pass
95 if do_copy:
96 macostools.copy(os.path.join(sys.exec_prefix, src), dst)
97 else:
98 macostools.mkalias(os.path.join(sys.exec_prefix, src), dst)
Jack Jansen09dcff71997-08-27 14:11:15 +000099 if verbose: print ' ', dst, '->', src
Jack Jansend09deac1996-10-22 15:32:06 +0000100 return 1
101
Jack Jansend8eb8a71995-08-09 15:16:58 +0000102
103def main():
Jack Jansend0240831997-09-08 13:16:29 +0000104 MacOS.splash(SPLASH_LOCATE)
Jack Jansene3c90a41996-08-28 14:19:53 +0000105 gotopluginfolder()
Jack Jansend8eb8a71995-08-09 15:16:58 +0000106
Jack Jansene3c90a41996-08-28 14:19:53 +0000107 loadtoolboxmodules()
108
Jack Jansenbb748621997-10-10 15:49:36 +0000109 sys.path.append('::Mac:Lib')
Jack Jansene3c90a41996-08-28 14:19:53 +0000110 import macostools
111
Jack Jansend8eb8a71995-08-09 15:16:58 +0000112 # Remove old .slb aliases and collect a list of .slb files
Jack Jansend0240831997-09-08 13:16:29 +0000113 didsplash = 0
Jack Jansend8eb8a71995-08-09 15:16:58 +0000114 LibFiles = []
115 allfiles = os.listdir(':')
Jack Jansen09dcff71997-08-27 14:11:15 +0000116 if verbose: print 'Removing old aliases...'
Jack Jansend8eb8a71995-08-09 15:16:58 +0000117 for f in allfiles:
118 if f[-4:] == '.slb':
119 finfo = macfs.FSSpec(f).GetFInfo()
120 if finfo.Flags & 0x8000:
Jack Jansend0240831997-09-08 13:16:29 +0000121 if not didsplash:
122 MacOS.splash(SPLASH_REMOVE)
123 didsplash = 1
Jack Jansen09dcff71997-08-27 14:11:15 +0000124 if verbose: print ' Removing', f
Jack Jansend8eb8a71995-08-09 15:16:58 +0000125 os.unlink(f)
126 else:
127 LibFiles.append(f)
Jack Jansen09dcff71997-08-27 14:11:15 +0000128 if verbose: print ' Found', f
129 if verbose: print
Jack Jansend8eb8a71995-08-09 15:16:58 +0000130
Jack Jansene3c90a41996-08-28 14:19:53 +0000131 # Create the new PPC aliases.
Jack Jansend0240831997-09-08 13:16:29 +0000132 didsplash = 0
Jack Jansen09dcff71997-08-27 14:11:15 +0000133 if verbose: print 'Creating PPC aliases...'
Jack Jansene3c90a41996-08-28 14:19:53 +0000134 for dst, src in ppc_goals:
135 if src in LibFiles:
Jack Jansend0240831997-09-08 13:16:29 +0000136 if not didsplash:
137 MacOS.splash(SPLASH_PPC)
138 didsplash = 1
Jack Jansene3c90a41996-08-28 14:19:53 +0000139 macostools.mkalias(src, dst)
Jack Jansen09dcff71997-08-27 14:11:15 +0000140 if verbose: print ' ', dst, '->', src
Jack Jansene3c90a41996-08-28 14:19:53 +0000141 else:
Jack Jansen09dcff71997-08-27 14:11:15 +0000142 if verbose: print '*', dst, 'not created:', src, 'not found'
143 if verbose: print
Jack Jansen3112bc12001-02-14 17:04:51 +0000144
Jack Jansend09deac1996-10-22 15:32:06 +0000145 # Create the PythonCore alias(es)
Jack Jansen09dcff71997-08-27 14:11:15 +0000146 if verbose: print 'Creating PythonCore aliases in Extensions folder...'
Jack Jansend09deac1996-10-22 15:32:06 +0000147 os.chdir('::')
148 n = 0
Jack Jansen78c3cc41997-08-19 13:58:57 +0000149 n = n + mkcorealias('PythonCore', 'PythonCore')
Jack Jansen3112bc12001-02-14 17:04:51 +0000150 n = n + mkcorealias('PythonCoreCarbon', 'PythonCoreCarbon')
Jack Jansend0240831997-09-08 13:16:29 +0000151
Jack Jansen09dcff71997-08-27 14:11:15 +0000152 if verbose and n == 0:
Jack Jansend09deac1996-10-22 15:32:06 +0000153 sys.exit(1)
Jack Jansend8eb8a71995-08-09 15:16:58 +0000154
155if __name__ == '__main__':
Jack Jansen09dcff71997-08-27 14:11:15 +0000156 if len(sys.argv) > 1 and sys.argv[1] == '-v':
157 verbose = 1
Jack Jansend8eb8a71995-08-09 15:16:58 +0000158 main()