From 0565d8ed78f436d622c83852bede0643e4ba7d7c Mon Sep 17 00:00:00 2001 From: Marko Ristin Date: Wed, 2 Jun 2021 16:02:53 +0200 Subject: [PATCH] Added entry point to `setup.py` This patch introduces the `entry_point` to `setup.py` so that Mapry can also be executed on Windows as a console script. --- bin/mapry-to | 10 ---------- mapry/__main__.py | 8 ++++++++ mapry/main.py | 20 ++++++++++++++++++-- precommit.py | 5 +++++ setup.py | 2 +- 5 files changed, 32 insertions(+), 13 deletions(-) delete mode 100755 bin/mapry-to create mode 100644 mapry/__main__.py diff --git a/bin/mapry-to b/bin/mapry-to deleted file mode 100755 index f06c852f..00000000 --- a/bin/mapry-to +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env python3 - -"""Generate the code based on a mapry schema.""" - -import sys - -import mapry.main - -if __name__ == "__main__": - sys.exit(mapry.main.main()) diff --git a/mapry/__main__.py b/mapry/__main__.py new file mode 100644 index 00000000..f65a7ec7 --- /dev/null +++ b/mapry/__main__.py @@ -0,0 +1,8 @@ +"""Link to ``mapry.main`` so that you can execute it with ``python`` CLI.""" + +import sys + +import mapry.main + +if __name__ == "__main__": + sys.exit(mapry.main.run(prog="mapry")) diff --git a/mapry/main.py b/mapry/main.py index 343598ef..8410d75b 100644 --- a/mapry/main.py +++ b/mapry/main.py @@ -190,9 +190,20 @@ def generate_py(schema: mapry.Schema, outdir: pathlib.Path) -> int: return 0 -def main() -> int: - """Execute the main routine.""" +def run(prog: str) -> int: + """ + Execute the main routine. + + :param prog: + name of the program as it will be executed. + + This differs between "mapry-to" (as a console script) and + "mapry" (as a Python module executed from ``__main__.py``) + + :return: exit code + """ parser = argparse.ArgumentParser( + prog=prog, description="Generate the code for de/serialization of object graphs " "from JSONables.") subparsers = parser.add_subparsers( @@ -261,3 +272,8 @@ def main() -> int: return generate_py(schema=schema, outdir=outdir) else: raise NotImplementedError('command: {}'.format(command)) + + +def entry_point() -> int: + """Provide the entry point as the console script.""" + return run(prog="mapry-to") diff --git a/precommit.py b/precommit.py index 4930999e..02ae6deb 100755 --- a/precommit.py +++ b/precommit.py @@ -113,6 +113,11 @@ def main() -> int: str(repo_root / "README.rst")]) for pth in sorted((repo_root / "mapry").glob("**/*.py")): + # ``__main__.py``'s cause doctest to go banana, so we need to skip them; + # see https://stackoverflow.com/questions/58731519/doctest-fails-on-main-py + if pth.name == '__main__.py': + continue + subprocess.check_call([sys.executable, "-m", "doctest", str(pth)]) print("pyicontract-lint'ing...") diff --git a/setup.py b/setup.py index f12c253f..47d61aa8 100644 --- a/setup.py +++ b/setup.py @@ -73,5 +73,5 @@ # yapf: enable }, py_modules=['mapry', 'mapry_meta'], - scripts=['bin/mapry-to'], + entry_points={"console_scripts": ["mapry-to = mapry.main:entry_point"]}, package_data={"mapry": ["py.typed"]})