File: makeexamples.py

package info (click to toggle)
contextfree 3.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,432 kB
  • sloc: cpp: 34,857; lex: 416; makefile: 124; sh: 42; python: 41
file content (52 lines) | stat: -rw-r--r-- 1,487 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import sys

def print_cfdg(cfdg_name):
    cfdg_file = os.path.join('input', cfdg_name)
    if not os.path.exists(cfdg_file):
        sys.stderr.write('Cannot find file: %s\n' % cfdg_file)
        sys.exit(2)

    sys.stdout.write('R"&&&(')
    length = 0

    with open(cfdg_file, 'r') as fp:
        for line in fp:
            sys.stdout.write(line)
            length += len(line)
            if line == '\n' and length > 16000:
                sys.stdout.write(')&&&" R"&&&(')
                length = 0
            if length > 16384:
                sys.stderr.write('String exceeds 16384 characters: %s\n' % cfdg_file)
                sys.exit(3)

    sys.stdout.write(')&&&"')

def print_examples(cfdg_files):
    print('const std::map<std::string, std::pair<const char*, const char*>>')
    print('CommandLineSystem::ExamplesMap{')

    for cfdg in cfdg_files:
        if cfdg.endswith('_v2.cfdg'):
            pass
        else:
            print('    { "%s", {' % cfdg)
            print_cfdg(cfdg)
            print(' ,')
            print_cfdg(cfdg.replace('.cfdg', '_v2.cfdg'))
            print('    } },')


    print('};')


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('Usage: %s file ...' % sys.argv[0])
        print('Takes the named files in the input directory and writes to standard output a ')
        print('std::map that maps the name to the file contents.')
        sys.exit(1)

    print_examples(sys.argv[1:])
    sys.exit(0)