mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
General cleanup, raise normalization in Lib/distutils.
This commit is contained in:
parent
a73bfee73d
commit
5b7e9d76f3
47 changed files with 963 additions and 1640 deletions
|
@ -3,8 +3,6 @@
|
|||
Utility functions for operating on single files.
|
||||
"""
|
||||
|
||||
# This module should be kept compatible with Python 2.1.
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
import os
|
||||
|
@ -17,7 +15,7 @@ _copy_action = { None: 'copying',
|
|||
'sym': 'symbolically linking' }
|
||||
|
||||
|
||||
def _copy_file_contents (src, dst, buffer_size=16*1024):
|
||||
def _copy_file_contents(src, dst, buffer_size=16*1024):
|
||||
"""Copy the file 'src' to 'dst'; both must be filenames. Any error
|
||||
opening either file, reading from 'src', or writing to 'dst', raises
|
||||
DistutilsFileError. Data is read/written in chunks of 'buffer_size'
|
||||
|
@ -26,7 +24,6 @@ def _copy_file_contents (src, dst, buffer_size=16*1024):
|
|||
"""
|
||||
# Stolen from shutil module in the standard library, but with
|
||||
# custom error-handling added.
|
||||
|
||||
fsrc = None
|
||||
fdst = None
|
||||
try:
|
||||
|
@ -34,31 +31,30 @@ def _copy_file_contents (src, dst, buffer_size=16*1024):
|
|||
fsrc = open(src, 'rb')
|
||||
except os.error as e:
|
||||
(errno, errstr) = e
|
||||
raise DistutilsFileError, \
|
||||
"could not open '%s': %s" % (src, errstr)
|
||||
raise DistutilsFileError("could not open '%s': %s" % (src, errstr))
|
||||
|
||||
if os.path.exists(dst):
|
||||
try:
|
||||
os.unlink(dst)
|
||||
except os.error as e:
|
||||
(errno, errstr) = e
|
||||
raise DistutilsFileError, \
|
||||
"could not delete '%s': %s" % (dst, errstr)
|
||||
raise DistutilsFileError(
|
||||
"could not delete '%s': %s" % (dst, errstr))
|
||||
|
||||
try:
|
||||
fdst = open(dst, 'wb')
|
||||
except os.error as e:
|
||||
(errno, errstr) = e
|
||||
raise DistutilsFileError, \
|
||||
"could not create '%s': %s" % (dst, errstr)
|
||||
raise DistutilsFileError(
|
||||
"could not create '%s': %s" % (dst, errstr))
|
||||
|
||||
while 1:
|
||||
while True:
|
||||
try:
|
||||
buf = fsrc.read(buffer_size)
|
||||
except os.error as e:
|
||||
(errno, errstr) = e
|
||||
raise DistutilsFileError, \
|
||||
"could not read from '%s': %s" % (src, errstr)
|
||||
raise DistutilsFileError(
|
||||
"could not read from '%s': %s" % (src, errstr))
|
||||
|
||||
if not buf:
|
||||
break
|
||||
|
@ -67,25 +63,16 @@ def _copy_file_contents (src, dst, buffer_size=16*1024):
|
|||
fdst.write(buf)
|
||||
except os.error as e:
|
||||
(errno, errstr) = e
|
||||
raise DistutilsFileError, \
|
||||
"could not write to '%s': %s" % (dst, errstr)
|
||||
|
||||
raise DistutilsFileError(
|
||||
"could not write to '%s': %s" % (dst, errstr))
|
||||
finally:
|
||||
if fdst:
|
||||
fdst.close()
|
||||
if fsrc:
|
||||
fsrc.close()
|
||||
|
||||
# _copy_file_contents()
|
||||
|
||||
def copy_file (src, dst,
|
||||
preserve_mode=1,
|
||||
preserve_times=1,
|
||||
update=0,
|
||||
link=None,
|
||||
verbose=0,
|
||||
dry_run=0):
|
||||
|
||||
def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
|
||||
link=None, verbose=0, dry_run=0):
|
||||
"""Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src' is
|
||||
copied there with the same name; otherwise, it must be a filename. (If
|
||||
the file exists, it will be ruthlessly clobbered.) If 'preserve_mode'
|
||||
|
@ -120,8 +107,8 @@ def copy_file (src, dst,
|
|||
from stat import ST_ATIME, ST_MTIME, ST_MODE, S_IMODE
|
||||
|
||||
if not os.path.isfile(src):
|
||||
raise DistutilsFileError, \
|
||||
"can't copy '%s': doesn't exist or not a regular file" % src
|
||||
raise DistutilsFileError(
|
||||
"can't copy '%s': doesn't exist or not a regular file" % src)
|
||||
|
||||
if os.path.isdir(dst):
|
||||
dir = dst
|
||||
|
@ -131,13 +118,12 @@ def copy_file (src, dst,
|
|||
|
||||
if update and not newer(src, dst):
|
||||
log.debug("not copying %s (output up-to-date)", src)
|
||||
return dst, 0
|
||||
return (dst, 0)
|
||||
|
||||
try:
|
||||
action = _copy_action[link]
|
||||
except KeyError:
|
||||
raise ValueError, \
|
||||
"invalid value '%s' for 'link' argument" % link
|
||||
raise ValueError("invalid value '%s' for 'link' argument" % link)
|
||||
if os.path.basename(dst) == os.path.basename(src):
|
||||
log.info("%s %s -> %s", action, src, dir)
|
||||
else:
|
||||
|
@ -152,8 +138,8 @@ def copy_file (src, dst,
|
|||
try:
|
||||
macostools.copy(src, dst, 0, preserve_times)
|
||||
except os.error as exc:
|
||||
raise DistutilsFileError, \
|
||||
"could not copy '%s' to '%s': %s" % (src, dst, exc[-1])
|
||||
raise DistutilsFileError(
|
||||
"could not copy '%s' to '%s': %s" % (src, dst, exc[-1]))
|
||||
|
||||
# If linking (hard or symbolic), use the appropriate system call
|
||||
# (Unix only, of course, but that's the caller's responsibility)
|
||||
|
@ -180,8 +166,6 @@ def copy_file (src, dst,
|
|||
|
||||
return (dst, 1)
|
||||
|
||||
# copy_file ()
|
||||
|
||||
|
||||
# XXX I suspect this is Unix-specific -- need porting help!
|
||||
def move_file (src, dst,
|
||||
|
@ -204,31 +188,30 @@ def move_file (src, dst,
|
|||
return dst
|
||||
|
||||
if not isfile(src):
|
||||
raise DistutilsFileError, \
|
||||
"can't move '%s': not a regular file" % src
|
||||
raise DistutilsFileError("can't move '%s': not a regular file" % src)
|
||||
|
||||
if isdir(dst):
|
||||
dst = os.path.join(dst, basename(src))
|
||||
elif exists(dst):
|
||||
raise DistutilsFileError, \
|
||||
"can't move '%s': destination '%s' already exists" % \
|
||||
(src, dst)
|
||||
raise DistutilsFileError(
|
||||
"can't move '%s': destination '%s' already exists" %
|
||||
(src, dst))
|
||||
|
||||
if not isdir(dirname(dst)):
|
||||
raise DistutilsFileError, \
|
||||
"can't move '%s': destination '%s' not a valid path" % \
|
||||
(src, dst)
|
||||
raise DistutilsFileError(
|
||||
"can't move '%s': destination '%s' not a valid path" %
|
||||
(src, dst))
|
||||
|
||||
copy_it = 0
|
||||
copy_it = False
|
||||
try:
|
||||
os.rename(src, dst)
|
||||
except os.error as e:
|
||||
(num, msg) = e
|
||||
if num == errno.EXDEV:
|
||||
copy_it = 1
|
||||
copy_it = True
|
||||
else:
|
||||
raise DistutilsFileError, \
|
||||
"couldn't move '%s' to '%s': %s" % (src, dst, msg)
|
||||
raise DistutilsFileError(
|
||||
"couldn't move '%s' to '%s': %s" % (src, dst, msg))
|
||||
|
||||
if copy_it:
|
||||
copy_file(src, dst)
|
||||
|
@ -240,15 +223,12 @@ def move_file (src, dst,
|
|||
os.unlink(dst)
|
||||
except os.error:
|
||||
pass
|
||||
raise DistutilsFileError, \
|
||||
("couldn't move '%s' to '%s' by copy/delete: " +
|
||||
"delete '%s' failed: %s") % \
|
||||
(src, dst, src, msg)
|
||||
|
||||
raise DistutilsFileError(
|
||||
"couldn't move '%s' to '%s' by copy/delete: "
|
||||
"delete '%s' failed: %s"
|
||||
% (src, dst, src, msg))
|
||||
return dst
|
||||
|
||||
# move_file ()
|
||||
|
||||
|
||||
def write_file (filename, contents):
|
||||
"""Create a file with the specified name and write 'contents' (a
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue