• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2"""Utility to generate character statistics about a number of source files"""
3
4# Copyright Abel Sinkovics (abel@sinkovics.hu) 2016.
5# Distributed under the Boost Software License, Version 1.0.
6#    (See accompanying file LICENSE_1_0.txt or copy at
7#          http://www.boost.org/LICENSE_1_0.txt)
8
9import argparse
10import os
11
12
13def count_characters(root, out):
14    """Count the occurrances of the different characters in the files"""
15    if os.path.isfile(root):
16        with open(root, 'rb') as in_f:
17            for line in in_f:
18                for char in line:
19                    if char not in out:
20                        out[char] = 0
21                    out[char] = out[char] + 1
22    elif os.path.isdir(root):
23        for filename in os.listdir(root):
24            count_characters(os.path.join(root, filename), out)
25
26
27def generate_statistics(root):
28    """Generate the statistics from all files in root (recursively)"""
29    out = dict()
30    count_characters(root, out)
31    return out
32
33
34def main():
35    """The main function of the script"""
36    desc = 'Generate character statistics from a source tree'
37    parser = argparse.ArgumentParser(description=desc)
38    parser.add_argument(
39        '--src',
40        dest='src',
41        required=True,
42        help='The root of the source tree'
43    )
44    parser.add_argument(
45        '--out',
46        dest='out',
47        default='chars.py',
48        help='The output filename'
49    )
50
51    args = parser.parse_args()
52
53    stats = generate_statistics(args.src)
54    with open(args.out, 'wb') as out_f:
55        out_f.write('CHARS={0}\n'.format(stats))
56
57
58if __name__ == '__main__':
59    main()
60