|
| 1 | +"""Utilities for intermediate files that are generated, but platform-independent |
| 2 | +and configuration-independent. |
| 3 | +""" |
| 4 | + |
| 5 | +# Copyright The Mbed TLS Contributors |
| 6 | +# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 7 | + |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +import subprocess |
| 11 | +import sys |
| 12 | +from typing import Dict, Iterable, List, Sequence, Set |
| 13 | + |
| 14 | + |
| 15 | +class Generator: |
| 16 | + """An abstract base class for generators of intermediate files.""" |
| 17 | + |
| 18 | + def generator_name(self) -> str: |
| 19 | + """A name for this generator. |
| 20 | +
|
| 21 | + Generator names must be unique and should not be identical to |
| 22 | + the name of any target. |
| 23 | + """ |
| 24 | + raise NotImplementedError |
| 25 | + |
| 26 | + def target_files(self) -> List[str]: |
| 27 | + """The list of files targeted by this generator. |
| 28 | +
|
| 29 | + File names are relative to the project root. |
| 30 | + """ |
| 31 | + raise NotImplementedError |
| 32 | + |
| 33 | + def outdated_files(self) -> Iterable[str]: |
| 34 | + """Return the list of targets that are out of date. |
| 35 | +
|
| 36 | + This is empty after running update(). |
| 37 | + Missing targets are considered out of date. |
| 38 | + """ |
| 39 | + raise NotImplementedError |
| 40 | + |
| 41 | + def update(self, always: bool) -> None: |
| 42 | + """Update the target(s) of this generator. |
| 43 | +
|
| 44 | + If always is false, avoid changing the output file if it already has |
| 45 | + the desired content. If always is true, make sure to update the |
| 46 | + time stamp on the output file even if it already has the desired content. |
| 47 | + """ |
| 48 | + raise NotImplementedError |
| 49 | + |
| 50 | + |
| 51 | +class TestDataGenerator(Generator): |
| 52 | + """A test data generator script. |
| 53 | +
|
| 54 | + Even though the test data generator scripts are written in Python, we |
| 55 | + run them as a separate process, because their output depends on the |
| 56 | + program name (they write sys.argv[0] in a comment in the .data file). |
| 57 | + """ |
| 58 | + |
| 59 | + def __init__(self, script: str) -> None: |
| 60 | + """Run the specified test generator to generate files. |
| 61 | +
|
| 62 | + Assume that the script is written in Python and has the command line |
| 63 | + interface of test_data_generation.py. |
| 64 | + """ |
| 65 | + self.script = script |
| 66 | + |
| 67 | + def generator_name(self) -> str: |
| 68 | + return os.path.basename(self.script) |
| 69 | + |
| 70 | + def target_files(self) -> List[str]: |
| 71 | + output = subprocess.check_output([sys.executable, self.script, '--list'], |
| 72 | + encoding='utf-8') |
| 73 | + return output.splitlines() |
| 74 | + |
| 75 | + def outdated_files(self) -> List[str]: |
| 76 | + output = subprocess.check_output([sys.executable, self.script, '--list-outdated'], |
| 77 | + encoding='utf-8') |
| 78 | + return output.splitlines() |
| 79 | + |
| 80 | + def update(self, _always) -> None: |
| 81 | + subprocess.check_call([sys.executable, self.script]) |
| 82 | + |
| 83 | + |
| 84 | +def assemble(available: Iterable[Generator]) -> Dict[str, Generator]: |
| 85 | + """Assemble the generators into a dictionary with both names and targets as keys.""" |
| 86 | + by_ident = {} #type: Dict[str, Generator] |
| 87 | + for generator in available: |
| 88 | + ident = generator.generator_name() |
| 89 | + if ident in by_ident: |
| 90 | + raise Exception(f'Generator conflict: name "{ident}" of {generator} ' |
| 91 | + f'already recorded for {by_ident[ident]}') |
| 92 | + by_ident[ident] = generator |
| 93 | + for ident in generator.target_files(): |
| 94 | + if ident in by_ident: |
| 95 | + raise Exception(f'Generator conflict: target "{ident}" of {generator} ' |
| 96 | + f'already recorded for {by_ident[ident]}') |
| 97 | + by_ident[ident] = generator |
| 98 | + return by_ident |
| 99 | + |
| 100 | +def list_names(available: Iterable[Generator]) -> List[str]: |
| 101 | + """Return the list of generator names.""" |
| 102 | + return sorted(generator.generator_name() for generator in available) |
| 103 | + |
| 104 | +def list_targets(available: Iterable[Generator]) -> List[str]: |
| 105 | + """Return the list of generator targets.""" |
| 106 | + return sorted(target |
| 107 | + for generator in available |
| 108 | + for target in generator.target_files()) |
| 109 | + |
| 110 | +def select(available: Dict[str, Generator], |
| 111 | + wanted: Iterable[str]) -> List[Generator]: |
| 112 | + """Select generators by name or target.""" |
| 113 | + wanted_names = set() #type: Set[str] |
| 114 | + for ident in wanted: |
| 115 | + if ident not in available: |
| 116 | + raise Exception(f'No generator found for {ident}') |
| 117 | + wanted_names.add(ident) |
| 118 | + return [available[name] for name in sorted(wanted_names)] |
| 119 | + |
| 120 | +def main(generators: Sequence[Generator], |
| 121 | + description: str) -> None: |
| 122 | + #pylint: disable=too-many-branches |
| 123 | + """Command line entry point. |
| 124 | + """ |
| 125 | + parser = argparse.ArgumentParser(description=description) |
| 126 | + parser.add_argument('--always-update', '-U', |
| 127 | + action='store_true', |
| 128 | + help=('Update target files unconditionally ' |
| 129 | + '(overrides --update)')) |
| 130 | + parser.add_argument('--list', |
| 131 | + action='store_true', |
| 132 | + help='List generator names and targets and exit') |
| 133 | + parser.add_argument('--list-names', |
| 134 | + action='store_true', |
| 135 | + help='List generator names and exit') |
| 136 | + parser.add_argument('--list-targets', |
| 137 | + action='store_true', |
| 138 | + help='List generator targets and exit') |
| 139 | + parser.add_argument('--update', '-u', |
| 140 | + action='store_true', |
| 141 | + help='Update target files if needed') |
| 142 | + parser.add_argument('--verbose', '-v', |
| 143 | + action='store_true', |
| 144 | + help='Be more verbose') |
| 145 | + parser.add_argument('idents', nargs='*', metavar='NAME|TARGET', |
| 146 | + help='List of generator names or targets (all targets if empty)') |
| 147 | + args = parser.parse_args() |
| 148 | + |
| 149 | + if args.list: |
| 150 | + args.list_names = True |
| 151 | + args.list_targets = True |
| 152 | + if args.list_names: |
| 153 | + for name in list_names(generators): |
| 154 | + print(name) |
| 155 | + if args.list_targets: |
| 156 | + for target in list_targets(generators): |
| 157 | + print(target) |
| 158 | + if args.list_names or args.list_targets: |
| 159 | + return |
| 160 | + |
| 161 | + if args.idents: |
| 162 | + available = assemble(generators) |
| 163 | + wanted = select(available, args.idents) #type: Sequence[Generator] |
| 164 | + else: |
| 165 | + wanted = generators |
| 166 | + if args.update or args.always_update: |
| 167 | + for generator in wanted: |
| 168 | + if args.verbose: |
| 169 | + sys.stderr.write(f'Running generator {generator.generator_name()}...\n') |
| 170 | + generator.update(args.always_update) |
| 171 | + else: |
| 172 | + outdated = [] #type: List[str] |
| 173 | + for generator in wanted: |
| 174 | + if args.verbose: |
| 175 | + sys.stderr.write(f'Checking targets of generator {generator.generator_name()}...\n') |
| 176 | + outdated += generator.outdated_files() |
| 177 | + if outdated: |
| 178 | + sys.stderr.write(f'Some targets are missing or out of date.\n') |
| 179 | + for target in outdated: |
| 180 | + print(target) |
| 181 | + sys.stderr.write(f'Run {sys.argv[0]} -u and commit the result.') |
| 182 | + sys.exit(1) |
0 commit comments