Debian Codegraph @

cmake/tribits/python_utils/GenerateDocUtilsOutput.py

raw
1 import sys
2 import os
3 import stat
4 import subprocess
5
6 #
7 # A) Set up basic paths and import modules
8 #
9
10 from GeneralScriptSupport import *
11
12
13 #
14 # B) Define some helper functions
15 #
16
17
18 def openWriteFilePermissions(filePath):
19 if os.path.exists(filePath):
20 os.chmod(filePath, stat.S_IREAD | stat.S_IWRITE \
21 | stat.S_IRGRP | stat.S_IWGRP)
22
23
24 def setGeneratedFilePermissions(filePath):
25 os.chmod(filePath, stat.S_IREAD | stat.S_IRGRP | stat.S_IROTH)
26
27
28 def generateFile(filePath, generateCmnd, outFile=None, workingDir="",
29 runTwice=False, echoCmnds=False \
30 ):
31 openWriteFilePermissions(filePath)
32 runSysCmnd(generateCmnd, outFile=outFile, workingDir=workingDir,
33 echoCmndForDebugging=echoCmnds)
34 if runTwice:
35 runSysCmnd(generateCmnd, outFile=outFile, workingDir=workingDir,
36 echoCmndForDebugging=echoCmnds)
37 setGeneratedFilePermissions(filePath)
38
39
40 def addCmndLineOptions(clp):
41
42 # Find the right default for the current system
43 rst2html = "rst2html"
44 rst2latex = "rst2latex"
45 rst2htmlWhich = getCmndOutput("which rst2html", True, False)
46 if rst2htmlWhich == "" or re.match(".+no rst2html.+", rst2htmlWhich):
47 rst2html = rst2html+".py"
48 rst2latex = rst2latex+".py"
49
50 clp.add_option(
51 "--file-base", dest="fileBase", type="string",
52 default="",
53 help="Base name for the reStructuredText *.rst file. This may include the" \
54 " relative or absolute path up to but not including the '.rst' extension." \
55 " [Required]"
56 )
57
58 clp.add_option(
59 "--generate-html", dest="generateHtml", type="string",
60 help="Generate the HTML output file using provided script (i.e. rst2html)" \
61 " [Default '"+rst2html+"']",
62 default=rst2html )
63
64 clp.add_option(
65 "--generate-latex", dest="generateLatex", type="string",
66 help="Generate the Latex (*.tex) output file using provided script" \
67 " (i.e. rst2latex) [Default '"+rst2latex+"']",
68 default=rst2latex )
69
70 clp.add_option(
71 "--generate-latex-options", dest="generateLatexOptions", type="string",
72 help="Options to pass to the generate latex command",
73 default="" )
74
75 clp.add_option(
76 "--generate-pdf", dest="generatePDF", type="string",
77 help="Generate the PDF output file from the latex file using provided" \
78 " script (i.e. pdflatex) [Default 'pdflatex']",
79 default="pdflatex" )
80
81 clp.add_option(
82 "--clean-temp-files", dest="cleanTempFiles", action="store_true",
83 help="Clean temporary files used in generation. [default]" )
84 clp.add_option(
85 "--no-clean-temp-files", dest="cleanTempFiles", action="store_false",
86 help="Do not delete temporary files.",
87 default=True )
88
89 clp.add_option(
90 "--echo-cmnds", dest="echoCmnds", action="store_true",
91 help="Echo the commands being run to STDOUT." )
92 clp.add_option(
93 "--no-echo-cmnds", dest="echoCmnds", action="store_false",
94 help="Do not echo the commands being run.",
95 default=False )
96
97
98 def generateDocutilsOuputFiles(options):
99
100 filesToClean = []
101
102 # Base name including path (must just be relative)
103 outputFileBase = options.fileBase
104 # The path of the rst file:
105 rstFile = outputFileBase+".rst"
106 # Just the base name
107 outputFileBaseName = os.path.basename(outputFileBase)
108
109 if options.generateHtml:
110 print("Generating " + outputFileBaseName + ".html ...")
111 outputHtmlFile = outputFileBase+".html"
112 generateFile(outputHtmlFile,
113 options.generateHtml+" "+rstFile+" "+outputHtmlFile, echoCmnds=options.echoCmnds)
114
115 if options.generateLatex:
116 print("Generating " + outputFileBaseName + ".tex ...")
117 outputLatexFile = outputFileBase+".tex"
118 runSysCmnd(options.generateLatex+" "+options.generateLatexOptions+ \
119 " "+rstFile+" "+outputLatexFile, echoCmndForDebugging=options.echoCmnds)
120 if options.generatePDF:
121 print("Generating " + outputFileBaseName + ".pdf ...")
122 outputPdfFile = outputFileBase+".pdf"
123 outputPdfFileLog = outputLatexFile+".log"
124 generateFile(outputPdfFile,
125 options.generatePDF+" -halt-on-error "+outputLatexFile,
126 outFile=outputPdfFileLog,
127 runTwice=True, echoCmnds=options.echoCmnds)
128 filesToClean.append(outputPdfFileLog)
129
130 #
131 # Clean the intermediate files
132 #
133
134 if options.cleanTempFiles:
135
136 print("Cleaning intermediate files ...")
137
138 filesToClean.extend(
139 [
140 outputFileBase+".aux",
141 outputFileBase+".log",
142 outputFileBase+".out",
143 outputFileBase+".tex",
144 outputFileBase+".toc",
145 ]
146 )
147
148 for tempFile in filesToClean:
149 if os.path.exists(tempFile):
150 runSysCmnd("rm "+tempFile)
151
152 else:
153
154 print("Keeping temp files ...")