Last active
November 7, 2024 10:29
-
-
Save raphiz/3e03f54cf2b81047e8cdcdd264b56010 to your computer and use it in GitHub Desktop.
A helper script to convert Gradle's verification metadata error output into the missing entries for the verification-metadata.xml file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import urllib.request | |
import hashlib | |
import re | |
# Example input: | |
# - kotlin-stdlib-1.9.20.module (org.jetbrains.kotlin:kotlin-stdlib:1.9.20) from repository MavenRepo | |
# - kotlin-stdlib-jdk7-1.7.10.pom (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10) from repository MavenRepo | |
# - kotlin-stdlib-jdk7-1.8.0.jar (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0) from repository MavenRepo | |
# - kotlin-stdlib-jdk7-1.8.0.pom (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0) from repository MavenRepo | |
# - kotlin-stdlib-jdk8-1.7.10.pom (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10) from repository MavenRepo | |
# - kotlin-stdlib-jdk8-1.8.0.jar (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0) from repository MavenRepo | |
# - kotlin-stdlib-jdk8-1.8.0.pom (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0) from repository MavenRepo | |
# - kotlin-daemon-embeddable-2.0.0.jar (org.jetbrains.kotlin:kotlin-daemon-embeddable:2.0.0) from repository Gradle Central Plugin Repository | |
# - kotlin-script-runtime-2.0.0.jar (org.jetbrains.kotlin:kotlin-script-runtime:2.0.0) from repository Gradle Central Plugin Repository | |
# - kotlin-stdlib-2.0.0.jar (org.jetbrains.kotlin:kotlin-stdlib:2.0.0) from repository Gradle Central Plugin Repository | |
# Regular expression to parse the input lines | |
artifact_pattern = re.compile( | |
r'\s+- (?P<artifact_file>.+?) \((?P<groupId>[^:]+):(?P<artifactId>[^:]+):(?P<version>[^)]+)\) from repository .*' | |
) | |
def parse_line(line): | |
match = artifact_pattern.match(line) | |
if match: | |
return { | |
'artifact_file': match.group('artifact_file'), | |
'groupId': match.group('groupId'), | |
'artifactId': match.group('artifactId'), | |
'version': match.group('version'), | |
} | |
else: | |
return None | |
def calculate_sha256(file_path): | |
sha256_hash = hashlib.sha256() | |
try: | |
with open(file_path, "rb") as f: | |
for byte_block in iter(lambda: f.read(4096), b""): | |
sha256_hash.update(byte_block) | |
return sha256_hash.hexdigest() | |
except Exception as e: | |
print(f"Error calculating sha256 for {file_path}: {e}", file=sys.stderr) | |
return None | |
def download_artifact(groupId, artifactId, version, artifact_file): | |
# Construct the URL to download the artifact file | |
group_path = groupId.replace('.', '/') | |
url = f'https://repo1.maven.org/maven2/{group_path}/{artifactId}/{version}/{artifact_file}' | |
local_file = f'/tmp/{artifact_file}' # Temporary download location | |
try: | |
urllib.request.urlretrieve(url, local_file) | |
return local_file | |
except Exception as e: | |
print(f"Error downloading {artifact_file}: {e}", file=sys.stderr) | |
return None | |
def main(): | |
data = {} | |
print("Please enter the artifact details line by line (press Enter on an empty line to finish):") | |
while True: | |
# Read input line by line | |
line = input() | |
if not line: | |
break # Stop when an empty line is encountered | |
parsed = parse_line(line) | |
if parsed is None: | |
print(f"Skipping invalid input line: {line}") | |
continue | |
key = (parsed['groupId'], parsed['artifactId'], parsed['version']) | |
artifacts = data.setdefault(key, []) | |
artifact_file = parsed['artifact_file'] | |
local_file = download_artifact(parsed['groupId'], parsed['artifactId'], parsed['version'], artifact_file) | |
if local_file is not None: | |
sha256 = calculate_sha256(local_file) | |
if sha256 is not None: | |
artifacts.append({ | |
'artifact_file': artifact_file, | |
'sha256': sha256, | |
}) | |
for (groupId, artifactId, version), artifacts in data.items(): | |
print(f' <component group="{groupId}" name="{artifactId}" version="{version}">') | |
for artifact in artifacts: | |
artifact_file = artifact['artifact_file'] | |
sha256 = artifact['sha256'] | |
print(f' <artifact name="{artifact_file}">') | |
print(f' <sha256 value="{sha256}" origin="Generated by script"/>') | |
print(f' </artifact>') | |
print(' </component>') | |
if __name__ == '__main__': | |
main() |
Author
raphiz
commented
Oct 13, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment