Passed
Push — master ( 42467a...9d011f )
by Matěj
03:19 queued 11s
created

utils.find_shadowed_files.parse_args()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 0
dl 0
loc 12
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
#!/usr/bin/python3
2
3
import os
4
import argparse
5
import sys
6
7
8
SSG_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
9
10
11
def safe_listdir(path):
12
    try:
13
        return os.listdir(path)
14
    except:
15
        return []
16
17
18
def print_shadows(resource, language, product):
19
    source_paths = [
20
        # shared templated checks
21
        os.path.join("build", product, resource, "shared", language),
22
        # shared checks
23
        os.path.join("shared", resource, language),
24
        # product templated checks
25
        os.path.join("build", product, resource, language),
26
        # product checks
27
        os.path.join(product, resource, language),
28
    ]
29
30
    source_files = [
31
        set(safe_listdir(os.path.join(SSG_DIR, path))) for path in source_paths
32
    ]
33
34
    all_source_files = set.union(*source_files)
35
36
    for source_file in all_source_files:
37
        i = 0
38
        while i < len(source_paths):
39
            if source_file in source_files[i]:
40
                break
41
            i += 1
42
43
        assert(i < len(source_paths))
44
45
        shadows = set()
46
        j = i + 1
47
        while j < len(source_paths):
48
            if source_file in source_files[j]:
49
                shadows.add(j)
50
            j += 1
51
52
        if shadows:
53
            msg = os.path.relpath(
54
                os.path.join(source_paths[i], source_file), SSG_DIR
55
            )
56
57
            for shadow in shadows:
58
                msg += " <- " + os.path.relpath(
59
                    os.path.join(source_paths[shadow], source_file), SSG_DIR
60
                )
61
62
            print(msg)
63
64
65
def parse_args():
66
    p = argparse.ArgumentParser(
67
        description="Looks through both source files and built files of the "
68
        "product and finds shadowed files. Outputs them in the format of "
69
        "shadowing. For example if D ends up being used and C, B and A are "
70
        "shadowed it will output A <- B <- C <- D."
71
    )
72
    p.add_argument("--product", default="rhel7",
73
                   help="Which product we should examine for shadowed checks "
74
                   "and fixes.")
75
76
    return p.parse_args()
77
78
79
def main():
80
    args = parse_args()
81
82
    check_languages = ["oval"]
83
    fix_languages = ["bash", "ansible", "anaconda", "puppet"]
84
85
    for product in [args.product]:
86
        for check_lang in check_languages:
87
            print("%s %s check shadows" % (product, check_lang))
88
            print_shadows("checks", check_lang, product)
89
            print()
90
91
        print()
92
93
        for fix_lang in fix_languages:
94
            print("%s %s fix shadows" % (product, fix_lang))
95
            print_shadows("fixes", fix_lang, product)
96
            print()
97
98
99
if __name__ == "__main__":
100
    main()
101