annotate Tools/scripts/ftpmirror.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 6c07d44a7081
children e17fe79305c7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 4544
diff changeset
1 #! /usr/bin/env python
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Mirror a remote ftp subtree into a local directory tree.
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # Basic usage: ftpmirror [options] host remotedir localdir
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # XXX To do:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # - handle symbolic links
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # - back up .mirrorinfo before overwriting
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # - use pickles for .mirrorinfo?
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 import os
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 import sys
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 import time
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 import getopt
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 import string
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 import ftplib
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 from fnmatch import fnmatch
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 usage_msg = """
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 usage: ftpmirror [-v] [-q] [-i] [-m] [-n] [-r] [-s pat]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 [-l username [-p passwd [-a account]]]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 hostname [remotedir [localdir]]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 -v: verbose
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 -q: quiet
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 -i: interactive mode
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 -m: macintosh server (NCSA telnet 2.4) (implies -n -s '*.o')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 -n: don't log in
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 -r: remove files no longer pertinent
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 -l username [-p passwd [-a account]]: login info (default anonymous ftp)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 -s pat: skip files matching pattern
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 hostname: remote host
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 remotedir: remote directory (default initial)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 localdir: local directory (default current)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 """
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def usage(*args):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 sys.stdout = sys.stderr
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 for msg in args: print msg
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 print usage_msg
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 sys.exit(2)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 verbose = 1 # 0 for -q, 2 for -v
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 interactive = 0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 mac = 0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 rmok = 0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 nologin = 0
3374
0c8821bf6db5 don't die on unreadable files etc.
Guido van Rossum <guido@python.org>
parents: 2761
diff changeset
46 skippats = ['.', '..', '.mirrorinfo']
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 def main():
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 global verbose, interactive, mac, rmok, nologin
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 try:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 opts, args = getopt.getopt(sys.argv[1:], 'a:bil:mnp:qrs:v')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 except getopt.error, msg:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 usage(msg)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 login = ''
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 passwd = ''
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 account = ''
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 for o, a in opts:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 if o == '-l': login = a
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 if o == '-p': passwd = a
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 if o == '-a': account = a
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 if o == '-v': verbose = verbose + 1
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if o == '-q': verbose = 0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 if o == '-i': interactive = 1
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 if o == '-m': mac = 1; nologin = 1; skippats.append('*.o')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 if o == '-n': nologin = 1
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if o == '-r': rmok = 1
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 if o == '-s': skippats.append(a)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 if not args: usage('hostname missing')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 host = args[0]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 remotedir = ''
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 localdir = ''
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 if args[1:]:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 remotedir = args[1]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 if args[2:]:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 localdir = args[2]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 if args[3:]: usage('too many arguments')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 #
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 f = ftplib.FTP()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 if verbose: print 'Connecting to %s...' % host
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 f.connect(host)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 if not nologin:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 if verbose:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 print 'Logging in as %s...' % (login or 'anonymous')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 f.login(login, passwd, account)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 if verbose: print 'OK.'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 pwd = f.pwd()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 if verbose > 1: print 'PWD =', `pwd`
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if remotedir:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 if verbose > 1: print 'cwd(%s)' % `remotedir`
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 f.cwd(remotedir)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 if verbose > 1: print 'OK.'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 pwd = f.pwd()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 if verbose > 1: print 'PWD =', `pwd`
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 #
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 mirrorsubdir(f, localdir)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 def mirrorsubdir(f, localdir):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 pwd = f.pwd()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 if localdir and not os.path.isdir(localdir):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 if verbose: print 'Creating local directory', localdir
3495
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
101 try:
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
102 makedir(localdir)
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
103 except os.error, msg:
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
104 print "Failed to establish local directory", localdir
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
105 return
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 infofilename = os.path.join(localdir, '.mirrorinfo')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 try:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 text = open(infofilename, 'r').read()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 except IOError, msg:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 text = '{}'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 try:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 info = eval(text)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 except (SyntaxError, NameError):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 print 'Bad mirror info in %s' % infofilename
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 info = {}
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 subdirs = []
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 listing = []
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if verbose: print 'Listing remote directory %s...' % pwd
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 f.retrlines('LIST', listing.append)
4544
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
120 filesfound = []
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 for line in listing:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 if verbose > 1: print '-->', `line`
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 if mac:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 # Mac listing has just filenames;
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 # trailing / means subdirectory
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 filename = string.strip(line)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 mode = '-'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 if filename[-1:] == '/':
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 filename = filename[:-1]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 mode = 'd'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 infostuff = ''
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 else:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 # Parse, assuming a UNIX listing
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 words = string.split(line)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 if len(words) < 6:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 if verbose > 1: print 'Skipping short line'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 continue
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 if words[-2] == '->':
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 if verbose > 1:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 print 'Skipping symbolic link %s -> %s' % \
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 (words[-3], words[-1])
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 continue
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 filename = words[-1]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 infostuff = words[-5:-1]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 mode = words[0]
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 skip = 0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 for pat in skippats:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 if fnmatch(filename, pat):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 if verbose > 1:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 print 'Skip pattern', pat,
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 print 'matches', filename
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 skip = 1
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 break
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 if skip:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 continue
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 if mode[0] == 'd':
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 if verbose > 1:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 print 'Remembering subdirectory', filename
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 subdirs.append(filename)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 continue
4544
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
161 filesfound.append(filename)
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 if info.has_key(filename) and info[filename] == infostuff:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 if verbose > 1:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 print 'Already have this version of', filename
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 continue
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 fullname = os.path.join(localdir, filename)
3495
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
167 tempname = os.path.join(localdir, '@'+filename)
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 if interactive:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 doit = askabout('file', filename, pwd)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 if not doit:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 if not info.has_key(filename):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 info[filename] = 'Not retrieved'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 continue
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 try:
3495
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
175 os.unlink(tempname)
3374
0c8821bf6db5 don't die on unreadable files etc.
Guido van Rossum <guido@python.org>
parents: 2761
diff changeset
176 except os.error:
0c8821bf6db5 don't die on unreadable files etc.
Guido van Rossum <guido@python.org>
parents: 2761
diff changeset
177 pass
0c8821bf6db5 don't die on unreadable files etc.
Guido van Rossum <guido@python.org>
parents: 2761
diff changeset
178 try:
5532
30d559827047 Open the output files with 'wb', not 'w'.
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
179 fp = open(tempname, 'wb')
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 except IOError, msg:
3495
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
181 print "Can't create %s: %s" % (tempname, str(msg))
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 continue
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 if verbose:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 print 'Retrieving %s from %s as %s...' % \
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 (filename, pwd, fullname)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 if verbose:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 fp1 = LoggingFile(fp, 1024, sys.stdout)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 else:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 fp1 = fp
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 t0 = time.time()
3374
0c8821bf6db5 don't die on unreadable files etc.
Guido van Rossum <guido@python.org>
parents: 2761
diff changeset
191 try:
0c8821bf6db5 don't die on unreadable files etc.
Guido van Rossum <guido@python.org>
parents: 2761
diff changeset
192 f.retrbinary('RETR ' + filename, fp1.write, 8*1024)
0c8821bf6db5 don't die on unreadable files etc.
Guido van Rossum <guido@python.org>
parents: 2761
diff changeset
193 except ftplib.error_perm, msg:
0c8821bf6db5 don't die on unreadable files etc.
Guido van Rossum <guido@python.org>
parents: 2761
diff changeset
194 print msg
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 t1 = time.time()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 bytes = fp.tell()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 fp.close()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 if fp1 != fp:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 fp1.close()
3495
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
200 try:
5546
6c07d44a7081 Unlink before rename (for NT).
Guido van Rossum <guido@python.org>
parents: 5532
diff changeset
201 os.unlink(fullname)
6c07d44a7081 Unlink before rename (for NT).
Guido van Rossum <guido@python.org>
parents: 5532
diff changeset
202 except os.error:
6c07d44a7081 Unlink before rename (for NT).
Guido van Rossum <guido@python.org>
parents: 5532
diff changeset
203 pass # Ignore the error
6c07d44a7081 Unlink before rename (for NT).
Guido van Rossum <guido@python.org>
parents: 5532
diff changeset
204 try:
6c07d44a7081 Unlink before rename (for NT).
Guido van Rossum <guido@python.org>
parents: 5532
diff changeset
205 os.rename(tempname, fullname)
3495
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
206 except os.error, msg:
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
207 print "Can't rename %s to %s: %s" % (tempname,
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
208 fullname,
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
209 str(msg))
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
210 continue
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 info[filename] = infostuff
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 writedict(info, infofilename)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 if verbose:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 dt = t1 - t0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 kbytes = bytes / 1024.0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 print int(round(kbytes)),
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 print 'Kbytes in',
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 print int(round(dt)),
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 print 'seconds',
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 if t1 > t0:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 print '(~%d Kbytes/sec)' % \
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 int(round(kbytes/dt),)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 print
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 #
4544
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
225 # Remove files from info that are no longer remote
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
226 deletions = 0
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
227 for filename in info.keys():
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
228 if filename not in filesfound:
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
229 if verbose:
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
230 print "Removing obsolete info entry for",
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
231 print filename, "in", localdir or "."
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
232 del info[filename]
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
233 deletions = deletions + 1
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
234 if deletions:
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
235 writedict(info, infofilename)
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
236 #
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 # Remove local files that are no longer in the remote directory
3495
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
238 try:
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
239 if not localdir: names = os.listdir(os.curdir)
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
240 else: names = os.listdir(localdir)
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
241 except os.error:
f70dbe608f03 Create files as temp file and move to real location only when complete.
Guido van Rossum <guido@python.org>
parents: 3374
diff changeset
242 names = []
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 for name in names:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 if name[0] == '.' or info.has_key(name) or name in subdirs:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 continue
4544
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
246 skip = 0
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
247 for pat in skippats:
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
248 if fnmatch(name, pat):
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
249 if verbose > 1:
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
250 print 'Skip pattern', pat,
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
251 print 'matches', name
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
252 skip = 1
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
253 break
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
254 if skip:
8d0b00ed8c2b Fixed a bug: files that no longer exist remotely would never be
Guido van Rossum <guido@python.org>
parents: 3495
diff changeset
255 continue
2761
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 fullname = os.path.join(localdir, name)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 if not rmok:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 if verbose:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 print 'Local file', fullname,
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 print 'is no longer pertinent'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 continue
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 if verbose: print 'Removing local file', fullname
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 try:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 os.unlink(fullname)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 except os.error, msg:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 print "Can't remove local file %s: %s" % \
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 (fullname, str(msg))
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 #
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 # Recursively mirror subdirectories
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 for subdir in subdirs:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 if interactive:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 doit = askabout('subdirectory', subdir, pwd)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 if not doit: continue
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 if verbose: print 'Processing subdirectory', subdir
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 localsubdir = os.path.join(localdir, subdir)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 pwd = f.pwd()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 if verbose > 1:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 print 'Remote directory now:', pwd
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 print 'Remote cwd', subdir
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 try:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 f.cwd(subdir)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 except ftplib.error_perm, msg:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 print "Can't chdir to", subdir, ":", msg
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 else:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 if verbose: print 'Mirroring as', localsubdir
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 mirrorsubdir(f, localsubdir)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 if verbose > 1: print 'Remote cwd ..'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 f.cwd('..')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 newpwd = f.pwd()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 if newpwd != pwd:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 print 'Ended up in wrong directory after cd + cd ..'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 print 'Giving up now.'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 break
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 else:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 if verbose > 1: print 'OK.'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 # Wrapper around a file for writing to write a hash sign every block.
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 class LoggingFile:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 def __init__(self, fp, blocksize, outfp):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 self.fp = fp
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 self.bytes = 0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 self.hashes = 0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 self.blocksize = blocksize
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 self.outfp = outfp
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 def write(self, data):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 self.bytes = self.bytes + len(data)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 hashes = int(self.bytes) / self.blocksize
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 while hashes > self.hashes:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 self.outfp.write('#')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 self.outfp.flush()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 self.hashes = self.hashes + 1
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 self.fp.write(data)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 def close(self):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 self.outfp.write('\n')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
315
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 # Ask permission to download a file.
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 def askabout(filetype, filename, pwd):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 prompt = 'Retrieve %s %s from %s ? [ny] ' % (filetype, filename, pwd)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 while 1:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 reply = string.lower(string.strip(raw_input(prompt)))
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 if reply in ['y', 'ye', 'yes']:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 return 1
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 if reply in ['', 'n', 'no', 'nop', 'nope']:
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 return 0
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 print 'Please answer yes or no.'
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
326
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 # Create a directory if it doesn't exist. Recursively create the
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 # parent directory as well if needed.
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 def makedir(pathname):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 if os.path.isdir(pathname):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 return
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 dirname = os.path.dirname(pathname)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 if dirname: makedir(dirname)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 os.mkdir(pathname, 0777)
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 # Write a dictionary to a file in a way that can be read back using
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 # rval() but is still somewhat readable (i.e. not a single long line).
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 def writedict(dict, filename):
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 fp = open(filename, 'w')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 fp.write('{\n')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 for key, value in dict.items():
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 fp.write('%s: %s,\n' % (`key`, `value`))
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 fp.write('}\n')
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 fp.close()
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
b40cabfdc91d added ftp mirror script
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 main()