Skip to content

Commit 6023deb

Browse files
committed
gradle plugin tests
1 parent 2b9d94f commit 6023deb

File tree

11 files changed

+681
-9
lines changed

11 files changed

+681
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id "application"
3+
id "org.graalvm.python"
4+
id "org.graalvm.buildtools.native" version "0.10.2"
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
application {
12+
mainClass = "org.example.GraalPy"
13+
}
14+
15+
dependencies {
16+
implementation("org.graalvm.python:python-community:24.2.0")
17+
}
18+
19+
run {
20+
enableAssertions = true
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
plugins {
2+
application
3+
id("org.graalvm.python")
4+
id("org.graalvm.buildtools.native") version "0.10.2"
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
application {
12+
// Define the main class for the application.
13+
mainClass = "org.example.GraalPy"
14+
}
15+
16+
// XXX how to run with asserts on, aka -ea?
17+
//run {
18+
// enableAssertions = true
19+
//}
20+
val r = tasks.run.get()
21+
r.enableAssertions = true
22+
r.outputs.upToDateWhen {false}
23+
24+
dependencies {
25+
implementation("org.graalvm.python:python-community:24.2.0")
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
rootProject.name = "graalpy-gradle-test-project"
2+
3+
buildscript {
4+
repositories {
5+
maven {
6+
url "https://repo.gradle.org/gradle/libs-releases/"
7+
}
8+
}
9+
dependencies {
10+
classpath "org.graalvm.python:graalpy-gradle-plugin:24.2.0"
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
rootProject.name = "graalpy-gradle-test-project"
2+
3+
buildscript {
4+
repositories {
5+
maven(url="https://repo.gradle.org/gradle/libs-releases/")
6+
}
7+
dependencies {
8+
classpath("org.graalvm.python:graalpy-gradle-plugin:24.2.0")
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.example;
42+
43+
import org.graalvm.polyglot.Context;
44+
import org.graalvm.polyglot.PolyglotException;
45+
import org.graalvm.polyglot.Source;
46+
import org.graalvm.polyglot.Value;
47+
import java.io.IOException;
48+
49+
import org.graalvm.python.embedding.utils.GraalPyResources;
50+
51+
public class GraalPy {
52+
private static final String PYTHON = "python";
53+
54+
public static void main(String[] args) {
55+
try (Context context = GraalPyResources.createContext()) {
56+
Source source;
57+
try {
58+
source = Source.newBuilder(PYTHON, "import hello", "<internal>").internal(true).build();
59+
} catch (IOException e) {
60+
throw new RuntimeException(e);
61+
}
62+
63+
context.eval(source);
64+
65+
// retrieve the python PyHello class
66+
Value pyHelloClass = context.getPolyglotBindings().getMember("PyHello");
67+
Value pyHello = pyHelloClass.newInstance();
68+
// and cast it to the Hello interface which matches PyHello
69+
Hello hello = pyHello.as(Hello.class);
70+
hello.hello("java");
71+
72+
} catch (PolyglotException e) {
73+
if (e.isExit()) {
74+
System.exit(e.getExitStatus());
75+
} else {
76+
throw e;
77+
}
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.example;
42+
43+
public interface Hello {
44+
void hello(String txt);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
["org.example.Hello"]
3+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
import polyglot
40+
from termcolor import colored
41+
42+
class PyHello:
43+
def hello(self, txt):
44+
colored_text = colored("hello " + str(txt), "red", attrs=["reverse", "blink"])
45+
print(colored_text)
46+
47+
# We export the PyHello class to Java as our explicit interface with the Java side
48+
polyglot.export_value("PyHello", PyHello)

0 commit comments

Comments
 (0)