15
15
import sys
16
16
import glob
17
17
import json
18
+ from xml .etree .ElementTree import ElementTree
19
+
18
20
from lxml import etree
19
21
import os
20
22
import re
@@ -92,7 +94,10 @@ def _is_cloud_client(existing_modules: List[module.Module]) -> bool:
92
94
93
95
94
96
def update_cloud_pom (
95
- filename : str , proto_modules : List [module .Module ], grpc_modules : List [module .Module ]
97
+ filename : str ,
98
+ proto_modules : List [module .Module ],
99
+ grpc_modules : List [module .Module ],
100
+ is_monorepo : bool ,
96
101
):
97
102
tree = etree .parse (filename )
98
103
root = tree .getroot ()
@@ -104,6 +109,9 @@ def update_cloud_pom(
104
109
if m .find ("{http://maven.apache.org/POM/4.0.0}artifactId" ) is not None
105
110
]
106
111
112
+ if is_monorepo :
113
+ _set_test_scoped_deps (dependencies )
114
+
107
115
try :
108
116
grpc_index = _find_dependency_index (
109
117
dependencies , "com.google.api.grpc" , "grpc-"
@@ -169,6 +177,39 @@ def update_cloud_pom(
169
177
tree .write (filename , pretty_print = True , xml_declaration = True , encoding = "utf-8" )
170
178
171
179
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
+
172
213
def update_parent_pom (filename : str , modules : List [module .Module ]):
173
214
tree = etree .parse (filename )
174
215
root = tree .getroot ()
@@ -492,7 +533,9 @@ def main(versions_file, monorepo):
492
533
if os .path .isfile (f"{ artifact_id } /pom.xml" ):
493
534
print ("updating modules in cloud pom.xml" )
494
535
if artifact_id not in excluded_poms_list :
495
- update_cloud_pom (f"{ artifact_id } /pom.xml" , proto_modules , grpc_modules )
536
+ update_cloud_pom (
537
+ f"{ artifact_id } /pom.xml" , proto_modules , grpc_modules , monorepo
538
+ )
496
539
elif artifact_id not in excluded_poms_list :
497
540
print ("creating missing cloud pom.xml" )
498
541
templates .render (
0 commit comments