changes by Barry, e.g. font lock & email addresses
[python/dscho.git] / Mac / scripts / unshar.py
blobfa60e5a70684a5d5b17c782cfeac614d0c1fc67f
1 # 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)
8 import string
10 def 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`