Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 1 | # |
Jack Jansen | 2d3f94e | 1996-02-14 15:58:30 +0000 | [diff] [blame] | 2 | # binhextree - Recursively descend a directory and |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 3 | # pack all resource files. |
| 4 | # |
| 5 | # Jack Jansen, CWI, August 1995. |
| 6 | # |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 7 | |
| 8 | import os |
| 9 | import binhex |
| 10 | import sys |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 11 | import macostools |
| 12 | import macfs |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 13 | |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 14 | import aetools |
| 15 | from Metrowerks_Shell_Suite import Metrowerks_Shell_Suite |
| 16 | from Required_Suite import Required_Suite |
| 17 | |
| 18 | class MwShell(aetools.TalkTo, Metrowerks_Shell_Suite, Required_Suite): |
| 19 | pass |
| 20 | |
| 21 | # Top-level directory |
| 22 | TOP='' |
| 23 | |
| 24 | # Where to put CW projects, relative to TOP |
| 25 | CWDIR=':Mac:mwerks:projects' |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 26 | # From which folders to put projects there |
| 27 | CWDIRDIRS=['build.macppc.stand', 'build.macppc.shared', 'build.mac68k.stand', 'build.mac68k.shared', 'PlugIns'] |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 28 | |
| 29 | # Helper routines |
| 30 | def binhexit(path, name): |
| 31 | dstfile = path + '.hqx' |
Jack Jansen | 5196d00 | 1996-03-25 15:38:36 +0000 | [diff] [blame] | 32 | if os.path.exists(dstfile): |
| 33 | print 'Compare', path,'...', |
| 34 | if binhexcompare(path, dstfile): |
| 35 | print 'Identical, skipped.' |
| 36 | return |
| 37 | else: |
| 38 | print 'Not up-to-date.' |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 39 | print 'Binhexing', path |
| 40 | binhex.binhex(path, dstfile) |
| 41 | |
Jack Jansen | 5196d00 | 1996-03-25 15:38:36 +0000 | [diff] [blame] | 42 | def binhexcompare(source, hqxfile): |
| 43 | """(source, hqxfile) - Check whether the two files match (forks only)""" |
| 44 | ifp = binhex.HexBin(hqxfile) |
| 45 | |
| 46 | sfp = open(source, 'rb') |
| 47 | while 1: |
| 48 | d = ifp.read(128000) |
| 49 | d2 = sfp.read(128000) |
| 50 | if d <> d2: |
| 51 | return 0 |
| 52 | if not d: break |
| 53 | sfp.close() |
| 54 | ifp.close_data() |
| 55 | |
| 56 | d = ifp.read_rsrc(128000) |
| 57 | if d: |
| 58 | sfp = binhex.openrsrc(source, 'rb') |
| 59 | d2 = sfp.read(128000) |
| 60 | if d <> d2: |
| 61 | return 0 |
| 62 | while 1: |
| 63 | d = ifp.read_rsrc(128000) |
| 64 | d2 = sfp.read(128000) |
| 65 | if d <> d2: |
| 66 | return 0 |
| 67 | if not d: break |
| 68 | return 1 |
| 69 | |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 70 | # Project files to handle |
| 71 | project_files = {} |
| 72 | |
| 73 | def hexbincwprojects(creator): |
| 74 | """Compact and hexbin all files remembered with a given creator""" |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 75 | try: |
Jack Jansen | b9e5e14 | 1996-09-20 15:30:52 +0000 | [diff] [blame] | 76 | mgr = MwShell(creator, start=1) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 77 | except 'foo': |
| 78 | print 'Not handled:', creator |
| 79 | return |
| 80 | for fss in project_files[creator]: |
| 81 | srcfile = fss.as_pathname() |
Jack Jansen | 9bc4690 | 1995-10-23 13:55:11 +0000 | [diff] [blame] | 82 | |
| 83 | if srcfile[-1] == 'µ': |
| 84 | dstfile = srcfile[:-1]+'mu.hqx' |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 85 | elif srcfile[-3] == '.mu': |
| 86 | dstfile = srcfile + '.hqx' |
Jack Jansen | 9bc4690 | 1995-10-23 13:55:11 +0000 | [diff] [blame] | 87 | elif ord(srcfile[-1]) >= 128: |
| 88 | dstfile = srcfile[:-1]+`ord(srcfile[-1])`+'.hqx' |
| 89 | else: |
| 90 | dstfile = srcfile + '.hqx' |
| 91 | |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 92 | if os.path.exists(dstfile) and \ |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 93 | os.stat(dstfile)[8] >= os.stat(srcfile)[8]: |
Jack Jansen | 54500bb | 1995-09-01 11:53:17 +0000 | [diff] [blame] | 94 | print 'Skip', dstfile,'- Up-to-date' |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 95 | continue |
| 96 | print 'Compacting', dstfile |
| 97 | mgr.open(fss) |
| 98 | mgr.Reset_File_Paths() |
| 99 | mgr.Remove_Binaries() |
| 100 | mgr.Close_Project() |
| 101 | |
| 102 | print 'Binhexing', dstfile |
| 103 | binhex.binhex(srcfile, dstfile) |
| 104 | mgr.quit() |
| 105 | |
| 106 | def copycwproject(path, name): |
| 107 | """Copy CW project (if needed) and remember for hexbinning""" |
| 108 | global project_files |
| 109 | |
| 110 | dstdir = os.path.join(TOP, CWDIR) |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 111 | if path[:len(dstdir)] == dstdir: |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 112 | return |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 113 | srcdir = os.path.split(path)[0] |
| 114 | srcdir = os.path.split(srcdir)[1] |
| 115 | if srcdir in CWDIRDIRS: |
| 116 | if not os.path.exists(dstdir): |
| 117 | print dstdir |
| 118 | print 'No CW-project dir, skip', name |
| 119 | return |
Jack Jansen | 297d7dd | 1996-11-09 18:36:00 +0000 | [diff] [blame] | 120 | dstfile = os.path.join(dstdir, os.path.join(srcdir, name)) |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 121 | else: |
| 122 | if path[-2:] != '.µ': |
| 123 | return |
| 124 | dstfile = path[:-2]+ '.mu' |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 125 | |
| 126 | # If the destination doesn't exists or is older that the source |
| 127 | # we copy and remember it |
| 128 | |
| 129 | if os.path.exists(dstfile) and \ |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 130 | os.stat(dstfile)[8] >= os.stat(path)[8]: |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 131 | print 'Not copying', path,'- Up-to-date' |
| 132 | else: |
| 133 | print 'Copy', path |
| 134 | macostools.copy(path, dstfile) |
| 135 | |
| 136 | fss = macfs.FSSpec(dstfile) |
| 137 | creator = fss.GetCreatorType()[0] |
| 138 | |
| 139 | if project_files.has_key(creator): |
| 140 | project_files[creator].append(fss) |
| 141 | else: |
| 142 | project_files[creator] = [fss] |
| 143 | |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 144 | def copycwexpfile(path, name): |
| 145 | """Copy CW export file""" |
| 146 | global project_files |
| 147 | |
| 148 | dstdir = os.path.join(TOP, CWDIR) |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 149 | if path[:len(dstdir)] == dstdir: |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 150 | return |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 151 | srcdir = os.path.split(path)[0] |
| 152 | srcdir = os.path.split(srcdir)[1] |
| 153 | if srcdir in CWDIRDIRS: |
| 154 | if not os.path.exists(dstdir): |
| 155 | print dstdir |
| 156 | print 'No CW-project dir, skip', name |
| 157 | return |
Jack Jansen | 297d7dd | 1996-11-09 18:36:00 +0000 | [diff] [blame] | 158 | dstfile = os.path.join(dstdir, os.path.join(srcdir, name)) |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 159 | else: |
| 160 | if path[-6:] != '.µ.exp': |
| 161 | return |
| 162 | dstfile = path[:-6] + '.mu.exp' |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 163 | if dstfile[-6:] == '.µ.exp': |
| 164 | dstfile = dstfile[:-6]+'.mu.exp' |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 165 | |
| 166 | # If the destination doesn't exists or is older that the source |
| 167 | # we copy and remember it |
| 168 | |
| 169 | if os.path.exists(dstfile) and \ |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 170 | os.stat(dstfile)[8] >= os.stat(path)[8]: |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 171 | print 'Not copying', path,'- Up-to-date' |
| 172 | else: |
| 173 | print 'Copy', path |
| 174 | macostools.copy(path, dstfile) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 175 | |
| 176 | extensions = [ |
| 177 | ('.rsrc', binhexit), |
Jack Jansen | 270f411 | 1996-04-10 14:51:38 +0000 | [diff] [blame] | 178 | ('.gif', binhexit), |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 179 | ('.µ', copycwproject), |
| 180 | ('.µ.exp', copycwexpfile) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 181 | ] |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 182 | |
| 183 | def walker(arg, top, names): |
Jack Jansen | 57fb8ce | 1996-03-07 15:16:27 +0000 | [diff] [blame] | 184 | lnames = names[:] |
| 185 | for n in lnames: |
| 186 | if n[0] == '(' and n[-1] == ')': |
| 187 | names.remove(n) |
| 188 | continue |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 189 | for ext, handler in extensions: |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 190 | if n[-len(ext):] == ext: |
| 191 | name = os.path.join(top, n) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 192 | handler(name, n) |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 193 | |
| 194 | def dodir(name): |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 195 | global TOP, project_files |
| 196 | TOP = name |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 197 | os.path.walk(name, walker, None) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 198 | |
| 199 | for creator in project_files.keys(): |
| 200 | hexbincwprojects(creator) |
| 201 | project_files = {} |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 202 | |
| 203 | def main(): |
| 204 | if len(sys.argv) > 1: |
| 205 | for dir in sys.argv[1:]: |
| 206 | dodir(dir) |
| 207 | elif os.name == 'mac': |
| 208 | import macfs |
| 209 | dir, ok = macfs.GetDirectory('Folder to search:') |
| 210 | if not ok: |
| 211 | sys.exit(0) |
| 212 | dodir(dir.as_pathname()) |
| 213 | else: |
| 214 | print 'Usage: hexbintree dir ...' |
| 215 | sys.exit(1) |
| 216 | if os.name == 'mac': |
| 217 | sys.exit(1) # Keep window |
| 218 | else: |
| 219 | sys.exit(0) |
| 220 | |
| 221 | if __name__ == '__main__': |
| 222 | main() |
| 223 | |