Skip to content

Commit 68d7101

Browse files
committed
[GR-58103] JBang integration templates do not work.
PullRequest: graalpython/3479
2 parents 8f69c73 + e69a09f commit 68d7101

File tree

17 files changed

+397
-221
lines changed

17 files changed

+397
-221
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:24.2.0}
4+
//PIP
5+
// one blank after PIP
6+
//PIP
7+
// three blanks after PIP
8+
//PIP
9+
10+
public class EmptyPIPComments {
11+
public static void main(String[] args) {
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:24.2.0}
4+
//PYTHON_RESOURCES_DIRECTORY
5+
6+
public class EmptyPythonResourceComment {
7+
public static void main(String[] args) {
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:24.2.0}
4+
// resource dir with blanks
5+
//PYTHON_RESOURCES_DIRECTORY
6+
7+
public class EmptyPythonResourceCommentWithBlanks {
8+
public static void main(String[] args) {
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:24.2.0}
4+
//PYTHON_RESOURCES_DIRECTORY python-resources
5+
6+
public class NoPackagesResourcesDir {
7+
public static void main(String[] args) {
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:24.2.0}
4+
//PYTHON_RESOURCES_DIRECTORY
5+
//PYTHON_RESOURCES_DIRECTORY
6+
7+
public class TwoPythonResourceComments {
8+
public static void main(String[] args) {
9+
}
10+
}

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

Lines changed: 179 additions & 24 deletions
Large diffs are not rendered by default.

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ def get_gp():
8585

8686
return graalpy
8787

88-
def replace_in_file(file, str, replace_str):
89-
with open(file, "r") as f:
90-
contents = f.read()
91-
assert str in contents
92-
with open(file, "w") as f:
93-
f.write(contents.replace(str, replace_str))
94-
9588
def patch_properties_file(properties_file, distribution_url_override):
9689
if distribution_url_override:
9790
new_lines = []
@@ -290,10 +283,10 @@ def test_gradle_generated_app_external_resources(self):
290283
shutil.copyfile(os.path.join(target_dir, "src", "main", "resources", "org.graalvm.python.vfs", "src", "hello.py"), os.path.join(src_dir, "hello.py"))
291284
shutil.rmtree(os.path.join(target_dir, "src", "main", "resources", "org.graalvm.python.vfs"))
292285
# patch GraalPy.java
293-
replace_in_file(os.path.join(target_dir, "src", "main", "java", "org", "example", "GraalPy.java"),
286+
util.replace_in_file(os.path.join(target_dir, "src", "main", "java", "org", "example", "GraalPy.java"),
294287
"package org.example;",
295288
"package org.example;\nimport java.nio.file.Path;")
296-
replace_in_file(os.path.join(target_dir, "src", "main", "java", "org", "example", "GraalPy.java"),
289+
util.replace_in_file(os.path.join(target_dir, "src", "main", "java", "org", "example", "GraalPy.java"),
297290
"GraalPyResources.createContext()",
298291
"GraalPyResources.contextBuilder(Path.of(\"python-resources\")).build()")
299292

@@ -336,7 +329,7 @@ def test_gradle_fail_without_graalpy_dep(self):
336329

337330
gradle_cmd = util.get_gradle_wrapper(target_dir, self.env)
338331

339-
replace_in_file(build_file,
332+
util.replace_in_file(build_file,
340333
"implementation(\"org.graalvm.python:python-community:24.2.0\")",
341334
"// implementation(\"org.graalvm.python:python-community:24.2.0\")")
342335

@@ -498,9 +491,9 @@ def copy_build_files(self, target_dir):
498491
mvn_repos = ""
499492
for idx, custom_repo in enumerate(custom_repos.split(",")):
500493
mvn_repos += f"maven {{ url \"{custom_repo}\" }}\n "
501-
replace_in_file(build_file,
494+
util.replace_in_file(build_file,
502495
"repositories {", f"repositories {{\n mavenLocal()\n {mvn_repos}")
503-
replace_in_file(settings_file,
496+
util.replace_in_file(settings_file,
504497
"repositories {", f"repositories {{\n {mvn_repos}")
505498

506499
#print_file(build_file)
@@ -576,8 +569,8 @@ def copy_build_files(self, target_dir):
576569
for idx, custom_repo in enumerate(custom_repos.split(",")):
577570
mvn_repos += f"maven(url=\"{custom_repo}\")\n "
578571

579-
replace_in_file(build_file, "repositories {", f"repositories {{\n mavenLocal()\n {mvn_repos}")
580-
replace_in_file(settings_file, "repositories {", f"repositories {{\n {mvn_repos}")
572+
util.replace_in_file(build_file, "repositories {", f"repositories {{\n mavenLocal()\n {mvn_repos}")
573+
util.replace_in_file(settings_file, "repositories {", f"repositories {{\n {mvn_repos}")
581574

582575
#print_file(build_file)
583576
#print_file(settings_file)
@@ -727,15 +720,15 @@ def test_generated_app_external_resources(self):
727720
shutil.copyfile(os.path.join(target_dir, "src", "main", "resources", "org.graalvm.python.vfs", "src", "hello.py"), os.path.join(src_dir, "hello.py"))
728721
shutil.rmtree(os.path.join(target_dir, "src", "main", "resources", "org.graalvm.python.vfs"))
729722
# patch GraalPy.java
730-
replace_in_file(os.path.join(target_dir, "src", "main", "java", "it", "pkg", "GraalPy.java"),
723+
util.replace_in_file(os.path.join(target_dir, "src", "main", "java", "it", "pkg", "GraalPy.java"),
731724
"package it.pkg;",
732725
"package it.pkg;\nimport java.nio.file.Path;")
733-
replace_in_file(os.path.join(target_dir, "src", "main", "java", "it", "pkg", "GraalPy.java"),
726+
util.replace_in_file(os.path.join(target_dir, "src", "main", "java", "it", "pkg", "GraalPy.java"),
734727
"GraalPyResources.createContext()",
735728
"GraalPyResources.contextBuilder(Path.of(\"python-resources\")).build()")
736729

737730
# patch pom.xml
738-
replace_in_file(os.path.join(target_dir, "pom.xml"),
731+
util.replace_in_file(os.path.join(target_dir, "pom.xml"),
739732
"<packages>",
740733
"<pythonResourcesDirectory>${project.basedir}/python-resources</pythonResourcesDirectory>\n<packages>")
741734

@@ -804,7 +797,7 @@ def test_gen_launcher_and_venv(self):
804797
util.check_ouput("termcolor", out, False)
805798

806799
# remove ujson pkg from plugin config and check if unistalled
807-
replace_in_file(os.path.join(target_dir, "pom.xml"), "<package>ujson</package>", "")
800+
util.replace_in_file(os.path.join(target_dir, "pom.xml"), "<package>ujson</package>", "")
808801

809802
cmd = mvnw_cmd + ["process-resources"]
810803
out, return_code = util.run_cmd(cmd, self.env, cwd=target_dir)
@@ -847,15 +840,15 @@ def test_check_home(self):
847840
# 2. process-resources with empty pythonHome includes and excludes
848841
shutil.copyfile(pom_template, os.path.join(target_dir, "pom.xml"))
849842
util.patch_pom_repositories(os.path.join(target_dir, "pom.xml"))
850-
replace_in_file(os.path.join(target_dir, "pom.xml"), "</configuration>", "<pythonHome></pythonHome></configuration>")
843+
util.replace_in_file(os.path.join(target_dir, "pom.xml"), "</configuration>", "<pythonHome></pythonHome></configuration>")
851844
out, return_code = util.run_cmd(process_resources_cmd, self.env, cwd=target_dir)
852845
util.check_ouput("BUILD SUCCESS", out)
853846
util.check_ouput("Copying std lib to ", out, False)
854847
self.check_tagfile(target_dir, [f'{self.graalvmVersion}\n', 'include:.*\n'])
855848

856849
shutil.copyfile(pom_template, os.path.join(target_dir, "pom.xml"))
857850
util.patch_pom_repositories(os.path.join(target_dir, "pom.xml"))
858-
replace_in_file(os.path.join(target_dir, "pom.xml"), "</configuration>", "<pythonHome><includes></includes><excludes></excludes></pythonHome></configuration>")
851+
util.replace_in_file(os.path.join(target_dir, "pom.xml"), "</configuration>", "<pythonHome><includes></includes><excludes></excludes></pythonHome></configuration>")
859852
out, return_code = util.run_cmd(process_resources_cmd, self.env, cwd=target_dir)
860853
util.check_ouput("BUILD SUCCESS", out)
861854
util.check_ouput("Copying std lib to ", out, False)
@@ -875,7 +868,7 @@ def test_check_home(self):
875868
</excludes>
876869
</pythonHome>
877870
"""
878-
replace_in_file(os.path.join(target_dir, "pom.xml"), "</configuration>", home_tag + "</configuration>")
871+
util.replace_in_file(os.path.join(target_dir, "pom.xml"), "</configuration>", home_tag + "</configuration>")
879872
out, return_code = util.run_cmd(process_resources_cmd, self.env, cwd=target_dir)
880873
util.check_ouput("BUILD SUCCESS", out)
881874
util.check_ouput("Copying std lib to ", out, False)
@@ -892,7 +885,7 @@ def test_check_home(self):
892885
</excludes>
893886
</pythonHome>
894887
"""
895-
replace_in_file(os.path.join(target_dir, "pom.xml"), "</configuration>", home_tag + "</configuration>")
888+
util.replace_in_file(os.path.join(target_dir, "pom.xml"), "</configuration>", home_tag + "</configuration>")
896889
out, return_code = util.run_cmd(process_resources_cmd, self.env, cwd=target_dir)
897890
util.check_ouput("BUILD SUCCESS", out)
898891
util.check_ouput("Deleting GraalPy home due to changed includes or excludes", out)
@@ -925,7 +918,7 @@ def test_empty_packages(self):
925918
out, return_code = util.run_cmd(cmd, self.env, cwd=target_dir)
926919
util.check_ouput("BUILD SUCCESS", out)
927920

928-
replace_in_file(os.path.join(target_dir, "pom.xml"), "</packages>", "<package></package><package> </package></packages>")
921+
util.replace_in_file(os.path.join(target_dir, "pom.xml"), "</packages>", "<package></package><package> </package></packages>")
929922

930923
cmd = mvnw_cmd + ["process-resources"]
931924
out, return_code = util.run_cmd(cmd, self.env, cwd=target_dir)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,11 @@ def patch_pom_repositories(pom):
183183
""" + '\n'.join(pluginRepos) + """
184184
</pluginRepositories>
185185
</project>
186-
"""))
186+
"""))
187+
188+
def replace_in_file(file, str, replace_str):
189+
with open(file, "r") as f:
190+
contents = f.read()
191+
assert str in contents
192+
with open(file, "w") as f:
193+
f.write(contents.replace(str, replace_str))

graalpython/graalpy-jbang/examples/hello.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@
4040
*/
4141
///usr/bin/env jbang "$0" "$@" ; exit $?
4242
//JAVA 17+
43-
//DEPS org.graalvm.python:python-language:${env.GRAALPY_VERSION:24.0.0}
44-
//DEPS org.graalvm.python:python-resources:${env.GRAALPY_VERSION:24.0.0}
45-
//DEPS org.graalvm.python:python-launcher:${env.GRAALPY_VERSION:24.0.0}
46-
//DEPS org.graalvm.python:python-embedding:${env.GRAALPY_VERSION:24.0.0}
47-
//PIP termcolor
43+
//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:24.2.0}
44+
// specify python packages and their versions as if used with pip
45+
//PIP termcolor==2.2
4846

4947
import org.graalvm.polyglot.Context;
5048
import org.graalvm.polyglot.PolyglotException;
@@ -56,7 +54,7 @@ public static void main(String[] args) {
5654
try (Context context = GraalPyResources.createContext()) {
5755
switch (args.length) {
5856
case 0:
59-
context.eval("python", "print('Hello from Python')");
57+
context.eval("python", "from termcolor import colored; print(print(colored('hello java', 'red', attrs=['reverse', 'blink'])))");
6058
break;
6159
case 1:
6260
context.eval("python", args[0]);

graalpython/graalpy-jbang/templates/graalpy-template.java.qute

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
{/for}
66
{#if dependencies.isEmpty()}// //DEPS <dependency1> <dependency2>{/if}
77
{|
8-
//DEPS org.graalvm.python:python-language:${env.GRAALPY_VERSION:24.1.0}
9-
//DEPS org.graalvm.python:python-resources:${env.GRAALPY_VERSION:24.1.0}
10-
//DEPS org.graalvm.python:python-launcher:${env.GRAALPY_VERSION:24.1.0}
11-
//DEPS org.graalvm.python:python-embedding:${env.GRAALPY_VERSION:24.1.0}
12-
//DEPS org.graalvm.python:python-embedding-tools:${env.GRAALPY_VERSION:24.1.0}
13-
//PIP termcolor
8+
//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:24.2.0}
9+
// specify python packages and their versions as if used with pip
10+
//PIP termcolor==2.2
1411
|}
1512
import org.graalvm.polyglot.Context;
1613
import org.graalvm.polyglot.Context.Builder;

graalpython/graalpy-jbang/templates/graalpy-template_local_repo.java.qute

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
//REPOS mc=https://repo1.maven.org/maven2/
99
//REPOS local=file://{path_to_local_repo}
1010
{|
11-
//DEPS org.graalvm.python:python-language:${env.GRAALPY_VERSION:24.1.0}
12-
//DEPS org.graalvm.python:python-resources:${env.GRAALPY_VERSION:24.1.0}
13-
//DEPS org.graalvm.python:python-launcher:${env.GRAALPY_VERSION:24.1.0}
14-
//DEPS org.graalvm.python:python-embedding:${env.GRAALPY_VERSION:24.1.0}
15-
//DEPS org.graalvm.python:python-embedding-tools:${env.GRAALPY_VERSION:24.1.0}
16-
//PIP termcolor
11+
//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:24.2.0}
12+
// specify python packages and their versions as if used with pip
13+
//PIP termcolor==2.2
1714
|}
1815
import org.graalvm.polyglot.Context;
1916
import org.graalvm.polyglot.Context.Builder;

graalpython/org.graalvm.python.embedding.tools/src/META-INF/jbang-integration.list

Lines changed: 0 additions & 1 deletion
This file was deleted.

graalpython/org.graalvm.python.embedding.tools/src/org/graalvm/python/embedding/tools/exec/SubprocessLog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ default void subProcessOut(CharSequence out) {
4747
}
4848

4949
default void subProcessErr(CharSequence err) {
50-
System.out.println(err);
50+
System.err.println(err);
5151
}
5252

5353
default void log(CharSequence txt) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.graalvm.python.jbang.JBangIntegration

0 commit comments

Comments
 (0)