-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtasks.py
70 lines (55 loc) · 1.75 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
from invoke import task, run, call
import contextlib
import os
import shutil
@task
def mkdocs(clean=False):
""" Only build site pages using MkDocs.
:param clean: should site folder be emptied before build?
"""
# use readme file for rendering index page
index_path = os.path.join("docs", "index.md")
index_backup_path = "index.md_original"
with backed_up_file(index_path, index_backup_path):
shutil.copy2("README.md", index_path)
run("mkdocs build" + (" --clean" if clean else ""), echo=True)
@task
def api():
""" Compiles API reference into site folder. """
lein("codox")
@task(post=[call(mkdocs, clean=True), call(api)])
def site():
""" Builds project site (including API docs). """
pass
################################################### Helpers
@contextlib.contextmanager
def backed_up_file(filepath, backup_path):
""" File will be returned to its initial state on context exit. """
with temp_file(backup_path):
try:
print("copy " + filepath + " to backup " + backup_path)
shutil.copy2(filepath, backup_path)
yield
finally:
print("recover " + filepath + " from backup " + backup_path)
shutil.copy2(backup_path, filepath)
@contextlib.contextmanager
def temp_file(filepath):
try:
print("create temp file " + filepath)
open(filepath, 'w').close()
yield
finally:
print("remove temp file " + filepath)
os.remove(filepath)
@contextlib.contextmanager
def chdir(dirname):
curdir = os.getcwd()
try:
os.chdir(dirname)
print("current dir: " + dirname)
yield
finally:
os.chdir(curdir)
def lein(args):
run("lein {0}".format(args), echo=True)