util.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import SCons
  2. from SCons.Subst import quote_spaces
  3. from SCons.Errors import StopError
  4. import re
  5. import os
  6. WINPATHSEP_RE = re.compile(r"\\([^\"'\\]|$)")
  7. # Used by default when globbing for files with GlobRecursive
  8. # Excludes all files ending with ~, usually created by editors as backup files
  9. GLOB_FILE_EXCLUSION = ["*~"]
  10. def tempfile_arg_esc_func(arg):
  11. arg = quote_spaces(arg)
  12. if SCons.Platform.platform_default() != "win32":
  13. return arg
  14. # GCC requires double Windows slashes, let's use UNIX separator
  15. return WINPATHSEP_RE.sub(r"/\1", arg)
  16. def wrap_tempfile(env, command):
  17. env[command] = '${TEMPFILE("' + env[command] + '","$' + command + 'STR")}'
  18. def link_dir(target_path, source_path, is_windows):
  19. # print(f"link_dir: {target_path} -> {source_path}")
  20. if os.path.lexists(target_path) or os.path.exists(target_path):
  21. os.unlink(target_path)
  22. if is_windows:
  23. # Crete junction
  24. import _winapi
  25. if not os.path.isdir(source_path):
  26. raise StopError(f"Source directory {source_path} is not a directory")
  27. if not os.path.exists(target_path):
  28. _winapi.CreateJunction(source_path, target_path)
  29. else:
  30. os.symlink(source_path, target_path)
  31. def single_quote(arg_list):
  32. return " ".join(f"'{arg}'" if " " in arg else str(arg) for arg in arg_list)
  33. def extract_abs_dir(node):
  34. if isinstance(node, SCons.Node.FS.EntryProxy):
  35. node = node.get()
  36. for repo_dir in node.get_all_rdirs():
  37. if os.path.exists(repo_dir.abspath):
  38. return repo_dir
  39. def extract_abs_dir_path(node):
  40. abs_dir_node = extract_abs_dir(node)
  41. if abs_dir_node is None:
  42. raise StopError(f"Can't find absolute path for {node.name}")
  43. return abs_dir_node.abspath
  44. def path_as_posix(path):
  45. if SCons.Platform.platform_default() == "win32":
  46. return path.replace(os.path.sep, os.path.altsep)
  47. return path