aboutsummaryrefslogtreecommitdiff
path: root/tools/cppmerge/main.py
blob: ed06a2f70566ffcc843b15329dedb19d9d267999 (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
import argparse
import os
import os.path
import re
import sys

parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", required=True, help="The name of output header and source files excluding extension.")
parser.add_argument("input", nargs="+", help="The input source files or folders to scan.")
parser.add_argument("-e", "--exclude", nargs="+", help="Filenames that exludes from merging.")
args = parser.parse_args()

input_dirs = []

header_file_list = []
source_file_list = []

def add_source_file(path):
    print("find source file: {}".format(path))
    source_file_list.append(path)

def add_header_file(path):
    print("find header file: {}".format(path))
    header_file_list.append(path)

cpp_header_file_regex = re.compile(r".*\.(h|hpp)")
cpp_source_file_regex = re.compile(r".*\.(cpp|cc|cxx)")


for path in args.input:
    if os.path.isdir(path):
        input_dirs.append(path)
    elif os.path.isfile(path):
        file_name = os.path.basename(path)
        if cpp_source_file_regex.match(file_name):
            if file_name in args.exclude:
                sys.exit("{} is specified excluding.")
            else:
                add_source_file(path)
        elif cpp_header_file_regex.match(file_name):
            if file_name in args.exclude:
                sys.exit("{} is specified excluding.")
            else:
                add_header_file(path)
        else:
            sys.exit("{} is a file but it is neither c++ source nor header.".format(path))
    else:
        sys.exit("{} is neither a file nor a directory.".format(path))

actual_exclude_count = 0

for input_dir in input_dirs:
    for root, subdirs, files in os.walk(input_dir):
        for f in files:
            if f in args.exclude:
                actual_exclude_count += 1
            else:
                if cpp_source_file_regex.match(f):
                    path = os.path.join(root, f)
                    add_source_file(path)
                elif cpp_header_file_regex.match(f):
                    path = os.path.join(root, f)
                    add_header_file(path)

print()
print("total find {} header file(s)".format(len(header_file_list)))
print("total find {} source file(s)".format(len(source_file_list)))
print("total actual exclude {} file(s)".format(actual_exclude_count))
print()