blob: d612c88a8e6a651215962e8785473c446b9c6aff [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
Guido van Rossumfa540641991-04-07 13:40:56 +00008import string
Guido van Rossum217a5fa1990-12-26 15:40:07 +00009
Guido van Rossumfff3a1ee1992-04-06 14:04:58 +000010# Print help
11def 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 Rossum217a5fa1990-12-26 15:40:07 +000020# Pack one file
21def pack(outfp, file, name):
22 fp = open(file, 'r')
Guido van Rossumfff3a1ee1992-04-06 14:04:58 +000023 outfp.write('echo ' + name + '\n')
Guido van Rossum217a5fa1990-12-26 15:40:07 +000024 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')
Guido van Rossumfff3a1ee1992-04-06 14:04:58 +000032 fp.close()
Guido van Rossum217a5fa1990-12-26 15:40:07 +000033
34# Pack some files from a directory
35def packsome(outfp, dirname, names):
36 for name in names:
37 print name
Guido van Rossumd9596e31992-03-31 19:05:11 +000038 file = os.path.join(dirname, name)
Guido van Rossum217a5fa1990-12-26 15:40:07 +000039 pack(outfp, file, name)
40
41# Pack all files from a directory
42def packall(outfp, dirname):
Guido van Rossumd9596e31992-03-31 19:05:11 +000043 names = os.listdir(dirname)
Guido van Rossum217a5fa1990-12-26 15:40:07 +000044 names.sort()
45 packsome(outfp, dirname, names)
46
47# Pack all files from a directory that are not older than a give one
48def packnotolder(outfp, dirname, oldest):
Guido van Rossumd9596e31992-03-31 19:05:11 +000049 names = os.listdir(dirname)
50 oldest = os.path.join(dirname, oldest)
51 st = os.stat(oldest)
Guido van Rossum217a5fa1990-12-26 15:40:07 +000052 mtime = st[ST_MTIME]
53 todo = []
54 for name in names:
55 print name, '...',
Guido van Rossumd9596e31992-03-31 19:05:11 +000056 st = os.stat(os.path.join(dirname, name))
Guido van Rossum217a5fa1990-12-26 15:40:07 +000057 if st[ST_MTIME] >= mtime:
58 print 'Yes.'
59 todo.append(name)
60 else:
61 print 'No.'
62 todo.sort()
63 packsome(outfp, dirname, todo)
Guido van Rossumfa540641991-04-07 13:40:56 +000064
65# Pack a whole tree (no exceptions)
66def packtree(outfp, dirname):
67 print 'packtree', dirname
68 outfp.write('mkdir ' + unixfix(dirname) + '\n')
Guido van Rossumd9596e31992-03-31 19:05:11 +000069 names = os.listdir(dirname)
Guido van Rossumfa540641991-04-07 13:40:56 +000070 subdirs = []
71 for name in names:
Guido van Rossumd9596e31992-03-31 19:05:11 +000072 fullname = os.path.join(dirname, name)
73 if os.path.isdir(fullname):
Guido van Rossumfa540641991-04-07 13:40:56 +000074 subdirs.append(fullname)
75 else:
76 print 'pack', fullname
77 pack(outfp, fullname, unixfix(fullname))
78 for subdirname in subdirs:
79 packtree(outfp, subdirname)
80
81def unixfix(name):
Guido van Rossumfff3a1ee1992-04-06 14:04:58 +000082 comps = string.splitfields(name, os.sep)
Guido van Rossumfa540641991-04-07 13:40:56 +000083 res = ''
84 for comp in comps:
85 if comp:
86 if res: res = res + '/'
87 res = res + comp
88 return res