File: code_cov_report.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (113 lines) | stat: -rw-r--r-- 3,241 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
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
import copy
import logging
import os
import xml.etree.ElementTree as ET

root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", ".."))
coverage_file = os.path.join(root_dir, "coverage.xml")


def create_coverage_report():
    if not os.path.exists(coverage_file):
        logging.info("No coverage file detected at {}".format(coverage_file))
        return
    logging.info("Modifying coverage file at {}".format(coverage_file))
    tree = ET.parse(coverage_file)
    root = tree.getroot()

    packages = root[1]

    recursive_set_name(packages)

    packages_to_report = []
    for child in packages:
        # Order should be: ['azure', '<package-name>', 'azure-<package-name>', ...]
        name = child.attrib['name'].split('.')
        logging.info("Name: {}".format(name))
        folder, package = name[1], name[2]
        if (folder, package) not in packages_to_report:
            packages_to_report.append((folder, package))
            logging.info("Found a package: {}".format(package))

    package_names = [p[1] for p in packages_to_report]

    packages_root = root.find('packages')
    packages_root = packages

    packages_nodes = []
    for folder, package_name in packages_to_report:
        condense_nodes = []
        for child in packages:

            test_str = "sdk.{}.{}.{}".format(
                folder,
                package_name,
                package_name.replace('-', '.')
            )

            if package_name in child.attrib['name']:
                condense_nodes.append(child)

        packages_nodes.append(condense_nodes)

    nodes_to_remove = []
    for nodes in packages_nodes:
        if len(nodes) > 1:
            first_package = nodes[0]

            first_package_classes = first_package.find('classes')

            for node in nodes[1:]:
                temp_classes = node.find('classes')

                for _class in temp_classes:
                    first_package_classes.append(_class)
                nodes_to_remove.append(node)

    for n in nodes_to_remove:
        if n not in packages_root:
            continue

        packages_root.remove(n)

    # Last thing with root, change the 'name' attrib to be just the package name
    packages_to_add = []
    for package in root.find('packages'):
        name = package.attrib['name'].split('.')
        package.attrib['name'] = name[2]

        packages_to_add.append(copy.deepcopy(package))

    write_final_xml(packages_to_add)


def write_final_xml(packages_to_add):
    if not os.path.exists(coverage_file):
        logging.info("No coverage file detected at {}".format(coverage_file))
        return

    logging.info("Modifying coverage file at {}".format(coverage_file))
    tree = ET.parse(coverage_file)
    root = tree.getroot()

    packages = root[1]

    for p in packages_to_add:
        packages.insert(0, p)

    with open(coverage_file, "wb") as f:
        data = ET.tostring(root)
        f.write(data)


def recursive_set_name(root):
    # Stopping condition
    if 'line' in root.attrib:
        return

    # Add file path to the filename
    if root.tag == 'class':
        root.set('name', root.attrib['filename'])

    for child in root:
        recursive_set_name(child)