forked from helpsystems/pcapy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
343 lines (260 loc) · 8.57 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021-2022 Hewlett Packard Enterprise Development LP.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""
Usage:
::
./setup.py sdist
./setup.py bdist_wheel
"""
from pathlib import Path
from setuptools import setup, find_packages, Extension
def check_directory():
"""
You must always change directory to the parent of this file before
executing the setup.py script. setuptools will fail reading files,
including and excluding files from the MANIFEST.in, defining the library
path, etc, if not.
"""
from os import chdir
here = Path(__file__).parent.resolve()
if Path.cwd().resolve() != here:
print('Changing path to {}'.format(here))
chdir(str(here))
check_directory()
#####################
# Utilities #
#####################
def read(filename):
"""
Read the content of a file.
:param str filename: The file to read.
:return: The content of the file.
:rtype: str
"""
return Path(filename).read_text(encoding='utf-8')
def find_files(search_path, include=('*', ), exclude=('.*', )):
"""
Find files in a directory.
:param str search_path: Path to search for files.
:param tuple include: Patterns of files to include.
:param tuple exclude: Patterns of files to exclude.
:return: List of files found that matched the include collection but didn't
matched the exclude collection.
:rtype: list
"""
from os import walk
from fnmatch import fnmatch
from os.path import join, normpath
def included_in(fname, patterns):
return any(fnmatch(fname, pattern) for pattern in patterns)
files_collected = []
for root, folders, files in walk(search_path):
files_collected.extend(
normpath(join(root, fname)) for fname in files
if included_in(fname, include) and not included_in(fname, exclude)
)
files_collected.sort()
return files_collected
#####################
# Finders #
#####################
def find_version(filename):
"""
Find version of a package.
This will read and parse a Python module that has defined a __version__
variable. This function does not import the file.
::
setup(
...
version=find_version('lib/package/__init__.py'),
...
)
:param str filename: Path to a Python module with a __version__ variable.
:return: The version of the package.
:rtype: str
"""
import re
content = read(filename)
version_match = re.search(
r"^__version__ = ['\"]([^'\"]*)['\"]", content, re.M
)
if not version_match:
raise RuntimeError('Unable to find version string.')
version = version_match.group(1)
print('Version found:')
print(' {}'.format(version))
print('--')
return version
def find_requirements(filename):
"""
Finds PyPI compatible requirements in a pip requirements.txt file.
In this way requirements needs to be specified only in one, centralized
place:
::
setup(
...
install_requires=find_requirements('requirements.txt'),
...
)
Supports comments and non PyPI requirements (which are ignored).
:param str filename: Path to a requirements.txt file.
:return: List of requirements with version.
:rtype: list
"""
import string
content = read(filename)
requirements = []
ignored = []
for line in content.splitlines():
line = line.strip()
# Comments
if line.startswith('#') or not line:
continue
if line[:1] not in string.ascii_letters:
ignored.append(line)
continue
requirements.append(line)
print('Requirements found:')
for requirement in requirements:
print(' {}'.format(requirement))
print('--')
print('Requirements ignored:')
for requirement in ignored:
print(' {}'.format(requirement))
print('--')
# FIXME for some reason the reqs are installed in reverse order
requirements.reverse()
return requirements
def find_data(package_path, data_path, **kwargs):
"""
Find data files in a package.
::
setup(
...
package_data={
'package': find_data(
'lib/package',
'data',
)
},
...
)
:param str package_path: Path to the root of the package.
:param str data_path: Relative path from the root of the package to the
data directory.
:param kwargs: Function supports inclusion and exclusion patterns as
specified in the find_files function.
:return: List of data files found.
:rtype: list
"""
from os.path import join, relpath
files_collected = find_files(join(package_path, data_path), **kwargs)
data_files = [relpath(fname, package_path) for fname in files_collected]
print('Data files found:')
for data_file in data_files:
print(' {}'.format(data_file))
print('--')
return data_files
def parse_args():
from sys import argv
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--link-static-pcap')
args, _ = parser.parse_known_args()
if args.link_static_pcap:
if not Path(args.link_static_pcap).is_file():
raise ValueError(
f'Static PCAP library file {args.link_static_pcap} '
'does not exists'
)
# Need to remove the custom flags so setuptools won't complain
if '--link-static-pcap' in argv:
index = argv.index('--link-static-pcap')
del argv[index]
del argv[index]
else:
argv.remove(f'--link-static-pcap={args.link_static_pcap}')
return args
def find_extension_options():
"""
Find the proper Extension parameters to link against libpcap
By default set the libpcap linking to the shared library (.so), unless the
user sets "--link-static-pcap" with the path of the libpcap.a binary.
This is useful when packaging pcapyplus to avoid the need for the
installation environment to have libpcap installed with the same version
used while building.
"""
args = parse_args()
if args.link_static_pcap:
return {
'extra_objects': [args.link_static_pcap]
}
return {
'libraries': ['pcap']
}
setup(
name='pcapyplus',
version=find_version('lib/pcapyplus/__init__.py'),
# Dependencies
install_requires=find_requirements('requirements.txt'),
extras_require={
},
# Package
package_dir={'': 'lib'},
packages=find_packages('lib'),
ext_modules=[
Extension(
name='pcapyplus._pcapyplus',
sources=find_files('lib', include=('*.cpp', )),
include_dirs=['lib/pcapyplus/include'],
**find_extension_options()
)
],
# Data files
package_data={
'pcapyplus': (
find_data('lib/pcapyplus', 'data')
)
},
# Metadata
author='Hewlett Packard Enterprise Development LP',
author_email='[email protected]',
description=(
'Python extension module to access libpcap'
),
long_description=read('README.rst'),
url='https://github.com/HPENetworking/pcapyplus',
keywords='pcapyplus',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.8',
],
# Sphinx autodoc cannot extract the documentation of zipped eggs with the
# ``.. autodata::`` directive, causing chaos in AutoAPI. This disables zip
# installation for correct AutoAPI generation.
zip_safe=False,
# Entry points
entry_points={
},
# Minimal Python version
python_requires='>=3.6',
)