forked from openstenoproject/plover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·426 lines (350 loc) · 12 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python2
# Copyright (c) 2010 Joshua Harlan Lifton.
# See LICENSE.txt for details.
import contextlib
import os
import re
import shutil
import subprocess
import sys
from distutils import log
import pkg_resources
import setuptools
from plover import (
__name__ as __software_name__,
__version__,
__description__,
__long_description__,
__url__,
__download_url__,
__license__,
__copyright__,
)
from utils.metadata import copy_metadata
# Don't use six to avoid dependency with 'write_requirements' command.
PY3 = sys.version_info[0] >= 3
PACKAGE = '%s-%s-%s' % (
__software_name__,
__version__,
'py3' if PY3 else 'py2',
)
def get_version():
if not os.path.exists('.git'):
return None
version = subprocess.check_output('git describe --tags --match=v[0-9]*'.split()).strip().decode()
m = re.match(r'^v(\d[\d.]*)(-\d+-g[a-f0-9]*)?$', version)
assert m is not None, version
version = m.group(1)
if m.group(2) is not None:
version += '+' + m.group(2)[1:].replace('-', '.')
return version
def pyinstaller(*args):
py_args = [
'--log-level=INFO',
'--specpath=build',
'--additional-hooks-dir=windows',
'--name=%s' % PACKAGE,
'--noconfirm',
'--windowed',
'--onefile',
]
py_args.extend(args)
py_args.append('windows/main.py')
main = pkg_resources.load_entry_point('PyInstaller', 'console_scripts', 'pyinstaller')
return main(py_args) or 0
class Command(setuptools.Command):
def build_in_place(self):
self.run_command('build_py')
self.reinitialize_command('build_ext', inplace=1)
self.run_command('build_ext')
@contextlib.contextmanager
def project_on_sys_path(self):
self.build_in_place()
ei_cmd = self.get_finalized_command("egg_info")
old_path = sys.path[:]
old_modules = sys.modules.copy()
try:
sys.path.insert(0, pkg_resources.normalize_path(ei_cmd.egg_base))
pkg_resources.working_set.__init__()
pkg_resources.add_activation_listener(lambda dist: dist.activate())
pkg_resources.require('%s==%s' % (ei_cmd.egg_name, ei_cmd.egg_version))
yield
finally:
sys.path[:] = old_path
sys.modules.clear()
sys.modules.update(old_modules)
pkg_resources.working_set.__init__()
class PyInstallerDist(Command):
user_options = []
extra_args = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
self.build_in_place()
code = pyinstaller(*self.extra_args)
if code != 0:
sys.exit(code)
class BinaryDistWin(PyInstallerDist):
description = 'create an executable for MS Windows'
extra_args = [
'--icon=plover/assets/plover.ico',
]
class Launch(Command):
description = 'run %s from source' % __software_name__.capitalize()
command_consumes_arguments = True
user_options = []
def initialize_options(self):
self.args = None
def finalize_options(self):
pass
def run(self):
with self.project_on_sys_path():
from plover.main import main
sys.argv = [' '.join(sys.argv[0:2]) + ' --'] + self.args
sys.exit(main())
class PatchVersion(Command):
description = 'patch package version from VCS'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
version = get_version()
if version is None:
sys.exit(1)
log.info('patching version to %s', version)
version_file = os.path.join('plover', '__init__.py')
with open(version_file, 'r') as fp:
contents = fp.read().split('\n')
contents = [re.sub(r'^__version__ = .*$', "__version__ = '%s'" % version, line)
for line in contents]
with open(version_file, 'w') as fp:
fp.write('\n'.join(contents))
class TagWeekly(Command):
description = 'tag weekly version'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
version = get_version()
if version is None:
sys.exit(1)
weekly_version = 'weekly-v%s' % version
log.info('tagging as %s', weekly_version)
subprocess.check_call('git tag -f'.split() + [weekly_version])
class Test(Command):
description = 'run unit tests after in-place build'
command_consumes_arguments = True
user_options = []
def initialize_options(self):
self.args = []
def finalize_options(self):
pass
def run(self):
with self.project_on_sys_path():
self.run_tests()
def run_tests(self):
test_dir = os.path.join(os.path.dirname(__file__), 'test')
# Remove __pycache__ directory so pytest does not freak out
# when switching between the Linux/Windows versions.
pycache = os.path.join(test_dir, '__pycache__')
if os.path.exists(pycache):
shutil.rmtree(pycache)
custom_testsuite = None
args = []
for a in self.args:
if '-' == a[0]:
args.append(a)
elif os.path.exists(a):
custom_testsuite = a
args.append(a)
else:
args.extend(('-k', a))
if custom_testsuite is None:
args.insert(0, test_dir)
sys.argv[1:] = args
main = pkg_resources.load_entry_point('pytest',
'console_scripts',
'py.test')
sys.exit(main())
class BinaryDistApp(setuptools.Command):
user_options = []
extra_args = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
# Make sure metadata are up-to-date first.
self.run_command('egg_info')
self.run_command('py2app')
app = 'dist/%s.app' % PACKAGE
libdir = '%s/Contents/Resources/lib/python2.7' % app
sitezip = '%s/site-packages.zip' % libdir
# Add version to filename and strip other architectures.
# (using py2app --arch is not enough).
tmp_app = 'dist/%s.app' % __software_name__
cmd = 'ditto --arch x86_64 %s %s' % (tmp_app, app)
log.info('running %s', cmd)
subprocess.check_call(cmd.split())
shutil.rmtree(tmp_app)
# We can't access package resources from the site zip,
# so extract module and package data to the lib directory.
cmd = 'unzip -d %s %s plover/*' % (libdir, sitezip)
log.info('running %s', cmd)
subprocess.check_call(cmd.split())
cmd = 'zip -d %s plover/*' % sitezip
log.info('running %s', cmd)
subprocess.check_call(cmd.split())
# Add packages metadata.
copy_metadata('.', libdir)
class BinaryDistDmg(Command):
user_options = []
extra_args = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
self.run_command('bdist_app')
app = 'dist/%s.app' % PACKAGE
dmg = 'dist/%s.dmg' % PACKAGE
cmd = 'bash -x osx/app2dmg.sh %s %s' % (app, dmg)
log.info('running %s', cmd)
subprocess.check_call(cmd.split())
cmdclass = {
'launch': Launch,
'patch_version': PatchVersion,
'tag_weekly': TagWeekly,
'test': Test,
}
setup_requires = ['setuptools-scm']
options = {}
kwargs = {}
if sys.platform.startswith('darwin'):
setup_requires.append('py2app')
options['py2app'] = {
'arch': 'x86_64',
'argv_emulation': False,
'iconfile': 'osx/plover.icns',
'plist': {
'CFBundleName': __software_name__.capitalize(),
'CFBundleShortVersionString': __version__,
'CFBundleVersion': __version__,
'CFBundleIdentifier': 'org.openstenoproject.plover',
'NSHumanReadableCopyright': __copyright__,
'CFBundleDevelopmentRegion': 'English',
}
}
# Py2app will not look at entry_points.
kwargs['app'] = 'plover/main.py',
cmdclass['bdist_app'] = BinaryDistApp
cmdclass['bdist_dmg'] = BinaryDistDmg
if sys.platform.startswith('win32'):
setup_requires.append('PyInstaller==3.1.1')
cmdclass['bdist_win'] = BinaryDistWin
setup_requires.append('pytest')
dependency_links = [
'https://github.com/benoit-pierre/pyobjc/releases/download/pyobjc-3.1.1+plover2/pyobjc-core-3.1.1-plover2.tar.gz#egg=pyobjc-core',
'https://github.com/benoit-pierre/pyobjc/releases/download/pyobjc-3.1.1+plover2/pyobjc-framework-Cocoa-3.1.1-plover2.tar.gz#egg=pyobjc-framework-Cocoa',
]
install_requires = [
'six',
'setuptools',
'pyserial>=2.7',
'appdirs>=1.3.0',
'hidapi',
]
extras_require = {
':"win32" in sys_platform': [
'pywin32>=219',
],
':"linux" in sys_platform': [
'python-xlib>=0.16',
],
':"darwin" in sys_platform': [
'pyobjc-core==3.1.1+plover2',
'pyobjc-framework-Cocoa==3.1.1+plover2',
'pyobjc-framework-Quartz>=3.0.3',
'appnope>=0.1.0',
],
}
tests_require = [
'mock',
]
def write_requirements(extra_features=()):
requirements = setup_requires + install_requires + tests_require
for feature, dependencies in extras_require.items():
if feature.startswith(':'):
condition = feature[1:]
for require in dependencies:
requirements.append('%s; %s' % (require, condition))
elif feature in extra_features:
requirements.extend(dependencies)
requirements = sorted(set(requirements))
with open('requirements.txt', 'w') as fp:
fp.write('\n'.join(requirements))
fp.write('\n')
with open('requirements_constraints.txt', 'w') as fp:
fp.write('\n'.join(dependency_links))
fp.write('\n')
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'write_requirements':
write_requirements(extra_features=sys.argv[2:])
sys.exit(0)
setuptools.setup(
name=__software_name__,
version=__version__,
description=__description__,
long_description=__long_description__,
url=__url__,
download_url=__download_url__,
license=__license__,
author='Joshua Harlan Lifton',
author_email='[email protected]',
maintainer='Ted Morin',
maintainer_email='[email protected]',
include_package_data=True,
zip_safe=True,
options=options,
cmdclass=cmdclass,
setup_requires=setup_requires,
install_requires=install_requires,
extras_require=extras_require,
tests_require=tests_require,
dependency_links=dependency_links,
entry_points={
'console_scripts': ['plover=plover.main:main'],
'setuptools.installation': ['eggsecutable=plover.main:main'],
},
packages=[
'plover', 'plover.machine', 'plover.gui',
'plover.oslayer', 'plover.dictionary',
'plover.system',
],
data_files=[
('share/applications', ['application/Plover.desktop']),
('share/pixmaps', ['plover/assets/plover.png']),
],
classifiers=[
'Programming Language :: Python :: 2.7',
'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
'Development Status :: 5 - Production/Stable',
'Environment :: X11 Applications',
'Environment :: MacOS X',
'Environment :: Win32 (MS Windows)',
'Intended Audience :: End Users/Desktop',
'Natural Language :: English',
'Operating System :: POSIX :: Linux',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Topic :: Adaptive Technologies',
'Topic :: Desktop Environment',
],
**kwargs
)