blob: 1d6996e764f2d47d6e5e9af057fa7c4321aada13 [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#
Jack Jansenc5900951998-08-20 14:51:12 +00005# Actually it doesn't binhex anymore, it only copies projects.
6#
Jack Jansen3050a2d1995-08-14 12:19:20 +00007# Jack Jansen, CWI, August 1995.
8#
Jack Jansen3050a2d1995-08-14 12:19:20 +00009
10import os
11import binhex
12import sys
Jack Jansen8094f0d1995-08-31 13:47:14 +000013import macostools
14import macfs
Jack Jansen3050a2d1995-08-14 12:19:20 +000015
Jack Jansen8094f0d1995-08-31 13:47:14 +000016import aetools
17from Metrowerks_Shell_Suite import Metrowerks_Shell_Suite
18from Required_Suite import Required_Suite
19
20class MwShell(aetools.TalkTo, Metrowerks_Shell_Suite, Required_Suite):
21 pass
22
23# Top-level directory
24TOP=''
25
26# Where to put CW projects, relative to TOP
27CWDIR=':Mac:mwerks:projects'
Jack Jansenc42c0b71996-10-23 15:52:16 +000028# From which folders to put projects there
Jack Jansenfb721cf1998-06-26 15:05:29 +000029CWDIRDIRS=['build.mac', 'build.macstand', 'build.macfreeze', 'PlugIns']
Jack Jansen8094f0d1995-08-31 13:47:14 +000030
31# Helper routines
32def binhexit(path, name):
33 dstfile = path + '.hqx'
Jack Jansen5196d001996-03-25 15:38:36 +000034 if os.path.exists(dstfile):
35 print 'Compare', path,'...',
36 if binhexcompare(path, dstfile):
37 print 'Identical, skipped.'
38 return
39 else:
40 print 'Not up-to-date.'
Jack Jansen8094f0d1995-08-31 13:47:14 +000041 print 'Binhexing', path
42 binhex.binhex(path, dstfile)
43
Jack Jansen5196d001996-03-25 15:38:36 +000044def binhexcompare(source, hqxfile):
45 """(source, hqxfile) - Check whether the two files match (forks only)"""
46 ifp = binhex.HexBin(hqxfile)
47
48 sfp = open(source, 'rb')
49 while 1:
50 d = ifp.read(128000)
51 d2 = sfp.read(128000)
52 if d <> d2:
53 return 0
54 if not d: break
55 sfp.close()
56 ifp.close_data()
57
58 d = ifp.read_rsrc(128000)
59 if d:
60 sfp = binhex.openrsrc(source, 'rb')
61 d2 = sfp.read(128000)
62 if d <> d2:
63 return 0
64 while 1:
65 d = ifp.read_rsrc(128000)
66 d2 = sfp.read(128000)
67 if d <> d2:
68 return 0
69 if not d: break
70 return 1
71
Jack Jansen8094f0d1995-08-31 13:47:14 +000072# Project files to handle
73project_files = {}
74
75def hexbincwprojects(creator):
76 """Compact and hexbin all files remembered with a given creator"""
Jack Jansene3fa8741997-08-08 14:51:54 +000077 cw_running = 0
Jack Jansen8094f0d1995-08-31 13:47:14 +000078 for fss in project_files[creator]:
79 srcfile = fss.as_pathname()
Jack Jansen9bc46901995-10-23 13:55:11 +000080
Jack Jansene3fa8741997-08-08 14:51:54 +000081 old_style = 0
Jack Jansen9bc46901995-10-23 13:55:11 +000082 if srcfile[-1] == 'µ':
83 dstfile = srcfile[:-1]+'mu.hqx'
Jack Jansene3fa8741997-08-08 14:51:54 +000084 old_style = 1
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
Jack Jansene3fa8741997-08-08 14:51:54 +000097 if old_style:
98 if not cw_running:
99 try:
100 mgr = MwShell(creator, start=1)
101 except 'foo':
102 print 'Not handled:', creator
103 return
104 cw_running = 1
105 mgr.open(fss)
106 mgr.Reset_File_Paths()
107 mgr.Remove_Binaries()
108 mgr.Close_Project()
Jack Jansen8094f0d1995-08-31 13:47:14 +0000109
110 print 'Binhexing', dstfile
111 binhex.binhex(srcfile, dstfile)
Jack Jansene3fa8741997-08-08 14:51:54 +0000112 if cw_running:
113 mgr.quit()
Jack Jansen8094f0d1995-08-31 13:47:14 +0000114
115def copycwproject(path, name):
116 """Copy CW project (if needed) and remember for hexbinning"""
117 global project_files
118
119 dstdir = os.path.join(TOP, CWDIR)
Jack Jansenc42c0b71996-10-23 15:52:16 +0000120 if path[:len(dstdir)] == dstdir:
Jack Jansen8094f0d1995-08-31 13:47:14 +0000121 return
Jack Jansenc42c0b71996-10-23 15:52:16 +0000122 srcdir = os.path.split(path)[0]
123 srcdir = os.path.split(srcdir)[1]
124 if srcdir in CWDIRDIRS:
125 if not os.path.exists(dstdir):
126 print dstdir
127 print 'No CW-project dir, skip', name
128 return
Jack Jansen297d7dd1996-11-09 18:36:00 +0000129 dstfile = os.path.join(dstdir, os.path.join(srcdir, name))
Jack Jansenc42c0b71996-10-23 15:52:16 +0000130 else:
Jack Jansene3fa8741997-08-08 14:51:54 +0000131 if path[-2:] == '.µ':
132 dstfile = path[:-2]+ '.mu'
133 elif path[-4:] == '.prj':
134 dstfile = None
135 else:
Jack Jansenc42c0b71996-10-23 15:52:16 +0000136 return
Jack Jansen8094f0d1995-08-31 13:47:14 +0000137
Jack Jansene3fa8741997-08-08 14:51:54 +0000138 if dstfile:
139 # If the destination doesn't exists or is older that the source
140 # we copy and remember it
141
142 if os.path.exists(dstfile) and \
143 os.stat(dstfile)[8] >= os.stat(path)[8]:
144 print 'Not copying', path,'- Up-to-date'
145 else:
146 print 'Copy', path
147 macostools.copy(path, dstfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000148 else:
Jack Jansene3fa8741997-08-08 14:51:54 +0000149 dstfile = path
Jack Jansen8094f0d1995-08-31 13:47:14 +0000150
151 fss = macfs.FSSpec(dstfile)
152 creator = fss.GetCreatorType()[0]
153
154 if project_files.has_key(creator):
155 project_files[creator].append(fss)
156 else:
157 project_files[creator] = [fss]
158
Jack Jansenb4c93811996-04-19 16:02:20 +0000159def copycwexpfile(path, name):
160 """Copy CW export file"""
161 global project_files
162
163 dstdir = os.path.join(TOP, CWDIR)
Jack Jansenc42c0b71996-10-23 15:52:16 +0000164 if path[:len(dstdir)] == dstdir:
Jack Jansenb4c93811996-04-19 16:02:20 +0000165 return
Jack Jansenc42c0b71996-10-23 15:52:16 +0000166 srcdir = os.path.split(path)[0]
167 srcdir = os.path.split(srcdir)[1]
168 if srcdir in CWDIRDIRS:
169 if not os.path.exists(dstdir):
170 print dstdir
171 print 'No CW-project dir, skip', name
172 return
Jack Jansen297d7dd1996-11-09 18:36:00 +0000173 dstfile = os.path.join(dstdir, os.path.join(srcdir, name))
Jack Jansenc42c0b71996-10-23 15:52:16 +0000174 else:
175 if path[-6:] != '.µ.exp':
176 return
177 dstfile = path[:-6] + '.mu.exp'
Jack Jansenb4c93811996-04-19 16:02:20 +0000178 if dstfile[-6:] == '.µ.exp':
179 dstfile = dstfile[:-6]+'.mu.exp'
Jack Jansenb4c93811996-04-19 16:02:20 +0000180
181 # If the destination doesn't exists or is older that the source
182 # we copy and remember it
183
184 if os.path.exists(dstfile) and \
Jack Jansenc42c0b71996-10-23 15:52:16 +0000185 os.stat(dstfile)[8] >= os.stat(path)[8]:
Jack Jansenb4c93811996-04-19 16:02:20 +0000186 print 'Not copying', path,'- Up-to-date'
187 else:
188 print 'Copy', path
189 macostools.copy(path, dstfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000190
191extensions = [
Jack Jansenc5900951998-08-20 14:51:12 +0000192## ('.rsrc', binhexit),
193## ('.gif', binhexit),
Jack Jansenb4c93811996-04-19 16:02:20 +0000194 ('.µ', copycwproject),
Jack Jansene3fa8741997-08-08 14:51:54 +0000195 ('.prj', copycwproject),
196 ('.prj.exp', copycwexpfile),
Jack Jansenb4c93811996-04-19 16:02:20 +0000197 ('.µ.exp', copycwexpfile)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000198 ]
Jack Jansen3050a2d1995-08-14 12:19:20 +0000199
200def walker(arg, top, names):
Jack Jansen57fb8ce1996-03-07 15:16:27 +0000201 lnames = names[:]
202 for n in lnames:
203 if n[0] == '(' and n[-1] == ')':
204 names.remove(n)
205 continue
Jack Jansen8094f0d1995-08-31 13:47:14 +0000206 for ext, handler in extensions:
Jack Jansen3050a2d1995-08-14 12:19:20 +0000207 if n[-len(ext):] == ext:
208 name = os.path.join(top, n)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000209 handler(name, n)
Jack Jansen3050a2d1995-08-14 12:19:20 +0000210
211def dodir(name):
Jack Jansen8094f0d1995-08-31 13:47:14 +0000212 global TOP, project_files
213 TOP = name
Jack Jansen3050a2d1995-08-14 12:19:20 +0000214 os.path.walk(name, walker, None)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000215
Jack Jansenc5900951998-08-20 14:51:12 +0000216## for creator in project_files.keys():
217## hexbincwprojects(creator)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000218 project_files = {}
Jack Jansen3050a2d1995-08-14 12:19:20 +0000219
220def main():
221 if len(sys.argv) > 1:
222 for dir in sys.argv[1:]:
223 dodir(dir)
224 elif os.name == 'mac':
225 import macfs
226 dir, ok = macfs.GetDirectory('Folder to search:')
227 if not ok:
228 sys.exit(0)
229 dodir(dir.as_pathname())
230 else:
231 print 'Usage: hexbintree dir ...'
232 sys.exit(1)
233 if os.name == 'mac':
234 sys.exit(1) # Keep window
235 else:
236 sys.exit(0)
237
238if __name__ == '__main__':
239 main()
240