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