path.py 10.0 KB

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