52 satır
1.6KB

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