Skip to content

Commit 9168c67

Browse files
committed
decouple
1 parent 4299b80 commit 9168c67

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

package.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
early = globals()["early"]
3+
4+
5+
name = "python"
6+
7+
authors = ["Guido van Rossum"]
8+
9+
description = "The Python programming language.(Ship via Miniconda)"
10+
11+
12+
@early()
13+
def version():
14+
"""Define Python version from command line option
15+
"""
16+
import sys
17+
import argparse
18+
19+
if any(help_ in sys.argv[1:] for help_ in ["-h", "--help"]):
20+
# Skip parsing version string if user is asking for help,
21+
# or the following parser will print out it's own help
22+
# message without rez-build's.
23+
return ""
24+
25+
parser = argparse.ArgumentParser()
26+
27+
with open("./parse_build_args.py", "r") as add_args:
28+
exec(add_args.read(), {"parser": parser})
29+
30+
args, unknown = parser.parse_known_args() # parse `sys.argv`
31+
python_version = args.version
32+
33+
return python_version
34+
35+
36+
variants = [
37+
["platform-*"],
38+
]
39+
40+
41+
build_requires = [
42+
"miniconda",
43+
]
44+
45+
46+
build_command = "python {root}/rezbuild.py {install}"
47+
48+
49+
def commands():
50+
env = globals()["env"]
51+
this = globals()["this"]
52+
system = globals()["system"]
53+
building = globals()["building"]
54+
55+
if system.platform == "windows":
56+
env.PATH.prepend("{root}/payload/Library/bin")
57+
env.PATH.prepend("{root}/payload/Scripts")
58+
env.PATH.prepend("{root}/payload")
59+
else:
60+
env.PATH.prepend("{root}/payload/bin")
61+
62+
63+
uuid = "repository.python"

parse_build_args.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
# `parser` var will be given by custom build system
3+
parser.add_argument("--version",
4+
required=True,
5+
type=str,
6+
help="Set Python version to build.")

rezbuild.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)