-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
setup.py
181 lines (141 loc) · 5.47 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import os
import sys
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from distutils.cmd import Command
from typing import Any, Type
if sys.version_info[0] <= 2:
raise OSError("PyPCAPKit does not support Python 2!")
try:
from setuptools import setup
except ImportError:
raise ImportError("setuptools is required to install PyPCAPKit!")
# get logger
logger = logging.getLogger("pcapkit.setup")
formatter = logging.Formatter(
fmt="[%(levelname)s] %(asctime)s - %(message)s", datefmt="%m/%d/%Y %I:%M:%S %p"
)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
logger.addHandler(handler)
def get_long_description() -> "str":
"""Extract description from README.rst, for PyPI's usage."""
with open("README.rst", encoding="utf-8") as file:
long_description = file.read()
return long_description
def refactor(path: "str") -> "None":
"""Refactor code."""
import subprocess # nosec: B404
if sys.version_info < (3, 6):
try:
subprocess.check_call( # nosec
[sys.executable, "-m", "f2format", "--no-archive", path]
)
except subprocess.CalledProcessError as error:
logger.error(
"Failed to perform assignment expression backport compiling. "
"Please consider manually install `bpc-f2format` and try again."
)
sys.exit(error.returncode)
if sys.version_info < (3, 8):
try:
subprocess.check_call( # nosec
[sys.executable, "-m", "walrus", "--no-archive", path]
)
except subprocess.CalledProcessError as error:
logger.error(
"Failed to perform assignment expression backport compiling. "
"Please consider manually install `bpc-walrus` and try again."
)
sys.exit(error.returncode)
try:
subprocess.check_call( # nosec
[sys.executable, "-m", "poseur", "--no-archive", path]
)
except subprocess.CalledProcessError as error:
logger.error(
"Failed to perform assignment expression backport compiling. "
"Please consider manually install `bpc-poseur` and try again."
)
sys.exit(error.returncode)
cmdclass = {} # type: dict[str, Type[Command]]
try:
from setuptools.command.sdist import sdist
class pcapkit_sdist(sdist):
"""Modified sdist to run PyBPC conversion."""
def make_release_tree(
self, base_dir: "str", *args: "Any", **kwargs: "Any"
) -> "None":
super(pcapkit_sdist, self).make_release_tree(base_dir, *args, **kwargs)
logger.info("running sdist")
# PyBPC compatibility enforcement
refactor(os.path.join(base_dir, "pcapkit"))
cmdclass["sdist"] = pcapkit_sdist
except ImportError:
logger.warning(
"setuptools version is too old to support PyBPC conversion in sdist."
)
try:
from setuptools.command.build_py import build_py
class pcapkit_build_py(build_py):
"""Modified build_py to run PyBPC conversion."""
def build_package_data(self) -> "None":
super(pcapkit_build_py, self).build_package_data()
logger.info("running build_py")
# PyBPC compatibility enforcement
refactor(os.path.join(self.build_lib, "pcapkit"))
cmdclass["build_py"] = pcapkit_build_py
except ImportError:
logger.warning(
"setuptools version is too old to support PyBPC conversion in build_py."
)
try:
from setuptools.command.develop import develop
class pcapkit_develop(develop):
"""Modified develop to run PyBPC conversion."""
def run(self) -> "None": # type: ignore[override]
super(pcapkit_develop, self).run()
logger.info("running develop")
# PyBPC compatibility enforcement
refactor(os.path.join(self.install_lib, "pcapkit"))
cmdclass["develop"] = pcapkit_develop
except ImportError:
logger.warning(
"setuptools version is too old to support PyBPC conversion in develop."
)
try:
from setuptools.command.install import install
class pcapkit_install(install):
"""Modified install to run PyBPC conversion."""
def run(self) -> "None":
super(pcapkit_install, self).run()
logger.info("running install")
# PyBPC compatibility enforcement
refactor(os.path.join(self.install_lib, "pcapkit")) # type: ignore[arg-type]
cmdclass["install"] = pcapkit_install
except ImportError:
logger.warning(
"setuptools version is too old to support PyBPC conversion in install."
)
try:
from setuptools.command.bdist_wheel import bdist_wheel
class pcapkit_bdist_wheel(bdist_wheel):
"""Modified bdist_wheel to run PyBPC conversion."""
def run(self) -> "None":
super(pcapkit_bdist_wheel, self).run()
logger.info("running bdist_wheel")
# PyBPC compatibility enforcement
refactor(os.path.join(self.dist_dir, "pcapkit")) # type: ignore[arg-type]
cmdclass["bdist_wheel"] = pcapkit_bdist_wheel
except ImportError:
logger.warning(
"setuptools version is too old to support PyBPC conversion in bdist_wheel."
)
setup(
cmdclass=cmdclass,
long_description=get_long_description(),
long_description_content_type="text/x-rst",
)