Back to index

python3.2  3.2.2
Functions | Variables
distutils.file_util Namespace Reference

Functions

def _copy_file_contents
def copy_file
def move_file
def write_file

Variables

dictionary _copy_action

Detailed Description

distutils.file_util

Utility functions for operating on single files.

Function Documentation

def distutils.file_util._copy_file_contents (   src,
  dst,
  buffer_size = 16*1024 
) [private]
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.

Definition at line 16 of file file_util.py.

00016 
00017 def _copy_file_contents(src, dst, buffer_size=16*1024):
00018     """Copy the file 'src' to 'dst'; both must be filenames.  Any error
00019     opening either file, reading from 'src', or writing to 'dst', raises
00020     DistutilsFileError.  Data is read/written in chunks of 'buffer_size'
00021     bytes (default 16k).  No attempt is made to handle anything apart from
00022     regular files.
00023     """
00024     # Stolen from shutil module in the standard library, but with
00025     # custom error-handling added.
00026     fsrc = None
00027     fdst = None
00028     try:
00029         try:
00030             fsrc = open(src, 'rb')
00031         except os.error as e:
00032             raise DistutilsFileError("could not open '%s': %s" % (src, e.strerror))
00033 
00034         if os.path.exists(dst):
00035             try:
00036                 os.unlink(dst)
00037             except os.error as e:
00038                 raise DistutilsFileError(
00039                       "could not delete '%s': %s" % (dst, e.strerror))
00040 
00041         try:
00042             fdst = open(dst, 'wb')
00043         except os.error as e:
00044             raise DistutilsFileError(
00045                   "could not create '%s': %s" % (dst, e.strerror))
00046 
00047         while True:
00048             try:
00049                 buf = fsrc.read(buffer_size)
00050             except os.error as e:
00051                 raise DistutilsFileError(
00052                       "could not read from '%s': %s" % (src, e.strerror))
00053 
00054             if not buf:
00055                 break
00056 
00057             try:
00058                 fdst.write(buf)
00059             except os.error as e:
00060                 raise DistutilsFileError(
00061                       "could not write to '%s': %s" % (dst, e.strerror))
00062     finally:
00063         if fdst:
00064             fdst.close()
00065         if fsrc:
00066             fsrc.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.file_util.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
None (the default), files are copied.  Don't set 'link' on systems that
don't support it: 'copy_file()' doesn't check if hard or symbolic
linking is available.

Under Mac OS, uses the native file copy function in macostools; on
other systems, uses '_copy_file_contents()' to copy file contents.

Return a tuple (dest_name, copied): 'dest_name' is the actual name of
the output file, and 'copied' is true if the file was copied (or would
have been copied, if 'dry_run' true).

Definition at line 68 of file file_util.py.

00068 
00069               link=None, verbose=1, dry_run=0):
00070     """Copy a file 'src' to 'dst'.  If 'dst' is a directory, then 'src' is
00071     copied there with the same name; otherwise, it must be a filename.  (If
00072     the file exists, it will be ruthlessly clobbered.)  If 'preserve_mode'
00073     is true (the default), the file's mode (type and permission bits, or
00074     whatever is analogous on the current platform) is copied.  If
00075     'preserve_times' is true (the default), the last-modified and
00076     last-access times are copied as well.  If 'update' is true, 'src' will
00077     only be copied if 'dst' does not exist, or if 'dst' does exist but is
00078     older than 'src'.
00079 
00080     'link' allows you to make hard links (os.link) or symbolic links
00081     (os.symlink) instead of copying: set it to "hard" or "sym"; if it is
00082     None (the default), files are copied.  Don't set 'link' on systems that
00083     don't support it: 'copy_file()' doesn't check if hard or symbolic
00084     linking is available.
00085 
00086     Under Mac OS, uses the native file copy function in macostools; on
00087     other systems, uses '_copy_file_contents()' to copy file contents.
00088 
00089     Return a tuple (dest_name, copied): 'dest_name' is the actual name of
00090     the output file, and 'copied' is true if the file was copied (or would
00091     have been copied, if 'dry_run' true).
00092     """
00093     # XXX if the destination file already exists, we clobber it if
00094     # copying, but blow up if linking.  Hmmm.  And I don't know what
00095     # macostools.copyfile() does.  Should definitely be consistent, and
00096     # should probably blow up if destination exists and we would be
00097     # changing it (ie. it's not already a hard/soft link to src OR
00098     # (not update) and (src newer than dst).
00099 
00100     from distutils.dep_util import newer
00101     from stat import ST_ATIME, ST_MTIME, ST_MODE, S_IMODE
00102 
00103     if not os.path.isfile(src):
00104         raise DistutilsFileError(
00105               "can't copy '%s': doesn't exist or not a regular file" % src)
00106 
00107     if os.path.isdir(dst):
00108         dir = dst
00109         dst = os.path.join(dst, os.path.basename(src))
00110     else:
00111         dir = os.path.dirname(dst)
00112 
00113     if update and not newer(src, dst):
00114         if verbose >= 1:
00115             log.debug("not copying %s (output up-to-date)", src)
00116         return (dst, 0)
00117 
00118     try:
00119         action = _copy_action[link]
00120     except KeyError:
00121         raise ValueError("invalid value '%s' for 'link' argument" % link)
00122 
00123     if verbose >= 1:
00124         if os.path.basename(dst) == os.path.basename(src):
00125             log.info("%s %s -> %s", action, src, dir)
00126         else:
00127             log.info("%s %s -> %s", action, src, dst)
00128 
00129     if dry_run:
00130         return (dst, 1)
00131 
00132     # If linking (hard or symbolic), use the appropriate system call
00133     # (Unix only, of course, but that's the caller's responsibility)
00134     elif link == 'hard':
00135         if not (os.path.exists(dst) and os.path.samefile(src, dst)):
00136             os.link(src, dst)
00137     elif link == 'sym':
00138         if not (os.path.exists(dst) and os.path.samefile(src, dst)):
00139             os.symlink(src, dst)
00140 
00141     # Otherwise (non-Mac, not linking), copy the file contents and
00142     # (optionally) copy the times and mode.
00143     else:
00144         _copy_file_contents(src, dst)
00145         if preserve_mode or preserve_times:
00146             st = os.stat(src)
00147 
00148             # According to David Ascher <da@ski.org>, utime() should be done
00149             # before chmod() (at least under NT).
00150             if preserve_times:
00151                 os.utime(dst, (st[ST_ATIME], st[ST_MTIME]))
00152             if preserve_mode:
00153                 os.chmod(dst, S_IMODE(st[ST_MODE]))
00154 
00155     return (dst, 1)
00156 
00157 
# XXX I suspect this is Unix-specific -- need porting help!

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.file_util.move_file (   src,
  dst,
  verbose = 1,
  dry_run = 0 
)
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.

