file_util.py 7.6 KB

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