builder.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import os
  2. import importlib
  3. from sccd.compiler.sccdc import generate, Platforms
  4. from lib.os_tools import *
  5. # Builds and loads test files.
  6. class Builder:
  7. def __init__(self, build_dir: str):
  8. self.build_dir = build_dir
  9. # Builds module for src_file is src_file is newer than potentially existing build.
  10. def build(self, src_file: str):
  11. target_file = os.path.join(self.build_dir, dropext(src_file)+'.py')
  12. # Get src_file and target_file modification times
  13. src_file_mtime = os.path.getmtime(src_file)
  14. target_file_mtime = 0.0
  15. try:
  16. target_file_mtime = os.path.getmtime(target_file)
  17. except FileNotFoundError:
  18. pass
  19. if src_file_mtime > target_file_mtime:
  20. # (Re-)Compile test
  21. make_dirs(target_file)
  22. generate(src_file, target_file, "python", Platforms.Threads)
  23. # Builds module and loads it. Returns loaded module.
  24. def build_and_load(self, src_file: str):
  25. self.build(src_file)
  26. module_name = os.path.join(self.build_dir, dropext(src_file)).replace(os.path.sep, ".")
  27. return importlib.import_module(module_name)