You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

paths.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import os
  2. import shutil
  3. import itertools
  4. import tempfile
  5. from contextlib import contextmanager
  6. from pathlib import Path
  7. from typing import Iterator, Optional
  8. # The root directory of the dds project
  9. PROJECT_ROOT = Path(__file__).absolute().parent.parent.parent
  10. #: The <repo>/tools directory
  11. TOOLS_DIR = PROJECT_ROOT / 'tools'
  12. #: The default build directory
  13. BUILD_DIR = PROJECT_ROOT / '_build'
  14. #: The directory were w prebuild/bootstrapped results will go, and scratch space for the build
  15. PREBUILT_DIR = PROJECT_ROOT / '_prebuilt'
  16. #: THe suffix of executable files on this system
  17. EXE_SUFFIX = '.exe' if os.name == 'nt' else ''
  18. #: The path to the prebuilt 'dds' executable
  19. PREBUILT_DDS = (PREBUILT_DIR / 'dds').with_suffix(EXE_SUFFIX)
  20. #: The path to the main built 'dds' executable
  21. CUR_BUILT_DDS = (BUILD_DIR / 'dds').with_suffix(EXE_SUFFIX)
  22. @contextmanager
  23. def new_tempdir() -> Iterator[Path]:
  24. """
  25. Create and yield a new temporary directory, which will be destroyed on
  26. context-manager exit
  27. """
  28. tdir = Path(tempfile.mkdtemp())
  29. try:
  30. yield tdir
  31. finally:
  32. shutil.rmtree(tdir)
  33. def find_exe(name: str) -> Optional[Path]:
  34. """
  35. Find a file on the system by searching through the PATH environment variable.
  36. """
  37. sep = ';' if os.name == 'nt' else ':'
  38. paths = os.environ['PATH'].split(sep)
  39. exts = os.environ['PATHEXT'].split(';') if os.name == 'nt' else ['']
  40. for dirpath, ext in itertools.product(paths, exts):
  41. cand = Path(dirpath) / (name + ext)
  42. if cand.is_file():
  43. return cand
  44. return None