-
Notifications
You must be signed in to change notification settings - Fork 262
/
fabfile.py
118 lines (102 loc) · 3.48 KB
/
fabfile.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
from fabric.api import local
import os
import sys
if sys.version_info[0] < 3:
import SimpleHTTPServer as httpserver
import SocketServer as socketserver
else:
import http.server as httpserver
import socketserver
BASE_DIR = os.path.realpath(os.path.dirname(__file__))
BUILD_DIR = os.path.join(BASE_DIR, '_build')
SOURCE_DIR = os.path.join(BASE_DIR, 'source')
LOCALE_DIR = os.path.join(SOURCE_DIR, 'locale',
'%s', 'LC_MESSAGES')
LANGUAGES = set(['en', 'de', 'ru', 'ko', 'es_CL', 'ro'])
MAIN_TARGET = 'html'
REPOSITORY = 'git@github.com:OpenTechSchool/python-beginners.git'
SERVE_PORT = 8000
def setup():
"""Setup html build directory to push to repo"""
clean()
target_dir = os.path.join(BUILD_DIR, MAIN_TARGET)
local('mkdir -p %s' % target_dir)
local('git clone %s -b %s --single-branch %s' %
(REPOSITORY, 'gh-pages', target_dir))
def build(language=None, target=MAIN_TARGET):
if language is None:
print('Please build a specific language; one of')
print(', '.join(LANGUAGES))
exit()
elif language not in LANGUAGES:
exit('Language %s not available.' % language)
if os.path.isdir(LOCALE_DIR % language):
compile_pos(language)
args = [
'sphinx-build',
'-b', # builder type
target,
'-d', # doctree path
os.path.join(BUILD_DIR, 'doctrees'),
'-D',
'language=%s' % language,
SOURCE_DIR,
os.path.join(BUILD_DIR, target, language), # output path
]
local(' '.join(args))
if 'html' in target:
static_files = os.path.join(BASE_DIR, '_static', '*')
target_dir = os.path.join(BUILD_DIR, target)
local('cp %s %s' % (static_files, target_dir))
print("build finished; the %s files are in %s." %
(target, os.path.join(BUILD_DIR, target, language)))
def clean(language=None, target=MAIN_TARGET):
if language is not None:
local('rm -rf %s' % os.path.join(BUILD_DIR, target, language))
else:
local('rm -rf %s' % os.path.join(BUILD_DIR, target))
def serve(port=SERVE_PORT, serve_dir=None):
"""Run a web server to serve the built project"""
if serve_dir is None:
serve_dir = os.path.join(BUILD_DIR, MAIN_TARGET)
port = int(port)
os.chdir(serve_dir)
handler = httpserver.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", port), handler)
print("serving on http://%s:%s" % httpd.server_address)
httpd.serve_forever()
def update_pos(language):
"""Update .po files from the source pot files"""
if language not in LANGUAGES:
exit('Language %s not available.' % language)
gen_pots(language)
args = [
'sphinx-intl update',
'-l %s ' % language,
'-p',
os.path.join(BUILD_DIR, 'locale', language),
'-c',
os.path.join(SOURCE_DIR, 'conf.py'),
]
local(' '.join(args))
def compile_pos(language):
"""Compile .po files into .mo files"""
if language not in LANGUAGES:
exit('Language %s not available.' % language)
args = [
'sphinx-intl build',
'-l %s' % language,
# '-c',
# os.path.join(SOURCE_DIR, 'conf.py'),
]
local(' '.join(args))
def gen_pots(language='en'):
"""Generate .pot templates from sphinx source files"""
args = [
'sphinx-build',
'-b gettext',
'-D language=%s' % language,
SOURCE_DIR,
os.path.join(BUILD_DIR, 'locale', language),
]
local(' '.join(args))