util.py 1.7 KB

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