Skip to content

Commit ec4e591

Browse files
committed
Properly set up sys.path, add logging to debug-exports
1 parent a08ca3b commit ec4e591

File tree

8 files changed

+106
-6
lines changed

8 files changed

+106
-6
lines changed

runtime/src/jycessing/RunnableSketch.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,44 @@
1111
*
1212
*/
1313
public interface RunnableSketch {
14+
/**
15+
* @return The main file of the sketch
16+
*/
1417
public abstract File getMainFile();
18+
19+
/**
20+
* @return The primary code to run
21+
*/
1522
public abstract String getMainCode();
23+
24+
/**
25+
* @return The main directory of the sketch
26+
*/
1627
public abstract File getHomeDirectory();
28+
29+
/**
30+
* @return Files to append to sys.path
31+
*/
32+
public abstract List<File> getPathEntries();
33+
34+
/**
35+
* @return Arguments to pass to PApplet
36+
*/
1737
public abstract String[] getPAppletArguments();
38+
39+
/**
40+
* @return Directories to search for Processing libraries
41+
*/
1842
public abstract List<File> getLibraryDirectories();
43+
44+
/**
45+
* @return How eagerly we should look for libraries
46+
*/
1947
public abstract LibraryPolicy getLibraryPolicy();
20-
public abstract boolean shouldRun(); // should probably be true, unless you're a warmup sketch
48+
49+
/**
50+
* Should probably be true, unless you're a warmup sketch
51+
* @return Whether the sketch should run or not
52+
*/
53+
public abstract boolean shouldRun();
2154
}

runtime/src/jycessing/Runner.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ public LibraryPolicy getLibraryPolicy() {
242242
public boolean shouldRun() {
243243
return false;
244244
}
245+
246+
@Override
247+
public List<File> getPathEntries() {
248+
final List<File> entries = new ArrayList<>();
249+
entries.add(getHomeDirectory());
250+
return entries;
251+
}
245252
}
246253

247254
/**
@@ -294,8 +301,10 @@ public synchronized static void runSketchBlocking(final RunnableSketch sketch,
294301
try {
295302
final InteractiveConsole interp = new InteractiveConsole();
296303

297-
// Add the sketch directory to the Python library path for auxilliary modules.
298-
sys.path.insert(0, Py.newString(sketchDirPath));
304+
// Add all of the sketch's requested sys.path entries
305+
for (final File entry : sketch.getPathEntries()) {
306+
sys.path.insert(0, Py.newString(entry.getAbsolutePath()));
307+
}
299308

300309
// For moar useful error messages.
301310
interp.set("__file__", sketch.getMainFile().getAbsolutePath());

runtime/src/jycessing/launcher/StandaloneSketch.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public StandaloneSketch(final String[] args) throws Exception {
8383

8484
// In case the sketch path points to "internal" we get it from the wrapper
8585
if (argsList.contains("--internal")) {
86-
this.sketchPath = new File(getRuntimeRoot(), "Runtime/sketch.py");
86+
this.sketchPath = new File(getRuntimeRoot(), "Runtime/sketch.py").getAbsoluteFile();
8787
} else {
88-
this.sketchPath = new File(args[args.length - 1]);
88+
this.sketchPath = new File(args[args.length - 1]).getAbsoluteFile();
8989
}
9090

9191
// Debug when using launcher
@@ -175,4 +175,19 @@ public boolean shouldRun() {
175175
return true;
176176
}
177177

178+
@Override
179+
public List<File> getPathEntries() {
180+
final List<File> entries = new ArrayList<>();
181+
182+
// Main sketch folder
183+
entries.add(sketchPath.getParentFile());
184+
185+
for (final File dir : getLibraryDirectories()) {
186+
// In case the user has added python libraries in their library directories
187+
entries.add(dir);
188+
}
189+
190+
return entries;
191+
}
192+
178193
}

runtime/src/jycessing/mode/export/ExportedSketch.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class ExportedSketch implements RunnableSketch {
2626
public static final String ARGS_EXPORTED = "--exported";
2727

2828
private final File sketchPath;
29+
private final File sketchHome;
2930
private final String code;
3031
private final DisplayType displayType;
3132
private final String backgroundColor;
@@ -39,6 +40,7 @@ public class ExportedSketch implements RunnableSketch {
3940
public ExportedSketch(final String[] args) throws Exception {
4041
// The last argument is the path to the sketch
4142
this.sketchPath = new File(args[args.length - 1]).getAbsoluteFile();
43+
this.sketchHome = sketchPath.getParentFile().getParentFile();
4244

4345
if (!sketchPath.exists()) {
4446
throw new FileNotFoundException("Something is terribly wrong - I can't find your sketch!");
@@ -97,7 +99,7 @@ public String getMainCode() {
9799

98100
@Override
99101
public File getHomeDirectory() {
100-
return sketchPath.getParentFile().getParentFile();
102+
return sketchHome;
101103
}
102104

103105
@Override
@@ -137,4 +139,15 @@ public boolean shouldRun() {
137139
return true;
138140
}
139141

142+
@Override
143+
public List<File> getPathEntries() {
144+
final List<File> entries = new ArrayList<>();
145+
146+
entries.add(sketchHome);
147+
entries.add(new File(sketchHome, "source"));
148+
entries.add(new File(sketchHome, "lib"));
149+
150+
return entries;
151+
}
152+
140153
}

runtime/src/jycessing/mode/export/LinuxExport.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ private void buildShellScript(final File destFolder, final boolean embedJava) th
136136

137137
options.add("-splash:\"$APPDIR/lib/jycessing/splash.png\"");
138138

139+
if (PythonMode.VERBOSE) {
140+
// If we're in debug mode, assume we want our exports to be, too
141+
options.add("-Dverbose=true");
142+
}
143+
139144
// Class to run
140145
options.add("jycessing.Runner");
141146

runtime/src/jycessing/mode/export/MacExport.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ private void setUpExecutable(final File binFolder, final File processingFolder,
224224
options.add("-Xdock:icon=\"$CONTENTS/Resources/sketch.icns\"");
225225
options.add("-Xdock:name=\"" + sketchName + "\"");
226226

227+
if (PythonMode.VERBOSE) {
228+
// If we're in debug mode, assume we want our exports to be, too
229+
options.add("-Dverbose=true");
230+
}
231+
227232
// Class to run
228233
options.add("jycessing.Runner");
229234

runtime/src/jycessing/mode/run/PdeSketch.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,16 @@ public LibraryPolicy getLibraryPolicy() {
124124
public boolean shouldRun() {
125125
return true;
126126
}
127+
128+
@Override
129+
public List<File> getPathEntries() {
130+
final List<File> entries = new ArrayList<>();
131+
entries.add(sketchHome);
132+
entries.add(new File(sketchHome, "source"));
133+
final File code = new File(sketchHome, "code");
134+
if (code.exists()) {
135+
entries.add(code);
136+
}
137+
return entries;
138+
}
127139
}

testing/src/test/jycessing/TestSketch.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.nio.file.Path;
55
import java.nio.file.Paths;
6+
import java.util.ArrayList;
67
import java.util.List;
78

89
import processing.core.PApplet;
@@ -61,4 +62,11 @@ public boolean shouldRun() {
6162
return true;
6263
}
6364

65+
@Override
66+
public List<File> getPathEntries() {
67+
final List<File> entries = new ArrayList<>();
68+
entries.add(getHomeDirectory());
69+
return entries;
70+
}
71+
6472
}

0 commit comments

Comments
 (0)