Skip to content

Instantly share code, notes, and snippets.

@njsmith
Last active December 1, 2016 09:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save njsmith/75818a6debbce9d7ff48 to your computer and use it in GitHub Desktop.
Save njsmith/75818a6debbce9d7ff48 to your computer and use it in GitHub Desktop.
# Sample usage (with hypothetical build_wheel method that returns the name of the built wheel):
# wheel_name = call_build_system("build/tmp", "build/tmp/venv/bin/python",
# "flit:build_system_api",
# "build_wheel", output_dir)
import json
import pkg_resources
from pip.utils import call_subprocess
def call_build_system(temp_dir, build_environment_python, specifier, method, *args, **kwargs):
# We assume we have a scratch directory to work with -- like the one we
# created to store the build environment.
worker_path = os.path.join(temp_dir, "pip-worker.py")
in_path = os.path.join(temp_dir, "in")
out_path = os.path.join(temp_dir, "out")
worker_code = pkg_resources.resource_string(__name__, "pip-worker.py")
with open(worker_path, "w") as worker_file:
worker_file.write(worker_code)
with open(in_path, "w") as in_file:
json.dump([specifier, method, args, kwargs], in_file)
# Same machinery we use to call python setup.py whatever today:
call_subprocess([build_environment_python, worker_path, in_path, out_path],
...)
with open(out_path) as out_file:
return json.load(out_file)
import sys
if "" in sys.path:
sys.path.remove("")
import json
(in_path, out_path) = sys.argv[1:]
def resolve_specifier(spec):
# Technically "foo.bar" and "foo.bar:" are legal entry point descriptors.
if ":" not in hook_string:
hook_string += ":"
module_part, dot_part = hook_string.split(":", 1)
module_segments = module_part.split(".")
if dot_part:
dot_part = dot_part.split(".")
else:
dot_part = []
__import__(module_part)
segments = module_segments + dot_segments
obj = sys.modules[segments[0]]
for segment in segments[1:]:
obj = getattr(hook, segment)
return obj
with open(in_path) as in_file:
specifier, method, args, kwargs = json.load(in_file)
build_system = resolve_specifier(specifier)
if getattr(build_system, "schema_version", 1) > 1:
sys.exit("Uh oh")
result = getattr(build_system, method)(*args, **kwargs)
with open(out_path, "w") as out_file):
json.dump(result, out_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment