-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·339 lines (281 loc) · 10.4 KB
/
setup.py
File metadata and controls
executable file
·339 lines (281 loc) · 10.4 KB
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
#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
"""
Setup script
"""
import contextlib
import os
import sys
from pathlib import Path
from setuptools import setup
from setuptools import Command
PROJECT_DIRECTORY = Path(__file__).parent.resolve()
class WriteVersionFile(Command):
description = 'Write a Java properties file for use by Jenkins'
user_options = []
def initialize_options(self):
"""Set default values for options."""
pass
def finalize_options(self):
"""Post-process options."""
pass
def run(self):
"""Run command."""
with open('version.properties', 'w') as f:
f.write("PROJECT_NAME = {!s}\n".format(
self.distribution.metadata.name))
f.write("VERSION = {!s}\n".format(
self.distribution.metadata.version))
print("get version")
class Cookiecutter(Command):
"""
Bake the cookies
"""
description = 'bake the cookiecutter'
user_options = [
('no-input', None, 'Do not prompt for parameters and only use '
'cookiecutter.json file content',
' [default: False]'),
('replay', None, 'Do not prompt for parameters and only use '
'information entered previously '
' [default: letter]')
]
def initialize_options(self):
"""Set default values for options."""
# Each user option must be listed here with their default value.
self.no_input = False
self.replay = False
def finalize_options(self):
"""Post-process options."""
pass
def run(self):
"""Run command."""
from cookiecutter.main import cookiecutter
return cookiecutter(".", overwrite_if_exists=True, output_dir="build",
no_input=self.no_input,
replay=self.replay)
class Documentation(Command):
"""
Bake the cookies
"""
description = 'create documentation distribution'
user_options = [
('builder=', None, 'documentation output format'
' [default: html]'),
('paper=', None, 'paper format [default: letter]'),
('dist-dir=', None, 'directory to put final built distributions in'
' [default: dist/docs]'),
('build-dir=', None, 'temporary directory for creating the distribution'
' [default: build/docs]'),
('src-dir=', None, 'documentation source directory'
' [default: docs'),
]
targets = {
"html": {
"comment": "The HTML pages are in {build_dir!s}.",
},
"dirhtml": {
"comment": "The HTML pages are in {build_dir!s}.",
},
"singlehtml": {
"comment": "The HTML pages are in {build_dir!s}.",
},
"latex": {
"comment": "The LaTeX files are in {build_dir!s}.",
},
}
def initialize_options(self):
"""Set default values for options."""
# Each user option must be listed here with their default value.
project_directory = PROJECT_DIRECTORY
self.builder = "html"
self.paper = "letter"
self.dist_dir = str(project_directory / "dist/docs")
self.build_root = str(project_directory / "build/docs")
self.src_dir = str(project_directory / "docs")
def finalize_options(self):
if self.builder in self.targets:
self._actual_targets = [self.builder]
elif self.builder == "all":
self._actual_targets = self.targets.keys()
else:
self._actual_targets = []
assert self.paper in ["a4", "letter"]
self._canonical_directories()
self.announce(
"Building {!s} documentation".format(self.builder), 2)
self.announce(
" using source at '{!s}'".format(self.src_dir), 2)
self.announce(
" putting work files at '{!s}'".format(self.build_root), 2)
self.announce(
" distributing documentation at '{!s}'".format(self.dist_root), 2)
def run(self):
"""Run command."""
result_dirs = {}
for target in self._actual_targets:
result_dirs[target] = self._build_doc(target)
for target, result_dir in result_dirs.items():
self.announce("Documentation target '{!s}' : {!s}".format(
target,
self.targets[target]["comment"].format(
build_dir=result_dir)), 2)
def _canonical_directories(self):
self.dist_root = Path(self.dist_dir)
#self.dist_root.mkdir(exist_ok=True, parents=True)
try:
os.makedirs(self.dist_dir)
except OSError:
if not os.path.isdir(self.dist_dir):
raise
self.dist_root.resolve()
self.build_root = Path(self.build_root)
#self.build_root.mkdir(exist_ok=True, parents=True)
print (self.build_root)
try:
os.makedirs(str(self.build_root))
except OSError:
if not os.path.isdir(str(self.build_root)):
raise
self.build_root.resolve()
self.src_dir = Path(self.src_dir)
self.src_dir.resolve()
def _build_doc(self, target):
import shutil
import sphinx
build_dir = self.build_root / target
dist_dir = self.dist_root / target
cached_directory = build_dir.parent / "doctrees"
all_sphinx_opts = [
'',
"-b", target,
"-d", cached_directory,
#"-D", "latex_paper_size={!s}".format(self.paper),
self.src_dir,
build_dir
]
sphinx.build_main([str(arg) for arg in all_sphinx_opts])
shutil.rmtree(str(dist_dir), ignore_errors=True)
try:
os.makedirs(str(dist_dir.parent))
except OSError:
if not os.path.isdir(self.dist_dir):
raise
#dist_dir.parent.mkdir(exist_ok=True, parents=True)
shutil.copytree(str(build_dir), str(dist_dir))
return dist_dir
class BakedDocumentation(Command):
"""
"""
description = 'create documentation distribution from baked project'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
"""Run command."""
import subprocess
baking_command = Cookiecutter(self.distribution)
baking_command.no_input = True
bakedproject_directory = Path(baking_command.run())
bakeddoc_build_directory = PROJECT_DIRECTORY / "build" / "baked-docs"
bakeddoc_dist_directory = PROJECT_DIRECTORY / "dist" / "baked-docs"
bakeddoc_docs_directory = bakedproject_directory / "docs"
doc_build_command = [sys.executable,
bakedproject_directory / "setup.py",
"docs",
"--src-dir={!s}".format(bakeddoc_docs_directory),
"--build-dir={!s}".format(bakeddoc_build_directory),
"--dist-dir={!s}".format(bakeddoc_dist_directory)]
subprocess.check_call([str(arg) for arg in doc_build_command],
cwd=str(bakedproject_directory))
class Venv(Command):
"""
Setup venvs for development or production
"""
description = 'create a virtualenv pre-installed with dependencies'
user_options = [
# The format is (long option, short option, description).
('deps=', None, 'path to requirements.txt'),
]
def initialize_options(self):
"""Set default values for options."""
# Each user option must be listed here with their default value.
self.deps = './requirements.txt'
def finalize_options(self):
"""Post-process options."""
if self.deps:
deps = Path(str(self.deps))
assert deps.exists(), \
('Requirements file %s does not exist.'.format(deps))
def run(self):
"""Run command."""
import venv
venv.EnvBuilder(clear=True, with_pip=True).create("venv")
class Clean(Command):
"""Custom clean command to tidy up the project root."""
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
import shutil
import glob
shutil.rmtree("./build", ignore_errors=True)
shutil.rmtree("./__pycache__", ignore_errors=True)
shutil.rmtree("./dist", ignore_errors=True)
for path in glob.glob("./*.egg-info"):
shutil.rmtree(path, ignore_errors=True)
def get_distribution_info():
return dict(
name='foxmask',
packages=['foxmask'],
version='2.1',
description='Python package to analyse camera traps images',
author='Eric Devost',
license='GNU General Public License v3',
author_email='ericdevost@gmail.com',
url='',
setup_requires=['pytest-runner', 'cookiecutter'],
tests_require=['pytest', 'pytest-cookies', 'sphinx', 'flake8'],
keywords=['cookiecutter', 'template', 'package'],
cmdclass={'docs': Documentation,
'venv': Venv,
'clean': Clean,
'cookiecutter': Cookiecutter,
'baked_docs': BakedDocumentation,
'jenkins_env_file': WriteVersionFile},
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Console',
'Intended Audience :: Developers',
'Natural Language :: English',
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: Implementation :: CPython',
'Topic :: Software Development'
],
entry_points='''
[console_scripts]
foxmask=cli.cli:main
''',
)
def main():
setup(**get_distribution_info())
@contextlib.contextmanager
def working_directory(path):
"""
A context manager which changes the working directory to the given
path, and then changes it back to its previous value on exit.
"""
prev_cwd = os.getcwd()
os.chdir(str(path))
try:
yield
finally:
os.chdir(str(prev_cwd))
if __name__ == "__main__":
main()