|
| 1 | + |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | + |
| 7 | + |
| 8 | +def logger(): |
| 9 | + import logging |
| 10 | + |
| 11 | + package_name = os.environ["REZ_BUILD_PROJECT_NAME"] |
| 12 | + log_name = package_name + ".build" |
| 13 | + |
| 14 | + # (TODO) Add formatter |
| 15 | + |
| 16 | + log_handler = logging.StreamHandler() |
| 17 | + log = logging.getLogger(log_name) |
| 18 | + log.addHandler(log_handler) |
| 19 | + log.setLevel(logging.INFO) |
| 20 | + |
| 21 | + return log |
| 22 | + |
| 23 | + |
| 24 | +def build(source_path, build_path, install_path, targets=None): |
| 25 | + log = logger() |
| 26 | + targets = targets or [] |
| 27 | + |
| 28 | + if "install" in targets: |
| 29 | + dst = install_path + "/payload" |
| 30 | + else: |
| 31 | + dst = build_path + "/payload" |
| 32 | + |
| 33 | + dst = os.path.normpath(dst) |
| 34 | + |
| 35 | + if os.path.isdir(dst): |
| 36 | + shutil.rmtree(dst) |
| 37 | + os.makedirs(dst) |
| 38 | + |
| 39 | + # Create with `--prefix` |
| 40 | + python_version = os.environ["REZ_BUILD_PROJECT_VERSION"] |
| 41 | + subprocess.check_output(["conda", |
| 42 | + "create", |
| 43 | + "--prefix", |
| 44 | + dst, |
| 45 | + "python=%s" % python_version, |
| 46 | + "--yes"]) |
| 47 | + |
| 48 | + # Unregister env location from `~/.conda/environment.txt` |
| 49 | + # see `../miniconda3/..Lib../site-packages/conda/core/envs_manager.py` |
| 50 | + # for conda environment registering implementation detail. |
| 51 | + # |
| 52 | + home = os.path.expanduser("~") |
| 53 | + registry = os.path.join(home, ".conda", "environments.txt") |
| 54 | + |
| 55 | + if not os.path.isfile(registry): |
| 56 | + log.warning("Conda environment.txt not found: %s\n" |
| 57 | + "Skip location unregistering." % registry) |
| 58 | + return |
| 59 | + |
| 60 | + # make sure that we are excluding the right location |
| 61 | + locations = [] |
| 62 | + with open(registry, "r") as f: |
| 63 | + for loc in f.readlines(): |
| 64 | + if dst != os.path.normpath(loc).strip(): |
| 65 | + locations.append(loc) |
| 66 | + |
| 67 | + # re-write without the location we just created |
| 68 | + with open(registry, "w") as f: |
| 69 | + f.writelines(locations) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + build(source_path=os.environ["REZ_BUILD_SOURCE_PATH"], |
| 74 | + build_path=os.environ["REZ_BUILD_PATH"], |
| 75 | + install_path=os.environ["REZ_BUILD_INSTALL_PATH"], |
| 76 | + targets=sys.argv[1:]) |
0 commit comments