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