util.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import SCons
  2. from SCons.Subst import quote_spaces
  3. from SCons.Errors import StopError
  4. import re
  5. import os
  6. import random
  7. import string
  8. WINPATHSEP_RE = re.compile(r"\\([^\"'\\]|$)")
  9. def tempfile_arg_esc_func(arg):
  10. arg = quote_spaces(arg)
  11. if SCons.Platform.platform_default() != "win32":
  12. return arg
  13. # GCC requires double Windows slashes, let's use UNIX separator
  14. return WINPATHSEP_RE.sub(r"/\1", arg)
  15. def wrap_tempfile(env, command):
  16. env[command] = '${TEMPFILE("' + env[command] + '","$' + command + 'STR")}'
  17. def link_dir(target_path, source_path, is_windows):
  18. # print(f"link_dir: {target_path} -> {source_path}")
  19. if os.path.lexists(target_path) or os.path.exists(target_path):
  20. os.unlink(target_path)
  21. if is_windows:
  22. # Crete junction
  23. import _winapi
  24. if not os.path.isdir(source_path):
  25. raise StopError(f"Source directory {source_path} is not a directory")
  26. if not os.path.exists(target_path):
  27. _winapi.CreateJunction(source_path, target_path)
  28. else:
  29. os.symlink(source_path, target_path)
  30. def single_quote(arg_list):
  31. return " ".join(f"'{arg}'" if " " in arg else str(arg) for arg in arg_list)