home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / Python 1.3 68K / scripts / binhextree.py next >
Encoding:
Python Source  |  1995-10-11  |  3.3 KB  |  150 lines  |  [TEXT/PYTH]

  1. #
  2. # hexbintree - Recursively descend a directory and
  3. # pack all resource files.
  4. #
  5. # Jack Jansen, CWI, August 1995.
  6. #
  7. # To do:
  8. # - Also do project files (.µ and .π), after using AppleEvents to the
  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
  16. import macostools
  17. import macfs
  18.  
  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()
  60.         dstfile = srcfile + '.hqx'
  61.         if os.path.exists(dstfile) and \
  62.                 os.stat(dstfile)[8] > os.stat(srcfile)[8]:
  63.             print 'Skip', dstfile,'- Up-to-date'
  64.             continue
  65.         print 'Compacting', dstfile
  66.         mgr.open(fss)
  67.         mgr.Reset_File_Paths()
  68.         mgr.Remove_Binaries()
  69.         mgr.Close_Project()
  70.         
  71.         print 'Binhexing', dstfile
  72.         binhex.binhex(srcfile, dstfile)
  73.     mgr.quit()
  74.     
  75. def copycwproject(path, name):
  76.     """Copy CW project (if needed) and remember for hexbinning"""
  77.     global project_files
  78.     
  79.     dstdir = os.path.join(TOP, CWDIR)
  80.     if not os.path.exists(dstdir):
  81.         print dstdir
  82.         print 'No CW-project dir, skip', name
  83.         return
  84.     dstfile = os.path.join(dstdir, name)
  85.     # Check that we're not in the dest directory
  86.     if dstfile == path:
  87.         return
  88.  
  89.     # If the destination doesn't exists or is older that the source
  90.     # we copy and remember it
  91.     
  92.     if os.path.exists(dstfile) and \
  93.             os.stat(dstfile)[8] > os.stat(path)[8]:
  94.         print 'Not copying', path,'- Up-to-date'
  95.     else:
  96.         print 'Copy', path
  97.         macostools.copy(path, dstfile)
  98.     
  99.     fss = macfs.FSSpec(dstfile)
  100.     creator = fss.GetCreatorType()[0]
  101.     
  102.     if project_files.has_key(creator):
  103.         project_files[creator].append(fss)
  104.     else:
  105.         project_files[creator] = [fss]    
  106.     
  107.  
  108. extensions = [
  109.     ('.rsrc', binhexit),
  110.     ('.µ', copycwproject)
  111.     ]
  112.  
  113. def walker(arg, top, names):
  114.     for n in names:
  115.         for ext, handler in extensions:
  116.             if n[-len(ext):] == ext:
  117.                 name = os.path.join(top, n)
  118.                 handler(name, n)
  119.                 
  120. def dodir(name):
  121.     global TOP, project_files
  122.     TOP = name
  123.     os.path.walk(name, walker, None)
  124.     
  125.     for creator in project_files.keys():
  126.         hexbincwprojects(creator)
  127.     project_files = {}
  128.                 
  129. def main():
  130.     if len(sys.argv) > 1:
  131.         for dir in sys.argv[1:]:
  132.             dodir(dir)
  133.     elif os.name == 'mac':
  134.         import macfs
  135.         dir, ok = macfs.GetDirectory('Folder to search:')
  136.         if not ok:
  137.             sys.exit(0)
  138.         dodir(dir.as_pathname())
  139.     else:
  140.         print 'Usage: hexbintree dir ...'
  141.         sys.exit(1)
  142.     if os.name == 'mac':
  143.         sys.exit(1)   # Keep window
  144.     else:
  145.         sys.exit(0)
  146.         
  147. if __name__ == '__main__':
  148.     main()
  149.     
  150.