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 |
Jack Jansen | fb721cf | 1998-06-26 15:05:29 +0000 | [diff] [blame] | 27 | CWDIRDIRS=['build.mac', 'build.macstand', 'build.macfreeze', '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 | e3fa874 | 1997-08-08 14:51:54 +0000 | [diff] [blame] | 75 | cw_running = 0 |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 76 | for fss in project_files[creator]: |
| 77 | srcfile = fss.as_pathname() |
Jack Jansen | 9bc4690 | 1995-10-23 13:55:11 +0000 | [diff] [blame] | 78 | |
Jack Jansen | e3fa874 | 1997-08-08 14:51:54 +0000 | [diff] [blame] | 79 | old_style = 0 |
Jack Jansen | 9bc4690 | 1995-10-23 13:55:11 +0000 | [diff] [blame] | 80 | if srcfile[-1] == 'µ': |
| 81 | dstfile = srcfile[:-1]+'mu.hqx' |
Jack Jansen | e3fa874 | 1997-08-08 14:51:54 +0000 | [diff] [blame] | 82 | old_style = 1 |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 83 | elif srcfile[-3] == '.mu': |
| 84 | dstfile = srcfile + '.hqx' |
Jack Jansen | 9bc4690 | 1995-10-23 13:55:11 +0000 | [diff] [blame] | 85 | elif ord(srcfile[-1]) >= 128: |
| 86 | dstfile = srcfile[:-1]+`ord(srcfile[-1])`+'.hqx' |
| 87 | else: |
| 88 | dstfile = srcfile + '.hqx' |
| 89 | |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 90 | if os.path.exists(dstfile) and \ |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 91 | os.stat(dstfile)[8] >= os.stat(srcfile)[8]: |
Jack Jansen | 54500bb | 1995-09-01 11:53:17 +0000 | [diff] [blame] | 92 | print 'Skip', dstfile,'- Up-to-date' |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 93 | continue |
| 94 | print 'Compacting', dstfile |
Jack Jansen | e3fa874 | 1997-08-08 14:51:54 +0000 | [diff] [blame] | 95 | if old_style: |
| 96 | if not cw_running: |
| 97 | try: |
| 98 | mgr = MwShell(creator, start=1) |
| 99 | except 'foo': |
| 100 | print 'Not handled:', creator |
| 101 | return |
| 102 | cw_running = 1 |
| 103 | mgr.open(fss) |
| 104 | mgr.Reset_File_Paths() |
| 105 | mgr.Remove_Binaries() |
| 106 | mgr.Close_Project() |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 107 | |
| 108 | print 'Binhexing', dstfile |
| 109 | binhex.binhex(srcfile, dstfile) |
Jack Jansen | e3fa874 | 1997-08-08 14:51:54 +0000 | [diff] [blame] | 110 | if cw_running: |
| 111 | mgr.quit() |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 112 | |
| 113 | def copycwproject(path, name): |
| 114 | """Copy CW project (if needed) and remember for hexbinning""" |
| 115 | global project_files |
| 116 | |
| 117 | dstdir = os.path.join(TOP, CWDIR) |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 118 | if path[:len(dstdir)] == dstdir: |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 119 | return |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 120 | srcdir = os.path.split(path)[0] |
| 121 | srcdir = os.path.split(srcdir)[1] |
| 122 | if srcdir in CWDIRDIRS: |
| 123 | if not os.path.exists(dstdir): |
| 124 | print dstdir |
| 125 | print 'No CW-project dir, skip', name |
| 126 | return |
Jack Jansen | 297d7dd | 1996-11-09 18:36:00 +0000 | [diff] [blame] | 127 | dstfile = os.path.join(dstdir, os.path.join(srcdir, name)) |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 128 | else: |
Jack Jansen | e3fa874 | 1997-08-08 14:51:54 +0000 | [diff] [blame] | 129 | if path[-2:] == '.µ': |
| 130 | dstfile = path[:-2]+ '.mu' |
| 131 | elif path[-4:] == '.prj': |
| 132 | dstfile = None |
| 133 | else: |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 134 | return |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 135 | |
Jack Jansen | e3fa874 | 1997-08-08 14:51:54 +0000 | [diff] [blame] | 136 | if dstfile: |
| 137 | # If the destination doesn't exists or is older that the source |
| 138 | # we copy and remember it |
| 139 | |
| 140 | if os.path.exists(dstfile) and \ |
| 141 | os.stat(dstfile)[8] >= os.stat(path)[8]: |
| 142 | print 'Not copying', path,'- Up-to-date' |
| 143 | else: |
| 144 | print 'Copy', path |
| 145 | macostools.copy(path, dstfile) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 146 | else: |
Jack Jansen | e3fa874 | 1997-08-08 14:51:54 +0000 | [diff] [blame] | 147 | dstfile = path |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 148 | |
| 149 | fss = macfs.FSSpec(dstfile) |
| 150 | creator = fss.GetCreatorType()[0] |
| 151 | |
| 152 | if project_files.has_key(creator): |
| 153 | project_files[creator].append(fss) |
| 154 | else: |
| 155 | project_files[creator] = [fss] |
| 156 | |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 157 | def copycwexpfile(path, name): |
| 158 | """Copy CW export file""" |
| 159 | global project_files |
| 160 | |
| 161 | dstdir = os.path.join(TOP, CWDIR) |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 162 | if path[:len(dstdir)] == dstdir: |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 163 | return |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 164 | srcdir = os.path.split(path)[0] |
| 165 | srcdir = os.path.split(srcdir)[1] |
| 166 | if srcdir in CWDIRDIRS: |
| 167 | if not os.path.exists(dstdir): |
| 168 | print dstdir |
| 169 | print 'No CW-project dir, skip', name |
| 170 | return |
Jack Jansen | 297d7dd | 1996-11-09 18:36:00 +0000 | [diff] [blame] | 171 | dstfile = os.path.join(dstdir, os.path.join(srcdir, name)) |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 172 | else: |
| 173 | if path[-6:] != '.µ.exp': |
| 174 | return |
| 175 | dstfile = path[:-6] + '.mu.exp' |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 176 | if dstfile[-6:] == '.µ.exp': |
| 177 | dstfile = dstfile[:-6]+'.mu.exp' |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 178 | |
| 179 | # If the destination doesn't exists or is older that the source |
| 180 | # we copy and remember it |
| 181 | |
| 182 | if os.path.exists(dstfile) and \ |
Jack Jansen | c42c0b7 | 1996-10-23 15:52:16 +0000 | [diff] [blame] | 183 | os.stat(dstfile)[8] >= os.stat(path)[8]: |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 184 | print 'Not copying', path,'- Up-to-date' |
| 185 | else: |
| 186 | print 'Copy', path |
| 187 | macostools.copy(path, dstfile) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 188 | |
| 189 | extensions = [ |
| 190 | ('.rsrc', binhexit), |
Jack Jansen | 270f411 | 1996-04-10 14:51:38 +0000 | [diff] [blame] | 191 | ('.gif', binhexit), |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 192 | ('.µ', copycwproject), |
Jack Jansen | e3fa874 | 1997-08-08 14:51:54 +0000 | [diff] [blame] | 193 | ('.prj', copycwproject), |
| 194 | ('.prj.exp', copycwexpfile), |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 195 | ('.µ.exp', copycwexpfile) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 196 | ] |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 197 | |
| 198 | def walker(arg, top, names): |
Jack Jansen | 57fb8ce | 1996-03-07 15:16:27 +0000 | [diff] [blame] | 199 | lnames = names[:] |
| 200 | for n in lnames: |
| 201 | if n[0] == '(' and n[-1] == ')': |
| 202 | names.remove(n) |
| 203 | continue |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 204 | for ext, handler in extensions: |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 205 | if n[-len(ext):] == ext: |
| 206 | name = os.path.join(top, n) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 207 | handler(name, n) |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 208 | |
| 209 | def dodir(name): |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 210 | global TOP, project_files |
| 211 | TOP = name |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 212 | os.path.walk(name, walker, None) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 213 | |
| 214 | for creator in project_files.keys(): |
| 215 | hexbincwprojects(creator) |
| 216 | project_files = {} |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 217 | |
| 218 | def main(): |
| 219 | if len(sys.argv) > 1: |
| 220 | for dir in sys.argv[1:]: |
| 221 | dodir(dir) |
| 222 | elif os.name == 'mac': |
| 223 | import macfs |
| 224 | dir, ok = macfs.GetDirectory('Folder to search:') |
| 225 | if not ok: |
| 226 | sys.exit(0) |
| 227 | dodir(dir.as_pathname()) |
| 228 | else: |
| 229 | print 'Usage: hexbintree dir ...' |
| 230 | sys.exit(1) |
| 231 | if os.name == 'mac': |
| 232 | sys.exit(1) # Keep window |
| 233 | else: |
| 234 | sys.exit(0) |
| 235 | |
| 236 | if __name__ == '__main__': |
| 237 | main() |
| 238 | |