__init__.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. # coding: utf-8
  2. import codecs
  3. import errno
  4. import logging
  5. import os
  6. import random
  7. import shutil
  8. import six
  9. import stat
  10. import sys
  11. import library.python.func
  12. import library.python.strings
  13. import library.python.windows
  14. logger = logging.getLogger(__name__)
  15. try:
  16. WindowsError
  17. except NameError:
  18. WindowsError = None
  19. _diehard_win_tries = 10
  20. errorfix_win = library.python.windows.errorfix
  21. class CustomFsError(OSError):
  22. def __init__(self, errno, message='', filename=None):
  23. super(CustomFsError, self).__init__(message)
  24. self.errno = errno
  25. self.strerror = os.strerror(errno)
  26. self.filename = filename
  27. # Directories creation
  28. # If dst is already exists and is a directory - does nothing
  29. # Throws OSError
  30. @errorfix_win
  31. def ensure_dir(path):
  32. try:
  33. os.makedirs(path)
  34. except OSError as e:
  35. if e.errno != errno.EEXIST or not os.path.isdir(path):
  36. raise
  37. # Directories creation
  38. # If dst is already exists and is a directory - does nothing
  39. # Returns path
  40. # Throws OSError
  41. @errorfix_win
  42. def create_dirs(path):
  43. ensure_dir(path)
  44. return path
  45. # Atomic file/directory move (rename)
  46. # Doesn't guarantee dst replacement
  47. # Atomic if no device boundaries are crossed
  48. # Depends on ctypes on Windows
  49. # Throws OSError
  50. # On Unix, if dst exists:
  51. # if dst is file or empty dir - replaces it
  52. # if src is dir and dst is not dir - throws OSError (errno ENOTDIR)
  53. # if src is dir and dst is non-empty dir - throws OSError (errno ENOTEMPTY)
  54. # if src is file and dst is dir - throws OSError (errno EISDIR)
  55. # On Windows, if dst exists - throws OSError (errno EEXIST)
  56. @errorfix_win
  57. @library.python.windows.diehard(library.python.windows.RETRIABLE_FILE_ERRORS, tries=_diehard_win_tries)
  58. def move(src, dst):
  59. os.rename(src, dst)
  60. # Atomic replacing file move (rename)
  61. # Replaces dst if exists and not a dir
  62. # Doesn't guarantee dst dir replacement
  63. # Atomic if no device boundaries are crossed
  64. # Depends on ctypes on Windows
  65. # Throws OSError
  66. # On Unix, if dst exists:
  67. # if dst is file - replaces it
  68. # if dst is dir - throws OSError (errno EISDIR)
  69. # On Windows, if dst exists:
  70. # if dst is file - replaces it
  71. # if dst is dir - throws OSError (errno EACCES)
  72. @errorfix_win
  73. @library.python.windows.diehard(library.python.windows.RETRIABLE_FILE_ERRORS, tries=_diehard_win_tries)
  74. def replace_file(src, dst):
  75. if library.python.windows.on_win():
  76. library.python.windows.replace_file(src, dst)
  77. else:
  78. os.rename(src, dst)
  79. # File/directory replacing move (rename)
  80. # Removes dst if exists
  81. # Non-atomic
  82. # Depends on ctypes on Windows
  83. # Throws OSError
  84. @errorfix_win
  85. def replace(src, dst):
  86. try:
  87. move(src, dst)
  88. except OSError as e:
  89. if e.errno not in (errno.EEXIST, errno.EISDIR, errno.ENOTDIR, errno.ENOTEMPTY):
  90. raise
  91. remove_tree(dst)
  92. move(src, dst)
  93. # Atomic file remove
  94. # Throws OSError
  95. @errorfix_win
  96. @library.python.windows.diehard(library.python.windows.RETRIABLE_FILE_ERRORS, tries=_diehard_win_tries)
  97. def remove_file(path):
  98. os.remove(path)
  99. # Atomic empty directory remove
  100. # Throws OSError
  101. @errorfix_win
  102. @library.python.windows.diehard(library.python.windows.RETRIABLE_DIR_ERRORS, tries=_diehard_win_tries)
  103. def remove_dir(path):
  104. os.rmdir(path)
  105. def fix_path_encoding(path):
  106. return library.python.strings.to_str(path, library.python.strings.fs_encoding())
  107. # File/directory remove
  108. # Non-atomic
  109. # Throws OSError, AssertionError
  110. @errorfix_win
  111. def remove_tree(path):
  112. @library.python.windows.diehard(library.python.windows.RETRIABLE_DIR_ERRORS, tries=_diehard_win_tries)
  113. def rmtree(path):
  114. if library.python.windows.on_win():
  115. library.python.windows.rmtree(path)
  116. else:
  117. shutil.rmtree(fix_path_encoding(path))
  118. st = os.lstat(path)
  119. if stat.S_ISLNK(st.st_mode) or stat.S_ISREG(st.st_mode):
  120. remove_file(path)
  121. elif stat.S_ISDIR(st.st_mode):
  122. rmtree(path)
  123. else:
  124. assert False
  125. # File/directory remove ignoring errors
  126. # Non-atomic
  127. @errorfix_win
  128. def remove_tree_safe(path):
  129. try:
  130. st = os.lstat(path)
  131. if stat.S_ISLNK(st.st_mode) or stat.S_ISREG(st.st_mode):
  132. os.remove(path)
  133. elif stat.S_ISDIR(st.st_mode):
  134. shutil.rmtree(fix_path_encoding(path), ignore_errors=True)
  135. # XXX
  136. except UnicodeDecodeError as e:
  137. logging.exception(u'remove_tree_safe with argument %s raise exception: %s', path, e)
  138. raise
  139. except OSError:
  140. pass
  141. # File/directory remove
  142. # If path doesn't exist - does nothing
  143. # Non-atomic
  144. # Throws OSError, AssertionError
  145. @errorfix_win
  146. def ensure_removed(path):
  147. try:
  148. remove_tree(path)
  149. except OSError as e:
  150. if e.errno != errno.ENOENT:
  151. raise
  152. # Atomic file hardlink
  153. # Dst must not exist
  154. # Depends on ctypes on Windows
  155. # Throws OSError
  156. # If dst exists - throws OSError (errno EEXIST)
  157. @errorfix_win
  158. def hardlink(src, lnk):
  159. if library.python.windows.on_win():
  160. library.python.windows.hardlink(src, lnk)
  161. else:
  162. os.link(src, lnk)
  163. @errorfix_win
  164. def hardlink_or_copy(src, lnk):
  165. def should_fallback_to_copy(exc):
  166. if WindowsError is not None and isinstance(exc, WindowsError) and exc.winerror == 1142: # too many hardlinks
  167. return True
  168. # cross-device hardlink or too many hardlinks, or some known WSL error
  169. if isinstance(exc, OSError) and exc.errno in (
  170. errno.EXDEV,
  171. errno.EMLINK,
  172. errno.EINVAL,
  173. errno.EACCES,
  174. errno.EPERM,
  175. ):
  176. return True
  177. return False
  178. try:
  179. hardlink(src, lnk)
  180. except Exception as e:
  181. logger.debug('Failed to hardlink %s to %s with error %s, will copy it', src, lnk, repr(e))
  182. if should_fallback_to_copy(e):
  183. copy2(src, lnk, follow_symlinks=False)
  184. else:
  185. raise
  186. # Atomic file/directory symlink (Unix only)
  187. # Dst must not exist
  188. # Throws OSError
  189. # If dst exists - throws OSError (errno EEXIST)
  190. @errorfix_win
  191. def symlink(src, lnk):
  192. if library.python.windows.on_win():
  193. library.python.windows.run_disabled(src, lnk)
  194. else:
  195. os.symlink(src, lnk)
  196. # shutil.copy2 with follow_symlinks=False parameter (Unix only)
  197. def copy2(src, lnk, follow_symlinks=True):
  198. if six.PY3:
  199. shutil.copy2(src, lnk, follow_symlinks=follow_symlinks)
  200. return
  201. if follow_symlinks or not os.path.islink(src):
  202. shutil.copy2(src, lnk)
  203. return
  204. symlink(os.readlink(src), lnk)
  205. # Recursively hardlink directory
  206. # Uses plain hardlink for files
  207. # Dst must not exist
  208. # Non-atomic
  209. # Throws OSError
  210. @errorfix_win
  211. def hardlink_tree(src, dst):
  212. if not os.path.exists(src):
  213. raise CustomFsError(errno.ENOENT, filename=src)
  214. if os.path.isfile(src):
  215. hardlink(src, dst)
  216. return
  217. for dirpath, _, filenames in walk_relative(src):
  218. src_dirpath = os.path.join(src, dirpath) if dirpath != '.' else src
  219. dst_dirpath = os.path.join(dst, dirpath) if dirpath != '.' else dst
  220. os.mkdir(dst_dirpath)
  221. for filename in filenames:
  222. hardlink(os.path.join(src_dirpath, filename), os.path.join(dst_dirpath, filename))
  223. # File copy
  224. # throws EnvironmentError (OSError, IOError)
  225. @errorfix_win
  226. def copy_file(src, dst, copy_function=shutil.copy2):
  227. if os.path.isdir(dst):
  228. raise CustomFsError(errno.EISDIR, filename=dst)
  229. copy_function(src, dst)
  230. # File/directory copy
  231. # throws EnvironmentError (OSError, IOError, shutil.Error)
  232. @errorfix_win
  233. def copy_tree(src, dst, copy_function=shutil.copy2):
  234. if os.path.isfile(src):
  235. copy_file(src, dst, copy_function=copy_function)
  236. return
  237. copytree3(src, dst, copy_function=copy_function)
  238. # File read
  239. # Throws OSError
  240. @errorfix_win
  241. def read_file(path, binary=True):
  242. with open(path, 'r' + ('b' if binary else '')) as f:
  243. return f.read()
  244. # Text file read
  245. # Throws OSError
  246. @errorfix_win
  247. def read_text(path):
  248. return read_file(path, binary=False)
  249. # Decoding file read
  250. # Throws OSError
  251. @errorfix_win
  252. def read_file_unicode(path, binary=True, enc='utf-8'):
  253. if not binary:
  254. if six.PY2:
  255. with open(path, 'r') as f:
  256. return library.python.strings.to_unicode(f.read(), enc)
  257. else:
  258. with open(path, 'r', encoding=enc) as f:
  259. return f.read()
  260. # codecs.open is always binary
  261. with codecs.open(path, 'r', encoding=enc, errors=library.python.strings.ENCODING_ERRORS_POLICY) as f:
  262. return f.read()
  263. @errorfix_win
  264. def open_file(*args, **kwargs):
  265. return (
  266. library.python.windows.open_file(*args, **kwargs) if library.python.windows.on_win() else open(*args, **kwargs)
  267. )
  268. # Atomic file write
  269. # Throws OSError
  270. @errorfix_win
  271. def write_file(path, data, binary=True):
  272. dir_path = os.path.dirname(path)
  273. if dir_path:
  274. ensure_dir(dir_path)
  275. tmp_path = path + '.tmp.' + str(random.random())
  276. with open_file(tmp_path, 'w' + ('b' if binary else '')) as f:
  277. if not isinstance(data, bytes) and binary:
  278. data = data.encode('UTF-8')
  279. f.write(data)
  280. replace_file(tmp_path, path)
  281. # Atomic text file write
  282. # Throws OSError
  283. @errorfix_win
  284. def write_text(path, data):
  285. write_file(path, data, binary=False)
  286. # File size
  287. # Throws OSError
  288. @errorfix_win
  289. def get_file_size(path):
  290. return os.path.getsize(path)
  291. # File/directory size
  292. # Non-recursive mode for directory counts size for immediates
  293. # While raise_all_errors is set to False, file size fallbacks to zero in case of getsize errors
  294. # Throws OSError
  295. @errorfix_win
  296. def get_tree_size(path, recursive=False, raise_all_errors=False):
  297. if os.path.isfile(path):
  298. return get_file_size(path)
  299. total_size = 0
  300. for dir_path, _, files in os.walk(path):
  301. for f in files:
  302. fp = os.path.join(dir_path, f)
  303. try:
  304. total_size += get_file_size(fp)
  305. except OSError as e:
  306. if raise_all_errors:
  307. raise
  308. logger.debug("Cannot calculate file size: %s", e)
  309. if not recursive:
  310. break
  311. return total_size
  312. # Directory copy ported from Python 3
  313. def copytree3(
  314. src,
  315. dst,
  316. symlinks=False,
  317. ignore=None,
  318. copy_function=shutil.copy2,
  319. ignore_dangling_symlinks=False,
  320. dirs_exist_ok=False,
  321. ):
  322. """Recursively copy a directory tree.
  323. The copytree3 is a port of shutil.copytree function from python-3.2.
  324. It has additional useful parameters and may be helpful while we are
  325. on python-2.x. It has to be removed as soon as we have moved to
  326. python-3.2 or higher.
  327. The destination directory must not already exist.
  328. If exception(s) occur, an Error is raised with a list of reasons.
  329. If the optional symlinks flag is true, symbolic links in the
  330. source tree result in symbolic links in the destination tree; if
  331. it is false, the contents of the files pointed to by symbolic
  332. links are copied. If the file pointed by the symlink doesn't
  333. exist, an exception will be added in the list of errors raised in
  334. an Error exception at the end of the copy process.
  335. You can set the optional ignore_dangling_symlinks flag to true if you
  336. want to silence this exception. Notice that this has no effect on
  337. platforms that don't support os.symlink.
  338. The optional ignore argument is a callable. If given, it
  339. is called with the `src` parameter, which is the directory
  340. being visited by copytree3(), and `names` which is the list of
  341. `src` contents, as returned by os.listdir():
  342. callable(src, names) -> ignored_names
  343. Since copytree3() is called recursively, the callable will be
  344. called once for each directory that is copied. It returns a
  345. list of names relative to the `src` directory that should
  346. not be copied.
  347. The optional copy_function argument is a callable that will be used
  348. to copy each file. It will be called with the source path and the
  349. destination path as arguments. By default, copy2() is used, but any
  350. function that supports the same signature (like copy()) can be used.
  351. """
  352. names = os.listdir(src)
  353. if ignore is not None:
  354. ignored_names = ignore(src, names)
  355. else:
  356. ignored_names = set()
  357. if not (dirs_exist_ok and os.path.isdir(dst)):
  358. os.makedirs(dst)
  359. errors = []
  360. for name in names:
  361. if name in ignored_names:
  362. continue
  363. srcname = os.path.join(src, name)
  364. dstname = os.path.join(dst, name)
  365. try:
  366. if os.path.islink(srcname):
  367. linkto = os.readlink(srcname)
  368. if symlinks:
  369. # We can't just leave it to `copy_function` because legacy
  370. # code with a custom `copy_function` may rely on copytree3
  371. # doing the right thing.
  372. os.symlink(linkto, dstname)
  373. else:
  374. # ignore dangling symlink if the flag is on
  375. if not os.path.exists(linkto) and ignore_dangling_symlinks:
  376. continue
  377. # otherwise let the copy occurs. copy2 will raise an error
  378. copy_function(srcname, dstname)
  379. elif os.path.isdir(srcname):
  380. copytree3(srcname, dstname, symlinks, ignore, copy_function, dirs_exist_ok=dirs_exist_ok)
  381. else:
  382. # Will raise a SpecialFileError for unsupported file types
  383. copy_function(srcname, dstname)
  384. # catch the Error from the recursive copytree3 so that we can
  385. # continue with other files
  386. except shutil.Error as err:
  387. errors.extend(err.args[0])
  388. except EnvironmentError as why:
  389. errors.append((srcname, dstname, str(why)))
  390. try:
  391. shutil.copystat(src, dst)
  392. except OSError as why:
  393. if WindowsError is not None and isinstance(why, WindowsError):
  394. # Copying file access times may fail on Windows
  395. pass
  396. else:
  397. errors.extend((src, dst, str(why)))
  398. if errors:
  399. raise shutil.Error(errors)
  400. def walk_relative(path, topdown=True, onerror=None, followlinks=False):
  401. for dirpath, dirnames, filenames in os.walk(path, topdown=topdown, onerror=onerror, followlinks=followlinks):
  402. yield os.path.relpath(dirpath, path), dirnames, filenames
  403. def supports_clone():
  404. if 'darwin' in sys.platform:
  405. import platform
  406. return list(map(int, platform.mac_ver()[0].split('.'))) >= [10, 13]
  407. return False
  408. def commonpath(paths):
  409. assert paths
  410. if len(paths) == 1:
  411. return next(iter(paths))
  412. split_paths = [path.split(os.sep) for path in paths]
  413. smin = min(split_paths)
  414. smax = max(split_paths)
  415. common = smin
  416. for i, c in enumerate(smin):
  417. if c != smax[i]:
  418. common = smin[:i]
  419. break
  420. return os.path.sep.join(common)
  421. def set_execute_bits(filename):
  422. stm = os.stat(filename).st_mode
  423. exe = stm | 0o111
  424. if stm != exe:
  425. os.chmod(filename, exe)