util.py 1.6 KB

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