blob: 9c2b5662f61f96d534a24c2073f0774ceadfea76 [file] [log] [blame]
Guido van Rossumfff3a1ee1992-04-06 14:04:58 +00001# 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 Rossum217a5fa1990-12-26 15:40:07 +00005
Guido van Rossumd9596e31992-03-31 19:05:11 +00006import os
Guido van Rossum217a5fa1990-12-26 15:40:07 +00007from stat import ST_MTIME
8
Guido van Rossumfff3a1ee1992-04-06 14:04:58 +00009# Print help
10def 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 Rossum217a5fa1990-12-26 15:40:07 +000019# Pack one file
20def pack(outfp, file, name):
21 fp = open(file, 'r')
Guido van Rossumfff3a1ee1992-04-06 14:04:58 +000022 outfp.write('echo ' + name + '\n')
Guido van Rossum9c93a691998-04-28 16:03:03 +000023 outfp.write('sed "s/^X//" >"' + name + '" <<"!"\n')
Guido van Rossum217a5fa1990-12-26 15:40:07 +000024 while 1:
25 line = fp.readline()
26 if not line: break
Fred Drake132dce22000-12-12 23:11:42 +000027 if line[-1:] != '\n':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000028 line = line + '\n'
29 outfp.write('X' + line)
30 outfp.write('!\n')
Guido van Rossumfff3a1ee1992-04-06 14:04:58 +000031 fp.close()
Guido van Rossum217a5fa1990-12-26 15:40:07 +000032
33# Pack some files from a directory
34def packsome(outfp, dirname, names):
35 for name in names:
36 print name
Guido van Rossumd9596e31992-03-31 19:05:11 +000037 file = os.path.join(dirname, name)
Guido van Rossum217a5fa1990-12-26 15:40:07 +000038 pack(outfp, file, name)
39
40# Pack all files from a directory
41def packall(outfp, dirname):
Guido van Rossumd9596e31992-03-31 19:05:11 +000042 names = os.listdir(dirname)
Guido van Rossumb6775db1994-08-01 11:34:53 +000043 try:
44 names.remove('.')
45 except:
46 pass
47 try:
48 names.remove('..')
49 except:
50 pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +000051 names.sort()
52 packsome(outfp, dirname, names)
53
54# Pack all files from a directory that are not older than a give one
55def packnotolder(outfp, dirname, oldest):
Guido van Rossumd9596e31992-03-31 19:05:11 +000056 names = os.listdir(dirname)
Guido van Rossumb6775db1994-08-01 11:34:53 +000057 try:
58 names.remove('.')
59 except:
60 pass
61 try:
62 names.remove('..')
63 except:
64 pass
Guido van Rossumd9596e31992-03-31 19:05:11 +000065 oldest = os.path.join(dirname, oldest)
66 st = os.stat(oldest)
Guido van Rossum217a5fa1990-12-26 15:40:07 +000067 mtime = st[ST_MTIME]
68 todo = []
69 for name in names:
70 print name, '...',
Guido van Rossumd9596e31992-03-31 19:05:11 +000071 st = os.stat(os.path.join(dirname, name))
Guido van Rossum217a5fa1990-12-26 15:40:07 +000072 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 Rossumfa540641991-04-07 13:40:56 +000079
80# Pack a whole tree (no exceptions)
81def packtree(outfp, dirname):
82 print 'packtree', dirname
83 outfp.write('mkdir ' + unixfix(dirname) + '\n')
Guido van Rossumd9596e31992-03-31 19:05:11 +000084 names = os.listdir(dirname)
Guido van Rossumb6775db1994-08-01 11:34:53 +000085 try:
86 names.remove('.')
87 except:
88 pass
89 try:
90 names.remove('..')
91 except:
92 pass
Guido van Rossumfa540641991-04-07 13:40:56 +000093 subdirs = []
94 for name in names:
Guido van Rossumd9596e31992-03-31 19:05:11 +000095 fullname = os.path.join(dirname, name)
96 if os.path.isdir(fullname):
Guido van Rossumfa540641991-04-07 13:40:56 +000097 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
104def unixfix(name):
Eric S. Raymond13b3ba42001-02-09 16:25:20 +0000105 comps = name.split(os.sep)
Guido van Rossumfa540641991-04-07 13:40:56 +0000106 res = ''
107 for comp in comps:
108 if comp:
109 if res: res = res + '/'
110 res = res + comp
111 return res