annotate Lib/shutil.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 6ab392d7a52f
children 8c129a567867
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
1 """Utility functions for copying files.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
2
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
3 XXX The functions here don't copy the data fork or other metadata on Mac.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
4
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
5 """
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
771
2e82dd0f8a9e posix -> os
Guido van Rossum <guido@python.org>
parents: 363
diff changeset
7 import os
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
8 import stat
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 def copyfile(src, dst):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
12 """Copy data from src to dst"""
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
13 fsrc = None
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
14 fdst = None
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
15 try:
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
16 fsrc = open(src, 'rb')
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
17 fdst = open(dst, 'wb')
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
18 while 1:
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
19 buf = fsrc.read(16*1024)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
20 if not buf:
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
21 break
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
22 fdst.write(buf)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
23 finally:
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
24 if fdst:
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
25 fdst.close()
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
26 if fsrc:
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
27 fsrc.close()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 def copymode(src, dst):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
30 """Copy mode bits from src to dst"""
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
31 st = os.stat(src)
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
32 mode = stat.S_IMODE(st[stat.ST_MODE])
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
33 os.chmod(dst, mode)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def copystat(src, dst):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
36 """Copy all stat info (mode bits, atime and mtime) from src to dst"""
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
37 st = os.stat(src)
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
38 mode = stat.S_IMODE(st[stat.ST_MODE])
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
39 os.chmod(dst, mode)
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
40 os.utime(dst, (st[stat.ST_ATIME], st[stat.ST_MODE]))
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
41
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 def copy(src, dst):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
44 """Copy data and mode bits ("cp src dst").
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
45
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
46 The destination may be a directory.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
47
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
48 """
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
49 if os.path.isdir(dst):
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
50 dst = os.path.join(dst, os.path.basename(src))
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
51 copyfile(src, dst)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
52 copymode(src, dst)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 def copy2(src, dst):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
55 """Copy data and all stat info ("cp -p src dst").
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
56
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
57 The destination may be a directory.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
58
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
59 """
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
60 if os.path.isdir(dst):
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
61 dst = os.path.join(dst, os.path.basename(src))
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
62 copyfile(src, dst)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
63 copystat(src, dst)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
65
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
66 def copytree(src, dst, symlinks=0):
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
67 """Recursively copy a directory tree using copy2().
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
68
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
69 The destination directory must not already exist.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
70 Error are reported to standard output.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
71
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
72 If the optional symlinks flag is true, symbolic links in the
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
73 source tree result in symbolic links in the destination tree; if
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
74 it is false, the contents of the files pointed to by symbolic
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
75 links are copied.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
76
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
77 XXX Consider this example code rather than the ultimate tool.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
78
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
79 """
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
80 names = os.listdir(src)
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
81 os.mkdir(dst)
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
82 for name in names:
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
83 srcname = os.path.join(src, name)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
84 dstname = os.path.join(dst, name)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
85 try:
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
86 if symlinks and os.path.islink(srcname):
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
87 linkto = os.readlink(srcname)
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
88 os.symlink(linkto, dstname)
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
89 elif os.path.isdir(srcname):
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
90 copytree(srcname, dstname)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
91 else:
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
92 copy2(srcname, dstname)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
93 # XXX What about devices, sockets etc.?
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
94 except (IOError, os.error), why:
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
95 print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))