-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
206 lines (179 loc) · 7.3 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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of ofunctions package
__intname__ = "ofunctions.setup"
__author__ = "Orsiris de Jong"
__copyright__ = "Copyright (C) 2021 Orsiris de Jong"
__licence__ = "BSD 3 Clause"
__build__ = "2021031601"
"""
Namespace packaging here
# Make sure we declare an __init__.py file as namespace holder in the package root containing the following
try:
__import__('pkg_resources').declare_namespace(__name__)
except ImportError:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
"""
import sys
import os
import shutil
import pkg_resources
import setuptools
def _read_file(filename):
here = os.path.abspath(os.path.dirname(__file__))
if sys.version_info[0] > 2:
with open(os.path.join(here, filename), "r", encoding="utf-8") as file_handle:
return file_handle.read()
else:
# With python 2.7, open has no encoding parameter, resulting in TypeError
# Fix with io.open (slow but works)
from io import open as io_open
with io_open(
os.path.join(here, filename), "r", encoding="utf-8"
) as file_handle:
return file_handle.read()
def get_metadata(package_file):
"""
Read metadata from package file
"""
_metadata = {}
for line in _read_file(package_file).splitlines():
if line.startswith("__version__") or line.startswith("__description__"):
delim = "="
_metadata[line.split(delim)[0].strip().strip("__")] = (
line.split(delim)[1].strip().strip("'\"")
)
return _metadata
def parse_requirements(filename):
"""
There is a parse_requirements function in pip but it keeps changing import path
Let's build a simple one
"""
try:
requirements_txt = _read_file(filename)
install_requires = [
str(requirement)
for requirement in pkg_resources.parse_requirements(requirements_txt)
]
return install_requires
except OSError:
print(
'WARNING: No requirements.txt file found as "{}". Please check path or create an empty one'.format(
filename
)
)
def clear_package_build_path(package_rel_path):
"""
We need to clean build path, but setuptools will wait for build/lib/package_name so we need to create that
"""
build_path = os.path.abspath(os.path.join("build", "lib", package_rel_path))
try:
# We need to use shutil.rmtree() instead of os.remove() since the latter implementation
# produces "WindowsError: [Error 5] Access is denied"
shutil.rmtree("build")
except FileNotFoundError:
print("build path: {} does not exist".format(build_path))
# Now we need to create the 'build/lib/package/subpackage' path so setuptools won't fail
os.makedirs(build_path)
# ######### ACTUAL SCRIPT ENTRY POINT
NAMESPACE_PACKAGE_NAME = "ofunctions"
namespace_package_path = os.path.abspath(NAMESPACE_PACKAGE_NAME)
namespace_package_file = os.path.join(namespace_package_path, "__init__.py")
metadata = get_metadata(namespace_package_file)
requirements = parse_requirements(
os.path.join(namespace_package_path, "requirements.txt")
)
long_description = _read_file("README.md")
# First lets make sure build path is clean (avoiding namespace package pollution in subpackages)
# Clean build dir before every run so we don't make cumulative wheel files
clear_package_build_path(NAMESPACE_PACKAGE_NAME)
# Generic namespace package
setuptools.setup(
name=NAMESPACE_PACKAGE_NAME,
namespace_packages=[NAMESPACE_PACKAGE_NAME],
packages=setuptools.find_namespace_packages(include=["ofunctions.*"]),
version=metadata["version"],
install_requires=requirements,
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Topic :: Software Development",
"Topic :: System",
"Topic :: System :: Operating System",
"Topic :: System :: Shells",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Operating System :: POSIX :: Linux",
"Operating System :: POSIX :: BSD :: FreeBSD",
"Operating System :: POSIX :: BSD :: NetBSD",
"Operating System :: POSIX :: BSD :: OpenBSD",
"Operating System :: Microsoft",
"Operating System :: Microsoft :: Windows",
"License :: OSI Approved :: BSD License",
],
description=metadata["description"],
author="NetInvent - Orsiris de Jong",
author_email="contact@netinvent.fr",
url="https://github.com/netinvent/ofunctions",
keywords=["network", "bisection", "logging"],
long_description=long_description,
long_description_content_type="text/markdown",
python_requires=">=3.5",
# namespace packages don't work well with zipped eggs
# ref https://packaging.python.org/guides/packaging-namespace-packages/
zip_safe=False,
)
for package in setuptools.find_namespace_packages(include=["ofunctions.*"]):
rel_package_path = package.replace(".", os.sep)
package_path = os.path.abspath(rel_package_path)
package_file = os.path.join(package_path, "__init__.py")
metadata = get_metadata(package_file)
requirements = parse_requirements(os.path.join(package_path, "requirements.txt"))
print(package_path)
print(package_file)
print(metadata)
print(requirements)
# Again, we need to clean build paths between runs
clear_package_build_path(rel_package_path)
setuptools.setup(
name=package,
namespace_packages=[NAMESPACE_PACKAGE_NAME],
packages=[package],
package_data={package: ["__init__.py"]},
version=metadata["version"],
install_requires=requirements,
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Topic :: Software Development",
"Topic :: System",
"Topic :: System :: Operating System",
"Topic :: System :: Shells",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Operating System :: POSIX :: Linux",
"Operating System :: POSIX :: BSD :: FreeBSD",
"Operating System :: POSIX :: BSD :: NetBSD",
"Operating System :: POSIX :: BSD :: OpenBSD",
"Operating System :: Microsoft",
"Operating System :: Microsoft :: Windows",
"License :: OSI Approved :: BSD License",
],
description=metadata["description"],
author="NetInvent - Orsiris de Jong",
author_email="contact@netinvent.fr",
url="https://github.com/netinvent/ofunctions",
keywords=["network", "bisection", "logging"],
long_description=long_description,
long_description_content_type="text/markdown",
python_requires=">=3.5",
# namespace packages don't work well with zipped eggs
# ref https://packaging.python.org/guides/packaging-namespace-packages/
zip_safe=False,
)