annotate Mac/scripts/unshar.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents b6fadf0231af
children caa6df9e0880
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`
4813
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
72
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
73 def main():
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
74 import sys
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
75 import os
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
76 if len(sys.argv) > 1:
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
77 for fname in sys.argv[1:]:
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
78 fp = open(fname, 'r')
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
79 dir, fn = os.path.split(fname)
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
80 if dir:
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
81 os.chdir(dir)
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
82 unshar(fp)
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
83 else:
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
84 import macfs
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
85 fss, ok = macfs.StandardGetFile('TEXT')
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
86 if not ok:
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
87 sys.exit(0)
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
88 fname = fss.as_pathname()
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
89 fp = open(fname, 'r')
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
90 fss, ok = macfs.GetDirectory('Folder to save files in:')
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
91 if not ok:
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
92 sys.exit(0)
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
93 os.chdir(fss.as_pathname())
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
94 unshar(fp)
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
95
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
96 if __name__ == '__main__':
b6fadf0231af If sys.argv is empty ask for input file and output folder
Jack Jansen <jack.jansen@cwi.nl>
parents: 1795
diff changeset
97 main()