-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
126 lines (109 loc) · 3.76 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
import os
import sys
import sysconfig
from setuptools import setup
from functools import partial
from setuptools.extension import Extension
class Packages(object):
def __init__(self, pkg_name=""):
self.name = pkg_name
self.base_dir = os.path.dirname(__file__)
basejoin = partial(self._join, self.base_dir)
self.source_dir = basejoin("src")
self.version_file = basejoin("src/_version.py")
self.des_file = basejoin("README.md")
self.req_file = basejoin("requirements.txt")
def _join(self, *args):
return os.path.join(*args)
@property
def listdir(self):
df, nc = [], []
if os.path.isdir(self.name):
nc = os.listdir(self.name)
for a, b, c in os.walk(self.source_dir):
if os.path.basename(a).startswith("__"):
continue
for i in c:
if i.startswith("__") or not i.endswith(".py") or i in nc:
continue
p = os.path.join(a[len(self.source_dir)+1:], i)
df.append(p)
return df
@property
def description(self):
des = ""
if os.path.isfile(self.des_file):
with open(self.des_file) as fi:
des = fi.read()
return des
@property
def version(self):
v = {}
if not os.path.isfile(self.version_file):
for f in os.listdir(self.source_dir):
if f.endswith("version.py"):
self.version_file = os.path.join(self.source_dir, f)
break
if not os.path.isfile(self.version_file):
raise OSError("version not found")
with open(self.version_file) as fi:
c = fi.read()
exec(compile(c, self.version_file, "exec"), v)
return v["__version__"]
@property
def requirements(self):
requires = []
if os.path.isfile(self.req_file):
with open(self.req_file) as fi:
for line in fi:
line = line.strip()
requires.append(line)
return requires
@property
def _extensions(self):
exts = []
for f in self.listdir:
e = Extension(self.name + "." + os.path.splitext(f)[0].replace("/", "."),
[os.path.join(self.source_dir, f), ], extra_compile_args=["-O3", ],)
e.cython_directives = {
'language_level': sysconfig._PY_VERSION_SHORT_NO_DOT[0]}
exts.append(e)
return exts
@property
def _package_dir(self):
pd = {}
for a, b, v in os.walk(self.source_dir):
p = a.replace(self.source_dir, self.name).replace("/", ".")
pd[p] = a.replace(self.source_dir, "src")
return pd
def install(self, ext=False):
kwargs = {}
kwargs.update(
name=self.name,
version=self.version,
license="MIT",
packages=list(self._package_dir.keys()),
package_dir=self._package_dir,
install_requires=self.requirements,
python_requires='>=3.7',
long_description=self.description,
long_description_content_type='text/markdown',
entry_points={'console_scripts': self._entrys},
url="https://github.com/yodeng/bpan",
)
if ext:
kwargs.pop("package_dir")
kwargs["ext_modules"] = self._extensions
setup(**kwargs)
@property
def _entrys(self):
eps = [
'{0} = {1}.main:main'.format(self.name.capitalize(), self.name),
'{0} = {0}.main:main'.format(self.name),
]
return eps
def main():
pkgs = Packages("bpan")
pkgs.install(ext=True)
if __name__ == "__main__":
main()