forked from apache/mynewt-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·83 lines (70 loc) · 2.5 KB
/
build.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
#!/usr/bin/env python
import os
import click
import sh
from git import Repo, Git
from mkdocs import config
import fileinput
@click.command()
@click.option('-s', '--site-branch', default='master', help='Use this branch as source for the top level pages')
def build(site_branch):
# make sure there are no local mods outstanding
repo = Repo(os.getcwd())
if repo.is_dirty() or repo.untracked_files:
print "ERROR: Your working directory has outstanding changes."
print "Commit or stash them."
return
mygit = Git(os.getcwd())
cfg = config.load_config()
# sanity check that the version branches exist as named
for version in cfg['extra']['versions']:
print 'Verifying branch %s' % (version['branch'])
mygit.checkout(version['branch'])
# sanity check - only one latest
latest = False
for version in cfg['extra']['versions']:
if not latest and version['latest']:
print 'Latest is %s' % (version['branch'])
latest = True
elif latest and version['latest']:
print 'ERROR: More than one version is latest.'
print 'Only one version can be latest: True.'
print 'Check mkdocs.yml.'
return
mygit.checkout(site_branch)
print "Building site pages from: %s..." % (site_branch)
sh.rm('-rf', 'site')
sh.mkdocs('build', '--clean')
for version in cfg['extra']['versions']:
mygit.checkout(version['branch'])
deployVersion(version)
def deployVersion(version):
mygit.checkout(version['branch'])
buildTo(version['branch'])
if version['latest']:
buildTo('latest')
def buildTo(branch):
print 'Building doc pages for: %s...' % (branch)
branchCfg = config.load_config()
if branchCfg['extra']['version'] != branch:
updateConfigVersion(branch)
sh.mkdocs('build', '--site-dir', 'site/%s' % (branch))
if branchCfg['extra']['version'] != branch:
sh.git('checkout', '--', 'mkdocs.yml')
def updateConfigVersion(branch):
updated = False
for line in fileinput.input('mkdocs.yml', inplace=True):
line = line.rstrip()
if line.find(" version:") == 0:
line = " version: '%s'" % (branch)
updated = True
print line
assert updated, "Could not fix the version field on %s" % (branch)
if __name__ == '__main__':
repo = Repo(os.getcwd())
branch = repo.active_branch
mygit = Git(os.getcwd())
try:
build()
finally:
mygit.checkout(branch.name)