-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DocGen.py updated
- Loading branch information
Foo
committed
Feb 10, 2024
1 parent
2e95113
commit 412b385
Showing
11 changed files
with
251 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,110 @@ | ||
from FileHandler import * | ||
import os | ||
import subprocess | ||
import shutil | ||
|
||
# clean up | ||
shutil.rmtree("./latex", ignore_errors=True) | ||
shutil.rmtree("./html", ignore_errors=True) | ||
|
||
# run doxygen | ||
os.system("\"C:\\Program Files\\doxygen\\bin\\doxygen\" doxy_config") | ||
|
||
# modify latex src | ||
texHandler = FileHandler("./latex/refman.tex") | ||
texHandler.addAfter("\\renewcommand{\\numberline}[1]{#1~}" , FileHandler("./src/packages.tex").getContent(), 1) | ||
texHandler.addAfter("%--- Begin generated contents ---" , FileHandler("./src/additional_Sections.tex").getContent(), 1) | ||
texHandler.addBefore("\\end{document}", "\\bibliography{../src/ref}{}" + "\n" + "\\bibliographystyle{plain}", 1) | ||
texHandler.reprint("./latex/refman.tex") | ||
|
||
# modify compile latex script | ||
docGenHandler = FileHandler("./latex/make.bat") | ||
docGenHandler.addBefore("setlocal enabledelayedexpansion", "bibtex refman") | ||
docGenHandler.replaceLine("set count=8", "set count=4") | ||
docGenHandler.addBefore("cd /D %Dir_Old%", "COPY refman.pdf \"../MT-RRT.pdf\"") | ||
docGenHandler.replaceLine("cd /D %Dir_Old%", "") | ||
docGenHandler.replaceLine("set Dir_Old=", "") | ||
docGenHandler.reprint("./latex/make.bat") | ||
|
||
# generate pdf | ||
subprocess.call([r'.\\latex\\make.bat']) | ||
|
||
# clean up | ||
shutil.rmtree("./latex", ignore_errors=True) | ||
shutil.rmtree("./html", ignore_errors=True) | ||
shutil.rmtree("__pycache__", ignore_errors=True) | ||
import os, subprocess, shutil | ||
|
||
class FileHandler: | ||
def __init__(self, fileName=None): | ||
self.src = fileName | ||
with open(fileName, 'r') as stream: | ||
self.contents = [line.strip() for line in stream.readlines()] | ||
|
||
def getContent(self): | ||
return '\n'.join(self.contents) | ||
|
||
def reprint(self, fileName = None): | ||
with open(self.src if fileName == None else fileName, 'w') as stream: | ||
stream.write('\n'.join(self.contents)) | ||
|
||
def replace(self, toReplace, toPut): | ||
self.contents = [line.replace(toReplace, toPut) for line in self.contents] | ||
return self | ||
|
||
def replaceLine(self, involvedLine, toPut, instances = None): | ||
for index in self.findLines_(involvedLine, instances): | ||
self.contents[index] = toPut | ||
return self | ||
|
||
def addBefore(self, involvedLine, toPut, instances = None): | ||
added = 0 | ||
for index in self.findLines_(involvedLine, instances): | ||
self.contents.insert(index + added, toPut) | ||
added += 1 | ||
return self | ||
|
||
def addAfter(self, involvedLine, toPut, instances = None): | ||
added = 0 | ||
for index in self.findLines_(involvedLine, instances): | ||
self.contents.insert(index + added + 1, toPut) | ||
added += 1 | ||
return self | ||
|
||
def findLines_(self, line, max_instances = None): | ||
indices = [] | ||
k = 0 | ||
for content in self.contents: | ||
if content == line: | ||
indices.append(k) | ||
if not max_instances == None and len(indices) == max_instances: | ||
break | ||
k += 1 | ||
return indices | ||
|
||
class Paths: | ||
THIS_FILE_PARENT = os.path.dirname(__file__) | ||
ROOT = os.path.dirname(THIS_FILE_PARENT) | ||
|
||
@staticmethod | ||
def make(*args, fromRoot= False): | ||
res = Paths.ROOT if fromRoot else Paths.THIS_FILE_PARENT | ||
for piece in args: | ||
res = os.path.join(res, piece) | ||
return res | ||
|
||
def run(cmd, show = False, cwd = None): | ||
print('running {}'.format(' '.join(cmd))) | ||
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, text=True) | ||
if show: | ||
print(res.stdout) | ||
if len(res.stderr) != 0: | ||
print(res.stderr) | ||
res.check_returncode() | ||
|
||
def forEachSubFolder(parent): | ||
for name in os.listdir(parent): | ||
name_abs = os.path.join(parent, name) | ||
if os.path.isdir(name_abs): | ||
yield name_abs | ||
|
||
class BuildFolder: | ||
def __init__(self): | ||
self.root = Paths.make('build') | ||
shutil.rmtree(self.root, ignore_errors=True) # clean up | ||
os.makedirs(self.root) | ||
self.latex = Paths.make('build', 'latex') | ||
self.html = Paths.make('build', 'html') | ||
self.doxy_config = os.path.join(self.root, 'doxy_config') | ||
shutil.copy(Paths.make('doxy_config'), self.doxy_config) | ||
shutil.copytree(Paths.make('src'), os.path.join(self.root, 'src')) | ||
src = [os.path.join(folder, 'header', 'MT-RRT') for folder in forEachSubFolder(Paths.make('src', fromRoot=True)) ] | ||
print('Identified sources:\n{}'.format('\n'.join(src))) | ||
FileHandler(self.doxy_config).replace('$THE_SOURCES', ' '.join(src)).reprint() | ||
|
||
def main(): | ||
build_folder = BuildFolder() | ||
|
||
run(['doxygen', 'doxy_config'], cwd=build_folder.root) | ||
|
||
# modify latex src | ||
texHandler = FileHandler(os.path.join(build_folder.latex, 'refman.tex')) | ||
texHandler.addAfter("\\renewcommand{\\numberline}[1]{#1~}" , FileHandler(Paths.make('src/packages.tex')).getContent(), 1) | ||
texHandler.addAfter("%--- Begin generated contents ---" , FileHandler(Paths.make("src/additional_Sections.tex")).getContent(), 1) | ||
texHandler.addBefore("\\end{document}", "\\bibliography{../src/ref}{}" + "\n" + "\\bibliographystyle{plain}", 1) | ||
texHandler.reprint() | ||
|
||
# compile | ||
run(['pdflatex', 'refman'], cwd=build_folder.latex) | ||
run(['bibtex', 'refman'], cwd=build_folder.latex) | ||
for _ in range(0, 3): | ||
run(['pdflatex', 'refman'], cwd=build_folder.latex) | ||
shutil.copy(os.path.join(build_folder.latex, 'refman.pdf'), Paths.make('MT-RRT.pdf')) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <iostream> | ||
#include <sstream> | ||
#include <stdexcept> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string> | ||
|
||
#if _WIN64 || _WIN32 | ||
#include <windows.h> | ||
#endif | ||
|
||
void setUpEnv() { | ||
std::stringstream ss(ENV); | ||
std::string token; | ||
while (ss >> token) { | ||
std::size_t sep = token.find('='); | ||
std::string key = std::string{token, 0, sep}; | ||
std::string val = std::string{token, sep + 1}; | ||
#if _WIN64 || _WIN32 | ||
SetEnvironmentVariable(key.c_str(), val.c_str()); | ||
#elif __linux__ | ||
setenv(key.c_str(), val.c_str(), 1); | ||
#endif | ||
} | ||
} | ||
|
||
struct Command { | ||
template <typename Arg> | ||
static void addArg_(std::stringstream &buff, Arg &&arg) { | ||
buff << arg; | ||
} | ||
|
||
template <typename... Args> | ||
Command(const std::string &executable, Args &&...args) | ||
: executable_{executable} { | ||
std::stringstream buff; | ||
(this->addArg_<Args>(buff, std::forward<Args>(args)), ...); | ||
args_ = buff.str(); | ||
} | ||
|
||
std::string executable_; | ||
std::string args_; | ||
|
||
void run() const { | ||
static const std::size_t BUFFER_SIZE = 128; | ||
|
||
std::string cmd = executable_ + " " + args_; | ||
|
||
std::cout << "running `" << cmd << '`' << std::endl << std::endl; | ||
|
||
auto throw_exc = [&cmd]() { | ||
std::stringstream msg; | ||
msg << "Something went wrong running `" << cmd << '`'; | ||
throw std::runtime_error{msg.str()}; | ||
}; | ||
|
||
std::string buffer_str; | ||
buffer_str.resize(BUFFER_SIZE); | ||
FILE *fp = | ||
#if _WIN64 || _WIN32 | ||
_popen(cmd.c_str(), "r") | ||
#elif __linux__ | ||
popen(cmd.c_str(), "r") | ||
#endif | ||
; | ||
if (fp == NULL) { | ||
throw_exc(); | ||
} | ||
|
||
while (fgets(buffer_str.data(), BUFFER_SIZE, fp) != NULL) { | ||
//std::cout << buffer_str; | ||
} | ||
|
||
#if _WIN64 || _WIN32 | ||
feof(fp); | ||
#endif | ||
|
||
int return_code = | ||
#if _WIN64 || _WIN32 | ||
_pclose(fp) | ||
#elif __linux__ | ||
pclose(fp) | ||
#endif | ||
; | ||
if (return_code != 0) { | ||
throw_exc(); | ||
} | ||
} | ||
}; | ||
|
||
int main() { | ||
setUpEnv(); | ||
|
||
Command{BIN_PATH, ARGS}.run(); | ||
Command{PYTHON_CMD, SCRIPT, ARGS_SCRIPT}.run(); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.