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