annotate Lib/distutils/file_util.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents a3ea65bfe370
children b2c776857dc4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
1 """distutils.file_util
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
2
14862
5b8cda6f3b5f Whitespace fix.
Greg Ward <gward@python.net>
parents: 14778
diff changeset
3 Utility functions for operating on single files.
5b8cda6f3b5f Whitespace fix.
Greg Ward <gward@python.net>
parents: 14778
diff changeset
4 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
5
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
6 __revision__ = "$Id$"
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
7
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
8 import os
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
9 from distutils.errors import DistutilsFileError
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21862
diff changeset
10 from distutils import log
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
11
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
12 # for generating verbose output in 'copy_file()'
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
13 _copy_action = { None: 'copying',
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
14 'hard': 'hard linking',
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
15 'sym': 'symbolically linking' }
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
16
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
17
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
18 def _copy_file_contents (src, dst, buffer_size=16*1024):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
19 """Copy the file 'src' to 'dst'; both must be filenames. Any error
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
20 opening either file, reading from 'src', or writing to 'dst', raises
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
21 DistutilsFileError. Data is read/written in chunks of 'buffer_size'
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
22 bytes (default 16k). No attempt is made to handle anything apart from
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
23 regular files.
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
24 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
25 # Stolen from shutil module in the standard library, but with
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
26 # custom error-handling added.
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
27
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
28 fsrc = None
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
29 fdst = None
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
30 try:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
31 try:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
32 fsrc = open(src, 'rb')
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
33 except os.error, (errno, errstr):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
34 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
35 "could not open '%s': %s" % (src, errstr)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19115
diff changeset
36
21862
53ac8284217a [Bug #220993; may also fix bug #479469] Fix flakiness when old
Andrew M. Kuchling <amk@amk.ca>
parents: 21267
diff changeset
37 if os.path.exists(dst):
53ac8284217a [Bug #220993; may also fix bug #479469] Fix flakiness when old
Andrew M. Kuchling <amk@amk.ca>
parents: 21267
diff changeset
38 try:
53ac8284217a [Bug #220993; may also fix bug #479469] Fix flakiness when old
Andrew M. Kuchling <amk@amk.ca>
parents: 21267
diff changeset
39 os.unlink(dst)
53ac8284217a [Bug #220993; may also fix bug #479469] Fix flakiness when old
Andrew M. Kuchling <amk@amk.ca>
parents: 21267
diff changeset
40 except os.error, (errno, errstr):
53ac8284217a [Bug #220993; may also fix bug #479469] Fix flakiness when old
Andrew M. Kuchling <amk@amk.ca>
parents: 21267
diff changeset
41 raise DistutilsFileError, \
53ac8284217a [Bug #220993; may also fix bug #479469] Fix flakiness when old
Andrew M. Kuchling <amk@amk.ca>
parents: 21267
diff changeset
42 "could not delete '%s': %s" % (dst, errstr)
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 26285
diff changeset
43
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
44 try:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
45 fdst = open(dst, 'wb')
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
46 except os.error, (errno, errstr):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
47 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
48 "could not create '%s': %s" % (dst, errstr)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19115
diff changeset
49
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
50 while 1:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
51 try:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
52 buf = fsrc.read(buffer_size)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
53 except os.error, (errno, errstr):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
54 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
55 "could not read from '%s': %s" % (src, errstr)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19115
diff changeset
56
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
57 if not buf:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
58 break
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
59
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
60 try:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
61 fdst.write(buf)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
62 except os.error, (errno, errstr):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
63 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
64 "could not write to '%s': %s" % (dst, errstr)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19115
diff changeset
65
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
66 finally:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
67 if fdst:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
68 fdst.close()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
69 if fsrc:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
70 fsrc.close()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
71
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
72 # _copy_file_contents()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
73
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
74 def copy_file (src, dst,
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
75 preserve_mode=1,
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
76 preserve_times=1,
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
77 update=0,
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
78 link=None,
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
79 verbose=1,
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
80 dry_run=0):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
81
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
82 """Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src' is
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
83 copied there with the same name; otherwise, it must be a filename. (If
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
84 the file exists, it will be ruthlessly clobbered.) If 'preserve_mode'
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
85 is true (the default), the file's mode (type and permission bits, or
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
86 whatever is analogous on the current platform) is copied. If
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
87 'preserve_times' is true (the default), the last-modified and
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
88 last-access times are copied as well. If 'update' is true, 'src' will
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
89 only be copied if 'dst' does not exist, or if 'dst' does exist but is
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21862
diff changeset
90 older than 'src'.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
91
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
92 'link' allows you to make hard links (os.link) or symbolic links
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
93 (os.symlink) instead of copying: set it to "hard" or "sym"; if it is
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
94 None (the default), files are copied. Don't set 'link' on systems that
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
95 don't support it: 'copy_file()' doesn't check if hard or symbolic
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
96 linking is available.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
97
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
98 Under Mac OS, uses the native file copy function in macostools; on
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
99 other systems, uses '_copy_file_contents()' to copy file contents.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
100
14942
e15e97a6c14a Changed 'copy_file()' so it returns a tuple (dest_name, copied) -- hopefully,
Greg Ward <gward@python.net>
parents: 14862
diff changeset
101 Return a tuple (dest_name, copied): 'dest_name' is the actual name of
e15e97a6c14a Changed 'copy_file()' so it returns a tuple (dest_name, copied) -- hopefully,
Greg Ward <gward@python.net>
parents: 14862
diff changeset
102 the output file, and 'copied' is true if the file was copied (or would
e15e97a6c14a Changed 'copy_file()' so it returns a tuple (dest_name, copied) -- hopefully,
Greg Ward <gward@python.net>
parents: 14862
diff changeset
103 have been copied, if 'dry_run' true).
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
104 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
105 # XXX if the destination file already exists, we clobber it if
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
106 # copying, but blow up if linking. Hmmm. And I don't know what
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
107 # macostools.copyfile() does. Should definitely be consistent, and
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
108 # should probably blow up if destination exists and we would be
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
109 # changing it (ie. it's not already a hard/soft link to src OR
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
110 # (not update) and (src newer than dst).
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
111
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
112 from distutils.dep_util import newer
18830
fdb388f2df4e Don't "import *" from stat at all -- just import what's needed, and
Greg Ward <gward@python.net>
parents: 16370
diff changeset
113 from stat import ST_ATIME, ST_MTIME, ST_MODE, S_IMODE
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
114
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
115 if not os.path.isfile(src):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
116 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
117 "can't copy '%s': doesn't exist or not a regular file" % src
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
118
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
119 if os.path.isdir(dst):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
120 dir = dst
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
121 dst = os.path.join(dst, os.path.basename(src))
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
122 else:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
123 dir = os.path.dirname(dst)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
124
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
125 if update and not newer(src, dst):
51366
a3ea65bfe370 using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51360
diff changeset
126 if verbose >= 1:
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
127 log.debug("not copying %s (output up-to-date)", src)
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21862
diff changeset
128 return dst, 0
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
129
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
130 try:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
131 action = _copy_action[link]
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
132 except KeyError:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
133 raise ValueError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
134 "invalid value '%s' for 'link' argument" % link
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
135
51366
a3ea65bfe370 using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51360
diff changeset
136 if verbose >= 1:
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
137 if os.path.basename(dst) == os.path.basename(src):
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
138 log.info("%s %s -> %s", action, src, dir)
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
139 else:
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
140 log.info("%s %s -> %s", action, src, dst)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19115
diff changeset
141
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
142 if dry_run:
14942
e15e97a6c14a Changed 'copy_file()' so it returns a tuple (dest_name, copied) -- hopefully,
Greg Ward <gward@python.net>
parents: 14862
diff changeset
143 return (dst, 1)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
144
14942
e15e97a6c14a Changed 'copy_file()' so it returns a tuple (dest_name, copied) -- hopefully,
Greg Ward <gward@python.net>
parents: 14862
diff changeset
145 # On Mac OS, use the native file copy routine
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
146 if os.name == 'mac':
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
147 import macostools
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
148 try:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
149 macostools.copy(src, dst, 0, preserve_times)
12454
486161894507 Sporadic, untested Python 1.5.1 compatibility changes.
Greg Ward <gward@python.net>
parents: 12226
diff changeset
150 except os.error, exc:
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
151 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
152 "could not copy '%s' to '%s': %s" % (src, dst, exc[-1])
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19115
diff changeset
153
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
154 # If linking (hard or symbolic), use the appropriate system call
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
155 # (Unix only, of course, but that's the caller's responsibility)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
156 elif link == 'hard':
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
157 if not (os.path.exists(dst) and os.path.samefile(src, dst)):
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
158 os.link(src, dst)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
159 elif link == 'sym':
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
160 if not (os.path.exists(dst) and os.path.samefile(src, dst)):
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
161 os.symlink(src, dst)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
162
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
163 # Otherwise (non-Mac, not linking), copy the file contents and
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
164 # (optionally) copy the times and mode.
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
165 else:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
166 _copy_file_contents(src, dst)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
167 if preserve_mode or preserve_times:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
168 st = os.stat(src)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
169
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
170 # According to David Ascher <da@ski.org>, utime() should be done
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
171 # before chmod() (at least under NT).
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
172 if preserve_times:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
173 os.utime(dst, (st[ST_ATIME], st[ST_MTIME]))
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
174 if preserve_mode:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
175 os.chmod(dst, S_IMODE(st[ST_MODE]))
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
176
14942
e15e97a6c14a Changed 'copy_file()' so it returns a tuple (dest_name, copied) -- hopefully,
Greg Ward <gward@python.net>
parents: 14862
diff changeset
177 return (dst, 1)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
178
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
179 # copy_file ()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
180
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
181
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
182 # XXX I suspect this is Unix-specific -- need porting help!
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
183 def move_file (src, dst,
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
184 verbose=1,
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
185 dry_run=0):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
186
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
187 """Move a file 'src' to 'dst'. If 'dst' is a directory, the file will
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
188 be moved into it with the same name; otherwise, 'src' is just renamed
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
189 to 'dst'. Return the new full name of the file.
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
190
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
191 Handles cross-device moves on Unix using 'copy_file()'. What about
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
192 other systems???
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
193 """
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
194 from os.path import exists, isfile, isdir, basename, dirname
19115
17525b6c532c Import the errno module
Andrew M. Kuchling <amk@amk.ca>
parents: 18830
diff changeset
195 import errno
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19115
diff changeset
196
51366
a3ea65bfe370 using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51360
diff changeset
197 if verbose >= 1:
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
198 log.info("moving %s -> %s", src, dst)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
199
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
200 if dry_run:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
201 return dst
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
202
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
203 if not isfile(src):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
204 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
205 "can't move '%s': not a regular file" % src
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
206
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
207 if isdir(dst):
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
208 dst = os.path.join(dst, basename(src))
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
209 elif exists(dst):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
210 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
211 "can't move '%s': destination '%s' already exists" % \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
212 (src, dst)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
213
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
214 if not isdir(dirname(dst)):
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
215 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
216 "can't move '%s': destination '%s' not a valid path" % \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
217 (src, dst)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
218
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
219 copy_it = 0
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
220 try:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
221 os.rename(src, dst)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
222 except os.error, (num, msg):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
223 if num == errno.EXDEV:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
224 copy_it = 1
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
225 else:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
226 raise DistutilsFileError, \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
227 "couldn't move '%s' to '%s': %s" % (src, dst, msg)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
228
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
229 if copy_it:
51360
50d89436aeab Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
230 copy_file(src, dst, verbose=verbose)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
231 try:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
232 os.unlink(src)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
233 except os.error, (num, msg):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
234 try:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
235 os.unlink(dst)
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
236 except os.error:
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
237 pass
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
238 raise DistutilsFileError, \
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19115
diff changeset
239 ("couldn't move '%s' to '%s' by copy/delete: " +
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
240 "delete '%s' failed: %s") % \
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
241 (src, dst, src, msg)
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
242
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
243 return dst
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
244
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
245 # move_file ()
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
246
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
247
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
248 def write_file (filename, contents):
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
249 """Create a file with the specified name and write 'contents' (a
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
250 sequence of strings without line terminators) to it.
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
251 """
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
252 f = open(filename, "w")
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff changeset
253 for line in contents:
14778
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
254 f.write(line + "\n")
8fae3f91161f Reformat docstrings.
Greg Ward <gward@python.net>
parents: 12989
diff changeset
255 f.close()