forked from tanghaibao/goatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_helper.py
87 lines (74 loc) · 2.8 KB
/
setup_helper.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import subprocess
import pkg_resources
class SetupHelper(object):
"""
SetupHelper streamlines the population of setup() function calls with
contents from __init__.py and README.md.
"""
def __init__(self, initfile="__init__.py", readmefile="README.md"):
self.author, self.email, self.license = self.get_init(initfile)
self.author = ", ".join(self.author)
self.long_description = self.get_long_description(readmefile)
def check_version(self, name, majorv=2, minorv=7):
""" Make sure the package runs on the supported Python version
"""
if sys.version_info.major == majorv and sys.version_info.minor != minorv:
sys.stderr.write(
"ERROR: %s is only for >= Python %d.%d but you are running %d.%d\n"
% (name, majorv, minorv, sys.version_info.major, sys.version_info.minor)
)
sys.exit(1)
def get_init(self, filename="__init__.py"):
""" Get various info from the package without importing them
"""
import ast
with open(filename) as init_file:
module = ast.parse(init_file.read())
itr = lambda x: (
ast.literal_eval(node.value)
for node in ast.walk(module)
if isinstance(node, ast.Assign) and node.targets[0].id == x
)
try:
return (
next(itr("__author__")),
next(itr("__email__")),
next(itr("__license__")),
)
except StopIteration:
raise ValueError(
"One of author, email, license"
" cannot be found in {}".format(filename)
)
def missing_requirements(self, specifiers):
""" Find what's missing
"""
for specifier in specifiers:
try:
pkg_resources.require(specifier)
except pkg_resources.DistributionNotFound:
yield specifier
def install_requirements(self, requires):
""" Install the listed requirements
"""
# Temporarily install dependencies required by setup.py before trying to import them.
sys.path[0:0] = ["setup-requires"]
pkg_resources.working_set.add_entry("setup-requires")
to_install = list(self.missing_requirements(requires))
if to_install:
cmd = [
sys.executable,
"-m",
"pip",
"install",
"-t",
"setup-requires",
] + to_install
subprocess.call(cmd)
def get_long_description(self, filename="README.md"):
""" I really prefer Markdown to reStructuredText. PyPi does not.
"""
return open("README.md").read()