Skip to content

Commit 52075cd

Browse files
authored
Moving to new rules_jvm_external lock file format (#149)
* pomgen changes * tests passing * re-work exclusions * some general clean up * pomgen * update comment * full transitive closure * test for bazel_buildable
1 parent 6f77cbe commit 52075cd

File tree

7 files changed

+175
-157
lines changed

7 files changed

+175
-157
lines changed

crawl/bazel.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def query_all_libraries(repository_root_path, packages, verbose=False):
9292
package = os.path.dirname(package)
9393
return sorted(lib_roots)
9494

95+
def _normalize_dependency_string_to_group_id_artifact_id(dependency):
96+
return ":".join(dependency.split(":")[:2]) # remove classifier/package, so we can grab version from artifacts list
9597

9698
def parse_maven_install(mvn_install_name, json_file_path):
9799
"""
@@ -108,34 +110,55 @@ def parse_maven_install(mvn_install_name, json_file_path):
108110
with open(json_file_path, "r") as f:
109111
content = f.read()
110112
install_json = json.loads(content)
111-
json_dep_tree = install_json["dependency_tree"]
112-
conflict_resolution = _parse_conflict_resolution(json_dep_tree, mvn_install_name)
113-
json_deps = json_dep_tree["dependencies"]
114-
for json_dep in json_deps:
115-
coord = json_dep["coord"]
113+
114+
artifacts = install_json["artifacts"]
115+
deps = install_json["dependencies"]
116+
repository_key = list(install_json["repositories"].keys())[0]
117+
all_artifacts = install_json["repositories"][repository_key]
118+
119+
conflict_resolution = _parse_conflict_resolution(install_json, mvn_install_name)
120+
121+
for json_dep in all_artifacts:
122+
group_id_artifact_id = _normalize_dependency_string_to_group_id_artifact_id(json_dep)
123+
version = artifacts[group_id_artifact_id]["version"]
124+
coord = json_dep + ":" + version
116125
dep = dependency.new_dep_from_maven_art_str(coord, mvn_install_name)
117126
if dep in conflict_resolution:
118127
dep = conflict_resolution[dep]
119128
if dep.classifier != "sources":
120129
transitives = []
121-
for transitive_gav in json_dep["dependencies"]:
122-
transitive_dep = dependency.new_dep_from_maven_art_str(transitive_gav, mvn_install_name)
123-
if transitive_dep in conflict_resolution:
124-
transitive_dep = conflict_resolution[transitive_dep]
125-
transitives.append(transitive_dep)
126-
# exclusions only specify group_id:artifact_id - we use
127-
# dependency.Dependency instances instead of raw strings for
128-
# consistency, but then we need to add a dummy version
129-
if "exclusions" in json_dep:
130-
exclusions = [dependency.new_dep_from_maven_art_str(
131-
"%s:%s" % (d, dependency.GA_DUMMY_DEP_VERSION),
132-
mvn_install_name) for d in json_dep["exclusions"]]
133-
else:
134-
exclusions = ()
130+
transitives_to_process = []
131+
if group_id_artifact_id in deps:
132+
for transitive_gav in deps[group_id_artifact_id]:
133+
transitive_group_id_artifact_id = _normalize_dependency_string_to_group_id_artifact_id(transitive_gav)
134+
version = artifacts[transitive_group_id_artifact_id]["version"]
135+
transitive_gav = transitive_gav + ":" + version
136+
transitive_dep = dependency.new_dep_from_maven_art_str(transitive_gav, mvn_install_name)
137+
if transitive_dep in conflict_resolution:
138+
transitive_dep = conflict_resolution[transitive_dep]
139+
transitives.append(transitive_dep)
140+
# We need to process the transitives dependencies too to get the full transitive closure.
141+
transitives_to_process.append(transitive_gav)
142+
# Process dependencies of transitives in the same way as we do above.
143+
# This is done recursively so if we encounter a transitive that we haven't processed yet, append it to be processed.
144+
for transitive_to_process in transitives_to_process:
145+
transitive_group_id_artifact_id = _normalize_dependency_string_to_group_id_artifact_id(transitive_to_process)
146+
if transitive_group_id_artifact_id in deps:
147+
for transitive_gav in deps[transitive_group_id_artifact_id]:
148+
transitive_group_id_artifact_id = _normalize_dependency_string_to_group_id_artifact_id(transitive_gav)
149+
version = artifacts[transitive_group_id_artifact_id]["version"]
150+
transitive_gav = transitive_gav + ":" + version
151+
transitive_dep = dependency.new_dep_from_maven_art_str(transitive_gav, mvn_install_name)
152+
if transitive_dep in conflict_resolution:
153+
transitive_dep = conflict_resolution[transitive_dep]
154+
if transitive_dep not in transitives:
155+
transitives.append(transitive_dep)
156+
if transitive_gav not in transitives_to_process:
157+
transitives_to_process.append(transitive_gav)
158+
exclusions = ()
135159
result.append((dep, transitives, exclusions))
136160
return result
137161

138-
139162
def target_pattern_to_path(target_pattern):
140163
"""
141164
Converts a bazel target pattern to a directory path.

crawl/pom.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,11 @@ def _gen_dependencies_xml(self, pomcontenttype, dependencies, indent):
578578
for dep in dependencies:
579579
content, indent = self._gen_dependency_element(pomcontenttype, dep, content, indent, close_element=False)
580580
# handle <exclusions>
581-
excluded_group_and_artifact_ids = [(d.group_id, d.artifact_id) for d in self._workspace.dependency_metadata.get_transitive_exclusions(dep)]
582-
excluded_group_and_artifact_ids += self._get_hardcoded_exclusions_for_dep(dep)
583-
if len(excluded_group_and_artifact_ids) > 0:
581+
# if a dep is built in the shared-repo, do not add any exclusions, they will do that themselves.
582+
if not dep.bazel_buildable:
583+
# exclude all transitives from <dependencies> as all transitives are already root level anyway
584+
excluded_group_and_artifact_ids = [("*", "*")]
584585
content, indent = self._gen_exclusions(content, indent, excluded_group_and_artifact_ids)
585-
586586
content, indent = self._xml(content, "dependency", indent, close_element=True)
587587
return content
588588

@@ -609,21 +609,6 @@ def _get_transitive_deps(self, dependencies):
609609

610610
return transitives
611611

612-
def _get_hardcoded_exclusions_for_dep(self, dep):
613-
"""
614-
A few jar artifacts reference dependencies that do not exist; these
615-
need to be excluded explicitly.
616-
617-
Returns tuples (group_id, artifact_id) to exclude.
618-
"""
619-
if dep.group_id == "com.twitter.common.zookeeper" and \
620-
dep.artifact_id in ("client", "group", "server-set"):
621-
return (("com.twitter", "finagle-core-java"),
622-
("com.twitter", "util-core-java"),
623-
("org.apache.zookeeper", "zookeeper-client"))
624-
625-
return ()
626-
627612

628613
class DependencyManagementPomGen(AbstractPomGen):
629614
"""

crawl/pomparser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ def __init__(self, dependencies=set(), dependency_to_exclusions=defaultdict(list
8080

8181
def get_parsed_exclusions_for(self, dependency):
8282
"""
83-
Returns the exclusions for the specified dependency that were parsed
83+
Returns the exclusions for the specified dependency that were parsed
8484
out of a pom template.
85-
8685
The exclusions are returned as a list of Dependency instances.
8786
"""
8887
parsed_dep = self.get_parsed_dependency_for(dependency)

misc/tests/extdeps_pomgentest.py

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -46,55 +46,35 @@ def _add_WORKSPACE_file(self):
4646
def _add_maven_install_json_file(self):
4747
content = """
4848
{
49-
"dependency_tree": {
50-
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": -1063907353,
51-
"conflict_resolution": {},
52-
"dependencies": [
53-
{
54-
"coord": "com.google.guava:guava:23.0",
55-
"dependencies": [],
56-
"directDependencies": [],
57-
"exclusions": [
58-
"*:*"
59-
],
60-
"file": "v1/https/repo1.maven.org/maven2/com/google/guava/guava/23.0/guava-23.0.jar",
61-
"mirror_urls": [
62-
"https://repo1.maven.org/maven2/com/google/guava/guava/23.0/guava-23.0.jar"
63-
],
64-
"sha256": "7baa80df284117e5b945b19b98d367a85ea7b7801bd358ff657946c3bd1b6596",
65-
"url": "https://repo1.maven.org/maven2/com/google/guava/guava/23.0/guava-23.0.jar"
49+
"artifacts": {
50+
"com.google.guava:guava": {
51+
"shasums": {
52+
"jar": "ef95ae468097f378880be69a8c6756f8d15180e0f07547fb0a99617ff421b2ac"
6653
},
67-
{
68-
"coord": "org.apache.commons:commons-lang3:3.9",
69-
"dependencies": [],
70-
"directDependencies": [],
71-
"exclusions": [
72-
"*:*"
73-
],
74-
"file": "v1/https/repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar",
75-
"mirror_urls": [
76-
"https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar"
77-
],
78-
"sha256": "de2e1dcdcf3ef917a8ce858661a06726a9a944f28e33ad7f9e08bea44dc3c230",
79-
"url": "https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar"
54+
"version": "23.0"
55+
},
56+
"org.apache.commons:commons-lang3": {
57+
"shasums": {
58+
"jar": "ef95ae468097f378880be69a8c6756f8d15180e0f07547fb0a99617ff421b2ac"
8059
},
81-
{
82-
"coord": "org.apache.commons:commons-math3:3.6.1",
83-
"dependencies": [],
84-
"directDependencies": [],
85-
"exclusions": [
86-
"*:*"
87-
],
88-
"file": "v1/https/repo1.maven.org/maven2/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1.jar",
89-
"mirror_urls": [
90-
"https://repo1.maven.org/maven2/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1.jar"
91-
],
92-
"sha256": "1e56d7b058d28b65abd256b8458e3885b674c1d588fa43cd7d1cbb9c7ef2b308",
93-
"url": "https://repo1.maven.org/maven2/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1.jar"
94-
}
95-
],
96-
"version": "0.1.0"
97-
}
60+
"version": "3.9"
61+
},
62+
"org.apache.commons:commons-math3": {
63+
"shasums": {
64+
"jar": "ef95ae468097f378880be69a8c6756f8d15180e0f07547fb0a99617ff421b2ac"
65+
},
66+
"version": "3.6.1"
67+
}
68+
},
69+
"dependencies": {},
70+
"repositories": {
71+
"https://maven.google.com/": [
72+
"com.google.guava:guava",
73+
"org.apache.commons:commons-lang3",
74+
"org.apache.commons:commons-math3"
75+
]
76+
},
77+
"version": "2"
9878
}
9979
"""
10080
self._write_file("", "", "maven_install.json", content)

tests/bazeltest.py

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@ def test_parse_maven_install(self):
2020
f.write(MVN_INSTALL_JSON_CONTENT)
2121

2222
result = bazel.parse_maven_install("maven", path)
23-
24-
self.assertEqual(1, len(result))
23+
self.assertEqual(3, len(result))
2524
dep, transitives, exclusions = result[0]
2625
self.assertEqual(dependency.new_dep_from_maven_art_str("ch.qos.logback:logback-classic:1.2.3", "maven"), dep)
2726
self.assertEqual(2, len(transitives))
2827
self.assertEqual(dependency.new_dep_from_maven_art_str("ch.qos.logback:logback-core:1.2.3", "maven"), transitives[0])
2928
self.assertEqual(dependency.new_dep_from_maven_art_str("org.slf4j:slf4j-api:jar:1.7.30", "maven"), transitives[1])
30-
self.assertEqual(2, len(exclusions))
31-
self.assertEqual("jakarta.el", exclusions[0].group_id)
32-
self.assertEqual("jakarta.el-api", exclusions[0].artifact_id)
33-
self.assertEqual("org.glassfish", exclusions[1].group_id)
34-
self.assertEqual("jakarta.el", exclusions[1].artifact_id)
3529

3630
def test_conflict_resolution_is_honored(self):
3731
"""
@@ -76,72 +70,76 @@ def test_ensure_unique_deps(self):
7670

7771
MVN_INSTALL_JSON_CONTENT = """
7872
{
79-
"dependency_tree": {
80-
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": -163681522,
81-
"conflict_resolution": {},
82-
"dependencies": [
83-
{
84-
"coord": "ch.qos.logback:logback-classic:1.2.3",
85-
"dependencies": [
86-
"ch.qos.logback:logback-core:1.2.3",
87-
"org.slf4j:slf4j-api:jar:1.7.30"
88-
],
89-
"directDependencies": [
90-
"org.slf4j:slf4j-api:1.7.30"
91-
],
92-
"exclusions": [
93-
"jakarta.el:jakarta.el-api",
94-
"org.glassfish:jakarta.el"
95-
]
73+
"artifacts": {
74+
"ch.qos.logback:logback-classic": {
75+
"shasums": {
76+
"jar": "ef95ae468097f378880be69a8c6756f8d15180e0f07547fb0a99617ff421b2ac",
77+
"sources": "ef95ae468097f378880be69a8c6756f8d15180e0f07547fb0a99617ff421b2ac"
9678
},
97-
{
98-
"coord": "ch.qos.logback:logback-classic:jar:sources:1.2.3",
99-
"dependencies": [
100-
"org.slf4j:slf4j-api:jar:sources:1.7.30",
101-
"ch.qos.logback:logback-core:jar:sources:1.2.3"
102-
],
103-
"directDependencies": [
104-
"ch.qos.logback:logback-core:jar:sources:1.2.3",
105-
"org.slf4j:slf4j-api:jar:sources:1.7.30"
106-
],
107-
"exclusions": [
108-
]
109-
110-
}
111-
],
112-
"version": "0.1.0"
113-
}
79+
"version": "1.2.3"
80+
},
81+
"ch.qos.logback:logback-core": {
82+
"shasums": {
83+
"jar": "ef95ae468097f378880be69a8c6756f8d15180e0f07547fb0a99617ff421b2ac"
84+
},
85+
"version": "1.2.3"
86+
},
87+
"org.slf4j:slf4j-api": {
88+
"shasums": {
89+
"jar": "ef95ae468097f378880be69a8c6756f8d15180e0f07547fb0a99617ff421b2ac"
90+
},
91+
"version": "1.7.30"
92+
}
93+
},
94+
"dependencies": {
95+
"ch.qos.logback:logback-classic": [
96+
"ch.qos.logback:logback-core",
97+
"org.slf4j:slf4j-api"
98+
]
99+
},
100+
"repositories": {
101+
"https://maven.google.com/": [
102+
"ch.qos.logback:logback-classic",
103+
"ch.qos.logback:logback-classic:jar:sources",
104+
"ch.qos.logback:logback-core",
105+
"org.slf4j:slf4j-api"
106+
]
107+
},
108+
"version": "2"
114109
}
115110
"""
116111

117112
MVN_INSTALL_JSON_CONTENT_CONFLICT_RESOLUTION = """
118113
{
119-
"dependency_tree": {
120-
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": -163681522,
121-
"conflict_resolution": {
122-
"com.google.guava:guava:31.0.1-jre": "com.google.guava:guava:31.0.1-jre-SNAPSHOT"
114+
"conflict_resolution": {
115+
"com.google.guava:guava:31.0.1-jre": "com.google.guava:guava:31.0.1-jre-SNAPSHOT"
116+
},
117+
"artifacts": {
118+
"ch.qos.logback:logback-classic": {
119+
"shasums": {
120+
"jar": "ef95ae468097f378880be69a8c6756f8d15180e0f07547fb0a99617ff421b2ac"
121+
},
122+
"version": "1.2.3"
123123
},
124-
"dependencies": [
125-
{
126-
"coord": "ch.qos.logback:logback-classic:1.2.3",
127-
"dependencies": [
128-
"com.google.guava:guava:31.0.1-jre-SNAPSHOT"
129-
],
130-
"directDependencies": [
131-
"com.google.guava:guava:31.0.1-jre-SNAPSHOT"
132-
]
124+
"com.google.guava:guava": {
125+
"shasums": {
126+
"jar": "ef95ae468097f378880be69a8c6756f8d15180e0f07547fb0a99617ff421b2ac"
133127
},
134-
{
135-
"coord": "com.google.guava:guava:31.0.1-jre-SNAPSHOT",
136-
"dependencies": [],
137-
"directDependencies": [],
138-
"exclusions": [
139-
"*:*"
140-
]
141-
}
142-
],
143-
"version": "0.1.0"
144-
}
128+
"version": "31.0.1-jre-SNAPSHOT"
129+
}
130+
},
131+
"dependencies": {
132+
"ch.qos.logback:logback-classic": [
133+
"com.google.guava:guava"
134+
]
135+
},
136+
"repositories": {
137+
"https://maven.google.com/": [
138+
"ch.qos.logback:logback-classic",
139+
"com.google.guava:guava"
140+
]
141+
},
142+
"version": "2"
145143
}
146144
"""
147145

tests/pomgentest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ def _setup_workspace(self):
4343
self._add_pom_template()
4444
self._write_file("","","maven_install.json", """
4545
{
46-
"dependency_tree": {
47-
"dependencies": []
46+
"artifacts": {},
47+
"dependencies": {},
48+
"repositories": {
49+
"https://maven.google.com/": []
4850
}
4951
}
5052
""")

0 commit comments

Comments
 (0)