-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.py
139 lines (122 loc) · 3.9 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
import sys
from setuptools import setup, Extension
import json
with open("about.json", "r") as f:
about = json.load(f)
del about['md4c-version']
with open("README.md", "r") as f:
long_description = f.read()
class PkgconfigExtensionList(list):
"""A subclass of list that does not require the pkgconfig module for
initialization, but imports it and updates the list as soon as it is
accessed."""
def __init__(self, exts):
"""Create PkgconfigExtensionList from the given list of extension info.
Each extension must be a dict where keys are the arguments for
Extension() with one additional key, 'libs', a string to be passed to
pkgconfig.parse()."""
super().__init__(exts)
self.pkgconfig_ready = False
def _fetch_pkgconfig(self, extension):
"""Convert the pkgconfig keys in the dict to the proper arguments for
Extension and then create the actual Extension"""
# If no 'pkgconfig' key, add as-is
try:
libs = extension['pkgconfig']
except KeyError:
return Extension(**extension)
import pkgconfig
# Add pkgconfig info
del extension['pkgconfig']
for k, v in pkgconfig.parse(libs).items():
try:
extension[k].extend(v)
except KeyError:
extension[k] = v
return Extension(**extension)
def _fetch_pkgconfig_all(self):
"""Fetch all the pkgconfig info for all the extensions"""
# If pkgconfig already fetched, return
if self.pkgconfig_ready:
return
# Use 'libs' key to add pkgconfig info to all extensions
orig_extensions = super().copy()
super().clear()
for extension in orig_extensions:
super().append(self._fetch_pkgconfig(extension))
# Mark pkgconfig fetch complete
self.pkgconfig_ready = True
def __iter__(self):
self._fetch_pkgconfig_all()
return super().__iter__()
def __getitem__(self, key):
self._fetch_pkgconfig_all()
return super().__getitem__(key)
if sys.platform.startswith('win'):
import os
import os.path
md4c_path = os.environ.get('MD4C_PATH', 'C:/Program Files (x86)/MD4C')
md4c_include = os.path.join(md4c_path, 'include')
md4c_lib = os.path.join(md4c_path, 'lib')
extensions = [
Extension(
'md4c._md4c',
sources=[
'src/pymd4c.c',
'src/generic_parser.c',
'src/html_renderer.c',
],
include_dirs=[md4c_include],
libraries=['md4c', 'md4c-html'],
library_dirs=[md4c_lib]),
Extension(
'md4c._enum_consts',
sources=['src/enum_consts.c'],
include_dirs=[md4c_include],
libraries=['md4c'],
library_dirs=[md4c_lib]),
]
else:
extensions = PkgconfigExtensionList([
{
'name': 'md4c._md4c',
'sources': [
'src/pymd4c.c',
'src/generic_parser.c',
'src/html_renderer.c',
],
'pkgconfig': 'md4c md4c-html',
'include_dirs': ['src'],
},
{
'name': 'md4c._enum_consts',
'sources': [
'src/enum_consts.c',
],
'pkgconfig': 'md4c',
'include_dirs': ['src'],
},
])
setup(
# Most package metadata is in about.json (added below via **about)
long_description=long_description,
long_description_content_type="text/markdown",
setup_requires=[
'pkgconfig',
],
packages=[
'md4c',
'md4c.domparser',
],
ext_modules=extensions,
python_requires='>=3.6',
zip_safe=False,
include_package_data=True,
extras_require={
'test': [
'pytest',
'flake8',
],
},
**about
)