forked from fossasia/kniteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
299 lines (236 loc) · 9 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
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
#!/usr/bin/python3
"""The setup and build script for the library named "PACKAGE_NAME"."""
import os
import sys
from setuptools.command.test import test as TestCommandBase
from distutils.core import Command
import subprocess
PACKAGE_NAME = "kniteditor"
PACKAGE_NAMES = ["kniteditor", "kniteditor.localization"]
HERE = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, HERE) # for package import
__version__ = __import__(PACKAGE_NAME).__version__
__author__ = 'Nicco Kunzmann'
def read_file_named(file_name):
file_path = os.path.join(HERE, file_name)
with open(file_path) as file:
return file.read()
def read_requirements_file(file_name):
content = read_file_named(file_name)
lines = []
for line in content.splitlines():
comment_index = line.find("#")
if comment_index >= 0:
line = line[:comment_index]
line = line.strip()
if not line:
continue
lines.append(line)
return lines
# The base package metadata to be used by both distutils and setuptools
METADATA = dict(
name=PACKAGE_NAME,
version=__version__,
packages=PACKAGE_NAMES,
author=__author__,
author_email='[email protected]',
description='An editor for knitwork.',
license='LGPL',
url='https://github.com/fossasia/' + PACKAGE_NAME,
keywords='knitting ayab fashion',
)
# Run tests in setup
class TestCommand(TestCommandBase):
TEST_ARGS = [PACKAGE_NAME]
def finalize_options(self):
TestCommandBase.finalize_options(self)
self.test_suite = True
self.test_args = self.TEST_ARGS
def run_tests(self):
import pytest
errcode = pytest.main(self.test_args)
sys.exit(errcode)
class CoverageTestCommand(TestCommand):
TEST_ARGS = [PACKAGE_NAME, "--cov=" + PACKAGE_NAME]
class PEP8TestCommand(TestCommand):
TEST_ARGS = [PACKAGE_NAME, "--pep8"]
class FlakesTestCommand(TestCommand):
TEST_ARGS = [PACKAGE_NAME, "--flakes"]
class CoveragePEP8TestCommand(TestCommand):
TEST_ARGS = [PACKAGE_NAME, "--cov=" + PACKAGE_NAME, "--pep8"]
class LintCommand(TestCommandBase):
def finalize_options(self):
TestCommandBase.finalize_options(self)
self.test_suite = True
self.test_args = [PACKAGE_NAME]
def run_tests(self):
from pylint.lint import Run
Run(self.test_args)
# command for linking
class LinkIntoSitePackagesCommand(Command):
description = "link this module into the site-packages so the latest "\
"version can always be used without installation."
user_options = []
library_path = os.path.join(HERE, PACKAGE_NAME)
site_packages = [p for p in sys.path if "site-packages" in p]
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
assert self.site_packages, "We need a folder to install to."
print("link: {} -> {}".format(
os.path.join(self.site_packages[0], PACKAGE_NAME),
self.library_path
))
try:
if "linux" == sys.platform:
self.run_linux_link()
elif "darwin" == sys.platform:
self.run_mac_link()
elif "win" in sys.platform:
self.run_windows_link()
else:
self.run_other_link()
except:
print("failed:")
raise
else:
print("linked")
def run_linux_link(self):
subprocess.check_call(["sudo", "ln", "-f", "-s", "-t",
self.site_packages[0], self.library_path])
run_other_link = run_mac_link = run_linux_link
def run_windows_link(self):
path = os.path.join(self.site_packages[0], PACKAGE_NAME)
if os.path.exists(path):
os.remove(path)
command = ["mklink", "/J", path, self.library_path]
subprocess.check_call(command, shell=True)
# Extra package metadata to be used only if setuptools is installed
required_packages = read_requirements_file("requirements.txt")
required_test_packages = read_requirements_file("test-requirements.txt")
# print requirements
class PrintRequiredPackagesCommand(Command):
description = "Print the packages to install. "\
"Use pip install `setup.py requirements`"
user_options = []
name = "requirements"
def initialize_options(self):
pass
def finalize_options(self):
pass
@staticmethod
def run():
packages = list(set(required_packages + required_test_packages))
packages.sort(key=lambda s: s.lower())
for package in packages:
print(package)
# set development status from __version__
DEVELOPMENT_STATES = {
"p": "Development Status :: 1 - Planning",
"pa": "Development Status :: 2 - Pre-Alpha",
"a": "Development Status :: 3 - Alpha",
"b": "Development Status :: 4 - Beta",
"": "Development Status :: 5 - Production/Stable",
"m": "Development Status :: 6 - Mature",
"i": "Development Status :: 7 - Inactive"
}
development_state = DEVELOPMENT_STATES[""]
for ending in DEVELOPMENT_STATES:
if ending and __version__.endswith(ending):
development_state = DEVELOPMENT_STATES[ending]
if not __version__[-1:].isdigit():
METADATA["version"] += "0"
# tag and upload to github to autodeploy with travis
class TagAndDeployCommand(Command):
description = "Create a git tag for this version and push it to origin."\
"To trigger a travis-ci build and and deploy."
user_options = []
name = "tag_and_deploy"
remote = "origin"
branch = "master"
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
if subprocess.call(["git", "--version"]) != 0:
print("ERROR:\n\tPlease install git.")
exit(1)
status_lines = subprocess.check_output(["git", "status"]).splitlines()
current_branch = status_lines[0].strip().split()[-1].decode()
print("On branch {}.".format(current_branch))
if current_branch != self.branch:
print("ERROR:\n\tNew tags can only be made from branch \"{}\"."
"".format(self.branch))
print("\tYou can use \"git checkout {}\" to switch the branch."
"".format(self.branch))
exit(1)
tags_output = subprocess.check_output(["git", "tag"])
tags = [tag.strip().decode() for tag in tags_output.splitlines()]
tag = "v" + __version__
if tag in tags:
print("Warning: \n\tTag {} already exists.".format(tag))
print("\tEdit the version information in {}".format(
os.path.join(HERE, PACKAGE_NAME, "__init__.py")
))
else:
print("Creating tag \"{}\".".format(tag))
subprocess.check_call(["git", "tag", tag])
print("Pushing tag \"{}\" to remote \"{}\".".format(tag, self.remote))
subprocess.check_call(["git", "push", self.remote, tag])
SETUPTOOLS_METADATA = dict(
install_requires=required_packages,
tests_require=required_test_packages,
include_package_data=True,
classifiers=[ # https://pypi.python.org/pypi?%3Aaction=list_classifiers
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU Lesser General Public License'
' v3 (LGPLv3)',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Artistic Software',
'Topic :: Home Automation',
'Topic :: Utilities',
'Intended Audience :: Manufacturing',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python :: 3 :: Only',
development_state
],
package_data=dict(
# If any package contains of these files, include them:
knitting=['*.json'],
),
zip_safe=False,
cmdclass={
"test": TestCommand,
"coverage": CoverageTestCommand,
"coverage_test": CoverageTestCommand,
"pep8": PEP8TestCommand,
"pep8_test": PEP8TestCommand,
"flakes": FlakesTestCommand,
"fakes_test": FlakesTestCommand,
"coverage_pep8_test": CoveragePEP8TestCommand,
"lint": LintCommand,
"link": LinkIntoSitePackagesCommand,
PrintRequiredPackagesCommand.name: PrintRequiredPackagesCommand,
TagAndDeployCommand.name: TagAndDeployCommand
},
)
def main():
# Build the long_description from the README and CHANGES
METADATA['long_description'] = read_file_named("README.rst")
# Use setuptools if available, otherwise fallback and use distutils
try:
import setuptools
METADATA.update(SETUPTOOLS_METADATA)
setuptools.setup(**METADATA)
except ImportError:
import distutils.core
distutils.core.setup(**METADATA)
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == PrintRequiredPackagesCommand.name:
PrintRequiredPackagesCommand.run()
else:
main()