blob: 19faf40b31d8e4f728c8890ea70888df778f8ffb [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"""
73 print 'Please start project mgr with signature', creator,'-'
74 sys.stdin.readline()
75 try:
76 mgr = MwShell(creator)
77 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'
85 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 \
91 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
95 mgr.open(fss)
96 mgr.Reset_File_Paths()
97 mgr.Remove_Binaries()
98 mgr.Close_Project()
99
100 print 'Binhexing', dstfile
101 binhex.binhex(srcfile, dstfile)
102 mgr.quit()
103
104def copycwproject(path, name):
105 """Copy CW project (if needed) and remember for hexbinning"""
106 global project_files
107
108 dstdir = os.path.join(TOP, CWDIR)
109 if not os.path.exists(dstdir):
110 print dstdir
111 print 'No CW-project dir, skip', name
112 return
113 dstfile = os.path.join(dstdir, name)
114 # Check that we're not in the dest directory
115 if dstfile == path:
116 return
117
118 # If the destination doesn't exists or is older that the source
119 # we copy and remember it
120
121 if os.path.exists(dstfile) and \
122 os.stat(dstfile)[8] > os.stat(path)[8]:
123 print 'Not copying', path,'- Up-to-date'
124 else:
125 print 'Copy', path
126 macostools.copy(path, dstfile)
127
128 fss = macfs.FSSpec(dstfile)
129 creator = fss.GetCreatorType()[0]
130
131 if project_files.has_key(creator):
132 project_files[creator].append(fss)
133 else:
134 project_files[creator] = [fss]
135
Jack Jansenb4c93811996-04-19 16:02:20 +0000136def copycwexpfile(path, name):
137 """Copy CW export file"""
138 global project_files
139
140 dstdir = os.path.join(TOP, CWDIR)
141 if not os.path.exists(dstdir):
142 print dstdir
143 print 'No CW-project dir, skip', name
144 return
145 dstfile = os.path.join(dstdir, name)
146 if dstfile[-6:] == '.µ.exp':
147 dstfile = dstfile[:-6]+'.mu.exp'
148 # Check that we're not in the dest directory
149 if dstfile == path:
150 return
151
152 # If the destination doesn't exists or is older that the source
153 # we copy and remember it
154
155 if os.path.exists(dstfile) and \
156 os.stat(dstfile)[8] > os.stat(path)[8]:
157 print 'Not copying', path,'- Up-to-date'
158 else:
159 print 'Copy', path
160 macostools.copy(path, dstfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000161
162extensions = [
163 ('.rsrc', binhexit),
Jack Jansen270f4111996-04-10 14:51:38 +0000164 ('.gif', binhexit),
Jack Jansenb4c93811996-04-19 16:02:20 +0000165 ('.µ', copycwproject),
166 ('.µ.exp', copycwexpfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000167 ]
Jack Jansen3050a2d1995-08-14 12:19:20 +0000168
169def walker(arg, top, names):
Jack Jansen57fb8ce1996-03-07 15:16:27 +0000170 lnames = names[:]
171 for n in lnames:
172 if n[0] == '(' and n[-1] == ')':
173 names.remove(n)
174 continue
Jack Jansen8094f0d1995-08-31 13:47:14 +0000175 for ext, handler in extensions:
Jack Jansen3050a2d1995-08-14 12:19:20 +0000176 if n[-len(ext):] == ext:
177 name = os.path.join(top, n)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000178 handler(name, n)
Jack Jansen3050a2d1995-08-14 12:19:20 +0000179
180def dodir(name):
Jack Jansen8094f0d1995-08-31 13:47:14 +0000181 global TOP, project_files
182 TOP = name
Jack Jansen3050a2d1995-08-14 12:19:20 +0000183 os.path.walk(name, walker, None)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000184
185 for creator in project_files.keys():
186 hexbincwprojects(creator)
187 project_files = {}
Jack Jansen3050a2d1995-08-14 12:19:20 +0000188
189def main():
190 if len(sys.argv) > 1:
191 for dir in sys.argv[1:]:
192 dodir(dir)
193 elif os.name == 'mac':
194 import macfs
195 dir, ok = macfs.GetDirectory('Folder to search:')
196 if not ok:
197 sys.exit(0)
198 dodir(dir.as_pathname())
199 else:
200 print 'Usage: hexbintree dir ...'
201 sys.exit(1)
202 if os.name == 'mac':
203 sys.exit(1) # Keep window
204 else:
205 sys.exit(0)
206
207if __name__ == '__main__':
208 main()
209