forked from christoph2/pyA2L
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
157 lines (131 loc) · 5.24 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/env/python
# pylint: disable=C0111
import os
import subprocess
import sys
from itertools import chain
from pathlib import Path
import setuptools.command.build_py
import setuptools.command.develop
from pkg_resources import parse_requirements
from setuptools import Command
from setuptools import find_packages
from setuptools import setup
def _parse_requirements(filepath):
with filepath.open() as text_io:
requirements = list(parse_requirements(text_io))
return requirements
ROOT_DIRPATH = Path(".")
BASE_REQUIREMENTS = _parse_requirements(ROOT_DIRPATH / "requirements.txt")
TEST_REQUIREMENTS = _parse_requirements(ROOT_DIRPATH / "requirements.test.txt")
ANTLR_VERSION = next(req.specs[0][1] for req in BASE_REQUIREMENTS if req.project_name == "antlr4-python3-runtime")
class AntlrAutogen(Command):
"""Custom command to autogenerate Python code using ANTLR."""
description = "generate python code using antlr"
user_options = [
("target-dir=", None, "(optional) output directory for antlr artifacts"),
]
def initialize_options(self):
"""Set default values for options."""
# Must be snake_case; variable created from user_options.
# pylint: disable=C0103
self.target_dir = None
def finalize_options(self):
"""Post-process options."""
a2lGrammar = str(ROOT_DIRPATH / "pya2l" / "a2l.g4")
a2llgGrammar = str(ROOT_DIRPATH / "pya2l" / "a2llg.g4")
amlGrammar = str(ROOT_DIRPATH / "pya2l" / "aml.g4")
# distutils.cmd.Command should not have __init__().
# pylint: disable=W0201
self.arguments = [a2lGrammar, a2llgGrammar, amlGrammar, "-Dlanguage=Python3"]
if self.target_dir is not None:
self.arguments.extend(["-o", self.target_dir])
def run(self):
"""Run ANTLR."""
pwd = Path(os.environ.get("PWD", "."))
antlrJar = pwd / Path("antlr-{}-complete.jar".format(ANTLR_VERSION))
if not antlrJar.exists():
print(f"{antlrJar} not found in '{pwd}'")
sys.exit(2)
antlrJar = str(antlrJar)
antlrCmd = ["java", "-Xmx500M", "-cp", antlrJar, "org.antlr.v4.Tool"]
self.announce(" ".join(antlrCmd + self.arguments))
subprocess.check_call(antlrCmd + self.arguments)
clean()
def clean():
"""Remove unneeded files."""
tokens = ROOT_DIRPATH.joinpath("pya2l").glob("*tokens")
interp = ROOT_DIRPATH.joinpath("pya2l").glob("*interp")
listener = (
list(ROOT_DIRPATH.joinpath("pya2l").glob(i + "Listener.py"))[0]
for i in ("a2l", "aml") # No listener for lexer grammars (a2llg.g4).
)
for filepath in chain(tokens, interp, listener):
os.remove(str(filepath))
# pylint: disable=R0901
class CustomBuildPy(setuptools.command.build_py.build_py):
"""Extended build_py which also runs ANTLR."""
def run(self):
self.run_command("antlr")
super().run()
class CustomDevelop(setuptools.command.develop.develop):
"""Extended develop which also runs ANTLR"""
def run(self):
self.run_command("antlr")
super().run()
with ROOT_DIRPATH.joinpath("pya2l", "version.py").open() as f:
for line in f:
if line.startswith("__version__"):
VERSION = line.split("=")[-1].strip().strip('"')
break
with ROOT_DIRPATH.joinpath("README.md").open() as fh:
LONG_DESCRIPTION = fh.read()
setup(
name="pya2ldb",
version=VERSION,
description="A2L for Python",
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
author="Christoph Schueler",
author_email="cpu12.gems@googlemail.com",
url="https://www.github.com/Christoph2/pyA2L",
cmdclass={
"antlr": AntlrAutogen,
"build_py": CustomBuildPy,
"develop": CustomDevelop,
},
packages=find_packages(where=str(ROOT_DIRPATH)),
package_dir={"pya2l": str(ROOT_DIRPATH / "pya2l")},
install_requires=list(map(str, BASE_REQUIREMENTS)),
tests_require=list(map(str, TEST_REQUIREMENTS)),
package_data={"pya2l.cgen.templates": ["*.tmpl"]},
test_suite="pya2l.tests",
license="GPLv2",
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
# How mature is this project? Common values are
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
"Development Status :: 4 - Beta",
# Indicate who your project is intended for
"Intended Audience :: Developers",
"Topic :: Software Development",
"Topic :: Scientific/Engineering",
# Pick your license as you wish (should match "license" above)
"License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
# entry_points={
# "console_scripts": [
# "a2ldb-imex = pya2l.scripts.a2ldb_imex:main",
# ],
# },
)