Debian Codegraph @

tools/fiddle/make_all_examples_cpp.py

raw
1 #!/usr/bin/env python3
2 # Copyright 2019 Google LLC.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5 """Run this script to re-generate the `all_examples.cpp` file after adding or
6 deleting example fiddles.
7 """
8 import argparse
9 import difflib
10 import glob
11 import os
12
13 ALL_EXAMPLES = 'all_examples.cpp'
14
15
16 parser = argparse.ArgumentParser()
17 parser.add_argument(
18 '--print-diff',
19 action='store_true',
20 help='Print the diff between the old and new `all_examples.cpp` file.',
21 )
22 args = parser.parse_args()
23
24
25 os.chdir(os.path.dirname(__file__))
26
27 with open(ALL_EXAMPLES, 'r') as o:
28 prev_lines = o.readlines()
29
30 with open(ALL_EXAMPLES, 'w+') as o:
31 o.write(
32 '// Copyright 2019 Google LLC.\n// Use of this source code is '
33 'governed by a BSD-style license that can be found in the '
34 'LICENSE file.\n'
35 )
36 for path in sorted(glob.glob('../../docs/examples/*.cpp')):
37 # strip ../../
38 path = path[6:]
39 o.write('#include "%s"\n' % path)
40
41 if args.print_diff:
42 with open(ALL_EXAMPLES, 'r') as o:
43 diff = difflib.unified_diff(prev_lines, o.readlines())
44 print(''.join(diff))