|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 5 | +# This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 6 | +# Copyright 2019-Present Datadog, Inc |
| 7 | + |
| 8 | +import sys |
| 9 | +import os |
| 10 | +import glob |
| 11 | + |
| 12 | +import xml.etree.ElementTree as ET |
| 13 | +import xml.dom.minidom as MD |
| 14 | + |
| 15 | +from typing import List |
| 16 | + |
| 17 | + |
| 18 | +DEFAULT_NS = "https://schema.gradle.org/dependency-verification" |
| 19 | +NS_PREFIX = "{%s}" % DEFAULT_NS |
| 20 | +NSMAP = {None : DEFAULT_NS} |
| 21 | + |
| 22 | +ROOT_TAG = (NS_PREFIX + "verification-metadata") |
| 23 | +CONFIGURATION_TAG = (NS_PREFIX + "configuration") |
| 24 | +METADATA_TAG = (NS_PREFIX + "verify-metadata") |
| 25 | +SIGNATURES_TAG = (NS_PREFIX + "verify-signatures") |
| 26 | + |
| 27 | +COMPONENTS_TAG = (NS_PREFIX + "components") |
| 28 | +COMPONENT_TAG = (NS_PREFIX + "component") |
| 29 | + |
| 30 | +def run_main() -> int: |
| 31 | + files = glob.glob('**/verification-metadata.xml', recursive=True) |
| 32 | + |
| 33 | + # prepare final xml |
| 34 | + root = ET.Element(ROOT_TAG) |
| 35 | + configuration = ET.Element(CONFIGURATION_TAG) |
| 36 | + metadata = ET.Element(METADATA_TAG) |
| 37 | + metadata.text = "true" |
| 38 | + configuration.insert(0, metadata) |
| 39 | + signatures = ET.Element(SIGNATURES_TAG) |
| 40 | + signatures.text = "false" # TODO RUM-3104 also copy signatures content |
| 41 | + configuration.insert(1, signatures) |
| 42 | + root.insert(0, configuration) |
| 43 | + |
| 44 | + components = ET.Element(COMPONENTS_TAG) |
| 45 | + root.insert(1, components) |
| 46 | + |
| 47 | + index = 0 |
| 48 | + for filename in files: |
| 49 | + data = ET.parse(filename).getroot() |
| 50 | + for child in data: |
| 51 | + if child.tag == COMPONENTS_TAG: |
| 52 | + for component in child: |
| 53 | + components.insert(index, component) |
| 54 | + index = index + 1 |
| 55 | + |
| 56 | + # remove unnecessary whitespaces in content |
| 57 | + for elem in root.iter(): |
| 58 | + if elem.text is not None: |
| 59 | + elem.text = elem.text.strip() |
| 60 | + if elem.tail is not None: |
| 61 | + elem.tail = elem.tail.strip() |
| 62 | + |
| 63 | + raw_xml = ET.tostring(root, method="xml").decode('utf-8') |
| 64 | + # A bug in the python etree xml api prevents writing standalone xml, so we need |
| 65 | + # to remove the default namespace: |
| 66 | + sanitized_xml = raw_xml.replace("<ns0:", "<").replace("</ns0:", "</").replace("xmlns:ns0=", "xmlns=") |
| 67 | + formatted_xml = MD.parseString(sanitized_xml).toprettyxml(indent=" ", encoding="utf-8") |
| 68 | + |
| 69 | + with open("verification-metadata.xml", "w") as output_file: |
| 70 | + output_file.write(formatted_xml.decode('utf-8')) |
| 71 | + |
| 72 | + return 0 |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + sys.exit(run_main()) |
0 commit comments