forked from CrazyChaoz/gradle-dot-nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradle-metadata-to-json.py
114 lines (91 loc) · 3.78 KB
/
gradle-metadata-to-json.py
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
114
import os
import sys
from xml.etree import ElementTree
# Define the namespaces
namespaces = {
'default': 'https://schema.gradle.org/dependency-verification'
}
def process_component(component):
group = component.attrib['group']
name = component.attrib['name']
version = component.attrib['version']
# Construct the subdirectory structure in the local repository
artifact_dir = os.path.join(group.replace('.', '/'), name, version)
# skipped files
skipped_files = []
pom_files = []
hash_for_artifact = {}
# module file
module_file = ""
# Iterate through the artifacts
# Download the artifact if it is a module file
for artifact in component.findall('.//default:artifact', namespaces):
artifact_name = artifact.attrib['name']
if not artifact_name.endswith('.module'):
skipped_files.append(artifact_name)
hash_for_artifact[artifact_name] = artifact.find('.//default:sha256', namespaces).attrib['value']
else:
module_file = '''
{
"name" : "''' + name + '''",
"group" : "''' + group + '''",
"version" : "''' + version + '''",
"artifact_name" : "''' + artifact_name + '''",
"artifact_dir" : "''' + artifact_dir + '''",
"has_module_file" : "false",
"is_added_pom_file" : "false",
"sha_256" : "''' + artifact.find('.//default:sha256', namespaces).attrib['value'] + '''"
}
'''
# desperation: add a definite .pom to the list of skipped files
pom_files.append(artifact_name.replace('.module', '.pom'))
skipped_files.append(artifact_name.replace('.module', '.pom'))
output_file.write(module_file)
if len(skipped_files) > 0 and module_file != "":
output_file.write(",")
#iterate through the skipped files using numbered index
for i, artifact in enumerate(set(skipped_files)):
if hash_for_artifact.get(artifact) is not None:
sha_256 = hash_for_artifact.get(artifact)
else:
sha_256 = "0"
if module_file != "":
has_module_file = "true"
else:
has_module_file = "false"
if pom_files.__contains__(artifact) and sha_256 == "0":
is_added_pom_file = "true"
else:
is_added_pom_file = "false"
if module_file != "":
text_for_module_file = ''',
"module_file" : ''' + module_file
else:
text_for_module_file = ""
output_file.write('''
{
"name" : "''' + name + '''",
"group" : "''' + group + '''",
"version" : "''' + version + '''",
"artifact_name" : "''' + artifact + '''",
"artifact_dir" : "''' + artifact_dir + '''",
"sha_256" : "''' + sha_256 + '''",
"has_module_file" : "''' + has_module_file + '''",
"is_added_pom_file" : "''' + is_added_pom_file + '''"'''+ text_for_module_file + '''
}
''')
if i < len(set(skipped_files)) - 1:
output_file.write(",")
# write the header to the output file
output_file = open(sys.argv[2], 'w')
output_file.write('''
{
"components" : [
''')
# Iterate through the components using numbered index
components = ElementTree.parse(sys.argv[1]).getroot().findall('.//default:component', namespaces)
for i, _component in enumerate(components):
process_component(_component)
if i < len(components) - 1:
output_file.write(",")
output_file.write("]}")