blob: a90ee342bd655c9bb52a116089633604a2886058 [file] [log] [blame]
Guido van Rossumd4d77281994-08-19 10:51:31 +00001# Extract files from a SHAR archive.
2# Run this on the Mac.
3# Usage:
4# >>> import unshar
5# >>> f = open('SHAR')
6# >>> unshar.unshar(f)
7
8import string
9
10def unshar(fp, verbose=0, overwrite=0):
11 ofp = None
12 file = None
13 while 1:
14 line = fp.readline()
15 if verbose > 3: print 'Got:', `line`
16 if line[:1] == 'X':
17 # Most common case first
18 if ofp: ofp.write(line[1:])
19 continue
20 if not line:
21 if verbose: print 'EOF'
22 if ofp:
23 print 'Unterminated file -- closing'
24 ofp.close()
25 ofp = None
26 break
27 if line[0] == '#':
28 if verbose: print line,
29 continue
30 if line[:14] == 'sed "s/^X//" >':
31 if verbose: print "!!!", `line`
32 i = string.find(line, "'")
33 j = string.find(line, "'", i+1)
34 if i >= 0 and j > i:
35 file = line[i+1:j]
36 if '/' in file:
37 words = string.splitfields(file, '/')
38 for funny in '', '.':
39 while funny in words: words.remove(funny)
40 for i in range(len(words)):
41 if words[i] == '..': words[i] = ''
42 words.insert(0, '')
43 file = string.joinfields(words, ':')
44 try:
45 ofp = open(file, 'r')
46 ofp.close()
47 ofp = None
48 over = 1
49 except IOError:
50 over = 0
51 if over and not overwrite:
52 print 'Skipping', file, '(already exists) ...'
53 continue
54 ofp = open(file, 'w')
55 if over:
56 print 'Overwriting', file, '...'
57 else:
58 print 'Writing', file, '...'
59 continue
60 if line == 'END_OF_FILE\n':
61 if not file:
62 print 'Unexpected END_OF_FILE marker'
63 if ofp:
64 print 'done'
65 ofp.close()
66 ofp = None
67 else:
68 print 'done skipping'
69 file = None
70 continue
71 if verbose: print "...", `line`
Jack Jansen8bd7c561996-12-23 16:54:51 +000072
73def main():
74 import sys
75 import os
76 if len(sys.argv) > 1:
77 for fname in sys.argv[1:]:
78 fp = open(fname, 'r')
79 dir, fn = os.path.split(fname)
80 if dir:
81 os.chdir(dir)
82 unshar(fp)
83 else:
84 import macfs
85 fss, ok = macfs.StandardGetFile('TEXT')
86 if not ok:
87 sys.exit(0)
88 fname = fss.as_pathname()
89 fp = open(fname, 'r')
90 fss, ok = macfs.GetDirectory('Folder to save files in:')
91 if not ok:
92 sys.exit(0)
93 os.chdir(fss.as_pathname())
94 unshar(fp)
95
96if __name__ == '__main__':
97 main()