forked from mozilla/glean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
156 lines (125 loc) · 5.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""The setup script."""
import os
import shutil
import sys
from setuptools import setup, Distribution, find_packages
from setuptools.command.install import install
import wheel.bdist_wheel
platform = sys.platform
if os.environ.get("GLEAN_PYTHON_MINGW_I686_BUILD"):
mingw_arch = "i686"
elif os.environ.get("GLEAN_PYTHON_MINGW_X86_64_BUILD"):
mingw_arch = "x86_64"
else:
mingw_arch = None
if mingw_arch is not None:
platform = "windows"
if sys.version_info < (3, 6):
print("glean requires at least Python 3.6", file=sys.stderr)
sys.exit(1)
from pathlib import Path # noqa
import toml # noqa
ROOT = Path(__file__).parent.absolute()
os.chdir(str(ROOT))
with (ROOT.parent.parent / "README.md").open() as readme_file:
readme = readme_file.read()
with (ROOT.parent.parent / "CHANGELOG.md").open() as history_file:
history = history_file.read()
with (ROOT.parent / "Cargo.toml").open() as cargo:
parsed_toml = toml.load(cargo)
version = parsed_toml["package"]["version"]
requirements = [
"cffi>=1",
"glean_parser==1.23.0",
"iso8601>=0.1.10; python_version<='3.6'",
]
setup_requirements = ["cffi>=1.0.0"]
# The environment variable `GLEAN_BUILD_VARIANT` can be set to `debug` or `release`
buildvariant = os.environ.get("GLEAN_BUILD_VARIANT", "debug")
if mingw_arch == "i686":
shared_object_build_dir = "../../target/i686-pc-windows-gnu"
elif mingw_arch == "x86_64":
shared_object_build_dir = "../../target/x86_64-pc-windows-gnu"
else:
shared_object_build_dir = "../../target"
if platform == "linux":
shared_object = "libglean_ffi.so"
elif platform == "darwin":
shared_object = "libglean_ffi.dylib"
elif platform.startswith("win"):
# `platform` can be both "windows" (if running within MinGW) or "win32"
# if running in a standard Python environment. Account for both.
shared_object = "glean_ffi.dll"
else:
raise ValueError(f"The platform {sys.platform} is not supported.")
shared_object_path = f"{shared_object_build_dir}/{buildvariant}/{shared_object}"
shutil.copyfile("../metrics.yaml", "glean/metrics.yaml")
shutil.copyfile("../pings.yaml", "glean/pings.yaml")
# When running inside of `requirements-builder`, the Rust shared object may not
# yet exist, so ignore the exception when trying to copy it. Under normal
# circumstances, this will still show up as an error when running the `build`
# command as a missing `package_data` file.
try:
shutil.copyfile(shared_object_path, "glean/" + shared_object)
except FileNotFoundError:
pass
class BinaryDistribution(Distribution):
def is_pure(self):
return False
def has_ext_modules(self):
return True
# The logic for specifying wheel tags in setuptools/wheel is very complex, hard
# to override, and is really meant for extensions that are compiled against
# libpython.so, not this case where we have a fairly portable Rust-compiled
# binary that should work across a number of Python versions. Therefore, we
# just skip all of its logic be overriding the `get_tag` method with something
# simple that only handles the cases we need.
class bdist_wheel(wheel.bdist_wheel.bdist_wheel):
def get_tag(self):
if platform == "linux":
return ("cp36", "abi3", "linux_x86_64")
elif platform == "darwin":
return ("cp36", "abi3", "macosx_10_7_x86_64")
elif platform == "windows":
if mingw_arch == "i686":
return ("py3", "none", "win32")
elif mingw_arch == "x86_64":
return ("py3", "none", "win_amd64")
else:
raise ValueError("Unsupported Windows platform")
class InstallPlatlib(install):
def finalize_options(self):
install.finalize_options(self)
if self.distribution.has_ext_modules():
self.install_lib = self.install_platlib
setup(
author="The Glean Team",
author_email="[email protected]",
classifiers=[
"Intended Audience :: Developers",
"Natural Language :: English",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
description="Mozilla's Glean Telemetry SDK: The Machine that Goes 'Ping!'",
install_requires=requirements,
long_description=readme + "\n\n" + history,
long_description_content_type="text/markdown",
include_package_data=True,
keywords="glean",
name="glean-sdk",
version=version,
packages=find_packages(include=["glean", "glean.*"]),
setup_requires=setup_requirements,
cffi_modules=["ffi_build.py:ffibuilder"],
url="https://github.com/mozilla/glean",
zip_safe=False,
package_data={"glean": [shared_object, "metrics.yaml", "pings.yaml"]},
distclass=BinaryDistribution,
cmdclass={"install": InstallPlatlib, "bdist_wheel": bdist_wheel},
)