blob: 535bc7b5e7dbf8598d6eebd6e3faa7deae7f8ab7 [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'
26
27# Helper routines
28def binhexit(path, name):
29 dstfile = path + '.hqx'
Jack Jansen5196d001996-03-25 15:38:36 +000030 if os.path.exists(dstfile):
31 print 'Compare', path,'...',
32 if binhexcompare(path, dstfile):
33 print 'Identical, skipped.'
34 return
35 else:
36 print 'Not up-to-date.'
Jack Jansen8094f0d1995-08-31 13:47:14 +000037 print 'Binhexing', path
38 binhex.binhex(path, dstfile)
39
Jack Jansen5196d001996-03-25 15:38:36 +000040def binhexcompare(source, hqxfile):
41 """(source, hqxfile) - Check whether the two files match (forks only)"""
42 ifp = binhex.HexBin(hqxfile)
43
44 sfp = open(source, 'rb')
45 while 1:
46 d = ifp.read(128000)
47 d2 = sfp.read(128000)
48 if d <> d2:
49 return 0
50 if not d: break
51 sfp.close()
52 ifp.close_data()
53
54 d = ifp.read_rsrc(128000)
55 if d:
56 sfp = binhex.openrsrc(source, 'rb')
57 d2 = sfp.read(128000)
58 if d <> d2:
59 return 0
60 while 1:
61 d = ifp.read_rsrc(128000)
62 d2 = sfp.read(128000)
63 if d <> d2:
64 return 0
65 if not d: break
66 return 1
67
Jack Jansen8094f0d1995-08-31 13:47:14 +000068# Project files to handle
69project_files = {}
70
71def hexbincwprojects(creator):
72 """Compact and hexbin all files remembered with a given creator"""
Jack Jansen8094f0d1995-08-31 13:47:14 +000073 try:
Jack Jansenb9e5e141996-09-20 15:30:52 +000074 mgr = MwShell(creator, start=1)
Jack Jansen8094f0d1995-08-31 13:47:14 +000075 except 'foo':
76 print 'Not handled:', creator
77 return
78 for fss in project_files[creator]:
79 srcfile = fss.as_pathname()
Jack Jansen9bc46901995-10-23 13:55:11 +000080
81 if srcfile[-1] == 'µ':
82 dstfile = srcfile[:-1]+'mu.hqx'
83 elif ord(srcfile[-1]) >= 128:
84 dstfile = srcfile[:-1]+`ord(srcfile[-1])`+'.hqx'
85 else:
86 dstfile = srcfile + '.hqx'
87
Jack Jansen8094f0d1995-08-31 13:47:14 +000088 if os.path.exists(dstfile) and \
89 os.stat(dstfile)[8] > os.stat(srcfile)[8]:
Jack Jansen54500bb1995-09-01 11:53:17 +000090 print 'Skip', dstfile,'- Up-to-date'
Jack Jansen8094f0d1995-08-31 13:47:14 +000091 continue
92 print 'Compacting', dstfile
93 mgr.open(fss)
94 mgr.Reset_File_Paths()
95 mgr.Remove_Binaries()
96 mgr.Close_Project()
97
98 print 'Binhexing', dstfile
99 binhex.binhex(srcfile, dstfile)
100 mgr.quit()
101
102def copycwproject(path, name):
103 """Copy CW project (if needed) and remember for hexbinning"""
104 global project_files
105
106 dstdir = os.path.join(TOP, CWDIR)
107 if not os.path.exists(dstdir):
108 print dstdir
109 print 'No CW-project dir, skip', name
110 return
111 dstfile = os.path.join(dstdir, name)
112 # Check that we're not in the dest directory
113 if dstfile == path:
114 return
115
116 # If the destination doesn't exists or is older that the source
117 # we copy and remember it
118
119 if os.path.exists(dstfile) and \
120 os.stat(dstfile)[8] > os.stat(path)[8]:
121 print 'Not copying', path,'- Up-to-date'
122 else:
123 print 'Copy', path
124 macostools.copy(path, dstfile)
125
126 fss = macfs.FSSpec(dstfile)
127 creator = fss.GetCreatorType()[0]
128
129 if project_files.has_key(creator):
130 project_files[creator].append(fss)
131 else:
132 project_files[creator] = [fss]
133
Jack Jansenb4c93811996-04-19 16:02:20 +0000134def copycwexpfile(path, name):
135 """Copy CW export file"""
136 global project_files
137
138 dstdir = os.path.join(TOP, CWDIR)
139 if not os.path.exists(dstdir):
140 print dstdir
141 print 'No CW-project dir, skip', name
142 return
143 dstfile = os.path.join(dstdir, name)
144 if dstfile[-6:] == '.µ.exp':
145 dstfile = dstfile[:-6]+'.mu.exp'
146 # Check that we're not in the dest directory
147 if dstfile == path:
148 return
149
150 # If the destination doesn't exists or is older that the source
151 # we copy and remember it
152
153 if os.path.exists(dstfile) and \
154 os.stat(dstfile)[8] > os.stat(path)[8]:
155 print 'Not copying', path,'- Up-to-date'
156 else:
157 print 'Copy', path
158 macostools.copy(path, dstfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000159
160extensions = [
161 ('.rsrc', binhexit),
Jack Jansen270f4111996-04-10 14:51:38 +0000162 ('.gif', binhexit),
Jack Jansenb4c93811996-04-19 16:02:20 +0000163 ('.µ', copycwproject),
164 ('.µ.exp', copycwexpfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000165 ]
Jack Jansen3050a2d1995-08-14 12:19:20 +0000166
167def walker(arg, top, names):
Jack Jansen57fb8ce1996-03-07 15:16:27 +0000168 lnames = names[:]
169 for n in lnames:
170 if n[0] == '(' and n[-1] == ')':
171 names.remove(n)
172 continue
Jack Jansen8094f0d1995-08-31 13:47:14 +0000173 for ext, handler in extensions:
Jack Jansen3050a2d1995-08-14 12:19:20 +0000174 if n[-len(ext):] == ext:
175 name = os.path.join(top, n)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000176 handler(name, n)
Jack Jansen3050a2d1995-08-14 12:19:20 +0000177
178def dodir(name):
Jack Jansen8094f0d1995-08-31 13:47:14 +0000179 global TOP, project_files
180 TOP = name
Jack Jansen3050a2d1995-08-14 12:19:20 +0000181 os.path.walk(name, walker, None)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000182
183 for creator in project_files.keys():
184 hexbincwprojects(creator)
185 project_files = {}
Jack Jansen3050a2d1995-08-14 12:19:20 +0000186
187def main():
188 if len(sys.argv) > 1:
189 for dir in sys.argv[1:]:
190 dodir(dir)
191 elif os.name == 'mac':
192 import macfs
193 dir, ok = macfs.GetDirectory('Folder to search:')
194 if not ok:
195 sys.exit(0)
196 dodir(dir.as_pathname())
197 else:
198 print 'Usage: hexbintree dir ...'
199 sys.exit(1)
200 if os.name == 'mac':
201 sys.exit(1) # Keep window
202 else:
203 sys.exit(0)
204
205if __name__ == '__main__':
206 main()
207