Skip to content

Commit 3cc1032

Browse files
committed
Fix semiwrap tools
1 parent b48f4a9 commit 3cc1032

File tree

7 files changed

+204
-280
lines changed

7 files changed

+204
-280
lines changed

src/semiwrap/tool/__main__.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
from .build_dep import BuildDep
55
from .create_gen import GenCreator
6-
from .create_imports import ImportCreator
7-
from .parse_maven import MavenParser
6+
from .create_imports import ImportCreator, UpdateInit
87
from .platform_info import PlatformInfo
98
from .show_override import ShowOverrides
109
from .scan_headers import HeaderScanner
1110

1211

1312
def main():
14-
parser = argparse.ArgumentParser(prog="robotpy-build")
13+
parser = argparse.ArgumentParser(prog="semiwrap")
1514
parent_parser = argparse.ArgumentParser(add_help=False)
1615
subparsers = parser.add_subparsers(dest="cmd")
1716
subparsers.required = True
@@ -23,7 +22,7 @@ def main():
2322
ImportCreator,
2423
PlatformInfo,
2524
ShowOverrides,
26-
MavenParser,
25+
UpdateInit,
2726
):
2827
cls.add_subparser(parent_parser, subparsers).set_defaults(cls=cls)
2928

src/semiwrap/tool/build_dep.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import subprocess
22
import sys
3-
4-
from .util import get_setup
3+
import tomli
54

65

76
class BuildDep:
@@ -21,10 +20,10 @@ def add_subparser(cls, parent_parser, subparsers):
2120
return parser
2221

2322
def run(self, args):
24-
s = get_setup()
25-
requirements = s.pyproject.get("build-system", {}).get("requires", [])
26-
requirements.extend(s.setup_kwargs.get("install_requires", ""))
27-
requirements.append("wheel")
23+
with open("pyproject.toml", "rb") as fp:
24+
pyproject = tomli.load(fp)
25+
requirements = pyproject.get("build-system", {}).get("requires", [])
26+
requirements.extend(pyproject.get("project", {}).get("dependencies", []))
2827

