Skip to content

Commit f62c8f4

Browse files
fix(dev): devices were shown as busy at end of script
1 parent 7491810 commit f62c8f4

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed

src/Device.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ class Device {
244244
return result;
245245
}
246246

247+
/**
248+
* Run a Python user script on this device
249+
* Running user scripts sets device.action to null. This ensures that they're shown correctly in the GUI.
250+
* @param {string} script
251+
* @param {import("micropython-ctl-cont").RunScriptOptions=} options
252+
*/
253+
async runUserScript(script, options) {
254+
script = "# user script\n" + script;
255+
this.runScript(script, options);
256+
}
257+
247258
/**
248259
* Creates a MicroPythonDevice
249260
*/
@@ -256,6 +267,7 @@ class Device {
256267
adapter.__proxyMeta.beforeEachCall(({ item }) => {
257268
this.action.set(item.field.toString());
258269
this.busy.set(true);
270+
if (item.field.toString() === "runScript" && item.args[0].startsWith("# user script\n")) this.action.set(null);
259271
});
260272

261273
// emit line break to trigger a `>>>`. This triggers the `busyStatusUpdater`

src/Watcher/DeviceManager.js

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,7 @@ class DeviceManager {
108108

109109
restartScript(modulesToDelete) {
110110
this.log.log("restart script");
111-
this.device.runScript(
112-
[
113-
"print('')",
114-
`print("[dev] \'${modulesToDelete[1]}\' changed. Restarting... ")`,
115-
"for name in sys.modules:",
116-
' if(hasattr(sys.modules[name], "__file__")):',
117-
` if sys.modules[name].__file__ in ${JSON.stringify(modulesToDelete)}:`,
118-
' print("[dev] Clear module: " + sys.modules[name].__file__)',
119-
" del sys.modules[name]",
120-
"try:",
121-
" print('[dev] Import boot.py')",
122-
" import boot",
123-
"except ImportError:",
124-
" print('[dev] No boot.py found. Skipped.')",
125-
"except Exception:",
126-
" print('[dev] Exception in boot.py')",
127-
" raise",
128-
"try:",
129-
" print('[dev] Import main.py')",
130-
" import main",
131-
"except KeyboardInterrupt: pass",
132-
"except ImportError:",
133-
" print('[dev] No main.py found. Skipped.')",
134-
"except Exception as e: raise e;",
135-
"",
136-
].join("\r\n"),
137-
{ resolveBeforeResult: true }
138-
);
111+
this.device.runUserScript(scripts.restart(modulesToDelete), { resolveBeforeResult: false });
139112
}
140113

141114
/**

src/Watcher/scripts.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const rmDoubleSlash = (str) => str.replace(/\/+/, "/");
2+
3+
const scripts = {
4+
addPymakrToPath: (rootPath) =>
5+
[
6+
"import sys",
7+
"try:",
8+
` sys.path.index('${rmDoubleSlash(rootPath + "/_pymakr_dev")}')`,
9+
"except Exception as e:",
10+
" if(e.args[0] == 'object not in sequence'):",
11+
` sys.path.append('${rmDoubleSlash(rootPath + "/_pymakr_dev")}')`,
12+
" else:",
13+
" raise",
14+
].join("\r\n"),
15+
restart: (modulesToDelete) =>
16+
[
17+
"print('')",
18+
modulesToDelete[1] && `print("[dev] \'${modulesToDelete[1]}\' changed. Restarting... ")`,
19+
"for name in sys.modules:",
20+
' if(hasattr(sys.modules[name], "__file__")):',
21+
` if sys.modules[name].__file__ in ${JSON.stringify(modulesToDelete)}:`,
22+
' print("[dev] Clear module: " + sys.modules[name].__file__)',
23+
" del sys.modules[name]",
24+
"try:",
25+
" print('[dev] Import boot.py')",
26+
" import boot",
27+
"except ImportError:",
28+
" print('[dev] No boot.py found. Skipped.')",
29+
"except Exception:",
30+
" print('[dev] Exception in boot.py')",
31+
" raise",
32+
"try:",
33+
" print('[dev] Import main.py')",
34+
" import main",
35+
"except KeyboardInterrupt: pass",
36+
"except ImportError:",
37+
" print('[dev] No main.py found. Skipped.')",
38+
"except Exception as e: raise e;",
39+
"",
40+
]
41+
.filter(Boolean)
42+
.join("\r\n"),
43+
};
44+
45+
module.exports = { scripts };

0 commit comments

Comments
 (0)