blob: fa67b9c71f27a612e105822a862be52c69435723 [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#
7# To do:
Jack Jansen32cb80c1996-03-20 16:15:35 +00008# - Also do project files (.µ and .†), after using AppleEvents to the
Jack Jansen3050a2d1995-08-14 12:19:20 +00009# various builders to clean the projects
10# - Don't hexbin (and clean) if there exists a .hqx file that is newer.
11#
12
13import os
14import binhex
15import sys
Jack Jansen8094f0d1995-08-31 13:47:14 +000016import macostools
17import macfs
Jack Jansen3050a2d1995-08-14 12:19:20 +000018
Jack Jansen8094f0d1995-08-31 13:47:14 +000019import aetools
20from Metrowerks_Shell_Suite import Metrowerks_Shell_Suite
21from Required_Suite import Required_Suite
22
23class MwShell(aetools.TalkTo, Metrowerks_Shell_Suite, Required_Suite):
24 pass
25
26# Top-level directory
27TOP=''
28
29# Where to put CW projects, relative to TOP
30CWDIR=':Mac:mwerks:projects'
31
32# Helper routines
33def binhexit(path, name):
34 dstfile = path + '.hqx'
Jack Jansen5196d001996-03-25 15:38:36 +000035 if os.path.exists(dstfile):
36 print 'Compare', path,'...',
37 if binhexcompare(path, dstfile):
38 print 'Identical, skipped.'
39 return
40 else:
41 print 'Not up-to-date.'
Jack Jansen8094f0d1995-08-31 13:47:14 +000042 print 'Binhexing', path
43 binhex.binhex(path, dstfile)
44
Jack Jansen5196d001996-03-25 15:38:36 +000045def binhexcompare(source, hqxfile):
46 """(source, hqxfile) - Check whether the two files match (forks only)"""
47 ifp = binhex.HexBin(hqxfile)
48
49 sfp = open(source, 'rb')
50 while 1:
51 d = ifp.read(128000)
52 d2 = sfp.read(128000)
53 if d <> d2:
54 return 0
55 if not d: break
56 sfp.close()
57 ifp.close_data()
58
59 d = ifp.read_rsrc(128000)
60 if d:
61 sfp = binhex.openrsrc(source, 'rb')
62 d2 = sfp.read(128000)
63 if d <> d2:
64 return 0
65 while 1:
66 d = ifp.read_rsrc(128000)
67 d2 = sfp.read(128000)
68 if d <> d2:
69 return 0
70 if not d: break
71 return 1
72
Jack Jansen8094f0d1995-08-31 13:47:14 +000073# Project files to handle
74project_files = {}
75
76def hexbincwprojects(creator):
77 """Compact and hexbin all files remembered with a given creator"""
78 print 'Please start project mgr with signature', creator,'-'
79 sys.stdin.readline()
80 try:
81 mgr = MwShell(creator)
82 except 'foo':
83 print 'Not handled:', creator
84 return
85 for fss in project_files[creator]:
86 srcfile = fss.as_pathname()
Jack Jansen9bc46901995-10-23 13:55:11 +000087
88 if srcfile[-1] == 'µ':
89 dstfile = srcfile[:-1]+'mu.hqx'
90 elif ord(srcfile[-1]) >= 128:
91 dstfile = srcfile[:-1]+`ord(srcfile[-1])`+'.hqx'
92 else:
93 dstfile = srcfile + '.hqx'
94
Jack Jansen8094f0d1995-08-31 13:47:14 +000095 if os.path.exists(dstfile) and \
96 os.stat(dstfile)[8] > os.stat(srcfile)[8]:
Jack Jansen54500bb1995-09-01 11:53:17 +000097 print 'Skip', dstfile,'- Up-to-date'
Jack Jansen8094f0d1995-08-31 13:47:14 +000098 continue
99 print 'Compacting', dstfile
100 mgr.open(fss)
101 mgr.Reset_File_Paths()
102 mgr.Remove_Binaries()
103 mgr.Close_Project()
104
105 print 'Binhexing', dstfile
106 binhex.binhex(srcfile, dstfile)
107 mgr.quit()
108
109def copycwproject(path, name):
110 """Copy CW project (if needed) and remember for hexbinning"""
111 global project_files
112
113 dstdir = os.path.join(TOP, CWDIR)
114 if not os.path.exists(dstdir):
115 print dstdir
116 print 'No CW-project dir, skip', name
117 return
118 dstfile = os.path.join(dstdir, name)
119 # Check that we're not in the dest directory
120 if dstfile == path:
121 return
122
123 # If the destination doesn't exists or is older that the source
124 # we copy and remember it
125
126 if os.path.exists(dstfile) and \
127 os.stat(dstfile)[8] > os.stat(path)[8]:
128 print 'Not copying', path,'- Up-to-date'
129 else:
130 print 'Copy', path
131 macostools.copy(path, dstfile)
132
133 fss = macfs.FSSpec(dstfile)
134 creator = fss.GetCreatorType()[0]
135
136 if project_files.has_key(creator):
137 project_files[creator].append(fss)
138 else:
139 project_files[creator] = [fss]
140
141
142extensions = [
143 ('.rsrc', binhexit),
144 ('.µ', copycwproject)
145 ]
Jack Jansen3050a2d1995-08-14 12:19:20 +0000146
147def walker(arg, top, names):
Jack Jansen57fb8ce1996-03-07 15:16:27 +0000148 lnames = names[:]
149 for n in lnames:
150 if n[0] == '(' and n[-1] == ')':
151 names.remove(n)
152 continue
Jack Jansen8094f0d1995-08-31 13:47:14 +0000153 for ext, handler in extensions:
Jack Jansen3050a2d1995-08-14 12:19:20 +0000154 if n[-len(ext):] == ext:
155 name = os.path.join(top, n)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000156 handler(name, n)
Jack Jansen3050a2d1995-08-14 12:19:20 +0000157
158def dodir(name):
Jack Jansen8094f0d1995-08-31 13:47:14 +0000159 global TOP, project_files
160 TOP = name
Jack Jansen3050a2d1995-08-14 12:19:20 +0000161 os.path.walk(name, walker, None)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000162
163 for creator in project_files.keys():
164 hexbincwprojects(creator)
165 project_files = {}
Jack Jansen3050a2d1995-08-14 12:19:20 +0000166
167def main():
168 if len(sys.argv) > 1:
169 for dir in sys.argv[1:]:
170 dodir(dir)
171 elif os.name == 'mac':
172 import macfs
173 dir, ok = macfs.GetDirectory('Folder to search:')
174 if not ok:
175 sys.exit(0)
176 dodir(dir.as_pathname())
177 else:
178 print 'Usage: hexbintree dir ...'
179 sys.exit(1)
180 if os.name == 'mac':
181 sys.exit(1) # Keep window
182 else:
183 sys.exit(0)
184
185if __name__ == '__main__':
186 main()
187