• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Copyright 2016 Google Inc
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
33README_TMPL = jinja_env.get_template('README.tmpl.rst')
34
35
36def get_help(file):
37    return subprocess.check_output(['python', file, '--help']).decode()
38
39
40def main():
41    parser = argparse.ArgumentParser()
42    parser.add_argument('source')
43    parser.add_argument('--destination', default='README.rst')
44
45    args = parser.parse_args()
46
47    source = os.path.abspath(args.source)
48    root = os.path.dirname(source)
49    destination = os.path.join(root, args.destination)
50
51    jinja_env.globals['get_help'] = get_help
52
53    with io.open(source, 'r') as f:
54        config = yaml.load(f)
55
56    # This allows get_help to execute in the right directory.
57    os.chdir(root)
58
59    output = README_TMPL.render(config)
60
61    with io.open(destination, 'w') as f:
62        f.write(output)
63
64
65if __name__ == '__main__':
66    main()
67