|
| 1 | +""" |
| 2 | +Script that setups the environment before build. |
| 3 | +""" |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from argparse import ArgumentParser |
| 7 | + |
| 8 | +import runpy |
| 9 | +import re |
| 10 | +import os |
| 11 | +import glob |
| 12 | +import shutil |
| 13 | +import json |
| 14 | + |
| 15 | + |
| 16 | +parse = ArgumentParser() |
| 17 | +parse.add_argument("--clean", action="store_true", default=False, help="If given, it will only clean instead of copy and run") |
| 18 | +parse.add_argument("--start-dir", default="./", dest="start_dir", help="Where to start looking for dep_local.json") |
| 19 | +args = parse.parse_args() |
| 20 | + |
| 21 | + |
| 22 | +CLEAN_ARG: bool = args.clean |
| 23 | +START_DIR_ARG: str = args.start_dir |
| 24 | + |
| 25 | +# Work relatively to the setup script location |
| 26 | +os.chdir(os.path.dirname(__file__)) |
| 27 | + |
| 28 | +for path, dirs, files in os.walk(START_DIR_ARG): |
| 29 | + for file in files: |
| 30 | + if file == "dep_local.json": |
| 31 | + file = os.path.join(path, file) |
| 32 | + # Copy files |
| 33 | + setup_file_data: dict = None |
| 34 | + with open(file, 'r', encoding="utf-8") as setup_file: |
| 35 | + setup_file_data = json.load(setup_file) |
| 36 | + |
| 37 | + # While copying change to dep-files cwd |
| 38 | + cwd = os.getcwd() |
| 39 | + os.chdir(os.path.abspath(os.path.dirname(file))) |
| 40 | + # [(from, to), (from, to)] |
| 41 | + destinations = setup_file_data["copy"] |
| 42 | + for dest in destinations: |
| 43 | + cp_from = dest["from"] |
| 44 | + cp_to = dest["to"] |
| 45 | + if re.search(r"\.[A-z]+$", cp_to) is None: # The path does not have extension -> assume a dir |
| 46 | + _src = [x for x in glob.glob(cp_from, recursive=True) if os.path.isfile(x)] |
| 47 | + _dest = [os.path.join(cp_to, os.path.basename(m)) for m in _src] |
| 48 | + else: |
| 49 | + _src = [cp_from] |
| 50 | + _dest = [cp_to] |
| 51 | + |
| 52 | + srcdest = zip(_src, _dest) |
| 53 | + for fromf, tof in srcdest: |
| 54 | + if CLEAN_ARG: |
| 55 | + if os.path.exists(tof): |
| 56 | + os.remove(tof) |
| 57 | + else: |
| 58 | + tof_dir = Path(os.path.dirname(tof)) |
| 59 | + tof_dir.mkdir(parents=True, exist_ok=True) |
| 60 | + shutil.copy2(fromf, tof) |
| 61 | + |
| 62 | + # Run scripts |
| 63 | + if CLEAN_ARG: |
| 64 | + os.chdir(cwd) |
| 65 | + continue |
| 66 | + |
| 67 | + scripts = setup_file_data["scripts"] |
| 68 | + for script in scripts: |
| 69 | + runpy.run_path(script) |
| 70 | + |
| 71 | + # Change cwd back to original |
| 72 | + os.chdir(cwd) |
0 commit comments