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