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' |
| 26 | |
| 27 | # Helper routines |
| 28 | def binhexit(path, name): |
| 29 | dstfile = path + '.hqx' |
Jack Jansen | 5196d00 | 1996-03-25 15:38:36 +0000 | [diff] [blame] | 30 | if os.path.exists(dstfile): |
| 31 | print 'Compare', path,'...', |
| 32 | if binhexcompare(path, dstfile): |
| 33 | print 'Identical, skipped.' |
| 34 | return |
| 35 | else: |
| 36 | print 'Not up-to-date.' |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 37 | print 'Binhexing', path |
| 38 | binhex.binhex(path, dstfile) |
| 39 | |
Jack Jansen | 5196d00 | 1996-03-25 15:38:36 +0000 | [diff] [blame] | 40 | def binhexcompare(source, hqxfile): |
| 41 | """(source, hqxfile) - Check whether the two files match (forks only)""" |
| 42 | ifp = binhex.HexBin(hqxfile) |
| 43 | |
| 44 | sfp = open(source, 'rb') |
| 45 | while 1: |
| 46 | d = ifp.read(128000) |
| 47 | d2 = sfp.read(128000) |
| 48 | if d <> d2: |
| 49 | return 0 |
| 50 | if not d: break |
| 51 | sfp.close() |
| 52 | ifp.close_data() |
| 53 | |
| 54 | d = ifp.read_rsrc(128000) |
| 55 | if d: |
| 56 | sfp = binhex.openrsrc(source, 'rb') |
| 57 | d2 = sfp.read(128000) |
| 58 | if d <> d2: |
| 59 | return 0 |
| 60 | while 1: |
| 61 | d = ifp.read_rsrc(128000) |
| 62 | d2 = sfp.read(128000) |
| 63 | if d <> d2: |
| 64 | return 0 |
| 65 | if not d: break |
| 66 | return 1 |
| 67 | |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 68 | # Project files to handle |
| 69 | project_files = {} |
| 70 | |
| 71 | def hexbincwprojects(creator): |
| 72 | """Compact and hexbin all files remembered with a given creator""" |
| 73 | print 'Please start project mgr with signature', creator,'-' |
| 74 | sys.stdin.readline() |
| 75 | try: |
| 76 | mgr = MwShell(creator) |
| 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' |
| 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 \ |
| 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 |
| 95 | mgr.open(fss) |
| 96 | mgr.Reset_File_Paths() |
| 97 | mgr.Remove_Binaries() |
| 98 | mgr.Close_Project() |
| 99 | |
| 100 | print 'Binhexing', dstfile |
| 101 | binhex.binhex(srcfile, dstfile) |
| 102 | mgr.quit() |
| 103 | |
| 104 | def copycwproject(path, name): |
| 105 | """Copy CW project (if needed) and remember for hexbinning""" |
| 106 | global project_files |
| 107 | |
| 108 | dstdir = os.path.join(TOP, CWDIR) |
| 109 | if not os.path.exists(dstdir): |
| 110 | print dstdir |
| 111 | print 'No CW-project dir, skip', name |
| 112 | return |
| 113 | dstfile = os.path.join(dstdir, name) |
| 114 | # Check that we're not in the dest directory |
| 115 | if dstfile == path: |
| 116 | return |
| 117 | |
| 118 | # If the destination doesn't exists or is older that the source |
| 119 | # we copy and remember it |
| 120 | |
| 121 | if os.path.exists(dstfile) and \ |
| 122 | os.stat(dstfile)[8] > os.stat(path)[8]: |
| 123 | print 'Not copying', path,'- Up-to-date' |
| 124 | else: |
| 125 | print 'Copy', path |
| 126 | macostools.copy(path, dstfile) |
| 127 | |
| 128 | fss = macfs.FSSpec(dstfile) |
| 129 | creator = fss.GetCreatorType()[0] |
| 130 | |
| 131 | if project_files.has_key(creator): |
| 132 | project_files[creator].append(fss) |
| 133 | else: |
| 134 | project_files[creator] = [fss] |
| 135 | |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 136 | def copycwexpfile(path, name): |
| 137 | """Copy CW export file""" |
| 138 | global project_files |
| 139 | |
| 140 | dstdir = os.path.join(TOP, CWDIR) |
| 141 | if not os.path.exists(dstdir): |
| 142 | print dstdir |
| 143 | print 'No CW-project dir, skip', name |
| 144 | return |
| 145 | dstfile = os.path.join(dstdir, name) |
| 146 | if dstfile[-6:] == '.µ.exp': |
| 147 | dstfile = dstfile[:-6]+'.mu.exp' |
| 148 | # Check that we're not in the dest directory |
| 149 | if dstfile == path: |
| 150 | return |
| 151 | |
| 152 | # If the destination doesn't exists or is older that the source |
| 153 | # we copy and remember it |
| 154 | |
| 155 | if os.path.exists(dstfile) and \ |
| 156 | os.stat(dstfile)[8] > os.stat(path)[8]: |
| 157 | print 'Not copying', path,'- Up-to-date' |
| 158 | else: |
| 159 | print 'Copy', path |
| 160 | macostools.copy(path, dstfile) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 161 | |
| 162 | extensions = [ |
| 163 | ('.rsrc', binhexit), |
Jack Jansen | 270f411 | 1996-04-10 14:51:38 +0000 | [diff] [blame] | 164 | ('.gif', binhexit), |
Jack Jansen | b4c9381 | 1996-04-19 16:02:20 +0000 | [diff] [blame] | 165 | ('.µ', copycwproject), |
| 166 | ('.µ.exp', copycwexpfile) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 167 | ] |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 168 | |
| 169 | def walker(arg, top, names): |
Jack Jansen | 57fb8ce | 1996-03-07 15:16:27 +0000 | [diff] [blame] | 170 | lnames = names[:] |
| 171 | for n in lnames: |
| 172 | if n[0] == '(' and n[-1] == ')': |
| 173 | names.remove(n) |
| 174 | continue |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 175 | for ext, handler in extensions: |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 176 | if n[-len(ext):] == ext: |
| 177 | name = os.path.join(top, n) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 178 | handler(name, n) |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 179 | |
| 180 | def dodir(name): |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 181 | global TOP, project_files |
| 182 | TOP = name |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 183 | os.path.walk(name, walker, None) |
Jack Jansen | 8094f0d | 1995-08-31 13:47:14 +0000 | [diff] [blame] | 184 | |
| 185 | for creator in project_files.keys(): |
| 186 | hexbincwprojects(creator) |
| 187 | project_files = {} |
Jack Jansen | 3050a2d | 1995-08-14 12:19:20 +0000 | [diff] [blame] | 188 | |
| 189 | def main(): |
| 190 | if len(sys.argv) > 1: |
| 191 | for dir in sys.argv[1:]: |
| 192 | dodir(dir) |
| 193 | elif os.name == 'mac': |
| 194 | import macfs |
| 195 | dir, ok = macfs.GetDirectory('Folder to search:') |
| 196 | if not ok: |
| 197 | sys.exit(0) |
| 198 | dodir(dir.as_pathname()) |
| 199 | else: |
| 200 | print 'Usage: hexbintree dir ...' |
| 201 | sys.exit(1) |
| 202 | if os.name == 'mac': |
| 203 | sys.exit(1) # Keep window |
| 204 | else: |
| 205 | sys.exit(0) |
| 206 | |
| 207 | if __name__ == '__main__': |
| 208 | main() |
| 209 | |