Skip to content

Commit f5ef79e

Browse files
committed
Common out Gradle/Maven properties file patching in our Python scripts
1 parent 5782a3b commit f5ef79e

File tree

5 files changed

+61
-46
lines changed

5 files changed

+61
-46
lines changed

graalpython/com.oracle.graal.python.test/src/tests/standalone/test_gradle_plugin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def generate_app(self, target_dir):
9898
if file.endswith(".j"):
9999
shutil.move(os.path.join(root, file), os.path.join(root, file[0:len(file)- 1] + "java"))
100100

101-
util.patch_properties_file(os.path.join(target_dir, "gradle", "wrapper", "gradle-wrapper.properties"), self.env.get("GRADLE_DISTRIBUTION_URL_OVERRIDE"))
102-
101+
util.override_gradle_properties_file(target_dir)
103102
self.copy_build_files(target_dir)
104103

105104
def check_filelist(self, target_dir, log, check_lib=True):

graalpython/com.oracle.graal.python.test/src/tests/standalone/test_maven_plugin.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ def generate_app(self, tmpdir, target_dir, target_name, pom_template=None):
7272
shutil.copy(os.path.join(mvnw_dir, "mvnw"), os.path.join(target_dir, "mvnw"))
7373
shutil.copy(os.path.join(mvnw_dir, "mvnw.cmd"), os.path.join(target_dir, "mvnw.cmd"))
7474
shutil.copytree(os.path.join(mvnw_dir, ".mvn"), os.path.join(target_dir, ".mvn"))
75-
util.patch_properties_file(os.path.join(target_dir, ".mvn", "wrapper", "maven-wrapper.properties"), self.env.get("MAVEN_DISTRIBUTION_URL_OVERRIDE"))
75+
util.override_maven_properties_file(target_dir)
7676

7777
@unittest.skipUnless(util.is_maven_plugin_test_enabled, "ENABLE_MAVEN_PLUGIN_UNITTESTS is not true")
7878
def test_generated_app(self):
79-
with tempfile.TemporaryDirectory() as tmpdir:
79+
with util.TemporaryTestDirectory() as tmpdir:
8080
target_name = "generated_app_test"
8181
target_dir = os.path.join(str(tmpdir), target_name)
8282
self.generate_app(tmpdir, target_dir, target_name)
@@ -140,7 +140,7 @@ def test_generated_app(self):
140140

141141
@unittest.skipUnless(util.is_maven_plugin_test_enabled, "ENABLE_MAVEN_PLUGIN_UNITTESTS is not true")
142142
def test_generated_app_external_resources(self):
143-
with tempfile.TemporaryDirectory() as tmpdir:
143+
with util.TemporaryTestDirectory() as tmpdir:
144144
target_name = "generated_app_ext_resources_test"
145145
target_dir = os.path.join(str(tmpdir), target_name)
146146
self.generate_app(tmpdir, target_dir, target_name)
@@ -193,7 +193,7 @@ def test_generated_app_external_resources(self):
193193

194194
@unittest.skipUnless(util.is_maven_plugin_test_enabled, "ENABLE_MAVEN_PLUGIN_UNITTESTS is not true")
195195
def test_fail_without_graalpy_dep(self):
196-
with tempfile.TemporaryDirectory() as tmpdir:
196+
with util.TemporaryTestDirectory() as tmpdir:
197197
target_name = "fail_without_graalpy_dep_test"
198198
target_dir = os.path.join(str(tmpdir), target_name)
199199
pom_template = os.path.join(os.path.dirname(__file__), "fail_without_graalpy_dep_pom.xml")
@@ -207,7 +207,7 @@ def test_fail_without_graalpy_dep(self):
207207

208208
@unittest.skipUnless(util.is_maven_plugin_test_enabled, "ENABLE_MAVEN_PLUGIN_UNITTESTS is not true")
209209
def test_gen_launcher_and_venv(self):
210-
with tempfile.TemporaryDirectory() as tmpdir:
210+
with util.TemporaryTestDirectory() as tmpdir:
211211
target_name = "gen_launcher_and_venv_test"
212212
target_dir = os.path.join(str(tmpdir), target_name)
213213
pom_template = os.path.join(os.path.dirname(__file__), "prepare_venv_pom.xml")
@@ -247,7 +247,7 @@ def check_tagfile(self, target_dir, expected):
247247

