This repository has been archived by the owner on Nov 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
222 lines (190 loc) · 6.58 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
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
#!/usr/bin/env python
"""
NOTE: THIS FILE HAS HAD ONLY LIMITED TESTING!!!
This file provides a default setup.py for use with any python/git source distribution.
The defaults can be overridden using setup.cfg
Defaults:
name = root folder name
description = root folder name
install_requires = requirements.txt
dependency_links = requirements.txt lines starting http
long_description = first README.*
license = first line of LICENSE.txt
version = root/version.py with a line __version__ = "9.9.9"
included files = everything managed by git (see git ls-files)
excluded files = .gitignore
scripts = files in bin folder
Example of setup.cfg:
[metadata]
# these settings get passed to setuptools.setup()
name=myproject # overrides default setting
author=simon # adds new setting
[setup]
# these settings control behaviour of the default setup.py
autoinc = 0,1,2 # increments major/minor/micro version when you call setup.py sdist
Usage:
To publish on pypi for first time
setup.py register sdist upload
To update pypi
setup.py sdist upload
To upload to github after setup run (setup.py stages changes but does not commit)
git commit -am <message> push
To install:
pip install <name>
"""
import ConfigParser
import logging as log
log.basicConfig(level=log.INFO)
from setuptools import setup, find_packages
import pkg_resources
from subprocess import check_output
import os
import sys
here = os.path.abspath(os.path.dirname(__file__))
os.chdir(here)
try:
from version import __version__
except:
# if version file missing then create it
__version__ = "0.0.0"
with open("version.py", "w") as f:
f.write("__version__='%s'"%__version__)
def main():
log.info("***** running setup.py with args=%s *****"%sys.argv)
# get setup.cfg
c = ConfigParser.ConfigParser(allow_no_value=True)
try:
c.readfp(open("setup.cfg"))
except:
log.warning("setup.cfg not found")
c = None
# sdist processing for setup not for install
# update version; then git add new and deleted files to ensure correct files included
if "sdist" in sys.argv:
updateversion(c)
try:
check_output(["git", "add", "-A"])
except:
log.exception("Error updating git files")
sys.exit()
# start with defaults then add setup.cfg
setupdict = defaultSetup()
setupdict.update(cfgSetup(c))
logsetup(setupdict)
setup(**setupdict)
def updateversion(c):
global __version__
# setup.cfg
try:
__version__ = c.get("metadata", "version")
with open("version.py", "w") as f:
f.write("__version__='%s'"%__version__)
return
except ConfigParser.NoOptionError:
pass
# autoincrement
try:
inc = int(c.get("setup", "autoinc"))
v = pkg_resources.parse_version(__version__)
vints = map(int, v[:3])
vints[inc] += 1
for vint in vints[inc+1:]:
vint = 0
__version__ = ".".join(map(str,vints))
with open("version.py", "w") as f:
f.write("__version__='%s'"%__version__)
return
except ConfigParser.NoOptionError:
pass
def defaultSetup():
""" returns default dict """
name = here.split("/")[-1]
setupdict=dict(
# basic settings to create sdist tar with files under version control
name = name,
description = name,
setup_requires = ["setuptools_git >= 0.3"],
version = __version__,
license = license(),
long_description = long_description(),
install_requires = install_requires(),
dependency_links = dependency_links(),
# additional settings for pip install
packages = find_packages(),
include_package_data = True,
scripts = scripts(),
data_files = [('', data_files())]
)
return setupdict
def cfgSetup(c):
""" process setup.cfg and return dict """
# get metadata section
try:
cfg = dict(c.items("metadata"))
cfg.update({k:v.splitlines() for k,v in cfg.items() if v.find("\n") >=0})
except:
cfg = dict()
return cfg
def logsetup(setupdict):
""" log the setupdict """
output=""
for k,v in setupdict.items():
if k == "long_description":
v=v.splitlines()[0]
output+="{k}={v}\n".format(**locals())
log.info(output)
def license():
try:
with open("LICENSE.txt","r") as f:
return f.readline().strip()
except:
return ""
def scripts():
""" get files from scripts folder and remove any not managed by git """
files = ["bin/"+f for f in os.listdir('bin') if os.path.isfile("bin/"+f)]
if "sdist" in sys.argv:
gitfiles = check_output(["git", "ls-files"]).splitlines()
# must remove else any scripts specified are included even if not managed by git
files = [script for script in files if script in gitfiles]
return files
def data_files():
""" get files from root folder and remove any not managed by git """
files = [f for f in os.listdir(here) if os.path.isfile(f)]
if "sdist" in sys.argv:
gitfiles = check_output(["git", "ls-files"]).splitlines()
# must remove else any scripts specified are included even if not managed by git
files = [f for f in files if f in gitfiles]
return files
def install_requires():
""" use requirements.txt """
try:
with open("requirements.txt", "r") as f:
reqs = [req.split("#")[0].strip() for req in f.read().splitlines()]
install_requires = [req for req in reqs if not req.startswith("http")]
return install_requires
except IOError:
return []
except:
log.exception("problem parsing install_requires")
sys.exit()
def dependency_links():
""" use requirements.txt """
try:
with open("requirements.txt", "r") as f:
reqs = [req.split("#")[0].strip() for req in f.read().splitlines()]
dependency_links = [req for req in reqs if req.startswith("http")]
return dependency_links
except IOError:
return []
except:
log.exception("problem parsing dependency_links")
sys.exit()
def long_description():
""" set long description = first README.* """
try:
readme = [f for f in os.listdir(here) if f.startswith("README")][0]
with open(readme, "r") as f:
return f.read()
except:
return ""
main()