From b3f806f1468e927b06221652816b9d0fd4b3272f Mon Sep 17 00:00:00 2001 From: Tom Kraljevic Date: Sun, 17 Aug 2014 12:48:20 -0700 Subject: [PATCH] Added dontrun_r_examples.py to R build workflow. --- Makefile | 1 + R/Makefile | 1 + scripts/dontrun_r_examples.py | 153 ++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 scripts/dontrun_r_examples.py diff --git a/Makefile b/Makefile index 48ee7fe73e..ae56f13b53 100644 --- a/Makefile +++ b/Makefile @@ -186,6 +186,7 @@ build_rcran: cd target/Rcran && tar zxvf tmp.tar.gz rm -f target/Rcran/tmp.tar.gz rm -f target/Rcran/h2o/inst/java/h2o.jar + cd target/Rcran/h2o && python ../../../scripts/dontrun_r_examples.py cd target/Rcran && tar zcvf $(H2O_R_SOURCE_FILE) h2o rm -fr target/Rcran/h2o diff --git a/R/Makefile b/R/Makefile index be1016a096..f2a993b3d4 100644 --- a/R/Makefile +++ b/R/Makefile @@ -40,6 +40,7 @@ build_rh2o: sed -e 's/SUBST_PROJECT_VERSION/$(PROJECT_VERSION)/' h2o-DESCRIPTION.template > h2o-package/DESCRIPTION sed -e 's/SUBST_PROJECT_VERSION/$(PROJECT_VERSION)/' h2o-package.template > h2o-package/man/h2o-package.Rd echo '$(BUILD_BRANCH)' > h2o-package/inst/branch.txt + echo 'https://github.com/0xdata/h2o' > h2o-package/inst/source_code_repository_info.txt ifeq ($(PDFLATEX),) @echo pdflatex not found, skipping pdf generation... else diff --git a/scripts/dontrun_r_examples.py b/scripts/dontrun_r_examples.py new file mode 100644 index 0000000000..ac1d79600e --- /dev/null +++ b/scripts/dontrun_r_examples.py @@ -0,0 +1,153 @@ +#!/usr/bin/python + +# +# This tool goes through every file in the 'man' directory and automatically makes the example \dontrun. +# + +import sys +import os +import re +import shutil + + +STATE_NONE = 1 +STATE_IN_EXAMPLES = 2 +STATE_IN_DONTRUN = 3 + + +class Example: + def __init__(self, dir_name, file_name, new_dir_name): + self.dir_name = dir_name + self.file_name = file_name + self.new_dir_name = new_dir_name + self.lineno = 0 + self.state = STATE_NONE + self.of = None + + def parse_error(self, message): + print("ERROR " + message + " " + self.file_name + " line " + str(self.lineno)) + sys.exit(1) + + def set_state(self, new_state): + self.state = new_state + # print("state set to " + str(self.state)) + + def emit_line(self, s): + self.of.write(s) + + def inject_line(self, s): + s2 = s + # s2 = s2 + " # injected" + s2 = s2 + "\n" + self.emit_line(s2) + + def process(self): + # print("Processing " + self.file_name + "...") + + self.set_state(STATE_NONE) + + examples_lines = 0 + found_examples = False + injected_dontrun = False + found_dontrun = False + found_dontrun_closebrace = False + + f = open(os.path.join(self.dir_name, self.file_name), "r") + self.of = open(os.path.join(self.new_dir_name, self.file_name), "w") + + s = f.readline() + while (len(s) > 0): + self.lineno = self.lineno + 1 + + if (self.state == STATE_IN_EXAMPLES): + examples_lines = examples_lines + 1 + + match_groups = re.search(r"^\\examples{", s) + if (match_groups is not None): + if (self.state == STATE_IN_EXAMPLES): + self.parse_error("examples may not be in examples") + + self.set_state(STATE_IN_EXAMPLES) + found_examples = True + + self.emit_line(s) + s = f.readline() + continue + + match_groups = re.search(r"^\\dontrun{", s) + if (match_groups is not None): + if (self.state != STATE_IN_EXAMPLES): + self.parse_error("dontrun must be in examples") + + #if (examples_lines != 1): + # self.parse_error("dontrun must be on the first line in examples") + + if (found_dontrun): + self.parse_error("only one dontrun section is supported") + + if (injected_dontrun): + self.inject_line("}") + injected_dontrun = False + + self.set_state(STATE_IN_DONTRUN) + found_dontrun = True + + self.emit_line(s) + s = f.readline() + continue + + match_groups = re.search(r"^}", s) + if (found_examples and (match_groups is not None)): + if (self.state == STATE_IN_EXAMPLES): + if (injected_dontrun): + self.inject_line("}") + injected_dontrun = False + self.set_state(STATE_NONE) + elif (self.state == STATE_IN_DONTRUN): + self.set_state(STATE_IN_EXAMPLES) + found_dontrun_closebrace = True + else: + self.parse_error("unaccounted for close brace") + sys.exit(1) + + self.emit_line(s) + s = f.readline() + continue + + if (found_dontrun_closebrace): + self.parse_error("extra stuff after dontrun close brace") + + if ((self.state == STATE_IN_EXAMPLES) and not injected_dontrun and not found_dontrun): + self.inject_line("\dontrun{") + injected_dontrun = True + + self.emit_line(s) + s = f.readline() + continue + + f.close() + self.of.close() + + # if (not found_examples): + # self.parse_error("did not find examples") + + +def main(argv): + if (not os.path.exists("DESCRIPTION")): + print("ERROR: You must run this script inside the generated R package source directory.") + sys.exit(1) + + os.mkdir("newman") + + for root, dirs, files in os.walk("man"): + for f in files: + ex = Example("man", f, "newman") + ex.process() + + # os.rename("man", "oldman") + shutil.rmtree("man") + os.rename("newman", "man") + + +if __name__ == "__main__": + main(sys.argv)