1
1
#!/usr/bin/env python
2
2
"""
3
- build docs for specified MAJOR, MINOR version
4
- and specified document (optional), format (optional)
5
- when document and format not specified, builds all documents in all formats.
6
-
7
- arg 1: MAJOR version number
8
- arg 2: MINOR version number
9
- optional arg 3: specified document or "all"
10
- optional arg 4: specified format or "all"
3
+ Build docs website and/or pdfs.
4
+
5
+ Command line arguments:
6
+
7
+ arg 1: MAJOR version number
8
+ arg 2: MINOR version number
9
+ optional arg 3: format, either "pdf" or "website"; defaults to both
10
+ optional arg 4: pdf doc, defaults to "all", ignored if arg3 is "website"
11
+
12
+ Quarto builds in subdirectory of `src`, and then
13
+ resulting files are moved to directory named `docs/MAJOR_MINOR`
14
+ Document pdf has subtitle "Version MAJOR dot MINOR".
15
+ Directory and filenames have version string "MAJOR underscore MINOR".
11
16
"""
12
17
13
18
import glob
18
23
import sys
19
24
import contextlib
20
25
21
- all_docs = ("functions-reference" , "reference-manual" , "stan-users-guide" , "cmdstan-guide" )
22
- all_formats = ("html" , "pdf" )
26
+ all_docs = (
27
+ "functions-reference" ,
28
+ "reference-manual" ,
29
+ "stan-users-guide" ,
30
+ "cmdstan-guide" ,
31
+ )
32
+
23
33
24
34
@contextlib .contextmanager
25
35
def pushd (new_dir ):
@@ -28,101 +38,98 @@ def pushd(new_dir):
28
38
yield
29
39
os .chdir (previous_dir )
30
40
41
+
31
42
def shexec (command ):
32
- ret = subprocess .run (command , shell = True , check = False , stdout = subprocess .PIPE , stderr = subprocess .PIPE , universal_newlines = True )
43
+ ret = subprocess .run (
44
+ command ,
45
+ shell = True ,
46
+ check = False ,
47
+ stdout = subprocess .PIPE ,
48
+ stderr = subprocess .PIPE ,
49
+ universal_newlines = True ,
50
+ )
33
51
if ret .returncode != 0 :
34
- print (' Command {} failed' .format (command ))
52
+ print (" Command {} failed" .format (command ))
35
53
print (ret .stderr )
36
54
raise Exception (ret .returncode )
37
55
38
- def check_bookdown_at_least_0_23 ():
39
- command = ["Rscript" , "-e" , "utils::packageVersion(\" bookdown\" ) > 0.22" ]
40
- check = subprocess .run (command , capture_output = True , text = True )
41
- if not 'TRUE' in check .stdout :
42
- print ("R Package bookdown version must be at least 0.23, see README for build tools requirements" )
43
- sys .exit (1 )
44
56
45
57
def safe_rm (fname ):
46
58
if os .path .exists (fname ):
47
59
os .remove (fname )
48
60
49
- def make_docs (docspath , version , document , formats ):
61
+
62
+ def make_pdfs (docspath , version , document ):
50
63
path = os .getcwd ()
51
64
srcpath = os .path .join (path , "src" , document )
52
65
with pushd (srcpath ):
53
- if ("pdf" in formats ):
54
- print ('render {} as pdf' .format (document ))
55
- command = "Rscript -e \" bookdown::render_book(\' index.Rmd\' , output_format=\' bookdown::pdf_book\' )\" "
56
- shexec (command )
57
- srcpdf = os .path .join (srcpath , "_book" , "_main.pdf" )
58
- pdfname = '' .join ([document ,'-' ,version ,".pdf" ])
59
- pdfpath = os .path .join (docspath , pdfname )
60
- shutil .move (srcpdf , pdfpath )
61
- print ('output file: {}' .format (pdfpath ))
62
- if ("html" in formats ):
63
- print ('render {} as html' .format (document ))
64
- command = "Rscript -e \" bookdown::render_book(\' index.Rmd\' , output_format=\' bookdown::gitbook\' )\" "
65
- shexec (command )
66
- [safe_rm (f ) for f in ("stan-manual.css" , "_main.rds" )]
67
- [safe_rm (f ) for f in glob .glob (os .path .join ("_book" ,"*.md" ))]
68
- htmlpath = os .path .join (docspath , document )
69
- shutil .rmtree (htmlpath , ignore_errors = True )
70
- command = ' ' .join (["mv _book" , htmlpath ])
71
- shexec (command )
72
- print ('output dir: {}' .format (htmlpath ))
73
- else :
74
- [safe_rm (f ) for f in ("stan-manual.css" , "_main.rds" )]
75
- shutil .rmtree ("_book" , ignore_errors = True )
66
+ pdfname = "" .join ([document , "-" , version , ".pdf" ])
67
+ print ("render {} as {}" .format (document , pdfname ))
68
+ command = " " .join (["quarto render" , "--output-dir _pdf" , "--output" , pdfname ])
69
+ shexec (command )
70
+ outpath = os .path .join (srcpath , "_pdf" , pdfname )
71
+ safe_rm (os .path .join (docspath , pdfname ))
72
+ shutil .move (outpath , docspath )
73
+
76
74
77
75
def main ():
78
- if sys .version_info < (3 , 7 ):
79
- print (' requires Python 3.7 or higher, found {}' .format (sys .version ))
76
+ if sys .version_info < (3 , 8 ): # required by shutil.copytree
77
+ print (" requires Python 3.8 or higher, found {}" .format (sys .version ))
80
78
sys .exit (1 )
81
- check_bookdown_at_least_0_23 ()
82
79
global all_docs
83
- global all_formats
84
- if (len (sys .argv ) > 2 ):
80
+ build_web = True
81
+ build_pdfs = True
82
+ docset = all_docs
83
+
84
+ if len (sys .argv ) > 2 :
85
85
stan_major = int (sys .argv [1 ])
86
86
stan_minor = int (sys .argv [2 ])
87
87
else :
88
- print ("Expecting arguments MAJOR MINOR version numbers " )
88
+ print ("Expecting version number args MAJOR MINOR " )
89
89
sys .exit (1 )
90
90
91
- stan_version = '_' .join ([str (stan_major ), str (stan_minor )])
91
+ stan_version = "_" .join ([str (stan_major ), str (stan_minor )])
92
92
path = os .getcwd ()
93
93
docspath = os .path .join (path , "docs" , stan_version )
94
- if ( not (os .path .exists (docspath ) )):
94
+ if not (os .path .exists (docspath )):
95
95
try :
96
96
os .makedirs (docspath )
97
97
except OSError :
98
- print ("Creation of the directory %s failed" % docspath )
98
+ print ("Failed to create directory %s" % docspath )
99
+ sys .exit (1 )
99
100
else :
100
- print ("Successfully created the directory %s " % docspath )
101
+ print ("Created directory %s " % docspath )
101
102
102
- docset = all_docs
103
- if (len (sys .argv ) > 3 ):
104
- if (sys .argv [3 ] != "all" ):
105
- if (sys .argv [3 ] not in docset ):
106
- print ("Expecting one of %s" % ' ' .join (docset ))
107
- sys .exit (1 )
108
- docset = (sys .argv [3 ],)
109
-
110
- formats = all_formats
111
- if (len (sys .argv ) > 4 ):
112
- if (sys .argv [4 ] != "all" ):
113
- if (sys .argv [4 ] not in formats ):
114
- print ("Expecting one of %s" % ' ' .join (formats ))
115
- sys .exit (1 )
116
- formats = (sys .argv [4 ],)
117
-
118
- if (len (sys .argv ) > 5 ):
119
- print ("Unused arguments: %s" % ' ' .join (sys .argv [5 : ]))
120
-
121
- # set environmental variable used in the index.Rmd files
122
- os .environ ['STAN_DOCS_VERSION' ] = '.' .join ([str (stan_major ), str (stan_minor )])
123
-
124
- for doc in docset :
125
- make_docs (docspath , stan_version , doc , formats )
103
+ if len (sys .argv ) > 3 :
104
+ if sys .argv [3 ] == "pdf" :
105
+ build_web = False
106
+ elif sys .argv [3 ] == "website" :
107
+ build_pdf = False
108
+ else :
109
+ print ("Bad arg[3], should be 'pdf' or 'website'" .format (sys .argv [3 ]))
110
+ sys .exit (1 )
111
+
112
+ if len (sys .argv ) > 4 :
113
+ if sys .argv [4 ] not in docset :
114
+ print ("Bad arg[4], should be one of %s" % " " .join (docset ))
115
+ sys .exit (1 )
116
+ docset = (sys .argv [4 ],)
117
+
118
+ if len (sys .argv ) > 4 :
119
+ print ("Unused arguments: %s" % " " .join (sys .argv [4 :]))
120
+
121
+ if build_web :
122
+ print ("render website" )
123
+ with pushd (os .path .join (path , "src" )):
124
+ command = "quarto render"
125
+ shexec (command )
126
+ shutil .copytree (
127
+ "_website" , docspath , copy_function = shutil .move , dirs_exist_ok = True
128
+ )
129
+
130
+ if build_pdfs :
131
+ for doc in docset :
132
+ make_pdfs (docspath , stan_version , doc )
126
133
127
134
128
135
if __name__ == "__main__" :
0 commit comments