17
17
18
18
import java .io .BufferedReader ;
19
19
import java .io .File ;
20
+ import java .io .FileFilter ;
20
21
import java .io .FileReader ;
22
+ import java .io .FilenameFilter ;
21
23
import java .io .IOException ;
22
24
import java .io .Reader ;
25
+ import java .lang .reflect .Field ;
26
+ import java .lang .reflect .Method ;
27
+ import java .net .URL ;
28
+ import java .net .URLClassLoader ;
23
29
24
30
import org .python .core .Py ;
25
31
import org .python .core .PyString ;
28
34
import processing .core .PApplet ;
29
35
30
36
public class Runner {
37
+ static boolean VERBOSE = false ;
38
+
31
39
private static String read (final Reader r ) throws IOException {
32
40
final BufferedReader reader = new BufferedReader (r );
33
41
final StringBuilder sb = new StringBuilder (1024 );
@@ -42,13 +50,73 @@ private static String read(final Reader r) throws IOException {
42
50
}
43
51
}
44
52
53
+ // See http://robertmaldon.blogspot.com/2007/11/dynamically-add-to-eclipse-junit.html
54
+ private static void addJar (final URL url ) throws Exception {
55
+ final URLClassLoader classLoader = (URLClassLoader )ClassLoader .getSystemClassLoader ();
56
+ final Method method = URLClassLoader .class .getDeclaredMethod ("addURL" , URL .class );
57
+ method .setAccessible (true );
58
+ method .invoke (classLoader , new Object [] { url });
59
+ if (VERBOSE ) {
60
+ System .err .println ("Added " + url + " to classpath." );
61
+ }
62
+ }
63
+
64
+ // See http://forums.sun.com/thread.jspa?threadID=707176
65
+ private static void addLibraryPath (final String newPath ) throws Exception {
66
+ final Field field = ClassLoader .class .getDeclaredField ("usr_paths" );
67
+ field .setAccessible (true );
68
+ final String [] paths = (String [])field .get (null );
69
+ for (final String path : paths ) {
70
+ if (newPath .equals (path )) {
71
+ return ;
72
+ }
73
+ }
74
+ final String [] tmp = new String [paths .length + 1 ];
75
+ System .arraycopy (paths , 0 , tmp , 0 , paths .length );
76
+ tmp [paths .length ] = newPath ;
77
+ field .set (null , tmp );
78
+ if (VERBOSE ) {
79
+ System .err .println ("Added " + newPath + " to java.library.path." );
80
+ }
81
+ }
82
+
83
+ private static void searchForExtraStuff (final File dir ) throws Exception {
84
+ final File [] dlls = dir .listFiles (new FilenameFilter () {
85
+ public boolean accept (final File dir , final String name ) {
86
+ return name .matches ("^.+\\ .(so|dll|jnilib)$" );
87
+ }
88
+ });
89
+ if (dlls != null && dlls .length > 0 ) {
90
+ addLibraryPath (dir .getAbsolutePath ());
91
+ }
92
+ final File [] jars = dir .listFiles (new FilenameFilter () {
93
+ public boolean accept (final File dir , final String name ) {
94
+ return name .matches ("^.+\\ .jar$" );
95
+ }
96
+ });
97
+ for (final File jar : jars ) {
98
+ addJar (jar .toURI ().toURL ());
99
+ }
100
+ final File [] dirs = dir .listFiles (new FileFilter () {
101
+ public boolean accept (final File f ) {
102
+ return f .isDirectory () && f .getName ().charAt (0 ) != '.' ;
103
+ }
104
+ });
105
+ for (final File d : dirs ) {
106
+ searchForExtraStuff (d );
107
+ }
108
+ }
109
+
45
110
public static void main (final String [] args ) throws Exception {
46
111
if (args .length < 1 ) {
47
112
System .err .println ("I need the path of your Python script as an argument." );
48
113
}
114
+ if (Boolean .getBoolean ("verbose" )) {
115
+ VERBOSE = true ;
116
+ }
49
117
final String pathname = args [args .length - 1 ];
50
118
final String text = read (new FileReader (pathname ));
51
-
119
+ searchForExtraStuff ( new File ( "libraries" ));
52
120
Py .initPython ();
53
121
final InteractiveConsole interp = new InteractiveConsole ();
54
122
final String path = new File (pathname ).getCanonicalFile ().getParent ();
0 commit comments