-
Notifications
You must be signed in to change notification settings - Fork 19
/
setup.py
163 lines (125 loc) · 6.03 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
158
159
160
161
162
163
# SPDX-FileCopyrightText: 2024 geisserml <[email protected]>
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
# NOTE Unfortunately, pip may run setup.py multiple times with different commands (dist_info, bdist_wheel).
# However, guarding code depending on command is tricky. You'd need to be careful not to cause inconsistent data. Also, it's hard to tell which command runs first because other tools (e.g. build) may run just bdist_wheel.
import os
import sys
from pathlib import Path
import setuptools
try:
from setuptools.command.bdist_wheel import bdist_wheel
except ImportError:
from wheel.bdist_wheel import bdist_wheel
from setuptools.command.build_py import build_py as build_py_orig
sys.path.insert(0, str(Path(__file__).parent / "setupsrc"))
from pypdfium2_setup.emplace import prepare_setup
from pypdfium2_setup.packaging_base import *
# Use a custom distclass declaring we have a binary extension, to prevent modules from being nested in a purelib/ subdirectory in wheels. This also sets `Root-Is-Purelib: false` in the WHEEL file.
class BinaryDistribution (setuptools.Distribution):
def has_ext_modules(self):
return True
def bdist_factory(pl_name):
class pypdfium_bdist (bdist_wheel):
def finalize_options(self, *args, **kws):
bdist_wheel.finalize_options(self, *args, **kws)
def get_tag(self, *args, **kws):
return "py3", "none", get_wheel_tag(pl_name)
return pypdfium_bdist
class pypdfium_build_py (build_py_orig):
def run(self, *args, **kwargs):
if hasattr(self, "editable_mode"):
helpers_info = read_json(ModuleDir_Helpers/VersionFN)
helpers_info["is_editable"] = bool(self.editable_mode)
write_json(ModuleDir_Helpers/VersionFN, helpers_info)
else:
print("!!! Warning: cmdclass does not provide `editable_mode` attribute. Please file a bug report.")
build_py_orig.run(self, *args, **kwargs)
# semi-static metadata
PROJECT_DESC = "Python bindings to PDFium"
LICENSES_SHARED = (
"LICENSES/Apache-2.0.txt",
"LICENSES/BSD-3-Clause.txt",
"LICENSES/CC-BY-4.0.txt",
)
LICENSES_WHEEL = (
"LICENSES/LicenseRef-PdfiumThirdParty.txt",
".reuse/dep5-wheel",
)
LICENSES_SDIST = (
"LICENSES/LicenseRef-FairUse.txt",
".reuse/dep5",
)
PLATFILES_GLOB = [BindingsFN, VersionFN, *LibnameForSystem.values()]
def assert_exists(dir, data_files):
missing = [f for f in data_files if not (dir/f).exists()]
if missing:
assert False, f"Missing data files: {missing}"
def run_setup(modnames, pl_name, pdfium_ver):
kwargs = dict(
name = "pypdfium2",
description = "Python bindings to PDFium",
license = "BSD-3-Clause, Apache-2.0, PdfiumThirdParty",
license_files = LICENSES_SHARED,
python_requires = ">= 3.6",
cmdclass = {},
package_dir = {},
package_data = {},
install_requires = [],
)
if modnames == [ModuleHelpers] and pl_name != ExtPlats.sdist:
# do not do this for sdist (none)
kwargs["name"] += "_helpers"
kwargs["description"] += " (helpers module)"
kwargs["install_requires"] += ["pypdfium2_raw"]
elif modnames == [ModuleRaw]:
kwargs["name"] += "_raw"
kwargs["description"] += " (raw module)"
kwargs["version"] = str(pdfium_ver)
if ModuleHelpers in modnames:
helpers_info = get_helpers_info()
if pl_name == ExtPlats.sdist:
if helpers_info["dirty"]:
# ignore dirty state due to craft_packages::tmp_ctypesgen_pin()
if int(os.environ.get("SDIST_IGNORE_DIRTY", 0)):
helpers_info["dirty"] = False
else:
print("!!! Warning: sdist built without ctypesgen pin?", file=sys.stderr)
kwargs["version"] = merge_tag(helpers_info, mode="py")
# is_editable = None: unknown/fallback in case the cmdclass is not reached
helpers_info["is_editable"] = None
write_json(ModuleDir_Helpers/VersionFN, helpers_info)
kwargs["cmdclass"]["build_py"] = pypdfium_build_py
kwargs["package_dir"]["pypdfium2"] = "src/pypdfium2"
kwargs["package_data"]["pypdfium2"] = [VersionFN]
kwargs["entry_points"] = dict(console_scripts=["pypdfium2 = pypdfium2.__main__:cli_main"])
if ModuleRaw in modnames:
kwargs["package_dir"]["pypdfium2_raw"] = "src/pypdfium2_raw"
if ModuleRaw not in modnames or pl_name == ExtPlats.sdist:
kwargs["exclude_package_data"] = {"pypdfium2_raw": PLATFILES_GLOB}
if pl_name == ExtPlats.sdist:
kwargs["license_files"] += LICENSES_SDIST
elif pl_name == ExtPlats.system:
kwargs["package_data"]["pypdfium2_raw"] = [VersionFN, BindingsFN]
else:
sys_name = plat_to_system(pl_name)
libname = LibnameForSystem[sys_name]
kwargs["package_data"]["pypdfium2_raw"] = [VersionFN, BindingsFN, libname]
kwargs["cmdclass"]["bdist_wheel"] = bdist_factory(pl_name)
kwargs["distclass"] = BinaryDistribution
kwargs["license_files"] += LICENSES_WHEEL
if "pypdfium2" in kwargs["package_data"]:
assert_exists(ModuleDir_Helpers, kwargs["package_data"]["pypdfium2"])
if "pypdfium2_raw" in kwargs["package_data"]:
assert_exists(ModuleDir_Raw, kwargs["package_data"]["pypdfium2_raw"])
setuptools.setup(**kwargs)
def main():
pl_spec = os.environ.get(PlatSpec_EnvVar, "")
modspec = os.environ.get(ModulesSpec_EnvVar, "")
# NOTE in principle, it may be possible to achieve the same as `prepared!` by just filling the data/ cache manually, but this is more explicit, formally disabling the generating code paths
with_prepare, pl_name, pdfium_ver, use_v8 = parse_pl_spec(pl_spec)
modnames = parse_modspec(modspec)
if ModuleRaw in modnames and with_prepare and pl_name != ExtPlats.sdist:
prepare_setup(pl_name, pdfium_ver, use_v8)
run_setup(modnames, pl_name, pdfium_ver)
if __name__ == "__main__":
main()