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
| # # type: classifier
# # category: classifier
# # keywords: tag, tag
# # classifiers: tag, trove, shortcuts
# # doc-format: text/markdown
# #
# # Long description used in lieu of README...
#
# A README.* will be read if present, else PMD comment used.
# Classifiers and license matching is very crude, just for
# the most common cases. Type:, Category: and Classifiers:
# or Keywords: are also scanned for trove classifers.
#
import os, re, glob
import setuptools
import pluginconf
def _name_to_fn(name):
""" find primary entry point.py from package name """
for pfx in "", "src/", "src/"+name+"/":
for sfx in ".py", "/__init__.py":
if os.path.exists(pfx+name+sfx):
return pfx+name+sfx
def _get_readme():
""" get README.md contents """
for fn,mime in ("README.md", "text/markdown"), ("README.rst", "text/x-rst"), ("README.txt", "text/plain"):
if os.path.exists(fn):
with open(fn, "r") as f:
return {
"long_description": f.read(),
"long_description_content_type": mime,
}
return {
"long_description": "",
"long_description_content_type": "text/plain",
}
def _plugin_doc(pmd):
""" use comment block """
return {
"long_description": pmd["doc"],
"long_description_content_type": pmd.get("doc_format", "text/plain"),
}
def _python_requires(pmd):
""" # depends: python >= 3.5 """
deps = re.findall("python\s*\(?(>=?\s?[\d.]+)", pmd.get("depends", ""))
if deps:
return {"python_requires": deps[0]}
return {}
def _install_requires(pmd):
""" # depends: python:module, pip:module """
deps = re.findall("(?:python|pip):([\w\-]+)\s*(\(?[<=>\s\d.\-]+)?", pmd.get("depends", ""))
if deps:
return {"install_requires": [name+re.sub("[^<=>\d.\-]", "", ver) for name,ver in deps]}
return {}
def _extras_require(pmd):
""" # suggest: line """
deps = re.findall("(?:python|pip):([\w\-]+)\s*\(?\s*([>=<]+\s*[\d.\-]+)", pmd.get("suggests", ""))
if deps:
return dict(deps)
return {}
def _project_urls(pmd, exclude=["url"]):
""" # other-url: https://... """
urls = {}
for k,url in pmd.items():
if type(url) is str and k not in exclude and re.match("https?://\S+", url):
urls[k.title()] = url
return urls
def _classifiers(pmd):
""" # classifiers: / keywords: / category: """
for field in ("api", "category", "type", "keywords", "classifiers"):
field = pmd.get(field, "")
field = re.findall("(\w{4,})", field)
rx = "|".join(field)
if not rx:
continue
for line in topic_trove:
if re.search("::[^:]*("+rx+")[^:]*$", line, re.I):
yield line
def _trove_license(pmd):
""" license: to License :: """
trove_licenses = {
"MITL?": "License :: OSI Approved :: MIT License",
"PD|Public Domain": "License :: Public Domain",
"ASL": "License :: OSI Approved :: Apache Software License",
"art": "License :: OSI Approved :: Artistic License",
"BSDL?": "License :: OSI Approved :: BSD License",
"CPL": "License :: OSI Approved :: Common Public License",
"AGPL.*3": "License :: OSI Approved :: GNU Affero General Public License v3",
"AGPLv*3\+": "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
"GPL": "License :: OSI Approved :: GNU General Public License (GPL)",
"GPL.*3": "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"LGPL": "License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)",
"MPL": "License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)",
"Pyth": "License :: OSI Approved :: Python Software Foundation License"
}
for rx, trove in trove_licenses.items():
if re.search(rx, pmd["license"], re.I):
return [trove]
return []
def _trove_status(pmd):
""" state: to DevStatus :: """
trove_status = {
"pre|release|cand": "Development Status :: 2 - Pre-Alpha",
"alpha": "Development Status :: 3 - Alpha",
"beta": "Development Status :: 4 - Beta",
"stable": "Development Status :: 5 - Production/Stable",
"mature": "Development Status :: 6 - Mature"
}
for rx, trove in trove_status.items():
state = pmd.get("state") or pmd.get("status") or "alpha"
if re.search(rx, state, re.I):
return [trove]
return []
def _datafiles_man():
""" data_files= """
for man in glob.glob("man*/*.[12345678]"):
section = man[-1]
yield ("man/man"+section, [man],)
def _entry_points(pmd):
""" collect console-scripts: """
params = {}
for field in ["console_scripts", "gui_scripts"]:
if not pmd.get(field):
continue
params[field] = params.get(field, []) + re.findall("(\w+[^,;\s]+=\w+[^,;\s]+)", pmd[field])
return params
def _keywords(pmd):
""" keywords= """
return pmd.get("keywords") or pmd.get("category")
def setup(debug=0, **kwargs):
"""
Wrapper around `setuptools.setup()` which adds some defaults
and plugin meta data import, with two shortcut params:
fn="pkg/main.py",
long_description="@README.md"
Other setup() params work as usual.
"""
# stub values
stub = {
"classifiers": [],
"project_urls": {},
"python_requires": ">= 2.7",
"install_requires": [],
"extras_require": {},
#"package_dir": {"": "."},
#"package_data": {},
#"data_files": [],
"entry_points": {},
"packages": setuptools.find_packages()
}
for k,v in stub.items():
if not k in kwargs:
kwargs[k] = v
# package name
if "name" not in kwargs and kwargs.get("packages"):
kwargs["name"] = kwargs["packages"][0]
# read README
if re.match("^$|^[@./]*README.{0,5}$", kwargs.get("long_description", "")):
kwargs.update(_get_readme())
# search name= package if no fn= given
if kwargs.get("filename"):
kwargs["fn"] = kwargs["filename"]
del kwargs["filename"]
if not kwargs.get("fn") and kwargs.get("name"):
kwargs["fn"] = _name_to_fn(kwargs["name"])
# read plugin meta data (PMD)
pmd = {}
pmd = pluginconf.plugin_meta(filename=kwargs["fn"])
# id: if no name= still
if pmd.get("id") and not kwargs.get("name"):
if pmd["id"] == "__init__":
pmd["id"] = re.findall("([\w\.\-]+)/__init__.+$", kwargs["fn"])[0]
kwargs["name"] = pmd["id"]
# cleanup
if "fn" in kwargs:
del kwargs["fn"]
# version:, description:, author:
for field in "version", "description", "license", "author", "url":
if field in pmd and not field in kwargs:
kwargs[field] = pmd[field]
# other urls:
kwargs["project_urls"].update(_project_urls(pmd))
# depends:
if "depends" in pmd:
kwargs.update(_python_requires(pmd))
if "depends" in pmd and not kwargs["install_requires"]:
kwargs.update(_install_requires(pmd))
# suggests:
if "suggests" in pmd and not kwargs["extras_require"]:
kwargs["extras_require"].update(_extras_require(pmd))
# doc:
if not kwargs.get("long_description"):
kwargs.update(_plugin_doc(pmd))
# keywords=
if not "keywords" in kwargs:
kwargs["keywords"] = _keywords(pmd)
# automatic inclusions
kwargs["data_files"] = kwargs.get("data_files", []) + list(_datafiles_man())
# entry points
for section, entries in _entry_points(pmd).items():
kwargs["entry_points"][section] = kwargs["entry_points"].get(section, []) + entries
# classifiers=
# license:
if pmd.get("license") and not any(re.match("License ::", l) for l in kwargs["classifiers"]):
kwargs["classifiers"].extend(_trove_license(pmd))
# state:
if pmd.get("state", pmd.get("status")) and not any(re.match("Development Status ::", l) for l in kwargs["classifiers"]):
kwargs["classifiers"].extend(_trove_status(pmd))
# topics::
kwargs["classifiers"].extend(list(_classifiers(pmd)))
# handover
if debug:
import pprint
pprint.pprint(kwargs)
setuptools.setup(**kwargs)
topic_trove="""Topic :: Adaptive Technologies
Topic :: Artistic Software
Topic :: Communications
Topic :: Communications :: BBS
Topic :: Communications :: Chat
Topic :: Communications :: Chat :: ICQ
Topic :: Communications :: Chat :: Internet Relay Chat
Topic :: Communications :: Chat :: Unix Talk
|
|
>
>
|
>
>
>
|
>
|
|
|
|
|
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
<
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
|
|
>
|
>
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
| 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
| # # type: classifier
# # category: classifier
# # keywords: tag, tag
# # classifiers: tag, trove, shortcuts
# # doc-format: text/markdown
# #
# # Long description used in lieu of README...
#
# A README.* will be read if present, else PMD comment used.
# Classifiers and license matching is very crude, just for
# the most common cases. Type:, Category: and Classifiers:
# or Keywords: are also scanned for trove classifers.
#
""" Simulates setuptools.setup() """
import os
import re
import glob
import pprint
import setuptools
import pluginconf
def name_to_fn(name):
""" find primary entry point.py from package name """
for pfx in "", "src/", "src/"+name+"/":
for sfx in ".py", "/__init__.py":
if os.path.exists(pfx+name+sfx):
return pfx+name+sfx
return ""
def get_readme():
""" get README.md contents """
for filename, mime in ("README.md", "text/markdown"), ("README.rst", "text/x-rst"), ("README.txt", "text/plain"):
if os.path.exists(filename):
with open(filename, "r") as read:
return {
"long_description": read.read(),
"long_description_content_type": mime,
}
return {
"long_description": "",
"long_description_content_type": "text/plain",
}
class MetaUtils(dict):
""" convenience access to PMD fields """
def __getattr__(self, name):
""" dict into properties """
return self.get(name, "")
def plugin_doc(self):
""" use comment block """
return {
"long_description": self.doc,
"long_description_content_type": self.doc_format or "text/plain"
}
def python_requires(self):
""" depends: python >= 3.5 """
deps = re.findall(r"python\s*\(?(>=?\s?[\d.]+)", self.get("depends", ""))
if deps:
return {"python_requires": deps[0]}
return {}
def install_requires(self):
""" depends: python:module, pip:module """
deps = re.findall(r"(?:python|pip):([\w\-]+)\s*(\(?[<=>\s\d.\-]+)?", self.get("depends", ""))
if deps:
return {"install_requires": [name+re.sub(r"[^<=>\d.\-]", "", ver) for name, ver in deps]}
return {}
def extras_require(self):
""" suggest: line """
deps = re.findall(r"(?:python|pip):([\w\-]+)\s*\(?\s*([>=<]+\s*[\d.\-]+)", self.get("suggests", ""))
if deps:
return dict(deps)
return {}
def project_urls(self, exclude=("url", "update",)):
""" other-url: https://... """
urls = {}
for key, url in self.items():
if isinstance(url, str) and key not in exclude and re.match(r"https?://\S+", url):
urls[key.title()] = url
return urls
def classifiers(self):
""" classifiers: / keywords: / category: """
for field in ("api", "category", "type", "keywords", "classifiers"):
field = self.get(field, "")
field = re.findall(r"(\w{4,})", field)
regex = "|".join(field)
if not regex:
continue
for line in TOPIC_TROVE:
if re.search("::[^:]*("+regex+")[^:]*$", line, re.I):
yield line
def trove_license(self):
""" license: to License :: """
trove_licenses = {
r"MITL?": "License :: OSI Approved :: MIT License",
r"\bPD\b|CC-?0|Public ?Domain|Unlicense": "License :: Public Domain",
r"ASL": "License :: OSI Approved :: Apache Software License",
r"art": "License :: OSI Approved :: Artistic License",
r"BSDL?": "License :: OSI Approved :: BSD License",
r"CPL": "License :: OSI Approved :: Common Public License",
r"AGPL.*3": "License :: OSI Approved :: GNU Affero General Public License v3",
r"AGPLv*3\+": "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
r"\bGPL": "License :: OSI Approved :: GNU General Public License (GPL)",
r"\bGPL.*3": "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
r"LGPL": "License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)",
r"MPL": "License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)",
r"Pyth": "License :: OSI Approved :: Python Software Foundation License"
}
for regex, trove in trove_licenses.items():
if re.search(regex, self.license, re.I):
return [trove]
return []
def trove_status(self):
""" state: to DevStatus :: """
trove_status = {
"pre|release|cand": "Development Status :: 2 - Pre-Alpha",
"alpha": "Development Status :: 3 - Alpha",
"beta": "Development Status :: 4 - Beta",
"stable": "Development Status :: 5 - Production/Stable",
"mature": "Development Status :: 6 - Mature"
}
for regex, trove in trove_status.items():
state = self.state or self.status or "alpha"
if re.search(regex, state, re.I):
return [trove]
return []
@staticmethod
def datafiles_man():
""" data_files= """
for man in glob.glob("man*/*.[12345678]"):
section = man[-1]
yield ("man/man"+section, [man],)
def entry_points(self):
""" collect console-scripts: """
params = {}
for field in ["console_scripts", "gui_scripts"]:
if not self.get(field):
continue
params[field] = params.get(field, []) + re.findall(r"(\w+[^,;\s]+=\w+[^,;\s]+)", self[field])
return params
def get_keywords(self):
""" keywords= """
return self.keywords or self.category or self.type
def setup(debug=0, **kwargs):
"""
Wrapper around `setuptools.setup()` which adds some defaults
and plugin meta data import, with two shortcut params:
Parameters
----------
fn : str
main file "pkg/main.py"
long_description : str
e.g. "README.md", else comment block used
Other setup() params work as usual, and are passed trough. Notably
entry_points= or data_files= can be used, even if they get augmented.
"""
# stub values
stub = {
"classifiers": [],
"project_urls": {},
"python_requires": ">= 2.7",
"install_requires": [],
"extras_require": {},
#"package_dir": {"": "."},
#"package_data": {},
#"data_files": [],
"entry_points": {},
"packages": setuptools.find_packages()
}
for key, val in stub.items():
if not key in kwargs:
kwargs[key] = val
# package name
if "name" not in kwargs and kwargs.get("packages"):
kwargs["name"] = kwargs["packages"][0]
# read README if field empty or says `@README`
if re.match("^$|^[@./]*README.{0,5}$", kwargs.get("long_description", "")):
kwargs.update(get_readme())
# search name= package if no fn= given
if kwargs.get("filename"):
kwargs["fn"] = kwargs["filename"]
del kwargs["filename"]
if not kwargs.get("fn") and kwargs.get("name"):
kwargs["fn"] = name_to_fn(kwargs["name"])
# read plugin meta data (PMD)
pmd = MetaUtils(
pluginconf.plugin_meta(filename=kwargs["fn"])
)
# id: if no name= still
if pmd.get("id") and not kwargs.get("name"):
if pmd["id"] == "__init__":
pmd["id"] = re.findall(r"([\w\.\-]+)/__init__.+$", kwargs["fn"])[0]
kwargs["name"] = pmd["id"]
# cleanup
if "fn" in kwargs:
del kwargs["fn"]
# version:, description:, author:
for field in "version", "description", "license", "author", "url":
if field in pmd and not field in kwargs:
kwargs[field] = pmd[field]
# other urls:
kwargs["project_urls"].update(pmd.project_urls())
# depends:
if "depends" in pmd:
kwargs.update(pmd.python_requires())
if "depends" in pmd and not kwargs["install_requires"]:
kwargs.update(pmd.install_requires())
# suggests:
if "suggests" in pmd and not kwargs["extras_require"]:
kwargs["extras_require"].update(pmd.extras_require())
# doc:
if not kwargs.get("long_description"):
kwargs.update(pmd.plugin_doc())
# keywords=
if "keywords" not in kwargs:
kwargs["keywords"] = pmd.get_keywords()
# automatic inclusions
kwargs["data_files"] = kwargs.get("data_files", []) + list(pmd.datafiles_man())
# entry points
for section, entries in pmd.entry_points().items():
kwargs["entry_points"][section] = kwargs["entry_points"].get(section, []) + entries
# classifiers=
# license:
if pmd.get("license") and not any(re.match("License ::", l) for l in kwargs["classifiers"]):
kwargs["classifiers"].extend(pmd.trove_license())
# state:
if pmd.get("state", pmd.get("status")) and not any(re.match("Development Status ::", l) for l in kwargs["classifiers"]):
kwargs["classifiers"].extend(pmd.trove_status())
# topics::
kwargs["classifiers"].extend(list(pmd.classifiers()))
# handover
if debug:
pprint.pprint(kwargs)
setuptools.setup(**kwargs)
TOPIC_TROVE = """Topic :: Adaptive Technologies
Topic :: Artistic Software
Topic :: Communications
Topic :: Communications :: BBS
Topic :: Communications :: Chat
Topic :: Communications :: Chat :: ICQ
Topic :: Communications :: Chat :: Internet Relay Chat
Topic :: Communications :: Chat :: Unix Talk
|