file_util.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. """distutils.file_util
  2. Utility functions for operating on single files.
  3. """
  4. __revision__ = "$Id$"
  5. import os
  6. from distutils.errors import DistutilsFileError
  7. from distutils import log
  8. # for generating verbose output in 'copy_file()'
  9. _copy_action = {None: 'copying',
  10. 'hard': 'hard linking',
  11. 'sym': 'symbolically linking'}
  12. def _copy_file_contents(src, dst, buffer_size=16*1024):
  13. """Copy the file 'src' to 'dst'.
  14. Both must be filenames. Any error opening either file, reading from
  15. 'src', or writing to 'dst', raises DistutilsFileError. Data is
  16. read/written in chunks of 'buffer_size' bytes (default 16k). No attempt
  17. is made to handle anything apart from regular files.
  18. """
  19. # Stolen from shutil module in the standard library, but with
  20. # custom error-handling added.
  21. fsrc = None
  22. fdst = None
  23. try:
  24. try:
  25. fsrc = open(src, 'rb')
  26. except os.error, (errno, errstr):
  27. raise DistutilsFileError("could not open '%s': %s" % (src, errstr))
  28. if os.path.exists(dst):
  29. try:
  30. os.unlink(dst)
  31. except os.error, (errno, errstr):
  32. raise DistutilsFileError(
  33. "could not delete '%s': %s" % (dst, errstr))
  34. try:
  35. fdst = open(dst, 'wb')
  36. except os.error, (errno, errstr):
  37. raise DistutilsFileError(
  38. "could not create '%s': %s" % (dst, errstr))
  39. while 1:
  40. try:
  41. buf = fsrc.read(buffer_size)
  42. except os.error, (errno, errstr):
  43. raise DistutilsFileError(
  44. "could not read from '%s': %s" % (src, errstr))
  45. if not buf:
  46. break
  47. try:
  48. fdst.write(buf)
  49. except os.error, (errno, errstr):
  50. raise DistutilsFileError(
  51. "could not write to '%s': %s" % (dst, errstr))
  52. finally:
  53. if fdst:
  54. fdst.close()
  55. if fsrc:
  56. fsrc.close()
  57. def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
  58. link=None, verbose=1, dry_run=0):
  59. """Copy a file 'src' to 'dst'.
  60. If 'dst' is a directory, then 'src' is copied there with the same name;
  61. otherwise, it must be a filename. (If the file exists, it will be
  62. ruthlessly clobbered.) If 'preserve_mode' is true (the default),
  63. the file's mode (type and permission bits, or whatever is analogous on
  64. the current platform) is copied. If 'preserve_times' is true (the
  65. default), the last-modified and last-access times are copied as well.
  66. If 'update' is true, 'src' will only be copied if 'dst' does not exist,
  67. or if 'dst' does exist but is older than 'src'.
  68. 'link' allows you to make hard links (os.link) or symbolic links
  69. (os.symlink) instead of copying: set it to "hard" or "sym"; if it is
  70. None (the default), files are copied. Don't set 'link' on systems that
  71. don't support it: 'copy_file()' doesn't check if hard or symbolic
  72. linking is available. If hardlink fails, falls back to
  73. _copy_file_contents().
  74. Under Mac OS, uses the native file copy function in macostools; on
  75. other systems, uses '_copy_file_contents()' to copy file contents.
  76. Return a tuple (dest_name, copied): 'dest_name' is the actual name of
  77. the output file, and 'copied' is true if the file was copied (or would
  78. have been copied, if 'dry_run' true).
  79. """
  80. # XXX if the destination file already exists, we clobber it if
  81. # copying, but blow up if linking. Hmmm. And I don't know what
  82. # macostools.copyfile() does. Should definitely be consistent, and
  83. # should probably blow up if destination exists and we would be
  84. # changing it (ie. it's not already a hard/soft link to src OR
  85. # (not update) and (src newer than dst).
  86. from distutils.dep_util import newer
  87. from stat import ST_ATIME, ST_MTIME, ST_MODE, S_IMODE
  88. if not os.path.isfile(src):
  89. raise DistutilsFileError(
  90. "can't copy '%s': doesn't exist or not a regular file" % src)
  91. if os.path.isdir(dst):
  92. dir = dst
  93. dst = os.path.join(dst, os.path.basename(src))
  94. else:
  95. dir = os.path.dirname(dst)
  96. if update and not newer(src, dst):
  97. if verbose >= 1:
  98. log.debug("not copying %s (output up-to-date)", src)
  99. return dst, 0
  100. try:
  101. action = _copy_action[link]
  102. except KeyError:
  103. raise ValueError("invalid value '%s' for 'link' argument" % link)
  104. if verbose >= 1:
  105. if os.path.basename(dst) == os.path.basename(src):
  106. log.info("%s %s -> %s", action, src, dir)
  107. else:
  108. log.info("%s %s -> %s", action, src, dst)
  109. if dry_run:
  110. return (dst, 1)
  111. # If linking (hard or symbolic), use the appropriate system call
  112. # (Unix only, of course, but that's the caller's responsibility)
  113. if link == 'hard':
  114. if not (os.path.exists(dst) and os.path.samefile(src, dst)):
  115. try:
  116. os.link(src, dst)
  117. return (dst, 1)
  118. except OSError:
  119. # If hard linking fails, fall back on copying file
  120. # (some special filesystems don't support hard linking
  121. # even under Unix, see issue #8876).
  122. pass
  123. elif link == 'sym':
  124. if not (os.path.exists(dst) and os.path.samefile(src, dst)):
  125. os.symlink(src, dst)
  126. return (dst, 1)
  127. # Otherwise (non-Mac, not linking), copy the file contents and
  128. # (optionally) copy the times and mode.
  129. _copy_file_contents(src, dst)
  130. if preserve_mode or preserve_times:
  131. st = os.stat(src)
  132. # According to David Ascher <da@ski.org>, utime() should be done
  133. # before chmod() (at least under NT).
  134. if preserve_times:
  135. os.utime(dst, (st[ST_ATIME], st[ST_MTIME]))
  136. if preserve_mode:
  137. os.chmod(dst, S_IMODE(st[ST_MODE]))
  138. return (dst, 1)
  139. # XXX I suspect this is Unix-specific -- need porting help!
  140. def move_file (src, dst, verbose=1, dry_run=0):
  141. """Move a file 'src' to 'dst'.
  142. If 'dst' is a directory, the file will be moved into it with the same
  143. name; otherwise, 'src' is just renamed to 'dst'. Return the new
  144. full name of the file.
  145. Handles cross-device moves on Unix using 'copy_file()'. What about
  146. other systems???
  147. """
  148. from os.path import exists, isfile, isdir, basename, dirname
  149. import errno
  150. if verbose >= 1:
  151. log.info("moving %s -> %s", src, dst)
  152. if dry_run:
  153. return dst
  154. if not isfile(src):
  155. raise DistutilsFileError("can't move '%s': not a regular file" % src)
  156. if isdir(dst):
  157. dst = os.path.join(dst, basename(src))
  158. elif exists(dst):
  159. raise DistutilsFileError(
  160. "can't move '%s': destination '%s' already exists" %
  161. (src, dst))
  162. if not isdir(dirname(dst)):
  163. raise DistutilsFileError(
  164. "can't move '%s': destination '%s' not a valid path" % \
  165. (src, dst))
  166. copy_it = 0
  167. try:
  168. os.rename(src, dst)
  169. except os.error, (num, msg):
  170. if num == errno.EXDEV:
  171. copy_it = 1
  172. else:
  173. raise DistutilsFileError(
  174. "couldn't move '%s' to '%s': %s" % (src, dst, msg))
  175. if copy_it:
  176. copy_file(src, dst, verbose=verbose)
  177. try:
  178. os.unlink(src)
  179. except os.error, (num, msg):
  180. try:
  181. os.unlink(dst)
  182. except os.error:
  183. pass
  184. raise DistutilsFileError(
  185. ("couldn't move '%s' to '%s' by copy/delete: " +
  186. "delete '%s' failed: %s") %
  187. (src, dst, src, msg))
  188. return dst
  189. def write_file (filename, contents):
  190. """Create a file with the specified name and write 'contents' (a
  191. sequence of strings without line terminators) to it.
  192. """
  193. f = open(filename, "w")
  194. try:
  195. for line in contents:
  196. f.write(line + "\n")
  197. finally:
  198. f.close()