Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 1 | # Module 'packmail' -- create a shell script out of some files. |
| 2 | |
Guido van Rossum | d9596e3 | 1992-03-31 19:05:11 +0000 | [diff] [blame^] | 3 | import os |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 4 | from stat import ST_MTIME |
Guido van Rossum | fa54064 | 1991-04-07 13:40:56 +0000 | [diff] [blame] | 5 | import string |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 6 | |
| 7 | # Pack one file |
| 8 | def pack(outfp, file, name): |
| 9 | fp = open(file, 'r') |
| 10 | outfp.write('sed "s/^X//" >' + name + ' <<"!"\n') |
| 11 | while 1: |
| 12 | line = fp.readline() |
| 13 | if not line: break |
| 14 | if line[-1:] <> '\n': |
| 15 | line = line + '\n' |
| 16 | outfp.write('X' + line) |
| 17 | outfp.write('!\n') |
| 18 | |
| 19 | # Pack some files from a directory |
| 20 | def packsome(outfp, dirname, names): |
| 21 | for name in names: |
| 22 | print name |
Guido van Rossum | d9596e3 | 1992-03-31 19:05:11 +0000 | [diff] [blame^] | 23 | file = os.path.join(dirname, name) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 24 | pack(outfp, file, name) |
| 25 | |
| 26 | # Pack all files from a directory |
| 27 | def packall(outfp, dirname): |
Guido van Rossum | d9596e3 | 1992-03-31 19:05:11 +0000 | [diff] [blame^] | 28 | names = os.listdir(dirname) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 29 | names.sort() |
| 30 | packsome(outfp, dirname, names) |
| 31 | |
| 32 | # Pack all files from a directory that are not older than a give one |
| 33 | def packnotolder(outfp, dirname, oldest): |
Guido van Rossum | d9596e3 | 1992-03-31 19:05:11 +0000 | [diff] [blame^] | 34 | names = os.listdir(dirname) |
| 35 | oldest = os.path.join(dirname, oldest) |
| 36 | st = os.stat(oldest) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 37 | mtime = st[ST_MTIME] |
| 38 | todo = [] |
| 39 | for name in names: |
| 40 | print name, '...', |
Guido van Rossum | d9596e3 | 1992-03-31 19:05:11 +0000 | [diff] [blame^] | 41 | st = os.stat(os.path.join(dirname, name)) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 42 | if st[ST_MTIME] >= mtime: |
| 43 | print 'Yes.' |
| 44 | todo.append(name) |
| 45 | else: |
| 46 | print 'No.' |
| 47 | todo.sort() |
| 48 | packsome(outfp, dirname, todo) |
Guido van Rossum | fa54064 | 1991-04-07 13:40:56 +0000 | [diff] [blame] | 49 | |
| 50 | # Pack a whole tree (no exceptions) |
| 51 | def packtree(outfp, dirname): |
| 52 | print 'packtree', dirname |
| 53 | outfp.write('mkdir ' + unixfix(dirname) + '\n') |
Guido van Rossum | d9596e3 | 1992-03-31 19:05:11 +0000 | [diff] [blame^] | 54 | names = os.listdir(dirname) |
Guido van Rossum | fa54064 | 1991-04-07 13:40:56 +0000 | [diff] [blame] | 55 | subdirs = [] |
| 56 | for name in names: |
Guido van Rossum | d9596e3 | 1992-03-31 19:05:11 +0000 | [diff] [blame^] | 57 | fullname = os.path.join(dirname, name) |
| 58 | if os.path.isdir(fullname): |
Guido van Rossum | fa54064 | 1991-04-07 13:40:56 +0000 | [diff] [blame] | 59 | subdirs.append(fullname) |
| 60 | else: |
| 61 | print 'pack', fullname |
| 62 | pack(outfp, fullname, unixfix(fullname)) |
| 63 | for subdirname in subdirs: |
| 64 | packtree(outfp, subdirname) |
| 65 | |
| 66 | def unixfix(name): |
| 67 | comps = string.splitfields(name, ':') |
| 68 | res = '' |
| 69 | for comp in comps: |
| 70 | if comp: |
| 71 | if res: res = res + '/' |
| 72 | res = res + comp |
| 73 | return res |