248248
@unittest.skipUnless(util.is_maven_plugin_test_enabled, "ENABLE_MAVEN_PLUGIN_UNITTESTS is not true")
249249
def test_check_home(self):
250-
with tempfile.TemporaryDirectory() as tmpdir:
250+
with util.TemporaryTestDirectory() as tmpdir:
251251
target_name = "check_home_test"
252252
target_dir = os.path.join(str(tmpdir), target_name)
253253
pom_template = os.path.join(os.path.dirname(__file__), "check_home_pom.xml")
@@ -340,7 +340,7 @@ def test_check_home(self):
340340

341341
@unittest.skipUnless(util.is_maven_plugin_test_enabled, "ENABLE_MAVEN_PLUGIN_UNITTESTS is not true")
342342
def test_empty_packages(self):
343-
with tempfile.TemporaryDirectory() as tmpdir:
343+
with util.TemporaryTestDirectory() as tmpdir:
344344
target_name = "empty_packages_test"
345345
target_dir = os.path.join(str(tmpdir), target_name)
346346
pom_template = os.path.join(os.path.dirname(__file__), "check_packages_pom.xml")

graalpython/com.oracle.graal.python.test/src/tests/standalone/util.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,12 @@ def replace_in_file(file, str, replace_str):
333333
with open(file, "w") as f:
334334
f.write(contents.replace(str, replace_str))
335335

336-
def patch_properties_file(properties_file, distribution_url_override):
337-
if distribution_url_override:
338-
new_lines = []
339-
with(open(properties_file)) as f:
340-
while line := f.readline():
341-
line.strip()
342-
if not line.startswith("#") and "distributionUrl" in line:
343-
new_lines.append(f"distributionUrl={distribution_url_override}\n")
344-
else:
345-
new_lines.append(line)
346-
with(open(properties_file, "w")) as f:
347-
f.writelines(new_lines)
336+
337+
def override_gradle_properties_file(gradle_project_root):
338+
if override_file:=os.environ.get('GRADLE_PROPERTIES_OVERRIDE'):
339+
shutil.copy(override_file, os.path.join(gradle_project_root, "gradle", "wrapper", "gradle-wrapper.properties"))
340+
341+
342+
def override_maven_properties_file(maven_project_root):
343+
if override_file:=os.environ.get('MAVEN_PROPERTIES_OVERRIDE'):
344+
shutil.copy(override_file, os.path.join(maven_project_root, ".mvn", "wrapper", "maven-wrapper.properties"))

mx.graalpython/mx_graalpython.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from pathlib import Path
4242
from textwrap import dedent
4343

44+
import mx_graalpython_gradleproject
4445
import mx_urlrewrites
4546

4647
if sys.version_info[0] < 3:
@@ -1276,9 +1277,11 @@ def graalpython_gate_runner(args, tasks):
12761277
mx_urlrewrites.rewriteurl('https://repo1.maven.org/maven2/'),
12771278
])
12781279

1279-
urls = get_wrapper_urls("graalpython/com.oracle.graal.python.test/src/tests/standalone/gradle/gradle-test-project/gradle/wrapper/gradle-wrapper.properties", ["distributionUrl"])
1280-
if "distributionUrl" in urls:
1281-
env["GRADLE_DISTRIBUTION_URL_OVERRIDE"] = mx_urlrewrites.rewriteurl(urls["distributionUrl"])
1280+
override_path = os.path.join(SUITE.get_mx_output_dir(), 'gradle-properties-override')
1281+
original_props_file = "graalpython/com.oracle.graal.python.test/src/tests/standalone/gradle/gradle-test-project/gradle/wrapper/gradle-wrapper.properties"
1282+
mx.copyfile(original_props_file, override_path)
1283+
mx_graalpython_gradleproject.patch_distributionUrl(override_path, original_props_file)
1284+
env['GRADLE_PROPERTIES_OVERRIDE'] = override_path
12821285

12831286
env["org.graalvm.maven.downloader.version"] = version
12841287
env["org.graalvm.maven.downloader.repository"] = f"{pathlib.Path(mvn_repo_path).as_uri()}/"
@@ -1309,9 +1312,11 @@ def graalpython_gate_runner(args, tasks):
13091312
mx_urlrewrites.rewriteurl('https://repo1.maven.org/maven2/'),
13101313
])
13111314

