path.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. if (os.stat(src).st_dev == os.stat(dst).st_dev):
  85. # Need to copy directories only with tar first since cp will error if two
  86. # writers try and create a directory at the same time
  87. 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)
  88. subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
  89. source = ''
  90. if os.path.isdir(src):
  91. if len(glob.glob('%s/.??*' % src)) > 0:
  92. source = './.??* '
  93. source += './*'
  94. s_dir = src
  95. else:
  96. source = src
  97. s_dir = os.getcwd()
  98. cmd = 'cp -afl --preserve=xattr %s %s' % (source, os.path.realpath(dst))
  99. subprocess.check_output(cmd, shell=True, cwd=s_dir, stderr=subprocess.STDOUT)
  100. else:
  101. copytree(src, dst)
  102. def copyhardlink(src, dst):
  103. """Make a hard link when possible, otherwise copy."""
  104. # We need to stat the destination directory as the destination file probably
  105. # doesn't exist yet.
  106. dstdir = os.path.dirname(dst)
  107. if os.stat(src).st_dev == os.stat(dstdir).st_dev:
  108. os.link(src, dst)
  109. else:
  110. shutil.copy(src, dst)
  111. def remove(path, recurse=True):
  112. """
  113. Equivalent to rm -f or rm -rf
  114. NOTE: be careful about passing paths that may contain filenames with
  115. wildcards in them (as opposed to passing an actual wildcarded path) -
  116. since we use glob.glob() to expand the path. Filenames containing
  117. square brackets are particularly problematic since the they may not
  118. actually expand to match the original filename.
  119. """
  120. for name in glob.glob(path):
  121. try:
  122. os.unlink(name)
  123. except OSError as exc:
  124. if recurse and exc.errno == errno.EISDIR:
  125. shutil.rmtree(name)
  126. elif exc.errno != errno.ENOENT:
  127. raise
  128. def symlink(source, destination, force=False):
  129. """Create a symbolic link"""
  130. try:
  131. if force:
  132. remove(destination)
  133. os.symlink(source, destination)
  134. except OSError as e:
  135. if e.errno != errno.EEXIST or os.readlink(destination) != source:
  136. raise
  137. def find(dir, **walkoptions):
  138. """ Given a directory, recurses into that directory,
  139. returning all files as absolute paths. """
  140. for root, dirs, files in os.walk(dir, **walkoptions):
  141. for file in files:
  142. yield os.path.join(root, file)
  143. ## realpath() related functions
  144. def __is_path_below(file, root):
  145. return (file + os.path.sep).startswith(root)
  146. def __realpath_rel(start, rel_path, root, loop_cnt, assume_dir):
  147. """Calculates real path of symlink 'start' + 'rel_path' below
  148. 'root'; no part of 'start' below 'root' must contain symlinks. """
  149. have_dir = True
  150. for d in rel_path.split(os.path.sep):
  151. if not have_dir and not assume_dir:
  152. raise OSError(errno.ENOENT, "no such directory %s" % start)
  153. if d == os.path.pardir: # '..'
  154. if len(start) >= len(root):
  155. # do not follow '..' before root
  156. start = os.path.dirname(start)
  157. else:
  158. # emit warning?
  159. pass
  160. else:
  161. (start, have_dir) = __realpath(os.path.join(start, d),
  162. root, loop_cnt, assume_dir)
  163. assert(__is_path_below(start, root))
  164. return start
  165. def __realpath(file, root, loop_cnt, assume_dir):
  166. while os.path.islink(file) and len(file) >= len(root):
  167. if loop_cnt == 0:
  168. raise OSError(errno.ELOOP, file)
  169. loop_cnt -= 1
  170. target = os.path.normpath(os.readlink(file))
  171. if not os.path.isabs(target):
  172. tdir = os.path.dirname(file)
  173. assert(__is_path_below(tdir, root))
  174. else:
  175. tdir = root
  176. file = __realpath_rel(tdir, target, root, loop_cnt, assume_dir)
  177. try:
  178. is_dir = os.path.isdir(file)
  179. except:
  180. is_dir = false
  181. return (file, is_dir)
  182. def realpath(file, root, use_physdir = True, loop_cnt = 100, assume_dir = False):
  183. """ Returns the canonical path of 'file' with assuming a
  184. toplevel 'root' directory. When 'use_physdir' is set, all
  185. preceding path components of 'file' will be resolved first;
  186. this flag should be set unless it is guaranteed that there is
  187. no symlink in the path. When 'assume_dir' is not set, missing
  188. path components will raise an ENOENT error"""
  189. root = os.path.normpath(root)
  190. file = os.path.normpath(file)
  191. if not root.endswith(os.path.sep):
  192. # letting root end with '/' makes some things easier
  193. root = root + os.path.sep
  194. if not __is_path_below(file, root):
  195. raise OSError(errno.EINVAL, "file '%s' is not below root" % file)
  196. try:
  197. if use_physdir:
  198. file = __realpath_rel(root, file[(len(root) - 1):], root, loop_cnt, assume_dir)
  199. else:
  200. file = __realpath(file, root, loop_cnt, assume_dir)[0]
  201. except OSError as e:
  202. if e.errno == errno.ELOOP:
  203. # make ELOOP more readable; without catching it, there will
  204. # be printed a backtrace with 100s of OSError exceptions
  205. # else
  206. raise OSError(errno.ELOOP,
  207. "too much recursions while resolving '%s'; loop in '%s'" %
  208. (file, e.strerror))
  209. raise
  210. return file
  211. def is_path_parent(possible_parent, *paths):
  212. """
  213. Return True if a path is the parent of another, False otherwise.
  214. Multiple paths to test can be specified in which case all
  215. specified test paths must be under the parent in order to
  216. return True.
  217. """
  218. def abs_path_trailing(pth):
  219. pth_abs = os.path.abspath(pth)
  220. if not pth_abs.endswith(os.sep):
  221. pth_abs += os.sep
  222. return pth_abs
  223. possible_parent_abs = abs_path_trailing(possible_parent)
  224. if not paths:
  225. return False
  226. for path in paths:
  227. path_abs = abs_path_trailing(path)
  228. if not path_abs.startswith(possible_parent_abs):
  229. return False
  230. return True
  231. def which_wild(pathname, path=None, mode=os.F_OK, *, reverse=False, candidates=False):
  232. """Search a search path for pathname, supporting wildcards.
  233. Return all paths in the specific search path matching the wildcard pattern
  234. in pathname, returning only the first encountered for each file. If
  235. candidates is True, information on all potential candidate paths are
  236. included.
  237. """
  238. paths = (path or os.environ.get('PATH', os.defpath)).split(':')
  239. if reverse:
  240. paths.reverse()
  241. seen, files = set(), []
  242. for index, element in enumerate(paths):
  243. if not os.path.isabs(element):
  244. element = os.path.abspath(element)
  245. candidate = os.path.join(element, pathname)
  246. globbed = glob.glob(candidate)
  247. if globbed:
  248. for found_path in sorted(globbed):
  249. if not os.access(found_path, mode):
  250. continue
  251. rel = os.path.relpath(found_path, element)
  252. if rel not in seen:
  253. seen.add(rel)
  254. if candidates:
  255. files.append((found_path, [os.path.join(p, rel) for p in paths[:index+1]]))
  256. else:
  257. files.append(found_path)
  258. return files