Skip to content

Commit

Permalink
Added dontrun_r_examples.py to R build workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkraljevic committed Aug 17, 2014
1 parent 9a64b6c commit b3f806f
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions R/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
153 changes: 153 additions & 0 deletions scripts/dontrun_r_examples.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit b3f806f

Please sign in to comment.