|
| 1 | +import copy |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import xml.etree.ElementTree as ET |
| 5 | + |
| 6 | +root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..")) |
| 7 | +coverage_file = os.path.join(root_dir, "coverage.xml") |
| 8 | + |
| 9 | + |
| 10 | +def create_coverage_report(): |
| 11 | + if not os.path.exists(coverage_file): |
| 12 | + logging.info("No coverage file detected at {}".format(coverage_file)) |
| 13 | + return |
| 14 | + logging.info("Modifying coverage file at {}".format(coverage_file)) |
| 15 | + tree = ET.parse(coverage_file) |
| 16 | + root = tree.getroot() |
| 17 | + |
| 18 | + packages = root[1] |
| 19 | + |
| 20 | + recursive_set_name(packages) |
| 21 | + |
| 22 | + packages_to_report = [] |
| 23 | + for child in packages: |
| 24 | + # Order should be: ['azure', '<package-name>', 'azure-<package-name>', ...] |
| 25 | + name = child.attrib['name'].split('.') |
| 26 | + logging.info("Name: {}".format(name)) |
| 27 | + folder, package = name[1], name[2] |
| 28 | + if (folder, package) not in packages_to_report: |
| 29 | + packages_to_report.append((folder, package)) |
| 30 | + logging.info("Found a package: {}".format(package)) |
| 31 | + |
| 32 | + package_names = [p[1] for p in packages_to_report] |
| 33 | + |
| 34 | + packages_root = root.find('packages') |
| 35 | + packages_root = packages |
| 36 | + |
| 37 | + packages_nodes = [] |
| 38 | + for folder, package_name in packages_to_report: |
| 39 | + condense_nodes = [] |
| 40 | + for child in packages: |
| 41 | + |
| 42 | + test_str = "sdk.{}.{}.{}".format( |
| 43 | + folder, |
| 44 | + package_name, |
| 45 | + package_name.replace('-', '.') |
| 46 | + ) |
| 47 | + |
| 48 | + if package_name in child.attrib['name']: |
| 49 | + condense_nodes.append(child) |
| 50 | + |
| 51 | + packages_nodes.append(condense_nodes) |
| 52 | + |
| 53 | + nodes_to_remove = [] |
| 54 | + for nodes in packages_nodes: |
| 55 | + if len(nodes) > 1: |
| 56 | + first_package = nodes[0] |
| 57 | + |
| 58 | + first_package_classes = first_package.find('classes') |
| 59 | + |
| 60 | + for node in nodes[1:]: |
| 61 | + temp_classes = node.find('classes') |
| 62 | + |
| 63 | + for _class in temp_classes: |
| 64 | + first_package_classes.append(_class) |
| 65 | + nodes_to_remove.append(node) |
| 66 | + |
| 67 | + for n in nodes_to_remove: |
| 68 | + if n not in packages_root: |
| 69 | + continue |
| 70 | + |
| 71 | + packages_root.remove(n) |
| 72 | + |
| 73 | + # Last thing with root, change the 'name' attrib to be just the package name |
| 74 | + packages_to_add = [] |
| 75 | + for package in root.find('packages'): |
| 76 | + name = package.attrib['name'].split('.') |
| 77 | + package.attrib['name'] = name[2] |
| 78 | + |
| 79 | + packages_to_add.append(copy.deepcopy(package)) |
| 80 | + |
| 81 | + write_final_xml(packages_to_add) |
| 82 | + |
| 83 | + |
| 84 | +def write_final_xml(packages_to_add): |
| 85 | + if not os.path.exists(coverage_file): |
| 86 | + logging.info("No coverage file detected at {}".format(coverage_file)) |
| 87 | + return |
| 88 | + |
| 89 | + logging.info("Modifying coverage file at {}".format(coverage_file)) |
| 90 | + tree = ET.parse(coverage_file) |
| 91 | + root = tree.getroot() |
| 92 | + |
| 93 | + packages = root[1] |
| 94 | + |
| 95 | + for p in packages_to_add: |
| 96 | + packages.insert(0, p) |
| 97 | + |
| 98 | + with open(coverage_file, "wb") as f: |
| 99 | + data = ET.tostring(root) |
| 100 | + f.write(data) |
| 101 | + |
| 102 | + |
| 103 | +def recursive_set_name(root): |
| 104 | + # Stopping condition |
| 105 | + if 'line' in root.attrib: |
| 106 | + return |
| 107 | + |
| 108 | + # Add file path to the filename |
| 109 | + if root.tag == 'class': |
| 110 | + root.set('name', root.attrib['filename']) |
| 111 | + |
| 112 | + for child in root: |
| 113 | + recursive_set_name(child) |
0 commit comments