forked from specialunderwear/setupreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupreader.py
75 lines (56 loc) · 2.31 KB
/
setupreader.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
import inspect
import imp
import json
import functools
import argparse
import mock
class BrokenSetupException(Exception):
"""
setupreader can not find a setup function in your setup.py.
please make sure your setup.py contains the call to setup.
If you have placed a guard around the call to setup like this::
def main():
setup(
...
)
if __name__ == '__main__':
main()
make sure the guarding function takes no parameters.
"""
def __init__(self):
super(BrokenSetupException, self).__init__(self.__doc__)
def _setup(target, *args, **kwargs):
target.result = dict(*args, **kwargs)
def load(path):
setupdict = argparse.Namespace(result=None)
with mock.patch('setuptools.setup', functools.partial(_setup, setupdict)):
setup_module = imp.load_source('packagesetup', path)
if setupdict.result is None:
# this can happen if the call to setup is inside a guard:
# if __name__ == '__main__'
# let's try to find a function that looks like it guards setup.
functions_found_in_setup_module = inspect.getmembers(
setup_module, inspect.isfunction)
for name, candidate in functions_found_in_setup_module:
argspec = inspect.getargspec(candidate)
# if the function is named 'main' and it receives no arguments, we
# declare it found.
# Also if this package was made by peopls without any knowledge or
# feeling for python idioms, they might have named it
# something else as main. We will declare any function that is
# defined inside the setup_module that received no arguments as a
# find.
if (name == 'main' or inspect.getmodule(candidate) == setup_module) and \
len(argspec.args) == 0:
candidate()
break
else: # we couldn't find anything resembling a guarded setup function.
raise BrokenSetupException()
return setupdict.result
def main():
p = argparse.ArgumentParser(description="Read data from setup.py.")
p.add_argument('path', help="path to setup.py")
args = p.parse_args()
print json.dumps(load(args.path), indent=4)
if __name__ == '__main__':
main()