annotate Mac/scripts/unshar.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents fa8bbd09f2e6
children b6fadf0231af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1795
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Extract files from a SHAR archive.
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Run this on the Mac.
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Usage:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # >>> import unshar
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # >>> f = open('SHAR')
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # >>> unshar.unshar(f)
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import string
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 def unshar(fp, verbose=0, overwrite=0):
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 ofp = None
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 file = None
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 while 1:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 line = fp.readline()
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 if verbose > 3: print 'Got:', `line`
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 if line[:1] == 'X':
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # Most common case first
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 if ofp: ofp.write(line[1:])
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 continue
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 if not line:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 if verbose: print 'EOF'
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 if ofp:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 print 'Unterminated file -- closing'
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 ofp.close()
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 ofp = None
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 break
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 if line[0] == '#':
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 if verbose: print line,
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 continue
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 if line[:14] == 'sed "s/^X//" >':
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 if verbose: print "!!!", `line`
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 i = string.find(line, "'")
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 j = string.find(line, "'", i+1)
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 if i >= 0 and j > i:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 file = line[i+1:j]
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 if '/' in file:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 words = string.splitfields(file, '/')
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 for funny in '', '.':
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 while funny in words: words.remove(funny)
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 for i in range(len(words)):
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 if words[i] == '..': words[i] = ''
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 words.insert(0, '')
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 file = string.joinfields(words, ':')
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 try:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 ofp = open(file, 'r')
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 ofp.close()
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 ofp = None
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 over = 1
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 except IOError:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 over = 0
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 if over and not overwrite:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 print 'Skipping', file, '(already exists) ...'
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 continue
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 ofp = open(file, 'w')
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 if over:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 print 'Overwriting', file, '...'
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 else:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 print 'Writing', file, '...'
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 continue
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 if line == 'END_OF_FILE\n':
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 if not file:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 print 'Unexpected END_OF_FILE marker'
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 if ofp:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 print 'done'
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 ofp.close()
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 ofp = None
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 else:
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 print 'done skipping'
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 file = None
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 continue
fa8bbd09f2e6 Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 if verbose: print "...", `line`