-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.py
More file actions
314 lines (286 loc) · 13.1 KB
/
Copy pathsetup.py
File metadata and controls
314 lines (286 loc) · 13.1 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#*****************************************************************************
#* setup.py
#*
#* pssparser Python extension setup file
#*****************************************************************************
import glob
import os
import sys
import platform
from setuptools import Extension, setup, find_namespace_packages
proj_dir = os.path.dirname(os.path.abspath(__file__))
pythondir = os.path.join(proj_dir, "python")
def _get_version():
"""The version from python/pssparser/__version__.py (see compute_version
there), recorded in python/pssparser/_version.py so that an installed
build reports it without running git."""
version_file = os.path.join(proj_dir, "python", "pssparser", "__version__.py")
glb = {}
with open(version_file) as f:
exec(f.read(), glb)
v = glb["compute_version"](proj_dir)
recorded = os.path.join(proj_dir, "python", "pssparser", "_version.py")
text = "# Written by setup.py; not tracked.\nversion = %r\n" % v
try:
with open(recorded) as f:
current = f.read()
except OSError:
current = None
if current != text:
with open(recorded, "w") as f:
f.write(text)
return v
version = _get_version()
isSrcBuild = False
isSrcTree = os.path.isdir(os.path.join(proj_dir, "src"))
try:
from ivpm_build.setup import setup
except ImportError as e:
# Any from-source build of pssparser (editable git checkout, sdist, or a
# pypi source-fallback when no wheel matches) goes through this setup.py and
# depends on the IVPM build backend: it runs the AST code generation (which
# produces python/PyBaseVisitor.cpp and friends) and copies the generated
# sources into place. Plain setuptools cannot do that, so the build would
# fail later with a confusing "PyBaseVisitor.cpp: No such file or directory".
# Fail now with an actionable message instead of silently falling back.
raise RuntimeError(
"pssparser: building from source requires the 'ivpm-build' package "
"(which depends on 'ivpm'), but it is not installed in the build "
"environment.\n"
"IVPM installs with --no-build-isolation, so [build-system].requires is "
"not auto-provisioned; install ivpm-build into the target venv first, "
"e.g.:\n"
" uv pip install ivpm-build\n"
"Original import error: %s" % e
) from e
isSrcBuild = isSrcTree
print("pssparser: isSrcBuild: %s" % str(isSrcBuild))
include_dirs = []
if isSrcTree:
include_dirs.append(pythondir)
include_dirs.append(os.path.join(proj_dir, "src", "include"))
include_dirs.append(os.path.join(proj_dir, "build", "include"))
# Add ciostream native header path (ciostream_native.h)
# Search pip-installed location first (Linux/macOS: lib/python*/site-packages,
# Windows: Lib/site-packages), then fall back to source layout.
import glob
_ciostream_found = False
for _pattern in [
os.path.join(proj_dir, "packages", "python", "lib", "python*", "site-packages", "ciostream"),
os.path.join(proj_dir, "packages", "python", "Lib", "site-packages", "ciostream"),
os.path.join(os.path.dirname(proj_dir), "ciostream", "src", "ciostream"),
os.path.join(proj_dir, "packages", "ciostream", "src", "ciostream"),
]:
_matches = glob.glob(_pattern)
if _matches and os.path.isdir(_matches[0]):
include_dirs.append(_matches[0])
_ciostream_found = True
break
# Add debug_mgr include path (needed for dmgr/IDebugMgr.h in PyParserUtils.h)
for _pattern in [
os.path.join(proj_dir, "packages", "python", "lib", "python*", "site-packages", "debug_mgr", "share", "include"),
os.path.join(proj_dir, "packages", "python", "Lib", "site-packages", "debug_mgr", "share", "include"),
]:
_matches = glob.glob(_pattern)
if _matches and os.path.isdir(_matches[0]):
include_dirs.append(_matches[0])
break
# Add site-packages to include_dirs so Cython can find .pxd files for
# debug_mgr, ciostream, etc. installed there by ivpm.
for _pattern in [
os.path.join(proj_dir, "packages", "python", "lib", "python*", "site-packages"),
os.path.join(proj_dir, "packages", "python", "Lib", "site-packages"),
]:
_matches = glob.glob(_pattern)
if _matches and os.path.isdir(_matches[0]):
include_dirs.append(_matches[0])
break
library_dirs = []
libraries = []
extra_link_args = []
# Note: the extensions deliberately link neither pssparser nor ast. Both
# libraries are reached at runtime through a ctypes-loaded *_getFactory entry
# point returning a pure-virtual interface (see Factory.inst() in core.pyx and
# in the generated ast.pyx), so the extensions carry no undefined symbols from
# them and need no import libraries on Windows.
ast_ext = Extension(
"pssparser.ast",
[
os.path.join(pythondir, "ast.pyx"),
os.path.join(pythondir, "PyBaseVisitor.cpp"),
],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
language="c++")
ext = Extension(
"pssparser.core",
[ os.path.join(pythondir, "core.pyx"),
os.path.join(pythondir, "PyParserUtils.cpp"),
],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
language="c++")
extensions=[ast_ext, ext]
setup_requires=['cython', 'ivpm-build', 'ivpm']
if isSrcBuild:
pass # pyastbuilder used at cmake time, not setup time
setup_args = dict(
name="pssparser",
packages=find_namespace_packages(where='python'),
package_dir={'' : 'python' },
package_data={
'pssparser': [
"ast.pyi",
"ast.pxd",
"ast_decl.pxd",
"core.pyi",
"core.pxd",
"decl.pxd",
"tokens.pyi",
"cst.pyi",
# The standard-library sources. A tool that documents or
# cross-references the core library needs the .pss text, not just
# the compiled-in copy, and an installed wheel is the only place it
# can look. See pssparser.get_stdlib_dir().
"stdlib/*.pss",
# Agent skills (pssparser.skills). A source build copies them in
# through ivpm_extra_data below; listing them here keeps them in a
# build that already has the copy in place.
"share/skills/**/*",
]
},
version=version,
author="Matthew Ballance",
author_email="matt.ballance@gmail.com",
# SPDX identifier. Every release up to 3.1.6 shipped with NO license
# metadata at all -- neither `License` nor `License-Expression` -- so
# anything resolving the package from PyPI alone saw an unlicensed
# project, LICENSE in the repository notwithstanding. A released
# version's metadata can never be edited, so this fixes it going forward
# only.
#
# This emits the legacy `License: Apache-2.0`, not PEP 639's
# `License-Expression:`. The expression form comes from a `[project]`
# table in pyproject.toml, and setuptools ignores a setup.py `license=`
# for that purpose. Introducing `[project]` here is NOT a drop-in:
# setuptools would then ignore every other field this setup.py passes --
# author, description, install_requires, python_requires, entry_points --
# unless each is restated there or listed as `dynamic`. That migration is
# worth doing and is deliberately not bundled with a metadata fix.
license="Apache-2.0",
description="Provides a PSS parser and related tools",
long_description="""
PSSParser - PSS language parser with ANTLR4 backend
""",
ext_modules=extensions,
entry_points={
"console_scripts": [
"pssparser = pssparser.cli.app:main",
],
# One entry point per skill: IVPM names an entry-point skill after the
# entry-point name, so a single callable returning several
# directories would name them all "pssparser". Each name equals the
# skill's directory and its SKILL.md `name`.
"agent.skills": [
"pssparser = pssparser.skills:pssparser_skill",
"pssparser-checkers = pssparser.skills:checkers_skill",
"pssparser-api = pssparser.skills:api_skill",
],
},
install_requires=[
'debug-mgr',
'ciostream',
# Configuration files are TOML. `tomllib` is stdlib only from 3.11,
# and the floor below is 3.10. `tomli` is the upstream of `tomllib`
# itself, so the parse behaviour is identical on both branches --
# `pssparser.cli.config` picks whichever is available.
'tomli; python_version < "3.11"',
],
# 3.10 is the floor, and declaring it is what makes that a message rather
# than a puzzle. Without python_requires, pip on 3.9 happily selects this
# project and then fails somewhere downstream -- on aarch64 it ended at
# "No matching distribution found for debug-mgr", because debug-mgr has
# never published a cp39 aarch64 wheel and ships no sdist. With it, pip
# says the package requires a different Python and stops.
#
# It also stops pip from offering an OLD pssparser to a 3.9 user as a
# substitute: every release published before this one carries no
# Requires-Python, so 3.9 resolves happily to one of those. That is the
# correct outcome for versions that genuinely built for 3.9; it is the
# reason this bound has to be declared going forward rather than applied
# retroactively.
python_requires='>=3.10',
setup_requires=setup_requires,
)
if isSrcBuild:
import shutil
setup_args["ivpm_extdep_pkgs"] = ["debug-mgr", "ciostream"]
setup_args["ivpm_extdep_data"] = [
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'ast_decl.pxd'),
os.path.join(proj_dir, "python", "pssparser", 'ast_decl.pxd')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'ast.pyi'),
os.path.join(proj_dir, "python", "pssparser", 'ast.pyi')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'ast.pxd'),
os.path.join(proj_dir, "python", "pssparser", 'ast.pxd')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'ast.pyx'),
os.path.join(proj_dir, "python", 'ast.pyx')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'PyBaseVisitor.cpp'),
os.path.join(proj_dir, "python", 'PyBaseVisitor.cpp')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'PyBaseVisitor.h'),
os.path.join(proj_dir, "python", 'PyBaseVisitor.h')),
]
if platform.system() == "Linux":
antlr4_rt_lib = None
for libdir in ("lib", "lib64"):
if os.path.exists("build/%s/libantlr4-runtime.so" % libdir):
for f in os.listdir("build/%s" % libdir):
if f.startswith("libantlr4-runtime.so."):
antlr4_rt_lib = "build/{libdir}/%s" % f
break
if antlr4_rt_lib is not None:
break
print("antlr4_rt_lib: %s" % antlr4_rt_lib)
elif platform.system() == "Windows":
# Windows links antlr4 statically into pssparser.dll (see the
# ANTLR_BUILD_SHARED=OFF branch in CMakeLists.txt), so there is no
# runtime library to ship. Leaving the entry in would abort the wheel
# build: install_lib raises when an ivpm_extra_data source is missing.
antlr4_rt_lib = None
else:
antlr4_rt_lib = "build/{libdir}/{libpref}antlr4-runtime{dllext}"
# Ship the standard-library sources inside the package. They live in
# src/stdlib, outside package_dir, so package_data alone cannot reach them
# in a source build; each file is copied in explicitly. Globbed rather
# than listed so a new stdlib package is picked up without editing here.
_stdlib_data = [
(os.path.join("src", "stdlib", os.path.basename(f)), "stdlib")
for f in sorted(glob.glob(os.path.join(proj_dir, "src", "stdlib", "*.pss")))
]
# Ship the agent skills under pssparser/share/skills/<name>. Only the
# SHIPPED ones: skills/pssparser-dev is for work on pssparser itself and
# must stay out of the wheel. A directory source is copied to
# <dst>/<basename>, hence "share/skills" and not ".../<name>".
_skills_glb = {}
with open(os.path.join(proj_dir, "python", "pssparser", "skills.py")) as f:
exec(f.read(), _skills_glb)
_skills_data = [
(os.path.join("skills", name), os.path.join("share", "skills"))
for name in _skills_glb["SHIPPED"]
]
extra_data = _stdlib_data + [
("build/include", "share"),
("build/{libdir}/{libpref}ast{dllext}", ""),
("build/{libdir}/{libpref}pssparser{dllext}", ""),
("python/PyBaseVisitor.h", "share/include"),
("python/PyParserUtils.h", "share/include"),
] + _skills_data
if antlr4_rt_lib is not None:
extra_data.insert(len(_stdlib_data) + 1, (antlr4_rt_lib, ""))
setup_args["ivpm_extra_data"] = {
"pssparser": extra_data
}
setup(**setup_args)