Skip to content

Commit e32b8af

Browse files
committed
Add update_init command to make updating __init__.py easier
- Implemented for now as a setup.py subcommand because it's a lot simpler... maybe if we move away from setuptools we can make it a robotpy-build command instead
1 parent 621a658 commit e32b8af

File tree

7 files changed

+105
-10
lines changed

7 files changed

+105
-10
lines changed

docs/tools.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,15 @@ python package.
4040
.. code-block:: sh
4141
4242
$ python -m robotpy_build create-imports rpydemo rpydemo._rpydemo
43+
44+
Use the ``--write`` argument to write the file.
45+
46+
To write a list of ``__init__.py`` files, you can specify them in the ``pyproject.toml``
47+
file like so:
48+
49+
.. code-block:: toml
50+
51+
[tool.robotpy-build]
52+
update_init = ["rpydemo rpydemo._rpydemo"]
53+
54+
To actually update the files, run ``python setup.py update_init``.

robotpy_build/command/update_init.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import json
2+
import os
3+
import subprocess
4+
import sys
5+
import typing
6+
7+
try:
8+
from setuptools.errors import BaseError
9+
except ImportError:
10+
from distutils.errors import DistutilsError as BaseError
11+
12+
from ._built_env import _BuiltEnv, _PackageFinder
13+
14+
15+
class UpdateInitError(BaseError):
16+
pass
17+
18+
19+
class UpdateInit(_BuiltEnv):
20+
update_list: typing.List[str]
21+
22+
command_name = "update_init"
23+
description = (
24+
"Updates __init__.py files using settings from tool.robotpy-build.update_init"
25+
)
26+
27+
def run(self):
28+
# cannot use when cross-compiling
29+
if (
30+
"_PYTHON_HOST_PLATFORM" in os.environ
31+
or "PYTHON_CROSSENV" in os.environ
32+
or not self.update_list
33+
):
34+
return
35+
36+
data, env = self.setup_built_env()
37+
data["update_list"] = self.update_list
38+
39+
data_json = json.dumps(data)
40+
41+
# Execute in a subprocess in case it crashes
42+
args = [sys.executable, "-m", __name__]
43+
try:
44+
subprocess.run(args, input=data_json.encode("utf-8"), env=env, check=True)
45+
except subprocess.CalledProcessError:
46+
raise UpdateInitError(
47+
"Failed to generate .pyi file (see above, or set RPYBUILD_SKIP_PYI=1 to ignore) via %s"
48+
% (args,)
49+
) from None
50+
51+
52+
def main():
53+
cfg = json.load(sys.stdin)
54+
55+
# Configure custom loader
56+
_PackageFinder.mapping = cfg["mapping"]
57+
sys.meta_path.insert(0, _PackageFinder)
58+
59+
from .. import tool
60+
61+
# Update init
62+
63+
for to_update in cfg["update_list"]:
64+
65+
sys.argv = ["<dummy>", "create-imports", "-w"] + to_update.split(" ", 1)
66+
67+
retval = tool.main()
68+
if retval != 0:
69+
break
70+
71+
72+
if __name__ == "__main__":
73+
main()

robotpy_build/config/pyproject_toml.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ class RobotpyBuildConfig(Model):
416416
#: Python package to store version information and robotpy-build metadata in
417417
base_package: str
418418

419+
#: List of python packages with __init__.py to update when ``python setup.py update_init``
420+
#: is called -- this is an argument to the ``robotpy-build create-imports`` command, and
421+
#: may contain a space and the second argument to create-imports.
422+
update_init: List[str] = []
423+
419424
#:
420425
#: .. seealso:: :class:`.SupportedPlatform`
421426
#:

robotpy_build/setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def finalize_options(self):
2323
from .command.build_ext import BuildExt
2424
from .command.build_pyi import BuildPyi
2525
from .command.develop import Develop
26+
from .command.update_init import UpdateInit
2627

2728
try:
2829
from .command.editable_wheel import EditableWheel
@@ -155,6 +156,7 @@ def _xform(v: str):
155156
"build_ext": BuildExt,
156157
"build_pyi": BuildPyi,
157158
"develop": Develop,
159+
"update_init": UpdateInit,
158160
}
159161
if EditableWheel:
160162
self.setup_kwargs["cmdclass"]["editable_wheel"] = EditableWheel
@@ -165,6 +167,7 @@ def _xform(v: str):
165167
cls.static_libs = self.static_libs
166168
cls.rpybuild_pkgcfg = self.pkgcfg
167169
BuildPyi.base_package = self.base_package
170+
UpdateInit.update_list = self.project.update_init
168171

169172
# We already know some of our packages, so collect those in addition
170173
# to using find_packages()

robotpy_build/static_libs.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def set_root(self, root: os.PathLike) -> None:
3333
self.incdir = join(self.root, self.name, "include")
3434

3535
def get_include_dirs(self) -> Optional[List[str]]:
36+
if self.incdir is None:
37+
return
38+
3639
includes = [self.incdir]
3740
if self.cfg.download:
3841
for dl in self.cfg.download:
@@ -41,7 +44,8 @@ def get_include_dirs(self) -> Optional[List[str]]:
4144
return includes
4245

4346
def get_library_dirs(self) -> Optional[List[str]]:
44-
return [self.libdir]
47+
if self.libdir:
48+
return [self.libdir]
4549

4650
def get_library_dirs_rel(self) -> Optional[List[str]]:
4751
pass
@@ -60,7 +64,8 @@ def get_extra_objects(self) -> Optional[List[str]]:
6064
if self.platform.os == "windows":
6165
return
6266

63-
return [join(self.libdir, lib) for lib in self._get_libnames()]
67+
if self.libdir:
68+
return [join(self.libdir, lib) for lib in self._get_libnames()]
6469

6570
def get_type_casters_cfg(self, casters: Dict[str, Dict[str, Any]]) -> None:
6671
pass

robotpy_build/tool.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
def get_setup() -> Setup:
3030
s = Setup()
3131
s.prepare()
32-
33-
temp_path = join(get_build_temp_path(), "dlstatic")
34-
for static_lib in s.static_libs:
35-
static_lib.set_root(temp_path)
36-
3732
return s
3833

3934

@@ -544,8 +539,8 @@ def main():
544539
else:
545540
retval = 0
546541

547-
exit(retval)
542+
return retval
548543

549544

550545
if __name__ == "__main__":
551-
main()
546+
exit(main())

robotpy_build/wrapper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ def all_deps(self):
288288
def _all_includes(self, include_rpyb):
289289
includes = self.get_include_dirs()
290290
for dep in self.all_deps():
291-
includes.extend(dep.get_include_dirs())
291+
dep_inc = dep.get_include_dirs()
292+
if dep_inc:
293+
includes.extend(dep_inc)
292294
if include_rpyb:
293295
includes.extend(self.pkgcfg.get_pkg("robotpy-build").get_include_dirs())
294296
return includes

0 commit comments

Comments
 (0)