From 9ad7bbc6370e370b3da7c60cc1ca74774d11957c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarek=20Ziad=C3=A9?= Date: Fri, 3 Jul 2009 19:14:49 +0000 Subject: cleaned distutils.file_util --- Lib/distutils/file_util.py | 121 +++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 69 deletions(-) diff --git a/Lib/distutils/file_util.py b/Lib/distutils/file_util.py index 0c89055..d8e8fd5 100644 --- a/Lib/distutils/file_util.py +++ b/Lib/distutils/file_util.py @@ -10,49 +10,48 @@ from distutils.errors import DistutilsFileError from distutils import log # for generating verbose output in 'copy_file()' -_copy_action = { None: 'copying', - 'hard': 'hard linking', - 'sym': 'symbolically linking' } +_copy_action = {None: 'copying', + 'hard': 'hard linking', + 'sym': 'symbolically linking'} -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' - bytes (default 16k). No attempt is made to handle anything apart from - regular files. +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' bytes (default 16k). No attempt + is made to handle anything apart from regular files. """ # Stolen from shutil module in the standard library, but with # custom error-handling added. - fsrc = None fdst = None try: try: fsrc = open(src, 'rb') except os.error, (errno, errstr): - 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, (errno, errstr): - 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, (errno, errstr): - raise DistutilsFileError, \ - "could not create '%s': %s" % (dst, errstr) + raise DistutilsFileError( + "could not create '%s': %s" % (dst, errstr)) while 1: try: buf = fsrc.read(buffer_size) except os.error, (errno, errstr): - raise DistutilsFileError, \ - "could not read from '%s': %s" % (src, errstr) + raise DistutilsFileError( + "could not read from '%s': %s" % (src, errstr)) if not buf: break @@ -60,8 +59,8 @@ def _copy_file_contents (src, dst, buffer_size=16*1024): try: fdst.write(buf) except os.error, (errno, errstr): - raise DistutilsFileError, \ - "could not write to '%s': %s" % (dst, errstr) + raise DistutilsFileError( + "could not write to '%s': %s" % (dst, errstr)) finally: if fdst: @@ -69,25 +68,18 @@ def _copy_file_contents (src, dst, buffer_size=16*1024): if fsrc: fsrc.close() -# _copy_file_contents() - -def copy_file (src, dst, - preserve_mode=1, - preserve_times=1, - update=0, - link=None, - verbose=1, - 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' - is true (the default), the file's mode (type and permission bits, or - whatever is analogous on the current platform) is copied. If - 'preserve_times' is true (the default), the last-modified and - last-access times are copied as well. If 'update' is true, 'src' will - only be copied if 'dst' does not exist, or if 'dst' does exist but is - older than 'src'. +def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0, + link=None, verbose=1, 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' is true (the default), + the file's mode (type and permission bits, or whatever is analogous on + the current platform) is copied. If 'preserve_times' is true (the + default), the last-modified and last-access times are copied as well. + If 'update' is true, 'src' will only be copied if 'dst' does not exist, + or if 'dst' does exist but is older than 'src'. 'link' allows you to make hard links (os.link) or symbolic links (os.symlink) instead of copying: set it to "hard" or "sym"; if it is @@ -113,8 +105,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 @@ -130,8 +122,7 @@ def copy_file (src, dst, 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 verbose >= 1: if os.path.basename(dst) == os.path.basename(src): @@ -148,8 +139,8 @@ def copy_file (src, dst, try: macostools.copy(src, dst, 0, preserve_times) except os.error, 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) @@ -176,17 +167,13 @@ 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, - verbose=1, - dry_run=0): +def move_file (src, dst, verbose=1, dry_run=0): + """Move a file 'src' to 'dst'. - """Move a file 'src' to 'dst'. If 'dst' is a directory, the file will - be moved into it with the same name; otherwise, 'src' is just renamed - to 'dst'. Return the new full name of the file. + If 'dst' is a directory, the file will be moved into it with the same + name; otherwise, 'src' is just renamed to 'dst'. Return the new + full name of the file. Handles cross-device moves on Unix using 'copy_file()'. What about other systems??? @@ -201,20 +188,19 @@ 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, \ + raise DistutilsFileError( "can't move '%s': destination '%s' not a valid path" % \ - (src, dst) + (src, dst)) copy_it = 0 try: @@ -223,8 +209,8 @@ def move_file (src, dst, if num == errno.EXDEV: copy_it = 1 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, verbose=verbose) @@ -235,15 +221,12 @@ def move_file (src, dst, os.unlink(dst) except os.error: pass - raise DistutilsFileError, \ + raise DistutilsFileError( ("couldn't move '%s' to '%s' by copy/delete: " + - "delete '%s' failed: %s") % \ - (src, dst, src, msg) - + "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 -- cgit v0.12