util.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import SCons
  2. from SCons.Subst import quote_spaces
  3. import re
  4. import os
  5. import random
  6. import string
  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 Exception(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 random_alnum(length):
  30. return "".join(
  31. random.choice(string.ascii_letters + string.digits) for _ in range(length)
  32. )