blob: b354e4ae7a99af1eeeebe8f63bbc82ada18b2d21 [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
27CWDIRDIRS=['build.macppc.stand', 'build.macppc.shared', 'build.mac68k.stand', 'build.mac68k.shared', '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 Jansen8094f0d1995-08-31 13:47:14 +000075 try:
Jack Jansenb9e5e141996-09-20 15:30:52 +000076 mgr = MwShell(creator, start=1)
Jack Jansen8094f0d1995-08-31 13:47:14 +000077 except 'foo':
78 print 'Not handled:', creator
79 return
80 for fss in project_files[creator]:
81 srcfile = fss.as_pathname()
Jack Jansen9bc46901995-10-23 13:55:11 +000082
83 if srcfile[-1] == 'µ':
84 dstfile = srcfile[:-1]+'mu.hqx'
Jack Jansenc42c0b71996-10-23 15:52:16 +000085 elif srcfile[-3] == '.mu':
86 dstfile = srcfile + '.hqx'
Jack Jansen9bc46901995-10-23 13:55:11 +000087 elif ord(srcfile[-1]) >= 128:
88 dstfile = srcfile[:-1]+`ord(srcfile[-1])`+'.hqx'
89 else:
90 dstfile = srcfile + '.hqx'
91
Jack Jansen8094f0d1995-08-31 13:47:14 +000092 if os.path.exists(dstfile) and \
Jack Jansenc42c0b71996-10-23 15:52:16 +000093 os.stat(dstfile)[8] >= os.stat(srcfile)[8]:
Jack Jansen54500bb1995-09-01 11:53:17 +000094 print 'Skip', dstfile,'- Up-to-date'
Jack Jansen8094f0d1995-08-31 13:47:14 +000095 continue
96 print 'Compacting', dstfile
97 mgr.open(fss)
98 mgr.Reset_File_Paths()
99 mgr.Remove_Binaries()
100 mgr.Close_Project()
101
102 print 'Binhexing', dstfile
103 binhex.binhex(srcfile, dstfile)
104 mgr.quit()
105
106def copycwproject(path, name):
107 """Copy CW project (if needed) and remember for hexbinning"""
108 global project_files
109
110 dstdir = os.path.join(TOP, CWDIR)
Jack Jansenc42c0b71996-10-23 15:52:16 +0000111 if path[:len(dstdir)] == dstdir:
Jack Jansen8094f0d1995-08-31 13:47:14 +0000112 return
Jack Jansenc42c0b71996-10-23 15:52:16 +0000113 srcdir = os.path.split(path)[0]
114 srcdir = os.path.split(srcdir)[1]
115 if srcdir in CWDIRDIRS:
116 if not os.path.exists(dstdir):
117 print dstdir
118 print 'No CW-project dir, skip', name
119 return
Jack Jansen297d7dd1996-11-09 18:36:00 +0000120 dstfile = os.path.join(dstdir, os.path.join(srcdir, name))
Jack Jansenc42c0b71996-10-23 15:52:16 +0000121 else:
122 if path[-2:] != '.µ':
123 return
124 dstfile = path[:-2]+ '.mu'
Jack Jansen8094f0d1995-08-31 13:47:14 +0000125
126 # If the destination doesn't exists or is older that the source
127 # we copy and remember it
128
129 if os.path.exists(dstfile) and \
Jack Jansenc42c0b71996-10-23 15:52:16 +0000130 os.stat(dstfile)[8] >= os.stat(path)[8]:
Jack Jansen8094f0d1995-08-31 13:47:14 +0000131 print 'Not copying', path,'- Up-to-date'
132 else:
133 print 'Copy', path
134 macostools.copy(path, dstfile)
135
136 fss = macfs.FSSpec(dstfile)
137 creator = fss.GetCreatorType()[0]
138
139 if project_files.has_key(creator):
140 project_files[creator].append(fss)
141 else:
142 project_files[creator] = [fss]
143
Jack Jansenb4c93811996-04-19 16:02:20 +0000144def copycwexpfile(path, name):
145 """Copy CW export file"""
146 global project_files
147
148 dstdir = os.path.join(TOP, CWDIR)
Jack Jansenc42c0b71996-10-23 15:52:16 +0000149 if path[:len(dstdir)] == dstdir:
Jack Jansenb4c93811996-04-19 16:02:20 +0000150 return
Jack Jansenc42c0b71996-10-23 15:52:16 +0000151 srcdir = os.path.split(path)[0]
152 srcdir = os.path.split(srcdir)[1]
153 if srcdir in CWDIRDIRS:
154 if not os.path.exists(dstdir):
155 print dstdir
156 print 'No CW-project dir, skip', name
157 return
Jack Jansen297d7dd1996-11-09 18:36:00 +0000158 dstfile = os.path.join(dstdir, os.path.join(srcdir, name))
Jack Jansenc42c0b71996-10-23 15:52:16 +0000159 else:
160 if path[-6:] != '.µ.exp':
161 return
162 dstfile = path[:-6] + '.mu.exp'
Jack Jansenb4c93811996-04-19 16:02:20 +0000163 if dstfile[-6:] == '.µ.exp':
164 dstfile = dstfile[:-6]+'.mu.exp'
Jack Jansenb4c93811996-04-19 16:02:20 +0000165
166 # If the destination doesn't exists or is older that the source
167 # we copy and remember it
168
169 if os.path.exists(dstfile) and \
Jack Jansenc42c0b71996-10-23 15:52:16 +0000170 os.stat(dstfile)[8] >= os.stat(path)[8]:
Jack Jansenb4c93811996-04-19 16:02:20 +0000171 print 'Not copying', path,'- Up-to-date'
172 else:
173 print 'Copy', path
174 macostools.copy(path, dstfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000175
176extensions = [
177 ('.rsrc', binhexit),
Jack Jansen270f4111996-04-10 14:51:38 +0000178 ('.gif', binhexit),
Jack Jansenb4c93811996-04-19 16:02:20 +0000179 ('.µ', copycwproject),
180 ('.µ.exp', copycwexpfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000181 ]
Jack Jansen3050a2d1995-08-14 12:19:20 +0000182
183def walker(arg, top, names):
Jack Jansen57fb8ce1996-03-07 15:16:27 +0000184 lnames = names[:]
185 for n in lnames:
186 if n[0] == '(' and n[-1] == ')':
187 names.remove(n)
188 continue
Jack Jansen8094f0d1995-08-31 13:47:14 +0000189 for ext, handler in extensions:
Jack Jansen3050a2d1995-08-14 12:19:20 +0000190 if n[-len(ext):] == ext:
191 name = os.path.join(top, n)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000192 handler(name, n)
Jack Jansen3050a2d1995-08-14 12:19:20 +0000193
194def dodir(name):
Jack Jansen8094f0d1995-08-31 13:47:14 +0000195 global TOP, project_files
196 TOP = name
Jack Jansen3050a2d1995-08-14 12:19:20 +0000197 os.path.walk(name, walker, None)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000198
199 for creator in project_files.keys():
200 hexbincwprojects(creator)
201 project_files = {}
Jack Jansen3050a2d1995-08-14 12:19:20 +0000202
203def main():
204 if len(sys.argv) > 1:
205 for dir in sys.argv[1:]:
206 dodir(dir)
207 elif os.name == 'mac':
208 import macfs
209 dir, ok = macfs.GetDirectory('Folder to search:')
210 if not ok:
211 sys.exit(0)
212 dodir(dir.as_pathname())
213 else:
214 print 'Usage: hexbintree dir ...'
215 sys.exit(1)
216 if os.name == 'mac':
217 sys.exit(1) # Keep window
218 else:
219 sys.exit(0)
220
221if __name__ == '__main__':
222 main()
223