Skip to content

Commit 18591bd

Browse files
committed
apply this change only on monorepos
1 parent c915614 commit 18591bd

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

library_generation/owlbot/src/fix_poms.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import sys
1616
import glob
1717
import json
18+
from xml.etree.ElementTree import ElementTree
19+
1820
from lxml import etree
1921
import os
2022
import re
@@ -95,7 +97,7 @@ def update_cloud_pom(
9597
filename: str,
9698
proto_modules: List[module.Module],
9799
grpc_modules: List[module.Module],
98-
repo_metadata: dict,
100+
is_monorepo: bool,
99101
):
100102
tree = etree.parse(filename)
101103
root = tree.getroot()
@@ -107,42 +109,8 @@ def update_cloud_pom(
107109
if m.find("{http://maven.apache.org/POM/4.0.0}artifactId") is not None
108110
]
109111

110-
# as of July 2024, we have two dependencies that should be declared as
111-
# test-scoped: grpc-google-common-protos and grpc-google-iam-v1. Only in
112-
# java-storage, java-spanner and java-dataproc we keep them as they are
113-
TEST_SCOPED_DEPENDENCIES = ["grpc-google-common-protos", "grpc-google-iam-v1"]
114-
print(
115-
'converting old dependencies "grpc-google-common-protos" and "grpc-google-iam-v1" to test-scoped'
116-
)
117-
for d in dependencies:
118-
if repo_metadata["repo_short"] in [
119-
"java-spanner",
120-
"java-storage",
121-
"java-dataproc",
122-
]:
123-
print(
124-
f"skipping test-scoped-dependency fix for special case repo: {repo_metadata['repo_short']}"
125-
)
126-
continue
127-
artifact_query = "{http://maven.apache.org/POM/4.0.0}artifactId"
128-
scope_query = "{http://maven.apache.org/POM/4.0.0}scope"
129-
current_scope = d.find(scope_query)
130-
artifact_id_elem = d.find(artifact_query)
131-
if artifact_id_elem is None:
132-
continue
133-
artifact_id = artifact_id_elem.text
134-
is_test_scoped = (
135-
current_scope.text == "test" if current_scope is not None else False
136-
)
137-
if artifact_id in TEST_SCOPED_DEPENDENCIES and not is_test_scoped:
138-
new_scope = etree.Element(scope_query)
139-
new_scope.text = "test"
140-
if current_scope is not None:
141-
d.replace(current_scope, new_scope)
142-
else:
143-
d.append(new_scope)
144-
new_scope.tail = "\n "
145-
new_scope.getprevious().tail = "\n "
112+
if is_monorepo:
113+
_set_test_scoped_deps(dependencies)
146114

147115
try:
148116
grpc_index = _find_dependency_index(
@@ -209,6 +177,39 @@ def update_cloud_pom(
209177
tree.write(filename, pretty_print=True, xml_declaration=True, encoding="utf-8")
210178

211179

180+
def _set_test_scoped_deps(dependencies: list[ElementTree]) -> None:
181+
"""
182+
As of July 2024, we have two dependencies that should be declared as
183+
test-scoped in a monorepo: grpc-google-common-protos and grpc-google-iam-v1.
184+
HW libraries are treated as usual
185+
:param dependencies: List of XML Objects representing a <dependency/>
186+
"""
187+
TEST_SCOPED_DEPENDENCIES = ["grpc-google-common-protos", "grpc-google-iam-v1"]
188+
print(
189+
'converting dependencies "grpc-google-common-protos" and "grpc-google-iam-v1" to test-scoped'
190+
)
191+
for d in dependencies:
192+
artifact_query = "{http://maven.apache.org/POM/4.0.0}artifactId"
193+
scope_query = "{http://maven.apache.org/POM/4.0.0}scope"
194+
current_scope = d.find(scope_query)
195+
artifact_id_elem = d.find(artifact_query)
196+
if artifact_id_elem is None:
197+
continue
198+
artifact_id = artifact_id_elem.text
199+
is_test_scoped = (
200+
current_scope.text == "test" if current_scope is not None else False
201+
)
202+
if artifact_id in TEST_SCOPED_DEPENDENCIES and not is_test_scoped:
203+
new_scope = etree.Element(scope_query)
204+
new_scope.text = "test"
205+
if current_scope is not None:
206+
d.replace(current_scope, new_scope)
207+
else:
208+
d.append(new_scope)
209+
new_scope.tail = "\n "
210+
new_scope.getprevious().tail = "\n "
211+
212+
212213
def update_parent_pom(filename: str, modules: List[module.Module]):
213214
tree = etree.parse(filename)
214215
root = tree.getroot()
@@ -533,7 +534,7 @@ def main(versions_file, monorepo):
533534
print("updating modules in cloud pom.xml")
534535
if artifact_id not in excluded_poms_list:
535536
update_cloud_pom(
536-
f"{artifact_id}/pom.xml", proto_modules, grpc_modules, repo_metadata
537+
f"{artifact_id}/pom.xml", proto_modules, grpc_modules, monorepo
537538
)
538539
elif artifact_id not in excluded_poms_list:
539540
print("creating missing cloud pom.xml")

0 commit comments

Comments
 (0)