1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
import imp |
4
|
|
|
import os |
5
|
|
|
import sys |
6
|
|
|
import warnings |
7
|
|
|
|
8
|
|
|
__all__ = ["REPO", "read", "readlines", "load_module"] |
9
|
|
|
|
10
|
|
|
REPO = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def read(path): |
14
|
|
|
if os.path.exists(path) and os.path.isfile(path): |
15
|
|
|
value = open(path).read().lstrip().rstrip() |
16
|
|
|
if value: |
17
|
|
|
return value |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def readlines(path): |
21
|
|
|
if os.path.exists(path) and os.path.isfile(path): |
22
|
|
|
lines = open(path).read().splitlines() |
23
|
|
|
lines = list(filter(lambda l: l.lstrip().rstrip(), lines)) |
24
|
|
|
lines = list(filter(lambda l: l, lines)) |
25
|
|
|
return lines |
26
|
|
|
return [] |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def _pyfiles(path): |
30
|
|
|
"""find python files of a directory""" |
31
|
|
|
listdir = os.listdir(path) |
32
|
|
|
listdir = filter(lambda l: os.path.splitext( |
33
|
|
|
l)[1] == ".py" and l.find("__") < 0, listdir) |
34
|
|
|
return listdir |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def moduledict(module): |
38
|
|
|
"""get module public objects dict""" |
39
|
|
|
kwargs = dict() |
40
|
|
|
for k in getattr(module, "__all__"): |
41
|
|
|
if getattr(module, k): |
42
|
|
|
kwargs[k] = getattr(module, k) |
43
|
|
|
return kwargs |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def load_module(path): |
47
|
|
|
with open(path, 'rb') as fhandler: |
48
|
|
|
# .hidden.py invisible for mdfind |
49
|
|
|
module = imp.load_module( |
50
|
|
|
path, fhandler, path, ('.py', 'rb', imp.PY_SOURCE)) |
51
|
|
|
# __all__ required |
52
|
|
|
if not hasattr(module, '__all__'): |
53
|
|
|
raise ValueError("ERROR: %s __all__ required" % path) |
54
|
|
|
return module |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def _update(**kwargs): |
58
|
|
|
for key, value in kwargs.items(): |
59
|
|
|
if key not in sys.modules["__main__"].__all__: |
60
|
|
|
sys.modules["__main__"].__all__.append(key) |
61
|
|
|
setattr(sys.modules["__main__"], key, value) |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def isstring(value): |
65
|
|
|
try: |
66
|
|
|
int(value) |
67
|
|
|
return False |
68
|
|
|
except ValueError: |
69
|
|
|
return True |
70
|
|
|
except Exception: |
71
|
|
|
return False |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def info(string): |
75
|
|
|
if len(sys.argv) == 1: |
76
|
|
|
print(string) |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
def main(): |
80
|
|
|
sys.modules["__main__"].__all__ = [] |
81
|
|
|
os.chdir(REPO) |
82
|
|
|
|
83
|
|
|
_setup = os.path.abspath(os.path.dirname(__file__)) |
84
|
|
|
files = _pyfiles(_setup) |
85
|
|
|
# RuntimeWarning: Parent module 'modname' not found while handling |
86
|
|
|
# absolute import |
87
|
|
|
warnings.simplefilter("ignore", RuntimeWarning) |
88
|
|
|
|
89
|
|
|
for file in files: |
90
|
|
|
try: |
91
|
|
|
fullpath = os.path.join(_setup, file) |
92
|
|
|
module = load_module(fullpath) |
93
|
|
|
kwargs = moduledict(module) |
94
|
|
|
_update(**kwargs) |
95
|
|
|
if kwargs: |
96
|
|
|
info(".setup/%s: %s" % (file[1:], kwargs)) |
97
|
|
|
except AttributeError: # variable from __all__ not initialized |
98
|
|
|
continue |
99
|
|
|
# ~/.setup_kwargs.py |
100
|
|
|
fullpath = os.path.join(os.environ["HOME"], ".setup_kwargs.py") |
101
|
|
|
if os.path.exists(fullpath): |
102
|
|
|
module = load_module(fullpath) |
103
|
|
|
setup_kwargs = moduledict(module) |
104
|
|
|
|
105
|
|
|
_update(**setup_kwargs) |
106
|
|
|
info("%s: %s" % ("~/.setup_kwargs.py", setup_kwargs)) |
107
|
|
|
else: |
108
|
|
|
info("SKIP: %s NOT EXISTS" % fullpath) |
109
|
|
|
|
110
|
|
|
kwargs = moduledict(sys.modules["__main__"]) |
111
|
|
|
if "name" in kwargs: |
112
|
|
|
name = kwargs["name"] |
113
|
|
|
del kwargs["name"] |
114
|
|
|
|
115
|
|
|
if len(sys.argv) == 1 and kwargs: # debug |
116
|
|
|
print('\nsetup(name="%s",' % name) |
117
|
|
|
for i, key in enumerate(sorted(list(kwargs.keys())), 1): # python3 |
118
|
|
|
value = kwargs[key] |
119
|
|
|
str_value = '"%s"' % value if isstring(value) else value |
120
|
|
|
comma = "," if i != len(kwargs) else "" |
121
|
|
|
print(" %s = %s%s" % (key, str_value, comma)) |
122
|
|
|
print(')') |
123
|
|
|
|
124
|
|
|
# 1) distutils (Python Standart Library) |
125
|
|
|
# https://docs.python.org/2/distutils/setupscript.html |
126
|
|
|
# https://docs.python.org/2/distutils/apiref.html (arguments) |
127
|
|
|
# 2) setuptools (extra commands and arguments) |
128
|
|
|
# extra commands: |
129
|
|
|
# http://pythonhosted.org/setuptools/setuptools.html#command-reference |
130
|
|
|
# extra arguments: |
131
|
|
|
# http://pythonhosted.org/setuptools/setuptools.html#new-and-changed-setup-keywords |
132
|
|
|
setuptools = True |
133
|
|
|
if "--manifest-only" in sys.argv: # distutils only |
134
|
|
|
setuptools = False |
135
|
|
|
if setuptools: |
136
|
|
|
try: |
137
|
|
|
import setuptools |
138
|
|
|
if "install" in sys.argv: |
139
|
|
|
print("setuptools.__version__: %s" % setuptools.__version__) |
140
|
|
|
setup = setuptools.setup |
141
|
|
|
if "zip_safe" not in kwargs: |
142
|
|
|
kwargs["zip_safe"] = False |
143
|
|
|
except ImportError: |
144
|
|
|
setuptools = False |
145
|
|
|
if not setuptools: |
146
|
|
|
if "install" in sys.argv: |
147
|
|
|
import distutils |
148
|
|
|
print("distutils.__version__: %s" % distutils.__version__) |
149
|
|
|
from distutils.core import setup |
150
|
|
|
|
151
|
|
|
if len(sys.argv) == 1: |
152
|
|
|
return |
153
|
|
|
setup(name=name, **kwargs) |
154
|
|
|
|
155
|
|
|
if __name__ == "__main__": |
156
|
|
|
main() |
157
|
|
|
|