Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-old/packmail.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 1940892d2fe3 |
| children | a8df090a12ba |
| rev | line source |
|---|---|
|
810
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
1 # Module 'packmail' -- create a self-unpacking shell archive. |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
2 |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
3 # This module works on UNIX and on the Mac; the archives can unpack |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
4 # themselves only on UNIX. |
| 103 | 5 |
| 777 | 6 import os |
| 103 | 7 from stat import ST_MTIME |
|
194
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
8 import string |
| 103 | 9 |
|
810
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
10 # Print help |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
11 def help(): |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
12 print 'All fns have a file open for writing as first parameter' |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
13 print 'pack(f, fullname, name): pack fullname as name' |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
14 print 'packsome(f, directory, namelist): selected files from directory' |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
15 print 'packall(f, directory): pack all files from directory' |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
16 print 'packnotolder(f, directory, name): pack all files from directory' |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
17 print ' that are not older than a file there' |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
18 print 'packtree(f, directory): pack entire directory tree' |
|
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
19 |
| 103 | 20 # Pack one file |
| 21 def pack(outfp, file, name): | |
| 22 fp = open(file, 'r') | |
|
810
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
23 outfp.write('echo ' + name + '\n') |
| 103 | 24 outfp.write('sed "s/^X//" >' + name + ' <<"!"\n') |
| 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') | |
|
810
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
32 fp.close() |
| 103 | 33 |
| 34 # Pack some files from a directory | |
| 35 def packsome(outfp, dirname, names): | |
| 36 for name in names: | |
| 37 print name | |
| 777 | 38 file = os.path.join(dirname, name) |
| 103 | 39 pack(outfp, file, name) |
| 40 | |
| 41 # Pack all files from a directory | |
| 42 def packall(outfp, dirname): | |
| 777 | 43 names = os.listdir(dirname) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
44 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
45 names.remove('.') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
46 except: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
47 pass |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
48 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
49 names.remove('..') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
50 except: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
51 pass |
| 103 | 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): | |
| 777 | 57 names = os.listdir(dirname) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
58 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
59 names.remove('.') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
60 except: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
61 pass |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
62 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
63 names.remove('..') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
64 except: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
65 pass |
| 777 | 66 oldest = os.path.join(dirname, oldest) |
| 67 st = os.stat(oldest) | |
| 103 | 68 mtime = st[ST_MTIME] |
| 69 todo = [] | |
| 70 for name in names: | |
| 71 print name, '...', | |
| 777 | 72 st = os.stat(os.path.join(dirname, name)) |
| 103 | 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) | |
|
194
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
80 |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
81 # Pack a whole tree (no exceptions) |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
82 def packtree(outfp, dirname): |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
83 print 'packtree', dirname |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
84 outfp.write('mkdir ' + unixfix(dirname) + '\n') |
| 777 | 85 names = os.listdir(dirname) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
86 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
87 names.remove('.') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
88 except: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
89 pass |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
90 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
91 names.remove('..') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
92 except: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
810
diff
changeset
|
93 pass |
|
194
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
94 subdirs = [] |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
95 for name in names: |
| 777 | 96 fullname = os.path.join(dirname, name) |
| 97 if os.path.isdir(fullname): | |
|
194
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
98 subdirs.append(fullname) |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
99 else: |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
100 print 'pack', fullname |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
101 pack(outfp, fullname, unixfix(fullname)) |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
102 for subdirname in subdirs: |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
103 packtree(outfp, subdirname) |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
104 |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
105 def unixfix(name): |
|
810
3773b6f6dd53
Added help() fn; use os.sep
Guido van Rossum <guido@python.org>
parents:
777
diff
changeset
|
106 comps = string.splitfields(name, os.sep) |
|
194
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
107 res = '' |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
108 for comp in comps: |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
109 if comp: |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
110 if res: res = res + '/' |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
111 res = res + comp |
|
145f9e14595f
Support packing whole trees.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
112 return res |
