-
Notifications
You must be signed in to change notification settings - Fork 27
/
tasks.py
79 lines (68 loc) · 2.45 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
import sys
from os import path, chdir
from invoke import run, task
if sys.version_info[0] < 3:
import SimpleHTTPServer as httpserver
import SocketServer as socketserver
else:
import http.server as httpserver
import socketserver
languages = ['en', 'de']
BASE_DIR = path.realpath(path.dirname(__file__))
BUILD_DIR = path.join(BASE_DIR, 'build')
SOURCE_DIR = path.join(BASE_DIR, 'source')
LOCALE_DIR = path.join(SOURCE_DIR, 'locale',
'%s', 'LC_MESSAGES')
LANGUAGES = set(['en', 'de'])
MAIN_TARGET = 'html'
REPOSITORY = 'git@github.com:%s/html-css-beginners.git'
SERVE_PORT = 8000
@task
def clean(language=None, target=MAIN_TARGET):
"""Clean the sphinx build folder"""
if language is not None:
run('rm -rf %s' % path.join(BUILD_DIR, target, language))
else:
run('rm -rf %s' % path.join(BUILD_DIR, target))
@task(clean)
def setup(owner='OpenTechSchool'):
"""Recreate the build html folder, under a new gh-pages branch checkout"""
repo = REPOSITORY % owner
target_dir = path.join(BUILD_DIR, MAIN_TARGET)
run('mkdir -p %s' % target_dir)
run('git clone %s -b %s --single-branch %s' %
(repo, 'gh-pages', target_dir))
@task
def build(language=None, target=MAIN_TARGET):
"""Build the sphinx project for a specific language"""
if language is None:
print('Please build a specific language; one of: %s' %
', '.join(LANGUAGES))
print('e.g: invoke build -l en')
exit()
elif language not in LANGUAGES:
exit('Language %s not available.' % language)
args = [
'sphinx-build',
'-b', target, # builder type
'-d', path.join(BUILD_DIR, 'doctrees'), # doctree path
'-D', 'language=%s' % language, # language
SOURCE_DIR,
path.join(BUILD_DIR, target, language), # output path
]
run(' '.join(args))
if 'html' in target:
static_files = path.join(BASE_DIR, '_static', '*')
target_dir = path.join(BUILD_DIR, target)
run('cp %s %s' % (static_files, target_dir))
@task
def serve(port=SERVE_PORT, serve_dir=None):
"""Run a web server to serve the built project"""
if serve_dir is None:
serve_dir = path.join(BUILD_DIR, MAIN_TARGET)
port = int(port)
chdir(serve_dir)
handler = httpserver.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", port), handler)
print("serving on http://%s:%s" % httpd.server_address)
httpd.serve_forever()