1312-
urls = get_wrapper_urls("graalpython/com.oracle.graal.python.test/src/tests/standalone/mvnw/.mvn/wrapper/maven-wrapper.properties", ["distributionUrl"])
1313-
if "distributionUrl" in urls:
1314-
env["MAVEN_DISTRIBUTION_URL_OVERRIDE"] = mx_urlrewrites.rewriteurl(urls["distributionUrl"])
1315+
override_path = os.path.join(SUITE.get_mx_output_dir(), 'maven-properties-override')
1316+
original_props_file = "graalpython/com.oracle.graal.python.test/src/tests/standalone/mvnw/.mvn/wrapper/maven-wrapper.properties"
1317+
mx.copyfile(original_props_file, override_path)
1318+
mx_graalpython_gradleproject.patch_distributionUrl(override_path, original_props_file, escape_colon=False)
1319+
env['MAVEN_PROPERTIES_OVERRIDE'] = override_path
13151320

13161321
env["org.graalvm.maven.downloader.version"] = version
13171322
env["org.graalvm.maven.downloader.repository"] = f"{pathlib.Path(mvn_repo_path).as_uri()}/"

mx.graalpython/mx_graalpython_gradleproject.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@
4848
import mx_urlrewrites
4949

5050

51+
def gradle_wrapper_properties(project_root):
52+
return os.path.join(project_root, "gradle", "wrapper", "gradle-wrapper.properties")
53+
54+
55+
def patch_distributionUrl(properties_file, original_file=None, escape_colon=True):
56+
def escape(s):
57+
return s.replace(':', '\\:') if escape_colon else s
58+
59+
def unescape(s):
60+
return s.replace('\\:', ':') if escape_colon else s
61+
62+
original_file = original_file if original_file else properties_file
63+
with open(properties_file, 'r') as f:
64+
wrapper_properties = [l.strip() for l in f.readlines()]
65+
found = False
66+
for i, line in enumerate(wrapper_properties):
67+
if line.strip().startswith('distributionUrl='):
68+
url = unescape(line[len('distributionUrl='):])
69+
new_url = mx_urlrewrites.rewriteurl(url)
70+
mx.logv("Rewritten Gradle distribution URL to: " + new_url)
71+
wrapper_properties[i] = 'distributionUrl=' + escape(new_url)
72+
found = True
73+
break
74+
if not found:
75+
mx.abort("Could not find 'distributionUrl' in " + original_file)
76+
mx.logvv(f'Patched gradle-wrapper.properties to:\n' + '\n'.join(wrapper_properties))
77+
with open(properties_file, 'w') as f:
78+
f.write('\n'.join(wrapper_properties) + '\n')
79+
80+
5181
class GradlePluginProject(mx.Distribution, mx.ClasspathDependency): # pylint: disable=too-many-instance-attributes
5282
"""
5383
Mx project and distribution for Gradle plugins projects. These have properties of both mx Java projects and
@@ -274,24 +304,8 @@ def _create_build_script(self, version: str | None = None):
274304
"""
275305
# Gradle wrapper
276306
shutil.copytree(os.path.join(self.subject.gradle_directory, "wrapper"), self.subject.get_output_root(), dirs_exist_ok=True)
277-
properties_path = os.path.join("gradle", "wrapper", "gradle-wrapper.properties")
278-
wrapper_properties_file = os.path.join(self.subject.get_output_root(), properties_path)
279-
with open(wrapper_properties_file, 'r') as f:
280-
wrapper_properties = [l.strip() for l in f.readlines()]
281-
found = False
282-
for i, line in enumerate(wrapper_properties):
283-
if line.strip().startswith('distributionUrl='):
284-
url = line[len('distributionUrl='):].replace('\\:', ':')
285-
new_url = mx_urlrewrites.rewriteurl(url)
286-
mx.logv("Rewritten Gradle distribution URL to: " + new_url)
287-
wrapper_properties[i] = 'distributionUrl=' + new_url.replace(':', '\\:')
288-
found = True
289-
break
290-
if not found:
291-
mx.abort("Could not find 'distributionUrl' in " + os.path.join(self.subject.gradle_directory, "wrapper", properties_path))
292-
mx.logvv(f'Patched {properties_path} to:\n' + '\n'.join(wrapper_properties))
293-
with open(wrapper_properties_file, 'w') as f:
294-
f.write('\n'.join(wrapper_properties))
307+
patch_distributionUrl(gradle_wrapper_properties(self.subject.get_output_root()),
308+
original_file=gradle_wrapper_properties(os.path.join(self.subject.gradle_directory, "wrapper")))
295309

296310
# build.gradle
297311
deps_decls = []
@@ -423,4 +437,4 @@ def _create_build_script(self, version: str | None = None):
423437
with open(settings_path, 'w+') as f:
424438
f.write(settings)
425439
mx.logv(f"Gradle settings written to {settings_path}")
426-
mx.logvv(settings)
440+
mx.logvv(settings)

0 commit comments

Comments
 (0)