aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/python/longbow-size-report.py
blob: fd4ae76c2fc0b7649a22e86e7610f19c5446d942 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/env python
# Copyright (c) 2017 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#

import sys
import os
import subprocess
import difflib
import csv
import argparse
sys.path.append("@INSTALL_PYTHON_DIR@")
import LongBow
from pprint import pprint

def textOutputFile(file, maximumFileNameLength):
    format = "%-" + str(maximumFileNameLength) + "s %6d"
    print format % (file[0], file[1])
    return

def textSummary(files):
    maximumFileNameLength = max(map(lambda file: len(file[0]), files))
    map(lambda file: textOutputFile(file, maximumFileNameLength), files)
    return

def csvOutputFile(file):
    format = "size,%s,%d"
    print format % (file[0], file[1])
    return

def csvSummary(files):
    map(lambda file: csvOutputFile(file), files)
    return

def textTotal(files):
    total = sum(map(lambda file: file[1], files))
    print total
    return

def csvTotal(files):
    total = sum(map(lambda file: file[1], files))
    print total
    return

def main():
    desc = '''
Report on number of lines of one or more C source or header files.

Input is either from a list of files supplied as command line parameters,
or as a list of newline separated file names read from standard input.
Output is a plain text (default) or a CSV file reporting
the file name and the total number of lines in the file.

Usage:

% longbow-size-report *.[ch]

Report the number of lines in .c and .h files specified as command line parameters.

% longbow-size-report -
Read the lists of files from standard input, one file per line.

$ longbow-size-report parc_JSON.c
parc_JSON.c    239
$
$
$ echo parc_JSON.c | longbow-size-report -o csv -
parc_JSON.c,239
$
'''

    parser = argparse.ArgumentParser(prog='longbow-size-report', formatter_class=argparse.RawDescriptionHelpFormatter, description=desc)
    parser.add_argument('-', '--stdin', default=False, action="store_true", required=False, help="read the list of files from standard input.")
    parser.add_argument('-s', '--summary', default=False, action="store_true", required=False, help="display the number of lines for each file")
    parser.add_argument('-t', '--total', default=False, action="store_true", required=False, help="display the total number of lines for all files")
    parser.add_argument('-o', '--output', default="text", action="store", required=False, type=str, help="the output format: \"text\" or \"csv\"")

    parser.add_argument("files", help="Files to check", nargs="*")

    args = parser.parse_args()

    if args.summary == False and args.total == False:
        args.summary = True

    targets = []

    if args.stdin:
        for line in sys.stdin:
            t = line.strip()
            if len(t) > 0:
                targets.append(t)
    else:
        targets = args.files

    if len(targets) == 0:
        parser.print_usage()
        sys.exit(1)

    files = map(lambda fileName: [ fileName, LongBow.countLines(fileName)], targets)
    total = sum(map(lambda element: element[1], files))

    if args.summary:
        if args.output == "text":
            textSummary(files)
        else:
            csvSummary(files)

    if args.total:
        if args.output == "text":
            textTotal(files)
        else:
            csvTotal(files)

if __name__ == '__main__':
    '''
    @(#) longbow-size-report @VERSION@ @DATE@
    @(#)   All Rights Reserved. Use is subject to license terms.
    '''
    main()