blob: f5ebc8e03ad15018257e63f9da3744542d7517f4 [file] [log] [blame]
Jack Jansen3050a2d1995-08-14 12:19:20 +00001#
Jack Jansen2d3f94e1996-02-14 15:58:30 +00002# binhextree - Recursively descend a directory and
Jack Jansen3050a2d1995-08-14 12:19:20 +00003# pack all resource files.
4#
5# Jack Jansen, CWI, August 1995.
6#
Jack Jansen3050a2d1995-08-14 12:19:20 +00007
8import os
9import binhex
10import sys
Jack Jansen8094f0d1995-08-31 13:47:14 +000011import macostools
12import macfs
Jack Jansen3050a2d1995-08-14 12:19:20 +000013
Jack Jansen8094f0d1995-08-31 13:47:14 +000014import aetools
15from Metrowerks_Shell_Suite import Metrowerks_Shell_Suite
16from Required_Suite import Required_Suite
17
18class MwShell(aetools.TalkTo, Metrowerks_Shell_Suite, Required_Suite):
19 pass
20
21# Top-level directory
22TOP=''
23
24# Where to put CW projects, relative to TOP
25CWDIR=':Mac:mwerks:projects'
Jack Jansenc42c0b71996-10-23 15:52:16 +000026# From which folders to put projects there
Jack Jansene3fa8741997-08-08 14:51:54 +000027CWDIRDIRS=['build.mac', 'build.macstand', 'PlugIns']
Jack Jansen8094f0d1995-08-31 13:47:14 +000028
29# Helper routines
30def binhexit(path, name):
31 dstfile = path + '.hqx'
Jack Jansen5196d001996-03-25 15:38:36 +000032 if os.path.exists(dstfile):
33 print 'Compare', path,'...',
34 if binhexcompare(path, dstfile):
35 print 'Identical, skipped.'
36 return
37 else:
38 print 'Not up-to-date.'
Jack Jansen8094f0d1995-08-31 13:47:14 +000039 print 'Binhexing', path
40 binhex.binhex(path, dstfile)
41
Jack Jansen5196d001996-03-25 15:38:36 +000042def binhexcompare(source, hqxfile):
43 """(source, hqxfile) - Check whether the two files match (forks only)"""
44 ifp = binhex.HexBin(hqxfile)
45
46 sfp = open(source, 'rb')
47 while 1:
48 d = ifp.read(128000)
49 d2 = sfp.read(128000)
50 if d <> d2:
51 return 0
52 if not d: break
53 sfp.close()
54 ifp.close_data()
55
56 d = ifp.read_rsrc(128000)
57 if d:
58 sfp = binhex.openrsrc(source, 'rb')
59 d2 = sfp.read(128000)
60 if d <> d2:
61 return 0
62 while 1:
63 d = ifp.read_rsrc(128000)
64 d2 = sfp.read(128000)
65 if d <> d2:
66 return 0
67 if not d: break
68 return 1
69
Jack Jansen8094f0d1995-08-31 13:47:14 +000070# Project files to handle
71project_files = {}
72
73def hexbincwprojects(creator):
74 """Compact and hexbin all files remembered with a given creator"""
Jack Jansene3fa8741997-08-08 14:51:54 +000075 cw_running = 0
Jack Jansen8094f0d1995-08-31 13:47:14 +000076 for fss in project_files[creator]:
77 srcfile = fss.as_pathname()
Jack Jansen9bc46901995-10-23 13:55:11 +000078
Jack Jansene3fa8741997-08-08 14:51:54 +000079 old_style = 0
Jack Jansen9bc46901995-10-23 13:55:11 +000080 if srcfile[-1] == 'µ':
81 dstfile = srcfile[:-1]+'mu.hqx'
Jack Jansene3fa8741997-08-08 14:51:54 +000082 old_style = 1
Jack Jansenc42c0b71996-10-23 15:52:16 +000083 elif srcfile[-3] == '.mu':
84 dstfile = srcfile + '.hqx'
Jack Jansen9bc46901995-10-23 13:55:11 +000085 elif ord(srcfile[-1]) >= 128:
86 dstfile = srcfile[:-1]+`ord(srcfile[-1])`+'.hqx'
87 else:
88 dstfile = srcfile + '.hqx'
89
Jack Jansen8094f0d1995-08-31 13:47:14 +000090 if os.path.exists(dstfile) and \
Jack Jansenc42c0b71996-10-23 15:52:16 +000091 os.stat(dstfile)[8] >= os.stat(srcfile)[8]:
Jack Jansen54500bb1995-09-01 11:53:17 +000092 print 'Skip', dstfile,'- Up-to-date'
Jack Jansen8094f0d1995-08-31 13:47:14 +000093 continue
94 print 'Compacting', dstfile
Jack Jansene3fa8741997-08-08 14:51:54 +000095 if old_style:
96 if not cw_running:
97 try:
98 mgr = MwShell(creator, start=1)
99 except 'foo':
100 print 'Not handled:', creator
101 return
102 cw_running = 1
103 mgr.open(fss)
104 mgr.Reset_File_Paths()
105 mgr.Remove_Binaries()
106 mgr.Close_Project()
Jack Jansen8094f0d1995-08-31 13:47:14 +0000107
108 print 'Binhexing', dstfile
109 binhex.binhex(srcfile, dstfile)
Jack Jansene3fa8741997-08-08 14:51:54 +0000110 if cw_running:
111 mgr.quit()
Jack Jansen8094f0d1995-08-31 13:47:14 +0000112
113def copycwproject(path, name):
114 """Copy CW project (if needed) and remember for hexbinning"""
115 global project_files
116
117 dstdir = os.path.join(TOP, CWDIR)
Jack Jansenc42c0b71996-10-23 15:52:16 +0000118 if path[:len(dstdir)] == dstdir:
Jack Jansen8094f0d1995-08-31 13:47:14 +0000119 return
Jack Jansenc42c0b71996-10-23 15:52:16 +0000120 srcdir = os.path.split(path)[0]
121 srcdir = os.path.split(srcdir)[1]
122 if srcdir in CWDIRDIRS:
123 if not os.path.exists(dstdir):
124 print dstdir
125 print 'No CW-project dir, skip', name
126 return
Jack Jansen297d7dd1996-11-09 18:36:00 +0000127 dstfile = os.path.join(dstdir, os.path.join(srcdir, name))
Jack Jansenc42c0b71996-10-23 15:52:16 +0000128 else:
Jack Jansene3fa8741997-08-08 14:51:54 +0000129 if path[-2:] == '.µ':
130 dstfile = path[:-2]+ '.mu'
131 elif path[-4:] == '.prj':
132 dstfile = None
133 else:
Jack Jansenc42c0b71996-10-23 15:52:16 +0000134 return
Jack Jansen8094f0d1995-08-31 13:47:14 +0000135
Jack Jansene3fa8741997-08-08 14:51:54 +0000136 if dstfile:
137 # If the destination doesn't exists or is older that the source
138 # we copy and remember it
139
140 if os.path.exists(dstfile) and \
141 os.stat(dstfile)[8] >= os.stat(path)[8]:
142 print 'Not copying', path,'- Up-to-date'
143 else:
144 print 'Copy', path
145 macostools.copy(path, dstfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000146 else:
Jack Jansene3fa8741997-08-08 14:51:54 +0000147 dstfile = path
Jack Jansen8094f0d1995-08-31 13:47:14 +0000148
149 fss = macfs.FSSpec(dstfile)
150 creator = fss.GetCreatorType()[0]
151
152 if project_files.has_key(creator):
153 project_files[creator].append(fss)
154 else:
155 project_files[creator] = [fss]
156
Jack Jansenb4c93811996-04-19 16:02:20 +0000157def copycwexpfile(path, name):
158 """Copy CW export file"""
159 global project_files
160
161 dstdir = os.path.join(TOP, CWDIR)
Jack Jansenc42c0b71996-10-23 15:52:16 +0000162 if path[:len(dstdir)] == dstdir:
Jack Jansenb4c93811996-04-19 16:02:20 +0000163 return
Jack Jansenc42c0b71996-10-23 15:52:16 +0000164 srcdir = os.path.split(path)[0]
165 srcdir = os.path.split(srcdir)[1]
166 if srcdir in CWDIRDIRS:
167 if not os.path.exists(dstdir):
168 print dstdir
169 print 'No CW-project dir, skip', name
170 return
Jack Jansen297d7dd1996-11-09 18:36:00 +0000171 dstfile = os.path.join(dstdir, os.path.join(srcdir, name))
Jack Jansenc42c0b71996-10-23 15:52:16 +0000172 else:
173 if path[-6:] != '.µ.exp':
174 return
175 dstfile = path[:-6] + '.mu.exp'
Jack Jansenb4c93811996-04-19 16:02:20 +0000176 if dstfile[-6:] == '.µ.exp':
177 dstfile = dstfile[:-6]+'.mu.exp'
Jack Jansenb4c93811996-04-19 16:02:20 +0000178
179 # If the destination doesn't exists or is older that the source
180 # we copy and remember it
181
182 if os.path.exists(dstfile) and \
Jack Jansenc42c0b71996-10-23 15:52:16 +0000183 os.stat(dstfile)[8] >= os.stat(path)[8]:
Jack Jansenb4c93811996-04-19 16:02:20 +0000184 print 'Not copying', path,'- Up-to-date'
185 else:
186 print 'Copy', path
187 macostools.copy(path, dstfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000188
189extensions = [
190 ('.rsrc', binhexit),
Jack Jansen270f4111996-04-10 14:51:38 +0000191 ('.gif', binhexit),
Jack Jansenb4c93811996-04-19 16:02:20 +0000192 ('.µ', copycwproject),
Jack Jansene3fa8741997-08-08 14:51:54 +0000193 ('.prj', copycwproject),
194 ('.prj.exp', copycwexpfile),
Jack Jansenb4c93811996-04-19 16:02:20 +0000195 ('.µ.exp', copycwexpfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000196 ]
Jack Jansen3050a2d1995-08-14 12:19:20 +0000197
198def walker(arg, top, names):
Jack Jansen57fb8ce1996-03-07 15:16:27 +0000199 lnames = names[:]
200 for n in lnames:
201 if n[0] == '(' and n[-1] == ')':
202 names.remove(n)
203 continue
Jack Jansen8094f0d1995-08-31 13:47:14 +0000204 for ext, handler in extensions:
Jack Jansen3050a2d1995-08-14 12:19:20 +0000205 if n[-len(ext):] == ext:
206 name = os.path.join(top, n)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000207 handler(name, n)
Jack Jansen3050a2d1995-08-14 12:19:20 +0000208
209def dodir(name):
Jack Jansen8094f0d1995-08-31 13:47:14 +0000210 global TOP, project_files
211 TOP = name
Jack Jansen3050a2d1995-08-14 12:19:20 +0000212 os.path.walk(name, walker, None)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000213
214 for creator in project_files.keys():
215 hexbincwprojects(creator)
216 project_files = {}
Jack Jansen3050a2d1995-08-14 12:19:20 +0000217
218def main():
219 if len(sys.argv) > 1:
220 for dir in sys.argv[1:]:
221 dodir(dir)
222 elif os.name == 'mac':
223 import macfs
224 dir, ok = macfs.GetDirectory('Folder to search:')
225 if not ok:
226 sys.exit(0)
227 dodir(dir.as_pathname())
228 else:
229 print 'Usage: hexbintree dir ...'
230 sys.exit(1)
231 if os.name == 'mac':
232 sys.exit(1) # Keep window
233 else:
234 sys.exit(0)
235
236if __name__ == '__main__':
237 main()
238