-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradle_resolve_missing_metadata.py
More file actions
executable file
·107 lines (88 loc) · 4.26 KB
/
gradle_resolve_missing_metadata.py
File metadata and controls
executable file
·107 lines (88 loc) · 4.26 KB
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
#!/usr/bin/env nix
#!nix run nixpkgs#python313
# Vendored from https://gist.github.com/raphiz/3e03f54cf2b81047e8cdcdd264b56010
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(' </artifact>')
print(' </component>')
if __name__ == '__main__':
main()