Skip to content

Commit 25c8306

Browse files
authored
Merge pull request #12 from makermelissa/main
bug fixes
2 parents 2bc32e7 + c748f5a commit 25c8306

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

repl.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export class FileOps {
8282
contents = btoa(byteString); // Convert binary string to base64
8383

8484
let code = `
85+
import os
8586
import binascii
8687
with open("${path}", "wb") as f:
8788
f.seek(${offset})
@@ -90,7 +91,8 @@ with open("${path}", "wb") as f:
9091
`;
9192

9293
if (modificationTime) {
93-
code += `os.utime("${path}", (os.path.getatime("${path}"), ${modificationTime}))\n`;
94+
modificationTime = Math.floor(modificationTime / 1000);
95+
code += `os.utime("${path}", (${modificationTime}, ${modificationTime}))\n`;
9496
}
9597
await this._repl.runCode(code);
9698
}
@@ -101,28 +103,26 @@ with open("${path}", "wb") as f:
101103
contents = contents.replace(/"/g, '\\"');
102104

103105
let code = `
106+
import os
104107
with open("${path}", "w") as f:
105108
f.seek(${offset})
106109
f.write("""${contents}""")
107110
`;
108111

109112
if (modificationTime) {
110-
code += `os.utime("${path}", (os.path.getatime("${path}"), ${modificationTime}))\n`;
113+
modificationTime = Math.floor(modificationTime / 1000);
114+
code += `os.utime("${path}", (${modificationTime}, ${modificationTime}))\n`;
111115
}
112116
await this._repl.runCode(code);
113117
}
114118

115119
// Write a file to the device path with contents beginning at offset. Modification time can be set and if raw is true, contents is written as binary
116120
async writeFile(path, contents, offset=0, modificationTime=null, raw=false) {
117-
this._repl.terminalOutput = DEBUG;
118-
119121
if (raw) {
120122
await this._writeRawFile(path, contents, offset, modificationTime);
121123
} else {
122124
await this._writeTextFile(path, contents, offset, modificationTime);
123125
}
124-
125-
this._repl.terminalOutput = true;
126126
}
127127

128128
async _readRawFile(path) {
@@ -178,22 +178,19 @@ with open("${path}", "r") as f:
178178
// Read a file from the device
179179
async readFile(path, raw=false) {
180180
let result;
181-
this._repl.terminalOutput = DEBUG;
182181

183182
if (raw) {
184183
result = await this._readRawFile(path);
185184
} else {
186185
result = await this._readTextFile(path);
187186
}
188187

189-
this._repl.terminalOutput = true;
190188
return result;
191189
}
192190

193191
// List files using paste mode on the device returning the result as a javascript array
194192
// We need the file name, whether or not it is a directory, file size and file date
195193
async listDir(path) {
196-
this._repl.terminalOutput = DEBUG;
197194
// Mask sure path has a trailing slash
198195
if (path[path.length - 1] != "/") {
199196
path += "/";
@@ -222,12 +219,10 @@ for item in contents:
222219
fileDate: parseInt(fileDate) * 1000,
223220
});
224221
}
225-
this._repl.terminalOutput = true;
226222
return contents;
227223
}
228224

229225
async isReadOnly() {
230-
this._repl.terminalOutput = DEBUG;
231226
// MicroPython doesn't have storage, but also doesn't have a CIRCUITPY drive
232227
let code = `
233228
try:
@@ -238,29 +233,28 @@ except:
238233
`;
239234
let result = await this._repl.runCode(code);
240235
let isReadOnly = result.match("True") != null;
241-
this._repl.terminalOutput = true;
242236

243237
return isReadOnly;
244238
}
245239

246240
async makeDir(path, modificationTime=null) {
247241
await this._checkReadOnly();
248-
this._repl.terminalOutput = DEBUG;
249-
let code = `os.mkdir("${path}")\n`;
242+
let code = `
243+
import os
244+
os.mkdir("${path}")
245+
`;
250246
if (modificationTime) {
251-
code += `os.utime("${path}", (os.path.getatime("${path}"), ${modificationTime}))\n`;
247+
modificationTime = Math.floor(modificationTime / 1000);
248+
code += `os.utime("${path}", (${modificationTime}, ${modificationTime}))\n`;
252249
}
253250
await this._repl.runCode(code);
254251
this._checkReplErrors();
255-
this._repl.terminalOutput = true;
256252
}
257253

258254
async delete(path) {
259255
await this._checkReadOnly();
260-
this._repl.terminalOutput = DEBUG;
261256
let code = `
262257
import os
263-
264258
stat = os.stat("${path}")
265259
if stat[0] == ${TYPE_FILE}:
266260
os.remove("${path}")
@@ -269,22 +263,20 @@ else:
269263
`;
270264
await this._repl.runCode(code);
271265
this._checkReplErrors();
272-
this._repl.terminalOutput = true;
273266
}
274267

275268
async move(oldPath, newPath) {
276269
await this._checkReadOnly();
277270
// we need to check if the new path already exists
278271
// Return true on success and false on failure
279272

280-
this._repl.terminalOutput = DEBUG;
281273
let code = `
282274
import os
283275
os.rename("${oldPath}", "${newPath}")
284276
`;
285277
await this._repl.runCode(code);
286278
let error = this._checkReplErrors();
287-
this._repl.terminalOutput = true;
279+
288280
return !error;
289281
}
290282
}
@@ -786,7 +778,7 @@ export class REPL {
786778
this.terminalOutput = DEBUG;
787779

788780
await this.getToPrompt();
789-
let result = this.execRawMode(code + LINE_ENDING_LF, codeTimeoutMs);
781+
let result = await this.execRawMode(code + LINE_ENDING_LF, codeTimeoutMs);
790782
this.terminalOutput = true;
791783
return result;
792784
}

0 commit comments

Comments
 (0)