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