Commit 89c6f3ff authored by Victor Poughon's avatar Victor Poughon
Browse files

REFAC: cookbook examples: support multiple usage

No related merge requests found
Showing with 28 additions and 29 deletions
+28 -29
...@@ -151,7 +151,6 @@ add_custom_target(generate_examples_rst ...@@ -151,7 +151,6 @@ add_custom_target(generate_examples_rst
# Add all applications as dependencies to rst generation # Add all applications as dependencies to rst generation
set(app_names ${OTB_APPLICATIONS_NAME_LIST}) set(app_names ${OTB_APPLICATIONS_NAME_LIST})
list(REMOVE_ITEM app_names "TestApplication") list(REMOVE_ITEM app_names "TestApplication")
list(REMOVE_ITEM app_names "ApplicationExample")
list(REMOVE_DUPLICATES app_names) list(REMOVE_DUPLICATES app_names)
foreach(app_name ${app_names}) foreach(app_name ${app_names})
add_dependencies(generate_otbapps_rst otbapp_${app_name}) add_dependencies(generate_otbapps_rst otbapp_${app_name})
......
...@@ -26,6 +26,8 @@ import os ...@@ -26,6 +26,8 @@ import os
from os.path import join from os.path import join
import re import re
from rst_utils import examples_usage_regex
def run_example(otb_root, otb_data, name, dry_run): def run_example(otb_root, otb_data, name, dry_run):
""" """
Run an example by name Run an example by name
...@@ -49,27 +51,24 @@ def run_example(otb_root, otb_data, name, dry_run): ...@@ -49,27 +51,24 @@ def run_example(otb_root, otb_data, name, dry_run):
filename = os.path.abspath(sources_files[0]) filename = os.path.abspath(sources_files[0])
# Extract example usage command arguments # Extract example usage command arguments
usage_rx = r"\/\* Example usage:\n\.\/[a-zA-Z]+ (.*?)\*\/" matches = list(re.finditer(examples_usage_regex, open(filename).read(), flags = re.MULTILINE | re.DOTALL))
match = re.search(usage_rx, open(filename).read(), flags = re.MULTILINE | re.DOTALL)
if not match:
raise RuntimeError("Can't find example usage in {}".format(filename))
example_args = match.group(1).replace("\\\n", "").split()
# Make sure Output dir exists if len(matches) == 0:
os.makedirs(join(otb_data, "Output"), exist_ok=True) print("Warning: no usage for example: {}".format(sources_files[0]))
print("$ " + binary + " " + " ".join(example_args))
if dry_run:
return return
# Execute the example with otb_data as working directory, for match in matches:
# because paths are given relative to otb_data in the example usage # Get command line call and print it
subprocess.check_call([binary, *example_args], cwd=otb_data) example_args = match.group(2).replace("\\\n", "").split()
print("$ " + binary + " " + " ".join(example_args))
if not dry_run:
# Make sure Output dir exists
os.makedirs(join(otb_data, "Output"), exist_ok=True)
# TODO handle examples with multiple usage (Examples/BasicFilters/DEMToRainbowExample.cxx) # Execute the example with otb_data as working directory,
# because paths are given relative to otb_data in the example usage
subprocess.check_call([binary, *example_args], cwd=otb_data)
def main(): def main():
parser = argparse.ArgumentParser(usage="Run one or all OTB cxx examples") parser = argparse.ArgumentParser(usage="Run one or all OTB cxx examples")
......
...@@ -27,7 +27,7 @@ from collections import defaultdict ...@@ -27,7 +27,7 @@ from collections import defaultdict
import re import re
import glob import glob
from rst_utils import rst_section, RstPageHeading from rst_utils import rst_section, RstPageHeading, examples_usage_regex
def generate_examples_index(rst_dir, list_of_examples): def generate_examples_index(rst_dir, list_of_examples):
...@@ -72,18 +72,16 @@ def render_example(filename, otb_root): ...@@ -72,18 +72,16 @@ def render_example(filename, otb_root):
examples_license_header = open("templates/examples_license_header.txt").read() examples_license_header = open("templates/examples_license_header.txt").read()
code = code.replace(examples_license_header, "") code = code.replace(examples_license_header, "")
# Extract usage # Extract usages
# TODO handle multiple usage example_usage = ""
rx_usage = r"\/\* Example usage:\n(\.\/[a-zA-Z]+ (.*?))\*\/" usage_matches = list(re.finditer(examples_usage_regex, code, flags = re.MULTILINE | re.DOTALL))
usage_match = re.search(rx_usage, code, flags = re.MULTILINE | re.DOTALL)
if usage_match is None: examples_usage_template = open("templates/example_usage.rst").read()
print("Warning: no usage found for example " + filename) for match in usage_matches:
example_usage = "" example_usage += examples_usage_template.format(indent(match.group(1).strip()))
else:
example_usage = open("templates/example_usage.rst").read().format(indent(usage_match.group(1).strip()))
# Don't show usage in example source # Don't show usage in example source
code = re.sub(rx_usage, "", code, flags = re.MULTILINE | re.DOTALL) code = re.sub(examples_usage_regex, "", code, flags = re.MULTILINE | re.DOTALL)
# Make the link to the source code # Make the link to the source code
link_name = os.path.basename(filename) link_name = os.path.basename(filename)
......
# Regex used to find examples usage
examples_usage_regex = r"\/\* Example usage:\n(\.\/\w+ (.*?))\*\/"
def rst_section(text, delimiter, ref=None): def rst_section(text, delimiter, ref=None):
"Make a rst section title" "Make a rst section title"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment