• Home
Name Date Size #Lines LOC

..--

templates/04-Jul-2025-178116

readme_gen.pyD04-Jul-20251.7 KiB7034

readme_gen.py

1#!/usr/bin/env python
2
3# Copyright 2024 Google LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Generates READMEs using configuration defined in yaml."""
18
19import argparse
20import io
21import os
22import subprocess
23
24import jinja2
25import yaml
26
27
28jinja_env = jinja2.Environment(
29    trim_blocks=True,
30    loader=jinja2.FileSystemLoader(
31        os.path.abspath(os.path.join(os.path.dirname(__file__), "templates"))
32    ),
33    autoescape=True,
34)
35
36README_TMPL = jinja_env.get_template("README.tmpl.rst")
37
38
39def get_help(file):
40    return subprocess.check_output(["python", file, "--help"]).decode()
41
42
43def main():
44    parser = argparse.ArgumentParser()
45    parser.add_argument("source")
46    parser.add_argument("--destination", default="README.rst")
47
48    args = parser.parse_args()
49
50    source = os.path.abspath(args.source)
51    root = os.path.dirname(source)
52    destination = os.path.join(root, args.destination)
53
54    jinja_env.globals["get_help"] = get_help
55
56    with io.open(source, "r") as f:
57        config = yaml.load(f)
58
59    # This allows get_help to execute in the right directory.
60    os.chdir(root)
61
62    output = README_TMPL.render(config)
63
64    with io.open(destination, "w") as f:
65        f.write(output)
66
67
68if __name__ == "__main__":
69    main()
70