2 # binhextree - Recursively descend a directory and
3 # pack all resource files.
5 # Actually it doesn't binhex anymore, it only copies projects.
7 # Jack Jansen, CWI, August 1995.
17 from Metrowerks_Shell_Suite
import Metrowerks_Shell_Suite
18 from Required_Suite
import Required_Suite
20 class MwShell(aetools
.TalkTo
, Metrowerks_Shell_Suite
, Required_Suite
):
26 # Where to put CW projects, relative to TOP
27 CWDIR
=':Mac:mwerks:projects'
28 # From which folders to put projects there
29 CWDIRDIRS
=['build.mac', 'build.macstand', 'build.macfreeze', 'PlugIns']
32 def binhexit(path
, name
):
33 dstfile
= path
+ '.hqx'
34 if os
.path
.exists(dstfile
):
35 print 'Compare', path
,'...',
36 if binhexcompare(path
, dstfile
):
37 print 'Identical, skipped.'
40 print 'Not up-to-date.'
41 print 'Binhexing', path
42 binhex
.binhex(path
, dstfile
)
44 def binhexcompare(source
, hqxfile
):
45 """(source, hqxfile) - Check whether the two files match (forks only)"""
46 ifp
= binhex
.HexBin(hqxfile
)
48 sfp
= open(source
, 'rb')
58 d
= ifp
.read_rsrc(128000)
60 sfp
= binhex
.openrsrc(source
, 'rb')
65 d
= ifp
.read_rsrc(128000)
72 # Project files to handle
75 def hexbincwprojects(creator
):
76 """Compact and hexbin all files remembered with a given creator"""
78 for fss
in project_files
[creator
]:
79 srcfile
= fss
.as_pathname()
82 if srcfile
[-1] == 'µ':
83 dstfile
= srcfile
[:-1]+'mu.hqx'
85 elif srcfile
[-3] == '.mu':
86 dstfile
= srcfile
+ '.hqx'
87 elif ord(srcfile
[-1]) >= 128:
88 dstfile
= srcfile
[:-1]+`
ord(srcfile
[-1])`
+'.hqx'
90 dstfile
= srcfile
+ '.hqx'
92 if os
.path
.exists(dstfile
) and \
93 os
.stat(dstfile
)[8] >= os
.stat(srcfile
)[8]:
94 print 'Skip', dstfile
,'- Up-to-date'
96 print 'Compacting', dstfile
100 mgr
= MwShell(creator
, start
=1)
102 print 'Not handled:', creator
106 mgr
.Reset_File_Paths()
107 mgr
.Remove_Binaries()
110 print 'Binhexing', dstfile
111 binhex
.binhex(srcfile
, dstfile
)
115 def copycwproject(path
, name
):
116 """Copy CW project (if needed) and remember for hexbinning"""
119 dstdir
= os
.path
.join(TOP
, CWDIR
)
120 if path
[:len(dstdir
)] == dstdir
:
122 srcdir
= os
.path
.split(path
)[0]
123 srcdir
= os
.path
.split(srcdir
)[1]
124 if srcdir
in CWDIRDIRS
:
125 if not os
.path
.exists(dstdir
):
127 print 'No CW-project dir, skip', name
129 dstfile
= os
.path
.join(dstdir
, os
.path
.join(srcdir
, name
))
131 if path
[-2:] == '.µ':
132 dstfile
= path
[:-2]+ '.mu'
133 elif path
[-4:] == '.prj':
139 # If the destination doesn't exists or is older that the source
140 # we copy and remember it
142 if os
.path
.exists(dstfile
) and \
143 os
.stat(dstfile
)[8] >= os
.stat(path
)[8]:
144 print 'Not copying', path
,'- Up-to-date'
147 macostools
.copy(path
, dstfile
)
151 fss
= macfs
.FSSpec(dstfile
)
152 creator
= fss
.GetCreatorType()[0]
154 if project_files
.has_key(creator
):
155 project_files
[creator
].append(fss
)
157 project_files
[creator
] = [fss
]
159 def copycwexpfile(path
, name
):
160 """Copy CW export file"""
163 dstdir
= os
.path
.join(TOP
, CWDIR
)
164 if path
[:len(dstdir
)] == dstdir
:
166 srcdir
= os
.path
.split(path
)[0]
167 srcdir
= os
.path
.split(srcdir
)[1]
168 if srcdir
in CWDIRDIRS
:
169 if not os
.path
.exists(dstdir
):
171 print 'No CW-project dir, skip', name
173 dstfile
= os
.path
.join(dstdir
, os
.path
.join(srcdir
, name
))
175 if path
[-6:] != '.µ.exp':
177 dstfile
= path
[:-6] + '.mu.exp'
178 if dstfile
[-6:] == '.µ.exp':
179 dstfile
= dstfile
[:-6]+'.mu.exp'
181 # If the destination doesn't exists or is older that the source
182 # we copy and remember it
184 if os
.path
.exists(dstfile
) and \
185 os
.stat(dstfile
)[8] >= os
.stat(path
)[8]:
186 print 'Not copying', path
,'- Up-to-date'
189 macostools
.copy(path
, dstfile
)
192 ## ('.rsrc', binhexit),
193 ## ('.gif', binhexit),
194 ('.µ', copycwproject
),
195 ('.prj', copycwproject
),
196 ('.prj.exp', copycwexpfile
),
197 ('.µ.exp', copycwexpfile
)
200 def walker(arg
, top
, names
):
203 if n
[0] == '(' and n
[-1] == ')':
206 for ext
, handler
in extensions
:
207 if n
[-len(ext
):] == ext
:
208 name
= os
.path
.join(top
, n
)
212 global TOP
, project_files
214 os
.path
.walk(name
, walker
, None)
216 ## for creator in project_files.keys():
217 ## hexbincwprojects(creator)
221 if len(sys
.argv
) > 1:
222 for dir in sys
.argv
[1:]:
224 elif os
.name
== 'mac':
226 dir, ok
= macfs
.GetDirectory('Folder to search:')
229 dodir(dir.as_pathname())
231 print 'Usage: hexbintree dir ...'
234 sys
.exit(1) # Keep window
238 if __name__
== '__main__':