1+ package qbubbles ;
2+
3+ import org .python .apache .commons .compress .utils .IOUtils ;
4+ import org .python .core .PySystemState ;
5+ import org .python .util .PythonInterpreter ;
6+
7+ import java .io .FileInputStream ;
8+ import java .io .IOException ;
9+ import java .net .URLConnection ;
10+ import java .nio .file .Files ;
11+ import java .nio .file .Path ;
12+ import java .nio .file .StandardOpenOption ;
13+ import java .util .jar .JarFile ;
14+ import java .util .zip .ZipEntry ;
15+ import java .util .zip .ZipFile ;
16+ import java .util .zip .ZipInputStream ;
17+
18+ public class Launcher {
19+ public static void main (String [] args ) throws IOException {
20+ System .setProperty ("python.import.site" , "false" );
21+
22+ PythonInterpreter .initialize (null , null , args );
23+ PySystemState state = new PySystemState ();
24+ Path tempDirectory = Files .createTempDirectory ("qbubbles_" );
25+ URLConnection connection = Launcher .class .getProtectionDomain ().getCodeSource ().getLocation ().openConnection ();
26+ try (var stream = new ZipInputStream (connection .getInputStream ())) {
27+ ZipEntry entry ;
28+ while ((entry = stream .getNextEntry ()) != null ) {
29+ copyEntry (entry , tempDirectory , stream );
30+ }
31+ }
32+
33+ state .path .add (0 , tempDirectory .toString ());
34+ state .setClassLoader (ClassLoader .getSystemClassLoader ());
35+ try (PythonInterpreter pythonInterpreter = new PythonInterpreter (null , state )) {
36+ pythonInterpreter .execfile (tempDirectory .resolve ("main.py" ).toString ());
37+ }
38+ }
39+
40+ private static void copyEntry (ZipEntry entry , Path tempDirectory , ZipInputStream stream ) throws IOException {
41+ if (entry .getName ().endsWith (".py" )) {
42+ String name = entry .getName ();
43+ if (name .startsWith ("/" )) name = name .substring (1 );
44+ if (name .equals ("main.py" ) || name .startsWith ("qbubbles/" )) {
45+ var path = tempDirectory .resolve (name );
46+ if (Files .notExists (path .getParent ())) {
47+ Files .createDirectories (path .getParent ());
48+ }
49+ byte [] bytes = stream .readAllBytes ();
50+ Files .write (path , bytes , StandardOpenOption .TRUNCATE_EXISTING , StandardOpenOption .CREATE );
51+ }
52+ }
53+ }
54+ }
0 commit comments