blob: 85b8a6c828e3024bc437e1bda0b31dec9eaef248 [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'
35 if os.path.exists(dstfile) and \
36 os.stat(dstfile)[8] > os.stat(path)[8]:
37 print 'Skip', path,'- Up-to-date'
38 return
39 print 'Binhexing', path
40 binhex.binhex(path, dstfile)
41
42# Project files to handle
43project_files = {}
44
45def hexbincwprojects(creator):
46 """Compact and hexbin all files remembered with a given creator"""
47 print 'Please start project mgr with signature', creator,'-'
48 sys.stdin.readline()
49 try:
50 mgr = MwShell(creator)
51 except 'foo':
52 print 'Not handled:', creator
53 return
54 for fss in project_files[creator]:
55 srcfile = fss.as_pathname()
Jack Jansen9bc46901995-10-23 13:55:11 +000056
57 if srcfile[-1] == 'µ':
58 dstfile = srcfile[:-1]+'mu.hqx'
59 elif ord(srcfile[-1]) >= 128:
60 dstfile = srcfile[:-1]+`ord(srcfile[-1])`+'.hqx'
61 else:
62 dstfile = srcfile + '.hqx'
63
Jack Jansen8094f0d1995-08-31 13:47:14 +000064 if os.path.exists(dstfile) and \
65 os.stat(dstfile)[8] > os.stat(srcfile)[8]:
Jack Jansen54500bb1995-09-01 11:53:17 +000066 print 'Skip', dstfile,'- Up-to-date'
Jack Jansen8094f0d1995-08-31 13:47:14 +000067 continue
68 print 'Compacting', dstfile
69 mgr.open(fss)
70 mgr.Reset_File_Paths()
71 mgr.Remove_Binaries()
72 mgr.Close_Project()
73
74 print 'Binhexing', dstfile
75 binhex.binhex(srcfile, dstfile)
76 mgr.quit()
77
78def copycwproject(path, name):
79 """Copy CW project (if needed) and remember for hexbinning"""
80 global project_files
81
82 dstdir = os.path.join(TOP, CWDIR)
83 if not os.path.exists(dstdir):
84 print dstdir
85 print 'No CW-project dir, skip', name
86 return
87 dstfile = os.path.join(dstdir, name)
88 # Check that we're not in the dest directory
89 if dstfile == path:
90 return
91
92 # If the destination doesn't exists or is older that the source
93 # we copy and remember it
94
95 if os.path.exists(dstfile) and \
96 os.stat(dstfile)[8] > os.stat(path)[8]:
97 print 'Not copying', path,'- Up-to-date'
98 else:
99 print 'Copy', path
100 macostools.copy(path, dstfile)
101
102 fss = macfs.FSSpec(dstfile)
103 creator = fss.GetCreatorType()[0]
104
105 if project_files.has_key(creator):
106 project_files[creator].append(fss)
107 else:
108 project_files[creator] = [fss]
109
110
111extensions = [
112 ('.rsrc', binhexit),
113 ('.µ', copycwproject)
114 ]
Jack Jansen3050a2d1995-08-14 12:19:20 +0000115
116def walker(arg, top, names):
Jack Jansen57fb8ce1996-03-07 15:16:27 +0000117 lnames = names[:]
118 for n in lnames:
119 if n[0] == '(' and n[-1] == ')':
120 names.remove(n)
121 continue
Jack Jansen8094f0d1995-08-31 13:47:14 +0000122 for ext, handler in extensions:
Jack Jansen3050a2d1995-08-14 12:19:20 +0000123 if n[-len(ext):] == ext:
124 name = os.path.join(top, n)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000125 handler(name, n)
Jack Jansen3050a2d1995-08-14 12:19:20 +0000126
127def dodir(name):
Jack Jansen8094f0d1995-08-31 13:47:14 +0000128 global TOP, project_files
129 TOP = name
Jack Jansen3050a2d1995-08-14 12:19:20 +0000130 os.path.walk(name, walker, None)
Jack Jansen8094f0d1995-08-31 13:47:14 +0000131
132 for creator in project_files.keys():
133 hexbincwprojects(creator)
134 project_files = {}
Jack Jansen3050a2d1995-08-14 12:19:20 +0000135
136def main():
137 if len(sys.argv) > 1:
138 for dir in sys.argv[1:]:
139 dodir(dir)
140 elif os.name == 'mac':
141 import macfs
142 dir, ok = macfs.GetDirectory('Folder to search:')
143 if not ok:
144 sys.exit(0)
145 dodir(dir.as_pathname())
146 else:
147 print 'Usage: hexbintree dir ...'
148 sys.exit(1)
149 if os.name == 'mac':
150 sys.exit(1) # Keep window
151 else:
152 sys.exit(0)
153
154if __name__ == '__main__':
155 main()
156