55
55
import java .nio .file .Paths ;
56
56
57
57
/**
58
- * This class provides utilities related to Python resources used in GraalPy embedding scenarios.
58
+ * This class provides utilities related to Python resources used in GraalPy embedding scenarios
59
+ * which can be of the following kind:
60
+ * <ul>
61
+ * <li>Python application files</li>
62
+ * <li>Third-party Python packages</li>
63
+ * <li>The Python standard library</li>
64
+ * </ul>
65
+ *
59
66
* <p>
60
67
* Resource files can be embedded and distributed in an <b>application file</b> or made available
61
68
* from an <b>external directory</b>.
62
69
* </p>
63
70
*
71
+ * <h3>Virtual Filesystem</h3>
64
72
* <p>
65
- * If the resource files are part of a <b>jar file</b> or a <b> native image</b> executable, then at
66
- * runtime they will be accessed as standard Java resources through GraalPy
73
+ * If the resource files are part of an <b>application file</b> (jar file or a native image
74
+ * executable), then at runtime they will be accessed as standard Java resources through GraalPy
67
75
* {@link VirtualFileSystem}. This will be transparent to Python code running in GraalPy so that it
68
- * can use standard Python IO to access those files. Note that in order to make this work, it is
69
- * necessary for those embedded resources to have their <b>root directory</b> set to
70
- * <code>/org.graalvm.python.vfs</code> which in python code will be mapped to the virtual
71
- * filesystem mount point, by default <code>/graalpy_vfs</code>. Refer to
76
+ * can use standard Python IO to access those files.
77
+ * </p>
78
+ *
79
+ * <p>
80
+ * In order to make this work, it is necessary for those embedded resources to have their <b>root
81
+ * directory</b> set to <code>/org.graalvm.python.vfs</code> which in python code will be mapped to
82
+ * the virtual filesystem <b>mount point</b>, by default <code>/graalpy_vfs</code>. Refer to
72
83
* {@link VirtualFileSystem.Builder} documentation for more details.
73
84
* </p>
85
+ *
86
+ * <h3>External Directory</h3>
87
+ * <p>
88
+ * As an alternative to Java resources with the Virtual Filesystem, it is also possible to configure
89
+ * the GraalPy context to use an external directory, which is not embedded as a Java resource into
90
+ * the resulting application. Python code will access the files directly from the real filesystem.
91
+ * </p>
92
+ *
93
+ * <h3>Conventions</h3>
94
+ * <p>
95
+ * The factory methods in GraalPyResources rely on the following conventions:
96
+ * <ul>
97
+ * <li>${resources_root}/src: used for Python application files. This directory will be configured
98
+ * as the default search path for Python module files (equivalent to PYTHONPATH environment
99
+ * variable).</li>
100
+ * <li>${resources_root}/venv: used for the Python virtual environment holding installed third-party
101
+ * Python packages. The Context will be configured as if it is executed from this virtual
102
+ * environment. Notably packages installed in this virtual environment will be automatically
103
+ * available for importing.</li>
104
+ * <li>${resources_root}/home: used for the Python standard library (equivalent to PYTHONHOME
105
+ * environment variable).</li>
106
+ * </ul>
107
+ * where ${resources_root} is either the resource root <code>/org.graalvm.python.vfs</code> or an
108
+ * external directory.
109
+ * </p>
110
+ *
74
111
* <p>
75
112
* <b>Example</b> creating a GraalPy context configured for the usage with a
76
113
* {@link VirtualFileSystem}:
77
- *
114
+ * </p>
115
+ *
78
116
* <pre>
79
117
* VirtualFileSystem.Builder builder = VirtualFileSystem.newBuilder();
80
118
* builder.unixMountPoint("/python-resources");
101
139
* <p>
102
140
* <b>GraalPy context</b> instances created by factory methods in this class are preconfigured with
103
141
* some particular resource paths:
104
- * <li><code>${resources_root_directory}/venv</code> - is reserved for a python virtual environment
105
- * holding third-party packages. The context will be configured as if it were executed from this
106
- * virtual environment. Notably packages installed in this virtual environment will be automatically
142
+ * <li><code>${resources_root}/home</code> - is reserved for the GraalPy Standard Library. GraalPy
143
+ * context will be configured to use this standard library as if set in PYTHONHOME environment
144
+ * variable.</li>
145
+ * <li><code>${resources_root}/venv</code> - is reserved for a python virtual environment holding
146
+ * third-party packages. The context will be configured as if it were executed from this virtual
147
+ * environment. Notably packages installed in this virtual environment will be automatically
107
148
* available for importing.</li>
108
- * <li><code>${resources_root_directory }/src</code> - is reserved for python application files -
109
- * e.g. python sources. GraalPy context will be configured to see those files as if set in
110
- * PYTHONPATH environment variable.</li>
149
+ * <li><code>${resources_root }/src</code> - is reserved for python application files - e.g. python
150
+ * sources. GraalPy context will be configured to see those files as if set in PYTHONPATH
151
+ * environment variable.</li>
111
152
* </ul>
112
- * where <code>${resources_root_directory }</code> is either an external directory or the virtual
113
- * filesystem resource root <code>/org.graalvm.python.vfs</code>.
153
+ * where <code>${resources_root }</code> is either an external directory or the virtual filesystem
154
+ * resource root <code>/org.graalvm.python.vfs</code>.
114
155
* </p>
115
156
* <p>
116
157
* <b>Example</b> creating a GraalPy context configured for the usage with an external resource
144
185
*
145
186
* @see VirtualFileSystem
146
187
* @see VirtualFileSystem.Builder
188
+ *
189
+ * @since 24.2.0
147
190
*/
148
191
public final class GraalPyResources {
149
192
@@ -162,6 +205,9 @@ private GraalPyResources() {
162
205
* <li><code>/org.graalvm.python.vfs/src</code> - is set as the python sources location</li>
163
206
* </ul>
164
207
* </p>
208
+ *
209
+ * @return a new {@link Context} instance
210
+ * @since 24.2.0
165
211
*/
166
212
public static Context createContext () {
167
213
return contextBuilder ().build ();
@@ -196,6 +242,8 @@ public static Context createContext() {
196
242
*
197
243
* @see <a href=
198
244
* "https://github.com/oracle/graalpython/blob/master/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java">PythonOptions</a>
245
+ * @return a new {@link org.graalvm.polyglot.Context.Builder} instance
246
+ * @since 24.2.0
199
247
*/
200
248
public static Context .Builder contextBuilder () {
201
249
VirtualFileSystem vfs = VirtualFileSystem .create ();
@@ -242,10 +290,13 @@ public static Context.Builder contextBuilder() {
242
290
* <li>use the context to invoke a python snippet reading a resource file</li>
243
291
* </ul>
244
292
* </p>
245
- *
293
+ *
294
+ * @param vfs the {@link VirtualFileSystem} to be used with the created {@link Context}
295
+ * @return a new {@link org.graalvm.polyglot.Context.Builder} instance
246
296
* @see VirtualFileSystem
247
297
* @see VirtualFileSystem.Builder
248
- *
298
+ *
299
+ * @since 24.2.0
249
300
*/
250
301
public static Context .Builder contextBuilder (VirtualFileSystem vfs ) {
251
302
return createContextBuilder ().
@@ -299,6 +350,8 @@ public static Context.Builder contextBuilder(VirtualFileSystem vfs) {
299
350
* </p>
300
351
*
301
352
* @param resourcesDirectory the root directory with GraalPy specific embedding resources
353
+ * @return a new {@link org.graalvm.polyglot.Context.Builder} instance
354
+ * @since 24.2.0
302
355
*/
303
356
public static Context .Builder contextBuilder (Path resourcesDirectory ) {
304
357
String execPath ;
@@ -364,6 +417,8 @@ private static Context.Builder createContextBuilder() {
364
417
*
365
418
* @return the native executable path if it could be retrieved, otherwise <code>null</code>.
366
419
* @see #contextBuilder(Path)
420
+ *
421
+ * @since 24.2.0
367
422
*/
368
423
public static Path getNativeExecutablePath () {
369
424
if (ImageInfo .inImageRuntimeCode ()) {
@@ -406,8 +461,13 @@ public static Path getNativeExecutablePath() {
406
461
* </pre>
407
462
* </p>
408
463
*
464
+ * @param vfs the {@link VirtualFileSystem} from which resources are to be extracted
465
+ * @param resourcesDirectory the target directory to extract the resources to
466
+ * @throws IOException if resources isn't a directory
409
467
* @see #contextBuilder(Path)
410
468
* @see VirtualFileSystem.Builder#resourceLoadingClass(Class)
469
+ *
470
+ * @since 24.2.0
411
471
*/
412
472
public static void extractVirtualFileSystemResources (VirtualFileSystem vfs , Path resourcesDirectory ) throws IOException {
413
473
if (Files .exists (resourcesDirectory ) && !Files .isDirectory (resourcesDirectory )) {
0 commit comments