/Mac/scripts/cachersrc.py

http://unladen-swallow.googlecode.com/ · Python · 44 lines · 34 code · 5 blank · 5 comment · 14 complexity · 1427fbe98137803edab334dfa0544ce5 MD5 · raw file

  1. # Scan the tree passed as argv[0] for .rsrc files, skipping .rsrc.df.rsrc
  2. # files, and open these. The effect of this is to create the .rsrc.df.rsrc
  3. # cache files if needed.
  4. # These are needed on OSX: the .rsrc files are in reality AppleSingle-encoded
  5. # files. We decode the resources into a datafork-based resource file.
  6. import macresource
  7. import os
  8. import sys
  9. import getopt
  10. class NoArgsError(Exception):
  11. pass
  12. def handler((verbose, force), dirname, fnames):
  13. for fn in fnames:
  14. if fn[-5:] == '.rsrc' and fn[-13:] != '.rsrc.df.rsrc':
  15. if force:
  16. try:
  17. os.unlink(os.path.join(dirname, fn + '.df.rsrc'))
  18. except IOError:
  19. pass
  20. macresource.open_pathname(os.path.join(dirname, fn), verbose=verbose)
  21. def main():
  22. try:
  23. opts, args = getopt.getopt(sys.argv[1:], 'vf')
  24. if not args:
  25. raise NoArgsError
  26. except (getopt.GetoptError, NoArgsError):
  27. sys.stderr.write('Usage: cachersrc.py dirname ...\n')
  28. sys.exit(1)
  29. verbose = 0
  30. force = 0
  31. for o, v in opts:
  32. if o == '-v':
  33. verbose = 1
  34. if o == '-f':
  35. force = 1
  36. for dir in sys.argv[1:]:
  37. os.path.walk(dir, handler, (verbose, force))
  38. if __name__ == '__main__':
  39. main()