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