|
| 1 | +#!/usr/bin/python -u |
| 2 | + |
| 3 | +# |
| 4 | +# Recognized tags |
| 5 | +# <EG BEGIN ID="string"> : Start a block |
| 6 | +# <EG END ID="string"> : End a block |
| 7 | +# Rules: |
| 8 | +# - The tag strings, regardless of ID, are removed from the final output |
| 9 | +# - If multiple blocks with the same ID exist then they are concatinated together |
| 10 | +# - ID can contain alphabetic and numberic characters and the following symbols: . _ |
| 11 | +# - Quote marks are stripped out |
| 12 | +# |
| 13 | +import sys |
| 14 | +import os |
| 15 | +import re |
| 16 | +import argparse |
| 17 | +import subprocess |
| 18 | +import shutil |
| 19 | + |
| 20 | +EG_BEGIN_STR="EG BEGIN ID=" |
| 21 | +EG_END_STR="EG END ID=" |
| 22 | +EG_ID_PATTERN="[A-Za-z0-9_\.\"]" |
| 23 | + |
| 24 | + |
| 25 | +class Example: |
| 26 | + """An example from the source code""" |
| 27 | + eid = None |
| 28 | + active = False |
| 29 | + filename = None |
| 30 | + code_block = "" |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + self.eid = "" |
| 34 | + self.active = False |
| 35 | + self.filename = None |
| 36 | + self.code_block = "" |
| 37 | + |
| 38 | + def __str__(self): |
| 39 | + return "in_fname=[%s] id=[%s]" % (self.filename, self.eid) |
| 40 | + |
| 41 | + def get_out_fname(self): |
| 42 | + f_id = self.eid.replace('"', '') |
| 43 | + return os.path.basename(self.filename) + "_" + f_id |
| 44 | + |
| 45 | + def append_line(self, line): |
| 46 | + self.code_block = self.code_block + line |
| 47 | + if line.endswith("\n") is False: |
| 48 | + self.code_block = self.code_block + "\n" |
| 49 | + |
| 50 | + def get_code_block(self): |
| 51 | + final_block = "" |
| 52 | + min_lead_spaces = 10000 |
| 53 | + lines = 0 |
| 54 | + total_lines = 0 |
| 55 | + skip_last_lines = 0 |
| 56 | + |
| 57 | + # First pass to find min spacing |
| 58 | + for line in self.code_block.splitlines(True): |
| 59 | + if re.search(r"\s*"+EG_BEGIN_STR, line) is not None: |
| 60 | + continue |
| 61 | + if re.search(r"\s*"+EG_END_STR, line) is not None: |
| 62 | + continue |
| 63 | + |
| 64 | + total_lines = total_lines + 1 |
| 65 | + # Skip empty lines |
| 66 | + if len(line) <= 1: |
| 67 | + skip_last_lines = skip_last_lines + 1 |
| 68 | + continue |
| 69 | + else: |
| 70 | + skip_last_lines = 0 |
| 71 | + |
| 72 | + m = re.match(r"^([ \t]+)", line) |
| 73 | + if m is not None: |
| 74 | + c_len = len(m.group(1)) |
| 75 | + if c_len > 1: |
| 76 | + min_lead_spaces = min(c_len, min_lead_spaces) |
| 77 | + else: |
| 78 | + # Indicates that there is a line that does not have leading spaces, but is not empty |
| 79 | + min_lead_spaces = 0 |
| 80 | + |
| 81 | + # Next pass to build the string |
| 82 | + lines = 0 |
| 83 | + for line in self.code_block.splitlines(True): |
| 84 | + if re.search(r"\s*"+EG_BEGIN_STR, line) is not None: |
| 85 | + continue |
| 86 | + if re.search(r"\s*"+EG_END_STR, line) is not None: |
| 87 | + continue |
| 88 | + |
| 89 | + # Clear off trailing empty lines |
| 90 | + if total_lines - skip_last_lines == lines: |
| 91 | + break |
| 92 | + lines = lines + 1 |
| 93 | + |
| 94 | + m = re.match(r"^([ \t]+)", line) |
| 95 | + if m is not None: |
| 96 | + line = line[min_lead_spaces:] |
| 97 | + final_block = final_block + line |
| 98 | + |
| 99 | + return final_block |
| 100 | + |
| 101 | + |
| 102 | +def process_file(filename): |
| 103 | + """Process all of the key/value splitting in the file""" |
| 104 | + all_examples = {} |
| 105 | + eg_id = None |
| 106 | + |
| 107 | + with open(filename, 'r') as fd: |
| 108 | + for line in fd: |
| 109 | + line = line.rstrip() |
| 110 | + m = re.search(r"\s*"+EG_BEGIN_STR+"("+EG_ID_PATTERN+"*)", line) |
| 111 | + if m is not None: |
| 112 | + eg_id = m.group(1) |
| 113 | + # Find this object and update it |
| 114 | + found = False |
| 115 | + for example_id in all_examples: |
| 116 | + if example_id == eg_id: |
| 117 | + example = all_examples[example_id] |
| 118 | + example.active = True |
| 119 | + example.append_line(line) |
| 120 | + found = True |
| 121 | + # Insert it if not found |
| 122 | + if found is False: |
| 123 | + x = Example() |
| 124 | + x.eid = eg_id |
| 125 | + x.active = True |
| 126 | + x.append_line(line) |
| 127 | + x.filename = filename |
| 128 | + all_examples[eg_id] = x |
| 129 | + continue |
| 130 | + |
| 131 | + m = re.search(r"\s*"+EG_END_STR+"("+EG_ID_PATTERN+"*)", line) |
| 132 | + if m is not None: |
| 133 | + eg_id = m.group(1) |
| 134 | + # Find this object and update it |
| 135 | + for example_id in all_examples: |
| 136 | + if example_id == eg_id: |
| 137 | + example = all_examples[example_id] |
| 138 | + example.active = False |
| 139 | + example.append_line("") # Add an empty line |
| 140 | + continue |
| 141 | + |
| 142 | + for example_id in all_examples: |
| 143 | + example = all_examples[example_id] |
| 144 | + if example.active is True: |
| 145 | + example.append_line(line) |
| 146 | + |
| 147 | + return all_examples |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + # |
| 152 | + # Command line parsing |
| 153 | + # |
| 154 | + parser = argparse.ArgumentParser(description="PMIx Standard Example Preprocessor") |
| 155 | + parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output") |
| 156 | + parser.add_argument("files", nargs='+', help="List of files to process") |
| 157 | + parser.parse_args() |
| 158 | + args = parser.parse_args() |
| 159 | + |
| 160 | + # |
| 161 | + # Create a temporary directory to store the snippets |
| 162 | + # |
| 163 | + gen_dir = "sources/_autogen_" |
| 164 | + if os.access(gen_dir, os.W_OK) is False: |
| 165 | + os.makedirs(gen_dir) |
| 166 | + |
| 167 | + # |
| 168 | + # Iterate through all examples and split out the snippets |
| 169 | + # |
| 170 | + for f in args.files: |
| 171 | + if os.path.exists(f) is False: |
| 172 | + print("ERROR: File does not exist: %s" % (f)) |
| 173 | + sys.exit(1) |
| 174 | + |
| 175 | + print("Processing File: %s" % (f)) |
| 176 | + example_blocks = process_file(f) |
| 177 | + for k, example in example_blocks.items(): |
| 178 | + out_fname = gen_dir + "/" + example.get_out_fname() |
| 179 | + print("\tExample: %s -- Stored in %s" % (example, out_fname)) |
| 180 | + if args.verbose: |
| 181 | + print("CODE BLOCK") |
| 182 | + print("-" * 50) |
| 183 | + print(example.get_code_block()), |
| 184 | + print("-" * 50) |
| 185 | + with open(out_fname, 'w') as fd: |
| 186 | + fd.write("%s" % (example.get_code_block())) |
| 187 | + |
| 188 | + sys.exit(0) |
0 commit comments