Mercurial > cpython-lru_cache-weakref
annotate Lib/distutils/file_util.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | a17b21c27a33 |
| children |
| 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 | 3 Utility functions for operating on single files. |
| 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()' |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
13 _copy_action = {None: 'copying', |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
14 'hard': 'hard linking', |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
15 'sym': 'symbolically linking'} |
|
12226
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 |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
18 def _copy_file_contents(src, dst, buffer_size=16*1024): |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
19 """Copy the file 'src' to 'dst'. |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
20 |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
21 Both must be filenames. Any error opening either file, reading from |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
22 'src', or writing to 'dst', raises DistutilsFileError. Data is |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
23 read/written in chunks of 'buffer_size' bytes (default 16k). No attempt |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
24 is made to handle anything apart from regular files. |
| 14778 | 25 """ |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
26 # 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
|
27 # custom error-handling added. |
|
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): |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
34 raise DistutilsFileError("could not open '%s': %s" % (src, errstr)) |
| 21267 | 35 |
|
21862
53ac8284217a
[Bug #220993; may also fix bug #479469] Fix flakiness when old
Andrew M. Kuchling <amk@amk.ca>
parents:
21267
diff
changeset
|
36 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
|
37 try: |
|
53ac8284217a
[Bug #220993; may also fix bug #479469] Fix flakiness when old
Andrew M. Kuchling <amk@amk.ca>
parents:
21267
diff
changeset
|
38 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
|
39 except os.error, (errno, errstr): |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
40 raise DistutilsFileError( |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
41 "could not delete '%s': %s" % (dst, errstr)) |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
26285
diff
changeset
|
42 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
43 try: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
44 fdst = open(dst, 'wb') |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
45 except os.error, (errno, errstr): |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
46 raise DistutilsFileError( |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
47 "could not create '%s': %s" % (dst, errstr)) |
| 21267 | 48 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
49 while 1: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
50 try: |
| 14778 | 51 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
|
52 except os.error, (errno, errstr): |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
53 raise DistutilsFileError( |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
54 "could not read from '%s': %s" % (src, errstr)) |
| 21267 | 55 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
56 if not buf: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
57 break |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
58 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
59 try: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
60 fdst.write(buf) |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
61 except os.error, (errno, errstr): |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
62 raise DistutilsFileError( |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
63 "could not write to '%s': %s" % (dst, errstr)) |
| 21267 | 64 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
65 finally: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
66 if fdst: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
67 fdst.close() |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
68 if fsrc: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
69 fsrc.close() |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
70 |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
71 def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0, |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
72 link=None, verbose=1, dry_run=0): |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
73 """Copy a file 'src' to 'dst'. |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
74 |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
75 If 'dst' is a directory, then 'src' is copied there with the same name; |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
76 otherwise, it must be a filename. (If the file exists, it will be |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
77 ruthlessly clobbered.) If 'preserve_mode' is true (the default), |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
78 the file's mode (type and permission bits, or whatever is analogous on |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
79 the current platform) is copied. If 'preserve_times' is true (the |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
80 default), the last-modified and last-access times are copied as well. |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
81 If 'update' is true, 'src' will only be copied if 'dst' does not exist, |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
82 or if 'dst' does exist but is older than 'src'. |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
83 |
| 14778 | 84 'link' allows you to make hard links (os.link) or symbolic links |
| 85 (os.symlink) instead of copying: set it to "hard" or "sym"; if it is | |
| 86 None (the default), files are copied. Don't set 'link' on systems that | |
| 87 don't support it: 'copy_file()' doesn't check if hard or symbolic | |
| 88 linking is available. | |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
89 |
| 14778 | 90 Under Mac OS, uses the native file copy function in macostools; on |
| 91 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
|
92 |
|
14942
e15e97a6c14a
Changed 'copy_file()' so it returns a tuple (dest_name, copied) -- hopefully,
Greg Ward <gward@python.net>
parents:
14862
diff
changeset
|
93 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
|
94 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
|
95 have been copied, if 'dry_run' true). |
| 14778 | 96 """ |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
97 # 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
|
98 # 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
|
99 # 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
|
100 # 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
|
101 # 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
|
102 # (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
|
103 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
104 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
|
105 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
|
106 |
| 14778 | 107 if not os.path.isfile(src): |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
108 raise DistutilsFileError( |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
109 "can't copy '%s': doesn't exist or not a regular file" % src) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
110 |
| 14778 | 111 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
|
112 dir = dst |
| 14778 | 113 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
|
114 else: |
| 14778 | 115 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
|
116 |
| 14778 | 117 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
|
118 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
|
119 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
|
120 return dst, 0 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
121 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
122 try: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
123 action = _copy_action[link] |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
124 except KeyError: |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
125 raise ValueError("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
|
126 |
|
51366
a3ea65bfe370
using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51360
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 else: |
|
50d89436aeab
Fixed #1276768: verbose option was not used in the code.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
131 log.info("%s %s -> %s", action, src, dst) |
| 21267 | 132 |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
133 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
|
134 return (dst, 1) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
135 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
136 # 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
|
137 # (Unix only, of course, but that's the caller's responsibility) |
|
61258
f6785bce54b5
In a number of places code still revers
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
55082
diff
changeset
|
138 if link == 'hard': |
| 14778 | 139 if not (os.path.exists(dst) and os.path.samefile(src, dst)): |
| 140 os.link(src, dst) | |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
141 elif link == 'sym': |
| 14778 | 142 if not (os.path.exists(dst) and os.path.samefile(src, dst)): |
| 143 os.symlink(src, dst) | |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
144 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
145 # 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
|
146 # (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
|
147 else: |
| 14778 | 148 _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
|
149 if preserve_mode or preserve_times: |
| 14778 | 150 st = os.stat(src) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
151 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
152 # 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
|
153 # 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
|
154 if preserve_times: |
| 14778 | 155 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
|
156 if preserve_mode: |
| 14778 | 157 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
|
158 |
|
14942
e15e97a6c14a
Changed 'copy_file()' so it returns a tuple (dest_name, copied) -- hopefully,
Greg Ward <gward@python.net>
parents:
14862
diff
changeset
|
159 return (dst, 1) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
160 |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
161 # XXX I suspect this is Unix-specific -- need porting help! |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
162 def move_file (src, dst, verbose=1, dry_run=0): |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
163 """Move a file 'src' to 'dst'. |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
164 |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
165 If 'dst' is a directory, the file will be moved into it with the same |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
166 name; otherwise, 'src' is just renamed to 'dst'. Return the new |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
167 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
|
168 |
| 14778 | 169 Handles cross-device moves on Unix using 'copy_file()'. What about |
| 170 other systems??? | |
| 171 """ | |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
172 from os.path import exists, isfile, isdir, basename, dirname |
| 19115 | 173 import errno |
| 21267 | 174 |
|
51366
a3ea65bfe370
using >= so setting verbose to 2 will work as well
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51360
diff
changeset
|
175 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
|
176 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
|
177 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
178 if dry_run: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
179 return dst |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
180 |
| 14778 | 181 if not isfile(src): |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
182 raise DistutilsFileError("can't move '%s': not a regular file" % src) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
183 |
| 14778 | 184 if isdir(dst): |
| 185 dst = os.path.join(dst, basename(src)) | |
| 186 elif exists(dst): | |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
187 raise DistutilsFileError( |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
188 "can't move '%s': destination '%s' already exists" % |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
189 (src, dst)) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
190 |
| 14778 | 191 if not isdir(dirname(dst)): |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
192 raise DistutilsFileError( |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
193 "can't move '%s': destination '%s' not a valid path" % \ |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
194 (src, dst)) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
195 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
196 copy_it = 0 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
197 try: |
| 14778 | 198 os.rename(src, dst) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
199 except os.error, (num, msg): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
200 if num == errno.EXDEV: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
201 copy_it = 1 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
202 else: |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
203 raise DistutilsFileError( |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
204 "couldn't move '%s' to '%s': %s" % (src, dst, msg)) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
205 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
206 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
|
207 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
|
208 try: |
| 14778 | 209 os.unlink(src) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
210 except os.error, (num, msg): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
211 try: |
| 14778 | 212 os.unlink(dst) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
213 except os.error: |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
214 pass |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
215 raise DistutilsFileError( |
| 21267 | 216 ("couldn't move '%s' to '%s' by copy/delete: " + |
|
55082
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
217 "delete '%s' failed: %s") % |
|
b2c776857dc4
cleaned distutils.file_util
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51366
diff
changeset
|
218 (src, dst, src, msg)) |
|
12226
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
219 return dst |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
220 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
221 |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
222 def write_file (filename, contents): |
|
ffd242fed0e2
Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents:
diff
changeset
|
223 """Create a file with the specified name and write 'contents' (a |
| 14778 | 224 sequence of strings without line terminators) to it. |
| 225 """ | |
| 226 f = open(filename, "w") | |
|
66008
a17b21c27a33
Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
61258
diff
changeset
|
227 try: |
|
a17b21c27a33
Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
61258
diff
changeset
|
228 for line in contents: |
|
a17b21c27a33
Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
61258
diff
changeset
|
229 f.write(line + "\n") |
|
a17b21c27a33
Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
61258
diff
changeset
|
230 finally: |
|
a17b21c27a33
Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents:
61258
diff
changeset
|
231 f.close() |