Handles cross-device moves on Unix using 'copy_file()'.  What about
other systems???

Definition at line 160 of file file_util.py.

00160 
00161                dry_run=0):
00162 
00163     """Move a file 'src' to 'dst'.  If 'dst' is a directory, the file will
00164     be moved into it with the same name; otherwise, 'src' is just renamed
00165     to 'dst'.  Return the new full name of the file.
00166 
00167     Handles cross-device moves on Unix using 'copy_file()'.  What about
00168     other systems???
00169     """
00170     from os.path import exists, isfile, isdir, basename, dirname
00171     import errno
00172 
00173     if verbose >= 1:
00174         log.info("moving %s -> %s", src, dst)
00175 
00176     if dry_run:
00177         return dst
00178 
00179     if not isfile(src):
00180         raise DistutilsFileError("can't move '%s': not a regular file" % src)
00181 
00182     if isdir(dst):
00183         dst = os.path.join(dst, basename(src))
00184     elif exists(dst):
00185         raise DistutilsFileError(
00186               "can't move '%s': destination '%s' already exists" %
00187               (src, dst))
00188 
00189     if not isdir(dirname(dst)):
00190         raise DistutilsFileError(
00191               "can't move '%s': destination '%s' not a valid path" %
00192               (src, dst))
00193 
00194     copy_it = False
00195     try:
00196         os.rename(src, dst)
00197     except os.error as e:
00198         (num, msg) = e
00199         if num == errno.EXDEV:
00200             copy_it = True
00201         else:
00202             raise DistutilsFileError(
00203                   "couldn't move '%s' to '%s': %s" % (src, dst, msg))
00204 
00205     if copy_it:
00206         copy_file(src, dst, verbose=verbose)
00207         try:
00208             os.unlink(src)
00209         except os.error as e:
00210             (num, msg) = e
00211             try:
00212                 os.unlink(dst)
00213             except os.error:
00214                 pass
00215             raise DistutilsFileError(
00216                   "couldn't move '%s' to '%s' by copy/delete: "
00217                   "delete '%s' failed: %s"
00218                   % (src, dst, src, msg))
00219     return dst
00220 

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.file_util.write_file (   filename,
  contents 
)
Create a file with the specified name and write 'contents' (a
sequence of strings without line terminators) to it.

Definition at line 221 of file file_util.py.

00221 
00222 def write_file (filename, contents):
00223     """Create a file with the specified name and write 'contents' (a
00224     sequence of strings without line terminators) to it.
00225     """
00226     f = open(filename, "w")
00227     try:
00228         for line in contents:
00229             f.write(line + "\n")
00230     finally:
00231         f.close()

Variable Documentation

Initial value:
00001 { None:   'copying',
00002                  'hard': 'hard linking',
00003                  'sym':  'symbolically linking' }

Definition at line 11 of file file_util.py.