Skip to content

Commit aba5cdd

Browse files
committed
minor edits and cleanups for running standalone tests
(cherry picked from commit 999b6b3)
1 parent bd290f2 commit aba5cdd

File tree

4 files changed

+62
-87
lines changed

4 files changed

+62
-87
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_standalone.py

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848
if 'MAVEN_REPO_OVERRIDE' in os.environ:
4949
MVN_CMD += ['-Dmaven.repo.remote=' + os.environ['MAVEN_REPO_OVERRIDE']]
5050

51+
def run_cmd(cmd, env, cwd=None):
52+
process = subprocess.Popen(cmd, env=env, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, text=True)
53+
out = []
54+
for line in iter(process.stdout.readline, ""):
55+
print(line, end="")
56+
out.append(line)
57+
return "".join(out)
58+
5159
def get_executable(file):
5260
if os.path.isfile(file):
5361
return file
@@ -99,24 +107,15 @@ def test_polyglot_app():
99107
target_dir = os.path.join(tmpdir, "polyglot_app_test")
100108

101109
cmd = [graalpy, "-m", "standalone", "--verbose", "polyglot_app", "-o", target_dir]
102-
p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
103-
out = p.stdout.decode(errors='backslashreplace')
104-
print(p.stdout.decode(errors='backslashreplace'))
105-
print(p.stderr.decode(errors='backslashreplace'))
110+
out = run_cmd(cmd, env)
106111
assert "Creating polyglot java python application in directory " + target_dir in out
107112

