This repository has been archived by the owner on Oct 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
281 lines (245 loc) · 7.93 KB
/
tasks.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
import invoke
from invoke import task
from invoke import Collection
import click
import subprocess
import getpass
import os
import re
import json
try: # py3
from shlex import quote
except ImportError: # py2
from pipes import quote
CONTEXT_SETTINGS = dict(
max_content_width=120
)
@click.group(context_settings=CONTEXT_SETTINGS)
def cli():
pass
@task
def _virtualenv(ctx, virtualenv_path=None):
"""
Initialize a virtualenv folder.
"""
virtualenv_path = virtualenv_path or ctx.virtualenv_path
if not check_virtualenv(ctx, virtualenv_path):
if not os.path.exists(os.path.dirname(virtualenv_path)):
os.makedirs(os.path.dirname(virtualenv_path))
ctx.run(' '.join(['virtualenv', virtualenv_path]))
if not check_virtualenv(ctx, virtualenv_path):
raise Exception('python install fails')
@task
def _sphinx(ctx, virtualenv_path=None, skip=None, version=None):
"""
Install sphinx inside a virtualenv folder.
"""
skip = skip if (skip is not None) \
else ctx.sphinx.skip
dependencies = ctx.sphinx.dependencies
if not skip:
virtualenv_path = virtualenv_path or ctx.virtualenv_path
package = ctx.sphinx.package_name
version = version or ctx.sphinx.version
_pip_package(ctx, package, version)
for dependency in dependencies:
_pip_package(ctx, dependency['name'],
dependency.get('version', None))
else:
print('sphinx not managed (sphinx.skip: yes)')
@task
def _recommonmark(ctx, virtualenv_path=None, skip=None, version=None):
"""
Install recommonmark inside a virtualenv folder.
"""
skip = skip if (skip is not None) \
else ctx.recommonmark.skip
dependencies = ctx.dependencies
if not skip:
virtualenv_path = virtualenv_path or ctx.virtualenv_path
package = ctx.recommonmark.package_name
version = version or ctx.recommonmark.version
_pip_package(ctx, package, version)
for dependency in dependencies:
_pip_package(ctx, dependency['name'],
dependency.get('version', None))
else:
print('recommonmark not managed (recommonmark.skip: yes)')
def _pip_package(ctx, package, version=None, virtualenv_path=None):
"""
Install a pypi package (with pip) inside a virtualenv folder.
"""
virtualenv_path = virtualenv_path or ctx.virtualenv_path
if not check_pip(ctx, virtualenv_path, package, version):
pip_install(ctx, virtualenv_path, package, version)
if not check_pip(ctx, virtualenv_path, package, version):
raise Exception('{} install fails'.format(package))
@task(pre=[_virtualenv, _sphinx, _recommonmark])
def configure(ctx):
"""
Trigger virtualenv and sphinx initialization.
All this tools are needed to handle documentation generation.
"""
pass
def _docs_makefile(target, ctx, virtualenv_path=None):
"""
Trigger a sphinx Makefile target. Used to delegate all documentation jobs to
original sphinx Makefile.
"""
virtualenv_path = virtualenv_path or ctx.virtualenv_path
os.environ['PATH'] = \
':'.join([
os.path.abspath(os.path.join(virtualenv_path, 'bin')),
os.environ['PATH']])
args = ['make', '-C', '.', target]
ctx.run(' '.join(args), pty=True)
@task(pre=[configure])
def docs(ctx, virtualenv_path=None):
"""
Rebuild documentation.
"""
_docs_makefile('html', ctx, virtualenv_path)
@task(name='docs-clean', pre=[configure])
def docs_clean(ctx, virtualenv_path=None):
"""
Clean generated documentation.
"""
_docs_makefile('clean', ctx, virtualenv_path)
@task(name='docs-live', pre=[docs, configure])
def docs_live(ctx, virtualenv_path=None):
"""
Live build of documentation on each modification. Open a browser with a
local server to serve documentation. Opened page is reloaded each time
documentation is generated.
"""
virtualenv_path = virtualenv_path or ctx.virtualenv_path
os.environ['PATH'] = \
':'.join([
os.path.abspath(os.path.join(virtualenv_path, 'bin')),
os.environ['PATH']])
command = ' '.join([
'sphinx-autobuild',
'-B',
'--ignore', '"*.swp"',
'--ignore', '"*.log"',
'--ignore', '"*~"',
'--ignore', '"*~"',
'-b', 'html',
os.path.dirname(__file__) + '/source',
os.path.dirname(__file__) + '/build/html'
])
ctx.run(command, pty=True)
@task(name='docs-publish', pre=[docs_clean, docs, configure])
def docs_publish(ctx, virtualenv_path=None):
"""
Push generated documentation to online website.
Rsync url configured by docs_rsync_target (invoke.yaml)
"""
host_path = ctx.config.docs_rsync_target.split(':')
host = host_path[0]
path = host_path[1]
if not path:
raise Error('path null or empty')
virtualenv_path = virtualenv_path or ctx.virtualenv_path
if not ctx.config.docs_rsync_target:
raise Error('Missing docs_rsync_target in configuration')
command = ' '.join([
'rsync',
'-avzr',
'--delete',
'--omit-dir-times',
'--no-owner',
'--no-group',
'--no-perms',
os.path.dirname(__file__) + '/build/html/',
'"{}"'.format(ctx.config.docs_rsync_target)
])
if not ctx.config.grp_exec:
raise Error('Missing grp_exec in configuration')
command_ssh = ' '.join([
'ssh',
'"{}"'.format(ctx.config.docs_rsync_target.split(':')[0]),
'find',
'"{}"'.format(ctx.config.docs_rsync_target.split(':')[1]),
'-user', '$USER', '-exec', 'chgrp', '-R', ctx.config.grp_exec, '{}', ' \\\\\\;'
])
ctx.run(command, pty=True)
ctx.run(command_ssh, pty=True)
if os.path.exists('/usr/bin/xdg-open'):
ctx.run('{} {}'.format('/usr/bin/xdg-open', ctx.config.docs_online_path))
def check_virtualenv(ctx, virtualenv_path):
"""
Check if virtualenv is initialized in virtualenv folder (based on
bin/python file).
"""
r = ctx.run(' '.join([
os.path.join(virtualenv_path, 'bin/python'),
'--version'
]), warn=True, hide='both')
return r.ok
def check_pip(ctx, virtualenv_path, package, version):
"""
Check if a pypi package is installed in virtualenv folder.
"""
r = ctx.run(' '.join([
os.path.join(virtualenv_path, 'bin/pip'),
'show',
package
]), hide='both', warn=True)
if not r.ok:
# pip show package error - package is not here
return False
if version is None:
# no version check needed
return True
# package here, check version
m = re.search(r'^Version: (.*)$', r.stdout, re.MULTILINE)
result = m is not None and m.group(1).strip() == version
return result
def pip_install(ctx, virtualenv_path, package, version):
"""
Install a pypi package in a virtualenv folder with pip.
"""
pkgspec = None
if version is None:
pkgspec = package
else:
pkgspec = '{}=={}'.format(package, version)
ctx.run(' '.join([
os.path.join(virtualenv_path, 'bin/pip'),
'install',
pkgspec
]))
def _vcommand(virtualenv_path, command, *args):
"""
Run a command from virtualenv folder.
"""
cl = []
cl.append(os.path.join(virtualenv_path, 'bin', command))
cl.extend(args)
return ' '.join(cl)
def _command(command, *args):
"""
Run a command.
"""
cl = []
cl.append(os.path.join(command))
cl.extend(args)
return ' '.join(cl)
ns = Collection(configure,
docs, docs_live, docs_clean, docs_publish)
ns.configure({
'sphinx': {
'package_name': 'sphinx',
'dependencies': [
{ 'name': 'sphinx-bootstrap-theme' },
{ 'name': 'sphinx-autobuild' }
]
},
'recommonmark': {
'package_name': 'recommonmark'
},
'dependencies': []
})
if __name__ == '__main__':
cli()