file_util.py 8.4 KB

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