gen_tmpl.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python3
  2. """Generate a file from a template, substituting the provided key/value pairs.
  3. The file format should match Python's `string.Template` substitution rules:
  4. - `$$` for a literal `$`
  5. - `$identifier` for some key `identifier` to be substituted
  6. - `${identifier}` when adjacent text would be interpreted as part of the
  7. identifier.
  8. The keys must be strings that are valid identifiers: `[_A-Za-z][_A-Za-z0-9]*`
  9. The values may not contain newlines or any vertical whitespace.
  10. The initial key/value pairs are read from the command line using repeated
  11. `--substitute=KEY=DEFAULT-VALUE` flags.
  12. Updated values for those keys will be read from any files provided to the
  13. `--status-file` flag. This flag can be given multiple times and the values will
  14. be read and updated from the files in order, meaning the last file's value will
  15. win. New keys are never read from these files. The file format parsed is Bazel's
  16. [status file format](https://bazel.build/docs/user-manual#workspace-status):
  17. each line is a single entry starting with a key using only characters `[_A-Z]`,
  18. one space character, and the rest of the line is the value. To assist with using
  19. Bazel status files, if the key parsed from the file begins with `STABLE_`, that
  20. prefix is removed. Any keys which are present in the substitutions provided on
  21. the command line will have their value updated with the string read from the
  22. file.
  23. """
  24. __copyright__ = """
  25. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  26. Exceptions. See /LICENSE for license information.
  27. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  28. """
  29. import argparse
  30. import sys
  31. from pathlib import Path
  32. from string import Template
  33. def main() -> None:
  34. parser = argparse.ArgumentParser(__doc__)
  35. parser.add_argument(
  36. "--template",
  37. metavar="FILE",
  38. type=Path,
  39. required=True,
  40. help="The template source file to use.",
  41. )
  42. parser.add_argument(
  43. "--output",
  44. metavar="FILE",
  45. type=Path,
  46. required=True,
  47. help="The output source file to produce.",
  48. )
  49. parser.add_argument(
  50. "--substitution",
  51. metavar="KEY=DEFAULT-VALUE",
  52. action="append",
  53. help="A substitution that should be supported and its default value.",
  54. )
  55. parser.add_argument(
  56. "--status-file",
  57. metavar="FILE",
  58. type=Path,
  59. action="append",
  60. default=[],
  61. help="A file of key/value updates in Bazel's status file format.",
  62. )
  63. parser.add_argument("-v", "--verbose", action="store_true")
  64. args = parser.parse_args()
  65. # Collect the supported substitutions from the command line.
  66. substitutions = {}
  67. for substitution_arg in args.substitution:
  68. key, value = substitution_arg.split("=", 1)
  69. substitutions.update({key: value})
  70. # Read either of the two status files provided to build up substitutions,
  71. # with the stable file last so its values override any duplicates.
  72. for status_file in args.status_file:
  73. if args.verbose:
  74. print(f"Reading status file: {status_file}", file=sys.stderr)
  75. for line in status_file.open():
  76. # Remove line endings.
  77. line = line.rstrip("\r\n")
  78. # Exactly matches our pattern
  79. (key, value) = line.split(" ", 1)
  80. key = key.removeprefix("STABLE_")
  81. if key in substitutions:
  82. if args.verbose:
  83. print(f"Parsed: '{key}': '{value}'", file=sys.stderr)
  84. substitutions.update({key: value})
  85. if args.verbose:
  86. print(f"Reading template file: {args.template}", file=sys.stderr)
  87. with open(args.template) as template_file:
  88. template = template_file.read()
  89. result = Template(template).substitute(substitutions)
  90. if args.verbose:
  91. print(f"Writing output file: {args.output}", file=sys.stderr)
  92. with open(args.output, mode="w") as output_file:
  93. output_file.write(result)
  94. if __name__ == "__main__":
  95. main()