Skip to content

Commit

Permalink
Collapse directory structure for VFS resources
Browse files Browse the repository at this point in the history
  • Loading branch information
georgestagg committed Aug 30, 2024
1 parent 34e2d17 commit 1c2e7a5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 37 deletions.
42 changes: 21 additions & 21 deletions _extensions/live/resources/live-runtime.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _extensions/live/resources/pyodide-worker.js

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions _extensions/live/templates/pyodide-setup.ojs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pyodideOjs = {
setupPython,
startPyodideWorker,
b64Decode,
collapsePath,
} = window._exercise_ojs_runtime;

const statusContainer = document.getElementById("exercise-loading-status");
Expand Down Expand Up @@ -59,19 +60,22 @@ pyodideOjs = {
file = name;
}

// Create directory tree, swallowing any `dir exists` errors
let path = file;
while (path = path.substr(0, path.lastIndexOf('/'))) {
try {
// Create directory tree, collapsing higher directory structure
let path = file = collapsePath(file);
try {
while (path = path.substr(0, path.lastIndexOf('/'))) {
await pyodide.FS.mkdir(path);
} catch (e) {
if (!(e.name == "ErrnoError" && e.errno == 20)) {
throw e;
}
}
return await pyodide.FS.writeFile(file, new Uint8Array(data));
} catch (e) {
if (e.name !== "ErrnoError") throw e;
// Ignore "dir exists" filesystem error
if (e.errno !== 20) {
const errorTextPtr = await pyodide._module._strerror(e.errno);
const errorText = await pyodide._module.UTF8ToString(errorTextPtr);
throw new Error(`Filesystem Error ${e.errno} "${errorText}".`);
}
}

return await pyodide.FS.writeFile(file, new Uint8Array(data));
}).reduce((cur, next) => cur.then(next), Promise.resolve());

statusText.textContent = `Pyodide environment setup`;
Expand Down
13 changes: 10 additions & 3 deletions _extensions/live/templates/webr-setup.ojs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
webROjs = {
const { WebR } = window._exercise_ojs_runtime.WebR;
const { WebREvaluator, WebREnvironmentManager, setupR, b64Decode } = window._exercise_ojs_runtime;
const {
WebREvaluator,
WebREnvironmentManager,
setupR,
b64Decode,
collapsePath
} = window._exercise_ojs_runtime;

const statusContainer = document.getElementById("exercise-loading-status");
const indicatorContainer = document.getElementById("exercise-loading-indicator");
Expand Down Expand Up @@ -60,12 +66,13 @@ webROjs = {
file = name;
}

// Create directory tree, swallowing any `dir exists` errors
let path = file;
// Create directory tree, collapsing higher directory structure
let path = file = collapsePath(file);
while (path = path.substr(0, path.lastIndexOf('/'))) {
try {
await webR.FS.mkdir(path);
} catch (e) {
// Ignore "dir exists" filesystem error
if (!e.message.includes("FS error")) {
throw e;
}
Expand Down
3 changes: 2 additions & 1 deletion docs/other/resources.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ summary(mod)
## Python

### Source

````{.markdown filename="resources.qmd"}
---
format: live-html
resources:
- data
---

````{.markdown filename="resources.qmd"}
```{{pyodide}}
from os import listdir
import pandas as pd
Expand Down
5 changes: 4 additions & 1 deletion live-runtime/src/live-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { WebREnvironment, PyodideEnvironment } from './environment';
import { WebRGrader } from './grader-webr';
import { PyodideGrader } from './grader-pyodide';
import { comlinkTransfer, imageBitmapTransfer, mapTransfer, proxyTransfer } from './pyodide-proxy';
import { b64Encode, b64Decode } from './utils';
import { b64Encode, b64Decode, collapsePath } from './utils';
import './css/live-runtime.css';
import './css/highlighting.css';

Expand Down Expand Up @@ -68,6 +68,7 @@ declare global {
startPyodideWorker: typeof startPyodideWorker;
b64Decode: typeof b64Decode;
b64Encode: typeof b64Encode;
collapsePath: typeof collapsePath;
};
}
}
Expand All @@ -90,6 +91,7 @@ window._exercise_ojs_runtime = {
startPyodideWorker,
b64Encode,
b64Decode,
collapsePath,
};

export {
Expand All @@ -110,4 +112,5 @@ export {
startPyodideWorker,
b64Encode,
b64Decode,
collapsePath,
}
15 changes: 15 additions & 0 deletions live-runtime/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,18 @@ export function loadScriptAsync(url: string): Promise<any> {
document.getElementsByTagName("head")[0].appendChild(script);
});
}

export function collapsePath(path: string) {
const parts = path.replace(/\/+/g, '/').split('/');
const stack = [];
for (const part of parts) {
if (part === '.' || part === '') {
continue;
} else if (part === '..') {
stack.pop();
} else {
stack.push(part);
}
}
return stack.join('/');
}

0 comments on commit 1c2e7a5

Please sign in to comment.