blob: e8597c87cef3e1d57e2217d02cfe84e7c7798104 [file] [log] [blame]
Jack Jansen3050a2d1995-08-14 12:19:20 +00001#
2# hexbintree - Recursively descend a directory and
3# pack all resource files.
4#
5# Jack Jansen, CWI, August 1995.
6#
7# To do:
8# - Also do project files (.µ and .¹), after using AppleEvents to the
9# 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 addpack
20addpack.addpack('Tools')
21addpack.addpack('bgen')
22addpack.addpack('AE')
23import aetools
24from Metrowerks_Shell_Suite import Metrowerks_Shell_Suite
25from Required_Suite import Required_Suite
26
27class MwShell(aetools.TalkTo, Metrowerks_Shell_Suite, Required_Suite):
28 pass
29
30# Top-level directory
31TOP=''
32
33# Where to put CW projects, relative to TOP
34CWDIR=':Mac:mwerks:projects'
35
36# Helper routines
37def binhexit(path, name):
38 dstfile = path + '.hqx'
39 if os.path.exists(dstfile) and \
40 os.stat(dstfile)[8] > os.stat(path)[8]:
41 print 'Skip', path,'- Up-to-date'
42 return
43 print 'Binhexing', path
44 binhex.binhex(path, dstfile)
45
46# Project files to handle
47project_files = {}
48
49def hexbincwprojects(creator):
50 """Compact and hexbin all files remembered with a given creator"""
51 print 'Please start project mgr with signature', creator,'-'
52 sys.stdin.readline()
53 try:
54 mgr = MwShell(creator)
55 except 'foo':
56 print 'Not handled:', creator
57 return
58 for fss in project_files[creator]:
59 srcfile = fss.as_pathname()
Jack Jansen9bc46901995-10-23 13:55:11 +000060
61 if srcfile[-1] == 'µ':
62 dstfile = srcfile[:-1]+'mu.hqx'
63 elif ord(srcfile[-1]) >= 128:
64 dstfile = srcfile[:-1]+`ord(srcfile[-1])`+'.hqx'
65 else:
66 dstfile = srcfile + '.hqx'
67
Jack Jansen8094f0d1995-08-31 13:47:14 +000068 if os.path.exists(dstfile) and \
69 os.stat(dstfile)[8] > os.stat(srcfile)[8]:
Jack Jansen54500bb1995-09-01 11:53:17 +000070 print 'Skip', dstfile,'- Up-to-date'
Jack Jansen8094f0d1995-08-31 13:47:14 +000071 continue
72 print 'Compacting', dstfile
73 mgr.open(fss)
74 mgr.Reset_File_Paths()
75 mgr.Remove_Binaries()
76 mgr.Close_Project()
77
78 print 'Binhexing', dstfile
79 binhex.binhex(srcfile, dstfile)
80 mgr.quit()
81
82def copycwproject(path, name):
83 """Copy CW project (if needed) and remember for hexbinning"""
84 global project_files
85
86 dstdir = os.path.join(TOP, CWDIR)
87 if not os.path.exists(dstdir):
88 print dstdir
89 print 'No CW-project dir, skip', name
90 return
91 dstfile = os.path.join(dstdir, name)
92 # Check that we're not in the dest directory
93 if dstfile == path:
94 return
95
96 # If the destination doesn't exists or is older that the source
97 # we copy and remember it
98
99 if os.path.exists(dstfile) and \
100 os.stat(dstfile)[8] > os.stat(path)[8]:
101 print 'Not copying', path,'- Up-to-date'
102 else:
103 print 'Copy', path
104 macostools.copy(path, dstfile)
105
106 fss = macfs.FSSpec(dstfile)
107 creator = fss.GetCreatorType()[0]
108
109 if project_files.has_key(creator):
110 project_files[creator].append(fss)
111 else:
112 project_files[creator] = [fss]
113
114
115extensions = [
116 ('.rsrc', binhexit),
117 ('.µ', copycwproject)
118 ]
Jack Jansen3050a2d1995-08-14 12:19:20 +0000119
120def walker(arg, top, names):
121 for n in names:
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