packmail.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Module 'packmail' -- create a self-unpacking shell archive.
  2. # This module works on UNIX and on the Mac; the archives can unpack
  3. # themselves only on UNIX.
  4. import os
  5. from stat import ST_MTIME
  6. # Print help
  7. def help():
  8. print 'All fns have a file open for writing as first parameter'
  9. print 'pack(f, fullname, name): pack fullname as name'
  10. print 'packsome(f, directory, namelist): selected files from directory'
  11. print 'packall(f, directory): pack all files from directory'
  12. print 'packnotolder(f, directory, name): pack all files from directory'
  13. print ' that are not older than a file there'
  14. print 'packtree(f, directory): pack entire directory tree'
  15. # Pack one file
  16. def pack(outfp, file, name):
  17. fp = open(file, 'r')
  18. outfp.write('echo ' + name + '\n')
  19. outfp.write('sed "s/^X//" >"' + name + '" <<"!"\n')
  20. while 1:
  21. line = fp.readline()
  22. if not line: break
  23. if line[-1:] != '\n':
  24. line = line + '\n'
  25. outfp.write('X' + line)
  26. outfp.write('!\n')
  27. fp.close()
  28. # Pack some files from a directory
  29. def packsome(outfp, dirname, names):
  30. for name in names:
  31. print name
  32. file = os.path.join(dirname, name)
  33. pack(outfp, file, name)
  34. # Pack all files from a directory
  35. def packall(outfp, dirname):
  36. names = os.listdir(dirname)
  37. try:
  38. names.remove('.')
  39. except:
  40. pass
  41. try:
  42. names.remove('..')
  43. except:
  44. pass
  45. names.sort()
  46. packsome(outfp, dirname, names)
  47. # Pack all files from a directory that are not older than a give one
  48. def packnotolder(outfp, dirname, oldest):
  49. names = os.listdir(dirname)
  50. try:
  51. names.remove('.')
  52. except:
  53. pass
  54. try:
  55. names.remove('..')
  56. except:
  57. pass
  58. oldest = os.path.join(dirname, oldest)
  59. st = os.stat(oldest)
  60. mtime = st[ST_MTIME]
  61. todo = []
  62. for name in names:
  63. print name, '...',
  64. st = os.stat(os.path.join(dirname, name))
  65. if st[ST_MTIME] >= mtime:
  66. print 'Yes.'
  67. todo.append(name)
  68. else:
  69. print 'No.'
  70. todo.sort()
  71. packsome(outfp, dirname, todo)
  72. # Pack a whole tree (no exceptions)
  73. def packtree(outfp, dirname):
  74. print 'packtree', dirname
  75. outfp.write('mkdir ' + unixfix(dirname) + '\n')
  76. names = os.listdir(dirname)
  77. try:
  78. names.remove('.')
  79. except:
  80. pass
  81. try:
  82. names.remove('..')
  83. except:
  84. pass
  85. subdirs = []
  86. for name in names:
  87. fullname = os.path.join(dirname, name)
  88. if os.path.isdir(fullname):
  89. subdirs.append(fullname)
  90. else:
  91. print 'pack', fullname
  92. pack(outfp, fullname, unixfix(fullname))
  93. for subdirname in subdirs:
  94. packtree(outfp, subdirname)
  95. def unixfix(name):
  96. comps = name.split(os.sep)
  97. res = ''
  98. for comp in comps:
  99. if comp:
  100. if res: res = res + '/'
  101. res = res + comp
  102. return res