108113
cmd = MVN_CMD + ["package", "-Pnative"]
109-
p = subprocess.run(cmd, cwd=target_dir, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
110-
out = p.stdout.decode(errors='backslashreplace')
111-
print(out)
112-
print(p.stderr.decode(errors='backslashreplace'))
114+
out = run_cmd(cmd, env, cwd=target_dir)
113115
assert "BUILD SUCCESS" in out
114116

115117
cmd = [os.path.join(target_dir, "target", "polyglot_app")]
116-
p = subprocess.run(cmd, cwd=target_dir, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
117-
out = p.stdout.decode(errors='backslashreplace')
118-
print(out)
119-
print(p.stderr.decode(errors='backslashreplace'))
118+
out = run_cmd(cmd, env, cwd=target_dir)
120119
assert out.endswith("hello java\n")
121120

122121

@@ -137,21 +136,15 @@ def test_native_executable_one_file():
137136
target_file = os.path.join(tmpdir, "hello")
138137
cmd = [graalpy, "-m", "standalone", "--verbose", "native", "-m", source_file, "-o", target_file]
139138

140-
p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
141-
out = p.stdout.decode(errors='backslashreplace')
142-
print(out)
143-
print(p.stderr.decode(errors='backslashreplace'))
139+
out = run_cmd(cmd, env)
144140
assert "Bundling Python resources into" in out
145141

146142
cmd = [target_file, "arg1", "arg2"]
147-
p = subprocess.run(" ".join(cmd), env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
148-
out = p.stdout.decode(errors='backslashreplace')
149-
print(out)
150-
print(p.stderr.decode(errors='backslashreplace'))
143+
out = run_cmd(cmd, env)
151144
assert "hello world, argv[1:]: " + str(cmd[1:]) in out
152145

153146
@unittest.skipUnless(is_enabled, "ENABLE_STANDALONE_UNITTESTS is not true")
154-
def test_native_executable_one_file_venv():
147+
def test_native_executable_venv_and_one_file():
155148
graalpy = get_gp()
156149
if graalpy is None:
157150
return
@@ -166,31 +159,19 @@ def test_native_executable_one_file_venv():
166159

167160
venv_dir = os.path.join(target_dir, "venv")
168161
cmd = [graalpy, "-m", "venv", venv_dir]
169-
p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
170-
out = p.stdout.decode(errors='backslashreplace')
171-
print(out)
172-
print(p.stderr.decode(errors='backslashreplace'))
162+
out = run_cmd(cmd, env)
173163

174164
venv_python = os.path.join(venv_dir, "Scripts", "python.cmd") if os.name == "nt" else os.path.join(venv_dir, "bin", "python")
175165
cmd = [venv_python, "-m", "pip", "--no-cache-dir", "install", "termcolor"]
176-
p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
177-
out = p.stdout.decode(errors='backslashreplace')
178-
print(out)
179-
print(p.stderr.decode(errors='backslashreplace'))
166+
out = run_cmd(cmd, env)
180167

181168
target_file = os.path.join(target_dir, "hello")
182169
cmd = [graalpy, "-m", "standalone", "--verbose", "native", "-Os", "-m", source_file, "--venv", venv_dir, "-o", target_file]
183-
p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
184-
out = p.stdout.decode(errors='backslashreplace')
185-
print(out)
186-
print(p.stderr.decode(errors='backslashreplace'))
170+
out = run_cmd(cmd, env)
187171
assert "Bundling Python resources into" in out
188172

189173
cmd = [target_file]
190-
p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
191-
out = p.stdout.decode(errors='backslashreplace')
192-
print(out)
193-
print(p.stderr.decode(errors='backslashreplace'))
174+
out = run_cmd(cmd, env)
194175

195176
assert "hello standalone world" in out
196177

@@ -219,15 +200,9 @@ def test_native_executable_module():
219200
target_file = os.path.join(tmp_dir, "hello")
220201
cmd = [graalpy, "-m", "standalone", "--verbose", "native", "-Os", "-m", module_dir, "-o", target_file]
221202

222-
p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
223-
out = p.stdout.decode(errors='backslashreplace')
224-
print(out)
225-
print(p.stderr.decode(errors='backslashreplace'))
203+
out = run_cmd(cmd, env)
226204
assert "Bundling Python resources into" in out
227205

228206
cmd = [target_file]
229-
p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
230-
out = p.stdout.decode(errors='backslashreplace')
231-
print(out)
232-
print(p.stderr.decode(errors='backslashreplace'))
207+
out = run_cmd(cmd, env)
233208
assert "hello standalone world" in out

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,25 +1075,6 @@ abstract static class GetGraalVmVersion extends PythonBuiltinNode {
10751075
@Specialization
10761076
TruffleString get() {
10771077
Version current = Version.getCurrent();
1078-
// TODO according to Version java doc, the output of toString is not standardized and may change without notice.
1079-
// Unfortunately, we have no way to tell what is the minimum count of version components and the snapshot suffix
1080-
// and have to guess is. For now, we can either do the same as version.toString does, or simply call it.
1081-
// String snapshotSuffix = "-dev";
1082-
// int min_version_components = 3;
1083-
//
1084-
// int i = 1;
1085-
// String format = "";
1086-
// while (true) {
1087-
// if (current.format("%[" + i + "XX]").isEmpty() && i > min_version_components) {
1088-
// break;
1089-
// }
1090-
// format += (format.isEmpty() ? "%d" : ".%d");
1091-
// i++;
1092-
// }
1093-
// String ret = current.format(format);
1094-
// if(current.isSnapshot()) {
1095-
// ret += snapshotSuffix;
1096-
// }
10971078
return TruffleString.fromJavaStringUncached(current.toString(), TS_ENCODING);
10981079
}
10991080
}

graalpython/lib-graalpython/modules/standalone/__main__.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,10 @@ def download_python(self):
449449
if self.parsed_args.verbose:
450450
print(f"downloading graalpython maven artefacts: {' '.join(cmd)}")
451451

452-
p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
453-
454452
if self.parsed_args.verbose:
455-
print(p.stdout.decode())
456-
print(p.stderr.decode())
453+
p = subprocess.run(cmd)
454+
else:
455+
p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
457456

458457
if p.returncode != 0:
459458
if not self.parsed_args.verbose:
@@ -467,9 +466,7 @@ def build_binary(self, ni, jc):
467466
os.chdir(self.target_dir)
468467

469468
try:
470-
modulepath = f"{self.modules_path}/org.graalvm.polyglot-polyglot-{self.graalvm_version}.jar"
471-
# it would seem it is enough to compile the launcher file, but on some linux setups it isn't
472-
cmd = [jc, "--module-path", modulepath, os.path.join(self.target_dir,MODULE_INFO_FILE), os.path.join(self.target_dir, MODULE_NAME, "VirtualFileSystem.java"), self.launcher_file]
469+
cmd = [jc, "--module-path", self.modules_path, os.path.join(self.target_dir,MODULE_INFO_FILE), os.path.join(self.target_dir, MODULE_NAME, "VirtualFileSystem.java"), self.launcher_file]
473470
if self.parsed_args.verbose:
474471
print(f"Compiling code for Python standalone entry point: {' '.join(cmd)}")
475472
p = subprocess.run(cmd, cwd=self.target_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -478,15 +475,8 @@ def build_binary(self, ni, jc):
478475
print(p.stderr.decode())
479476
exit(1)
480477

481-
modules = []
482-
for f in os.listdir(self.modules_path):
483-
if f.endswith("jar"):
484-
modules.append(f)
485-
mp = f":{self.modules_path}/".join(modules)
486-
modulepath = f"{self.modules_path}/{mp}:."
487-
488-
add_modules = "standalone"
489-
cmd = [ni, "--module-path", modulepath, "--add-modules", add_modules] + self.parsed_args.ni_args[:]
478+
ni_modules = ":".join([os.path.join(self.modules_path, f) for f in os.listdir(self.modules_path) if f.endswith(".jar")] + [self.target_dir])
479+
cmd = [ni, "--module-path", ni_modules] + self.parsed_args.ni_args[:]
490480

491481
if self.parsed_args.Os:
492482
cmd +=[
@@ -496,7 +486,7 @@ def build_binary(self, ni, jc):
496486
cmd += [
497487
"--no-fallback",
498488
"-H:-CopyLanguageResources",
499-
"-H:ResourceConfigurationFiles=native-image-resources.json",
489+
f"-H:ResourceConfigurationFiles={self.target_dir}/native-image-resources.json",
500490
"-o",
501491
output,
502492
f"{MODULE_NAME}.{NATIVE_EXEC_LAUNCHER}",

mx.graalpython/mx_graalpython.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import itertools
3232
import json
3333
import os
34+
import pathlib
3435
import re
3536
import shlex
3637
import shutil
@@ -1503,25 +1504,53 @@ def graalpython_gate_runner(args, tasks):
15031504

15041505
with Task('GraalPython standalone module tests', tasks, tags=[GraalPythonTags.unittest_standalone]) as task:
15051506
if task:
1507+
env = {}
1508+
env['ENABLE_STANDALONE_UNITTESTS'] = 'true'
1509+
env['MAVEN_REPO_OVERRIDE'] = mx_urlrewrites.rewriteurl('https://repo1.maven.org/maven2/')
15061510
# build graalvm jdk
15071511
mx_args = ['-p', os.path.join(mx.suite('truffle').dir, '..', 'vm'), '--env', 'ce']
1508-
mx.run_mx(mx_args + ["build", "--dep", f"GRAALVM_COMMUNITY_JAVA{get_jdk().javaCompliance}"])
1512+
if not DISABLE_REBUILD:
1513+
mx.run_mx(mx_args + ["build", "--dep", f"GRAALVM_COMMUNITY_JAVA{get_jdk().javaCompliance}"])
15091514
out = mx.OutputCapture()
15101515
mx.run_mx(mx_args + ["graalvm-home"], out=out)
15111516
home = out.data.splitlines()[-1].strip()
1512-
env = os.environ.copy()
1513-
env['ENABLE_STANDALONE_UNITTESTS'] = 'true'
1514-
env['MAVEN_REPO_OVERRIDE'] = mx_urlrewrites.rewriteurl('https://repo1.maven.org/maven2/')
15151517
env['JAVA_HOME'] = home
15161518
# build python standalone
15171519
mx_args = ['-p', os.path.join(mx.suite('truffle').dir, '..', 'vm'), '--env', 'ce-python']
1518-
mx.run_mx(mx_args + ["--native-images=", "build", "--dep", f"PYTHON_JAVA_STANDALONE_SVM_JAVA{get_jdk().javaCompliance}"])
1520+
if not DISABLE_REBUILD:
1521+
mx.run_mx(mx_args + ["build", "--dep", f"PYTHON_JAVA_STANDALONE_SVM_JAVA{get_jdk().javaCompliance}"])
15191522
out = mx.OutputCapture()
15201523
mx.run_mx(mx_args + ["standalone-home", "--type", "jvm", "python"], out=out)
15211524
python_home = out.data.splitlines()[-1].strip()
15221525
env['PYTHON_STANDALONE_HOME'] = python_home
1526+
# deploy maven artifacts
1527+
import mx_sdk_vm_impl
1528+
version = mx_sdk_vm_impl.graalvm_version('graalvm')
1529+
path = os.path.join(SUITE.get_mx_output_dir(), 'public-maven-repo')
1530+
licenses = ['EPL-2.0', 'PSF-License', 'GPLv2-CPE', 'ICU,GPLv2', 'BSD-simplified', 'BSD-new', 'UPL', 'MIT']
1531+
deploy_args = [
1532+
'--tags=public',
1533+
'--all-suites',
1534+
'--all-distribution-types',
1535+
f'--version-string={version}',
1536+
'--validate=none',
1537+
'--licenses', ','.join(licenses),
1538+
'--suppress-javadoc',
1539+
'local',
1540+
pathlib.Path(path).as_uri(),
1541+
]
1542+
if not DISABLE_REBUILD:
1543+
mx.rmtree(path, ignore_errors=True)
1544+
os.mkdir(path)
1545+
mx.maven_deploy(deploy_args)
1546+
# setup maven downloader overrides
1547+
env["org.graalvm.maven.downloader.version"] = version
1548+
env["org.graalvm.maven.downloader.repository"] = f"{pathlib.Path(path).as_uri()}/"
15231549
# run the test
1524-
run_python_unittests(python_gvm(), paths=["test_standalone.py"], javaAsserts=True, report=report(), env=env)
1550+
mx.logv(f"running with {env=}")
1551+
full_env = os.environ.copy()
1552+
full_env.update(env)
1553+
mx.run([sys.executable, _graalpytest_driver(), "-v", "graalpython/com.oracle.graal.python.test/src/tests/test_standalone.py"], env=full_env)
15251554

15261555
with Task('GraalPython Python tests', tasks, tags=[GraalPythonTags.tagged]) as task:
15271556
if task:

0 commit comments

Comments
 (0)