2928
pipargs = [
3029
sys.executable,

src/semiwrap/tool/create_gen.py

+57-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from os.path import exists
1+
import pathlib
22

3-
from .util import get_setup
43
from ..autowrap.generator_data import MissingReporter
4+
from ..cmd.header2dat import make_argparser, generate_wrapper
5+
from ..makeplan import InputFile, makeplan, BuildTarget
56

67

78
class GenCreator:
@@ -15,35 +16,63 @@ def add_subparser(cls, parent_parser, subparsers):
1516
parser.add_argument(
1617
"--write", help="Write to files if they don't exist", action="store_true"
1718
)
18-
parser.add_argument("--strip-prefixes", action="append")
1919

2020
return parser
2121

2222
def run(self, args):
23-
pfx = ""
24-
if args.strip_prefixes:
25-
pfx = "strip_prefixes:\n- " + "\n- ".join(args.strip_prefixes) + "\n\n"
23+
project_root = pathlib.Path.cwd()
24+
25+
plan = makeplan(project_root, missing_yaml_ok=True)
26+
27+
for item in plan:
28+
if not isinstance(item, BuildTarget) or item.command != "header2dat":
29+
continue
30+
31+
# convert args to string so we can parse it
32+
# .. this is weird, but less annoying than other alternatives
33+
# that I can think of?
34+
argv = []
35+
for arg in item.args:
36+
if isinstance(arg, str):
37+
argv.append(arg)
38+
elif isinstance(arg, InputFile):
39+
argv.append(str(arg.path.absolute()))
40+
elif isinstance(arg, pathlib.Path):
41+
argv.append(str(arg.absolute()))
42+
else:
43+
# anything else shouldn't matter
44+
argv.append("ignored")
45+
46+
sparser = make_argparser()
47+
sargs = sparser.parse_args(argv)
2648

27-
s = get_setup()
28-
for wrapper in s.wrappers:
2949
reporter = MissingReporter()
30-
wrapper.on_build_gen("", reporter)
31-
32-
nada = True
33-
for name, report in reporter.as_yaml():
34-
report = f"---\n\n{pfx}{report}"
35-
36-
nada = False
37-
if args.write:
38-
if not exists(name):
39-
print("Writing", name)
40-
with open(name, "w") as fp:
41-
fp.write(report)
42-
else:
43-
print(name, "already exists!")
44-
45-
print("===", name, "===")
46-
print(report)
47-
48-
if nada:
49-
print("Nothing to do!")
50+
51+
generate_wrapper(
52+
name=sargs.name,
53+
src_yml=sargs.src_yml,
54+
src_h=sargs.src_h,
55+
src_h_root=sargs.src_h_root,
56+
dst_dat=None,
57+
dst_depfile=None,
58+
include_paths=sargs.include_paths,
59+
casters={},
60+
pp_defines=sargs.pp_defines,
61+
missing_reporter=reporter,
62+
report_only=True,
63+
)
64+
65+
if reporter:
66+
for name, report in reporter.as_yaml():
67+
report = f"---\n\n{report}"
68+
69+
if args.write:
70+
if not name.exists():
71+
print("Writing", name)
72+
with open(name, "w") as fp:
73+
fp.write(report)
74+
else:
75+
print(name, "already exists!")
76+
77+
print("===", name, "===")
78+
print(report)

src/semiwrap/tool/create_imports.py

+46-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import inspect
22
import posixpath
33
import subprocess
4+
import sys
45
import typing
56
import types
67

8+
from ..pyproject import PyProject
9+
710

811
class ImportCreator:
912
@classmethod
1013
def add_subparser(cls, parent_parser, subparsers):
1114
parser = subparsers.add_parser(
1215
"create-imports",
13-
help="Generate suitable imports for a module",
16+
help="Generate suitable imports for a module. Prefer using update-init",
1417
parents=[parent_parser],
1518
)
1619
parser.add_argument("base", help="Ex: wpiutil")
@@ -35,11 +38,17 @@ def create(self, base: str, compiled: typing.Optional[str], write: bool):
3538
try:
3639
import black
3740
except:
38-
print("Error, The following module is required to run this tool: black")
39-
exit(1)
41+
print(
42+
"Error, The following module is required to run this tool: black",
43+
file=sys.stderr,
44+
)
45+
return 1
4046

4147
if not compiled:
4248
compiled = f"{base}._{base.split('.')[-1]}"
49+
print(f"CHK base={base} compiled={compiled} (auto)")
50+
else:
51+
print(f"CHK base={base} compiled={compiled}")
4352

4453
# TODO: could probably generate this from parsed code, but seems hard
4554
ctx = {}
@@ -51,7 +60,8 @@ def create(self, base: str, compiled: typing.Optional[str], write: bool):
5160
relimport = self._rel(base, compiled)
5261

5362
stmt_compiled = "" if not compiled else f" {compiled}"
54-
begin_stmt = f"# autogenerated by 'robotpy-build create-imports {base}"
63+
begin_stmt = f"# autogenerated by 'semiwrap create-imports {base}"
64+
old_begin_stmt = f"# autogenerated by 'robotpy-build create-imports {base}"
5565

5666
stmt = inspect.cleandoc(
5767
f"""
@@ -77,6 +87,9 @@ def create(self, base: str, compiled: typing.Optional[str], write: bool):
7787

7888
# Find the beginning statement
7989
idx = startidx = fcontent.find(begin_stmt)
90+
if startidx == -1:
91+
idx = startidx = fcontent.find(old_begin_stmt)
92+
8093
if startidx != -1:
8194
for to_find in ("from", "__all__", "[", "]", "\n"):
8295
idx = fcontent.find(to_find, idx)
@@ -99,3 +112,32 @@ def create(self, base: str, compiled: typing.Optional[str], write: bool):
99112

100113
else:
101114
print(content)
115+
116+
117+
class UpdateInit:
118+
@classmethod
119+
def add_subparser(cls, parent_parser, subparsers):
120+
parser = subparsers.add_parser(
121+
"update-init",
122+
help="Updates __init__.py files using settings from tool.semiwrap.update_init",
123+
parents=[parent_parser],
124+
)
125+
return parser
126+
127+
def run(self, args):
128+
project = PyProject().project
129+
130+
if not project.update_init:
131+
print("[tool.semiwrap].update_init not set", file=sys.stderr)
132+
return 1
133+
134+
ic = ImportCreator()
135+
136+
for to_update in project.update_init:
137+
if " " in to_update:
138+
base, compiled = to_update.split(" ", 1)
139+
else:
140+
base = to_update
141+
compiled = None
142+
143+
ic.create(base, compiled, True)

0 commit comments

Comments
 (0)