-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from robotpy/update-gen
Update various generation related tooling
- Loading branch information
Showing
12 changed files
with
317 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import importlib.util | ||
import os | ||
from os.path import abspath, exists, dirname, join | ||
|
||
from setuptools import Command | ||
|
||
from .util import get_install_root | ||
|
||
|
||
class _BuiltEnv(Command): | ||
|
||
user_options = [("build-lib=", "d", 'directory to "build" (copy) to')] | ||
|
||
def initialize_options(self): | ||
self.build_lib = None | ||
|
||
def finalize_options(self): | ||
self.set_undefined_options("build", ("build_lib", "build_lib")) | ||
|
||
def setup_built_env(self): | ||
|
||
# Gather information for n | ||
data = {"mapping": {}} | ||
|
||
# OSX-specific: need to set DYLD_LIBRARY_PATH otherwise modules don't | ||
# work. Luckily, that information was computed when building the | ||
# extensions... | ||
env = os.environ.copy() | ||
dyld_path = set() | ||
|
||
# Requires information from build_ext to work | ||
build_ext = self.get_finalized_command("build_ext") | ||
if build_ext.inplace: | ||
data["out"] = get_install_root(self) | ||
else: | ||
data["out"] = self.build_lib | ||
|
||
# Ensure that the associated packages can always be found locally | ||
for wrapper in build_ext.wrappers: | ||
pkgdir = wrapper.package_name.split(".") | ||
init_py = abspath(join(self.build_lib, *pkgdir, "__init__.py")) | ||
if exists(init_py): | ||
data["mapping"][wrapper.package_name] = init_py | ||
|
||
# Ensure that the built extension can always be found | ||
build_ext.resolve_libs() | ||
for ext in build_ext.extensions: | ||
fname = build_ext.get_ext_filename(ext.name) | ||
data["mapping"][ext.name] = abspath(join(self.build_lib, fname)) | ||
|
||
rpybuild_libs = getattr(ext, "rpybuild_libs", None) | ||
if rpybuild_libs: | ||
for pth, _ in rpybuild_libs.values(): | ||
dyld_path.add(dirname(pth)) | ||
|
||
# OSX-specific | ||
if dyld_path: | ||
dyld_path = ":".join(dyld_path) | ||
if "DYLD_LIBRARY_PATH" in env: | ||
dyld_path += ":" + env["DYLD_LIBRARY_PATH"] | ||
env["DYLD_LIBRARY_PATH"] = dyld_path | ||
|
||
return data, env | ||
|
||
|
||
class _PackageFinder: | ||
""" | ||
Custom loader to allow loading built modules from their location | ||
in the build directory (as opposed to their install location) | ||
""" | ||
|
||
# Set this to mapping returned from _BuiltEnv.setup_built_env | ||
mapping = {} | ||
|
||
@classmethod | ||
def find_spec(cls, fullname, path, target=None): | ||
m = cls.mapping.get(fullname) | ||
if m: | ||
return importlib.util.spec_from_file_location(fullname, m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import json | ||
import os | ||
import subprocess | ||
import sys | ||
import typing | ||
|
||
try: | ||
from setuptools.errors import BaseError | ||
except ImportError: | ||
from distutils.errors import DistutilsError as BaseError | ||
|
||
from ._built_env import _BuiltEnv, _PackageFinder | ||
|
||
|
||
class UpdateInitError(BaseError): | ||
pass | ||
|
||
|
||
class UpdateInit(_BuiltEnv): | ||
update_list: typing.List[str] | ||
|
||
command_name = "update_init" | ||
description = ( | ||
"Updates __init__.py files using settings from tool.robotpy-build.update_init" | ||
) | ||
|
||
def run(self): | ||
# cannot use when cross-compiling | ||
if ( | ||
"_PYTHON_HOST_PLATFORM" in os.environ | ||
or "PYTHON_CROSSENV" in os.environ | ||
or not self.update_list | ||
): | ||
return | ||
|
||
data, env = self.setup_built_env() | ||
data["update_list"] = self.update_list | ||
|
||
data_json = json.dumps(data) | ||
|
||
# Execute in a subprocess in case it crashes | ||
args = [sys.executable, "-m", __name__] | ||
try: | ||
subprocess.run(args, input=data_json.encode("utf-8"), env=env, check=True) | ||
except subprocess.CalledProcessError: | ||
raise UpdateInitError( | ||
"Failed to generate .pyi file (see above, or set RPYBUILD_SKIP_PYI=1 to ignore) via %s" | ||
% (args,) | ||
) from None | ||
|
||
|
||
def main(): | ||
cfg = json.load(sys.stdin) | ||
|
||
# Configure custom loader | ||
_PackageFinder.mapping = cfg["mapping"] | ||
sys.meta_path.insert(0, _PackageFinder) | ||
|
||
from .. import tool | ||
|
||
# Update init | ||
|
||
for to_update in cfg["update_list"]: | ||
|
||
sys.argv = ["<dummy>", "create-imports", "-w"] + to_update.split(" ", 1) | ||
|
||
retval = tool.main() | ||
if retval != 0: | ||
break | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.