cpython/Lib/packmail.py

113 lines
2.6 KiB
Python
Raw Normal View History

1992-04-06 11:04:58 -03:00
# Module 'packmail' -- create a self-unpacking shell archive.
# This module works on UNIX and on the Mac; the archives can unpack
# themselves only on UNIX.
1990-12-26 11:40:07 -04:00
1992-03-31 15:05:11 -04:00
import os
1990-12-26 11:40:07 -04:00
from stat import ST_MTIME
1991-04-07 10:40:56 -03:00
import string
1990-12-26 11:40:07 -04:00
1992-04-06 11:04:58 -03:00
# Print help
def help():
print 'All fns have a file open for writing as first parameter'
print 'pack(f, fullname, name): pack fullname as name'
print 'packsome(f, directory, namelist): selected files from directory'
print 'packall(f, directory): pack all files from directory'
print 'packnotolder(f, directory, name): pack all files from directory'
print ' that are not older than a file there'
print 'packtree(f, directory): pack entire directory tree'
1990-12-26 11:40:07 -04:00
# Pack one file
def pack(outfp, file, name):
fp = open(file, 'r')
1992-04-06 11:04:58 -03:00
outfp.write('echo ' + name + '\n')
outfp.write('sed "s/^X//" >"' + name + '" <<"!"\n')
1990-12-26 11:40:07 -04:00
while 1:
line = fp.readline()
if not line: break
if line[-1:] <> '\n':
line = line + '\n'
outfp.write('X' + line)
outfp.write('!\n')
1992-04-06 11:04:58 -03:00
fp.close()
1990-12-26 11:40:07 -04:00
# Pack some files from a directory
def packsome(outfp, dirname, names):
for name in names:
print name
1992-03-31 15:05:11 -04:00
file = os.path.join(dirname, name)
1990-12-26 11:40:07 -04:00
pack(outfp, file, name)
# Pack all files from a directory
def packall(outfp, dirname):
1992-03-31 15:05:11 -04:00
names = os.listdir(dirname)
try:
names.remove('.')
except:
pass
try:
names.remove('..')
except:
pass
1990-12-26 11:40:07 -04:00
names.sort()
packsome(outfp, dirname, names)
# Pack all files from a directory that are not older than a give one
def packnotolder(outfp, dirname, oldest):
1992-03-31 15:05:11 -04:00
names = os.listdir(dirname)
try:
names.remove('.')
except:
pass
try:
names.remove('..')
except:
pass
1992-03-31 15:05:11 -04:00
oldest = os.path.join(dirname, oldest)
st = os.stat(oldest)
1990-12-26 11:40:07 -04:00
mtime = st[ST_MTIME]
todo = []
for name in names:
print name, '...',
1992-03-31 15:05:11 -04:00
st = os.stat(os.path.join(dirname, name))
1990-12-26 11:40:07 -04:00
if st[ST_MTIME] >= mtime:
print 'Yes.'
todo.append(name)
else:
print 'No.'
todo.sort()
packsome(outfp, dirname, todo)
1991-04-07 10:40:56 -03:00
# Pack a whole tree (no exceptions)
def packtree(outfp, dirname):
print 'packtree', dirname
outfp.write('mkdir ' + unixfix(dirname) + '\n')
1992-03-31 15:05:11 -04:00
names = os.listdir(dirname)
try:
names.remove('.')
except:
pass
try:
names.remove('..')
except:
pass
1991-04-07 10:40:56 -03:00
subdirs = []
for name in names:
1992-03-31 15:05:11 -04:00
fullname = os.path.join(dirname, name)
if os.path.isdir(fullname):
1991-04-07 10:40:56 -03:00
subdirs.append(fullname)
else:
print 'pack', fullname
pack(outfp, fullname, unixfix(fullname))
for subdirname in subdirs:
packtree(outfp, subdirname)
def unixfix(name):
1992-04-06 11:04:58 -03:00
comps = string.splitfields(name, os.sep)
1991-04-07 10:40:56 -03:00
res = ''
for comp in comps:
if comp:
if res: res = res + '/'
res = res + comp
return res