This repository has been archived by the owner on Jun 9, 2020. It is now read-only.
forked from stan-dev/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
113 lines (97 loc) · 3.61 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
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
#!/usr/bin/env python
"""
build docs for specified MAJOR, MINOR version
and specified document (optional), format (optional)
when document and format not specified, builds all documents in all formats.
arg 1: MAJOR version number
arg 2: MINOR version number
optional arg 3: specified document or "all"
optional arg 4: specified format or "all"
"""
import glob
import os
import os.path
import shutil
import subprocess
import sys
import contextlib
all_docs = ("functions-reference", "reference-manual", "stan-users-guide")
all_formats = ("html", "pdf")
@contextlib.contextmanager
def pushd(new_dir):
previous_dir = os.getcwd()
os.chdir(new_dir)
yield
os.chdir(previous_dir)
def shexec(command):
returncode = subprocess.call(command, shell=True)
if returncode != 0:
print('Command {} failed'.format(command))
raise Exception(returncode)
def safe_rm(fname):
if os.path.exists(fname):
os.remove(fname)
def make_docs(docspath, version, document, formats):
path = os.getcwd()
srcpath = os.path.join(path, "src", document)
with pushd(srcpath):
if ("pdf" in formats):
command = "Rscript -e \"bookdown::render_book(\'index.Rmd\', output_format=\'bookdown::pdf_book\')\""
shexec(command)
srcpdf = os.path.join(srcpath, "_book", "_main.pdf")
pdfname = ''.join([document,'-',version,".pdf"])
pdfpath = os.path.join(docspath, pdfname)
shutil.move(srcpdf, pdfpath)
if ("html" in formats):
command = "Rscript -e \"bookdown::render_book(\'index.Rmd\', output_format=\'bookdown::gitbook\')\""
shexec(command)
[safe_rm(f) for f in ("stan-manual.css", "_main.rds")]
[safe_rm(f) for f in glob.glob(os.path.join("_book","*.md"))]
htmlpath = os.path.join(docspath, document)
shutil.rmtree(htmlpath, ignore_errors=True)
command = ' '.join(["mv _book", htmlpath])
shexec(command)
else:
[safe_rm(f) for f in ("stan-manual.css", "_main.rds")]
shutil.rmtree("_book", ignore_errors=True)
def main():
global all_docs
global all_formats
if (len(sys.argv) > 2):
stan_major = int(sys.argv[1])
stan_minor = int(sys.argv[2])
else:
print("Expecting arguments MAJOR MINOR version numbers")
sys.exit(1)
stan_version = '_'.join([str(stan_major), str(stan_minor)])
path = os.getcwd()
docspath = os.path.join(path, "docs", stan_version)
if (not(os.path.exists(docspath))):
try:
os.makedirs(docspath)
except OSError:
print("Creation of the directory %s failed" % docspath)
else:
print("Successfully created the directory %s " % docspath)
docset = all_docs
if (len(sys.argv) > 3):
if (sys.argv[3] != "all"):
if (sys.argv[3] not in docset):
print("Expecting one of %s" % ' '.join(docset))
sys.exit(1)
docset = (sys.argv[3],)
formats = all_formats
if (len(sys.argv) > 4):
if (sys.argv[4] != "all"):
if (sys.argv[4] not in formats):
print("Expecting one of %s" % ' '.join(formats))
sys.exit(1)
formats = (sys.argv[4],)
if (len(sys.argv) > 5):
print("Unused arguments: %s" % ' '.join(sys.argv[5: ]))
# set environmental variable used in the index.Rmd files
os.environ['STAN_DOCS_VERSION'] = '.'.join([str(stan_major), str(stan_minor)])
for doc in docset:
make_docs(docspath, stan_version, doc, formats)
if __name__ == "__main__":
main()