path.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. import errno
  5. import glob
  6. import shutil
  7. import subprocess
  8. import os.path
  9. def join(*paths):
  10. """Like os.path.join but doesn't treat absolute RHS specially"""
  11. return os.path.normpath("/".join(paths))
  12. def relative(src, dest):
  13. """ Return a relative path from src to dest.
  14. >>> relative("/usr/bin", "/tmp/foo/bar")
  15. ../../tmp/foo/bar
  16. >>> relative("/usr/bin", "/usr/lib")
  17. ../lib
  18. >>> relative("/tmp", "/tmp/foo/bar")
  19. foo/bar
  20. """
  21. return os.path.relpath(dest, src)
  22. def make_relative_symlink(path):
  23. """ Convert an absolute symlink to a relative one """
  24. if not os.path.islink(path):
  25. return
  26. link = os.readlink(path)
  27. if not os.path.isabs(link):
  28. return
  29. # find the common ancestor directory
  30. ancestor = path
  31. depth = 0
  32. while ancestor and not link.startswith(ancestor):
  33. ancestor = ancestor.rpartition('/')[0]
  34. depth += 1
  35. if not ancestor:
  36. print("make_relative_symlink() Error: unable to find the common ancestor of %s and its target" % path)
  37. return
  38. base = link.partition(ancestor)[2].strip('/')
  39. while depth > 1:
  40. base = "../" + base
  41. depth -= 1
  42. os.remove(path)
  43. os.symlink(base, path)
  44. def replace_absolute_symlinks(basedir, d):
  45. """
  46. Walk basedir looking for absolute symlinks and replacing them with relative ones.
  47. The absolute links are assumed to be relative to basedir
  48. (compared to make_relative_symlink above which tries to compute common ancestors
  49. using pattern matching instead)
  50. """
  51. for walkroot, dirs, files in os.walk(basedir):
  52. for file in files + dirs:
  53. path = os.path.join(walkroot, file)
  54. if not os.path.islink(path):
  55. continue
  56. link = os.readlink(path)
  57. if not os.path.isabs(link):
  58. continue
  59. walkdir = os.path.dirname(path.rpartition(basedir)[2])
  60. base = os.path.relpath(link, walkdir)
  61. bb.debug(2, "Replacing absolute path %s with relative path %s" % (link, base))
  62. os.remove(path)
  63. os.symlink(base, path)
  64. def format_display(path, metadata):
  65. """ Prepare a path for display to the user. """
  66. rel = relative(metadata.getVar("TOPDIR"), path)
  67. if len(rel) > len(path):
  68. return path
  69. else:
  70. return rel
  71. def copytree(src, dst):
  72. # We could use something like shutil.copytree here but it turns out to
  73. # to be slow. It takes twice as long copying to an empty directory.
  74. # If dst already has contents performance can be 15 time slower
  75. # This way we also preserve hardlinks between files in the tree.
  76. bb.utils.mkdirhier(dst)
  77. cmd = "tar --xattrs --xattrs-include='*' -cf - -S -C %s -p . | tar --xattrs --xattrs-include='*' -xf - -C %s" % (src, dst)
  78. subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
  79. def copyhardlinktree(src, dst):
  80. """Make a tree of hard links when possible, otherwise copy."""
  81. bb.utils.mkdirhier(dst)
  82. if os.path.isdir(src) and not len(os.listdir(src)):
  83. return
  84. canhard = False
  85. testfile = None
  86. for root, dirs, files in os.walk(src):
  87. if len(files):
  88. testfile = os.path.join(root, files[0])
  89. break
  90. if testfile is not None:
  91. try:
  92. os.link(testfile, os.path.join(dst, 'testfile'))
  93. os.unlink(os.path.join(dst, 'testfile'))
  94. canhard = True
  95. except Exception as e:
  96. bb.debug(2, "Hardlink test failed with " + str(e))
  97. if (canhard):
  98. # Need to copy directories only with tar first since cp will error if two
  99. # writers try and create a directory at the same time
  100. cmd = "cd %s; find . -type d -print | tar --xattrs --xattrs-include='*' -cf - -S -C %s -p --no-recursion --files-from - | tar --xattrs --xattrs-include='*' -xhf - -C %s" % (src, src, dst)
  101. subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
  102. source = ''
  103. if os.path.isdir(src):
  104. if len(glob.glob('%s/.??*' % src)) > 0:
  105. source = './.??* '
  106. source += './*'
  107. s_dir = src
  108. else:
  109. source = src
  110. s_dir = os.getcwd()
  111. cmd = 'cp -afl --preserve=xattr %s %s' % (source, os.path.realpath(dst))
  112. subprocess.check_output(cmd, shell=True, cwd=s_dir, stderr=subprocess.STDOUT)
  113. else:
  114. copytree(src, dst)
  115. def copyhardlink(src, dst):
  116. """Make a hard link when possible, otherwise copy."""
  117. try:
  118. os.link(src, dst)
  119. except OSError:
  120. shutil.copy(src, dst)
  121. def remove(path, recurse=True):
  122. """
  123. Equivalent to rm -f or rm -rf
  124. NOTE: be careful about passing paths that may contain filenames with
  125. wildcards in them (as opposed to passing an actual wildcarded path) -
  126. since we use glob.glob() to expand the path. Filenames containing
  127. square brackets are particularly problematic since the they may not
  128. actually expand to match the original filename.
  129. """
  130. for name in glob.glob(path):
  131. try:
  132. os.unlink(name)
  133. except OSError as exc:
  134. if recurse and exc.errno == errno.EISDIR:
  135. shutil.rmtree(name)
  136. elif exc.errno != errno.ENOENT:
  137. raise
  138. def symlink(source, destination, force=False):
  139. """Create a symbolic link"""
  140. try:
  141. if force:
  142. remove(destination)
  143. os.symlink(source, destination)
  144. except OSError as e:
  145. if e.errno != errno.EEXIST or os.readlink(destination) != source:
  146. raise
  147. def find(dir, **walkoptions):
  148. """ Given a directory, recurses into that directory,
  149. returning all files as absolute paths. """
  150. for root, dirs, files in os.walk(dir, **walkoptions):
  151. for file in files:
  152. yield os.path.join(root, file)
  153. ## realpath() related functions
  154. def __is_path_below(file, root):
  155. return (file + os.path.sep).startswith(root)
  156. def __realpath_rel(start, rel_path, root, loop_cnt, assume_dir):
  157. """Calculates real path of symlink 'start' + 'rel_path' below
  158. 'root'; no part of 'start' below 'root' must contain symlinks. """
  159. have_dir = True
  160. for d in rel_path.split(os.path.sep):
  161. if not have_dir and not assume_dir:
  162. raise OSError(errno.ENOENT, "no such directory %s" % start)
  163. if d == os.path.pardir: # '..'
  164. if len(start) >= len(root):
  165. # do not follow '..' before root
  166. start = os.path.dirname(start)
  167. else:
  168. # emit warning?
  169. pass
  170. else:
  171. (start, have_dir) = __realpath(os.path.join(start, d),
  172. root, loop_cnt, assume_dir)
  173. assert(__is_path_below(start, root))
  174. return start
  175. def __realpath(file, root, loop_cnt, assume_dir):
  176. while os.path.islink(file) and len(file) >= len(root):
  177. if loop_cnt == 0:
  178. raise OSError(errno.ELOOP, file)
  179. loop_cnt -= 1
  180. target = os.path.normpath(os.readlink(file))
  181. if not os.path.isabs(target):
  182. tdir = os.path.dirname(file)
  183. assert(__is_path_below(tdir, root))
  184. else:
  185. tdir = root
  186. file = __realpath_rel(tdir, target, root, loop_cnt, assume_dir)
  187. try:
  188. is_dir = os.path.isdir(file)
  189. except:
  190. is_dir = false
  191. return (file, is_dir)
  192. def realpath(file, root, use_physdir = True, loop_cnt = 100, assume_dir = False):
  193. """ Returns the canonical path of 'file' with assuming a
  194. toplevel 'root' directory. When 'use_physdir' is set, all
  195. preceding path components of 'file' will be resolved first;
  196. this flag should be set unless it is guaranteed that there is
  197. no symlink in the path. When 'assume_dir' is not set, missing
  198. path components will raise an ENOENT error"""
  199. root = os.path.normpath(root)
  200. file = os.path.normpath(file)
  201. if not root.endswith(os.path.sep):
  202. # letting root end with '/' makes some things easier
  203. root = root + os.path.sep
  204. if not __is_path_below(file, root):
  205. raise OSError(errno.EINVAL, "file '%s' is not below root" % file)
  206. try:
  207. if use_physdir:
  208. file = __realpath_rel(root, file[(len(root) - 1):], root, loop_cnt, assume_dir)
  209. else:
  210. file = __realpath(file, root, loop_cnt, assume_dir)[0]
  211. except OSError as e:
  212. if e.errno == errno.ELOOP:
  213. # make ELOOP more readable; without catching it, there will
  214. # be printed a backtrace with 100s of OSError exceptions
  215. # else
  216. raise OSError(errno.ELOOP,
  217. "too much recursions while resolving '%s'; loop in '%s'" %
  218. (file, e.strerror))
  219. raise
  220. return file
  221. def is_path_parent(possible_parent, *paths):
  222. """
  223. Return True if a path is the parent of another, False otherwise.
  224. Multiple paths to test can be specified in which case all
  225. specified test paths must be under the parent in order to
  226. return True.
  227. """
  228. def abs_path_trailing(pth):
  229. pth_abs = os.path.abspath(pth)
  230. if not pth_abs.endswith(os.sep):
  231. pth_abs += os.sep
  232. return pth_abs
  233. possible_parent_abs = abs_path_trailing(possible_parent)
  234. if not paths:
  235. return False
  236. for path in paths:
  237. path_abs = abs_path_trailing(path)
  238. if not path_abs.startswith(possible_parent_abs):
  239. return False
  240. return True
  241. def which_wild(pathname, path=None, mode=os.F_OK, *, reverse=False, candidates=False):
  242. """Search a search path for pathname, supporting wildcards.
  243. Return all paths in the specific search path matching the wildcard pattern
  244. in pathname, returning only the first encountered for each file. If
  245. candidates is True, information on all potential candidate paths are
  246. included.
  247. """
  248. paths = (path or os.environ.get('PATH', os.defpath)).split(':')
  249. if reverse:
  250. paths.reverse()
  251. seen, files = set(), []
  252. for index, element in enumerate(paths):
  253. if not os.path.isabs(element):
  254. element = os.path.abspath(element)
  255. candidate = os.path.join(element, pathname)
  256. globbed = glob.glob(candidate)
  257. if globbed:
  258. for found_path in sorted(globbed):
  259. if not os.access(found_path, mode):
  260. continue
  261. rel = os.path.relpath(found_path, element)
  262. if rel not in seen:
  263. seen.add(rel)
  264. if candidates:
  265. files.append((found_path, [os.path.join(p, rel) for p in paths[:index+1]]))
  266. else:
  267. files.append(found_path)
  268. return files