@@ -82,6 +82,7 @@ export class FileOps {
82
82
contents = btoa ( byteString ) ; // Convert binary string to base64
83
83
84
84
let code = `
85
+ import os
85
86
import binascii
86
87
with open("${ path } ", "wb") as f:
87
88
f.seek(${ offset } )
@@ -90,7 +91,8 @@ with open("${path}", "wb") as f:
90
91
` ;
91
92
92
93
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` ;
94
96
}
95
97
await this . _repl . runCode ( code ) ;
96
98
}
@@ -101,28 +103,26 @@ with open("${path}", "wb") as f:
101
103
contents = contents . replace ( / " / g, '\\"' ) ;
102
104
103
105
let code = `
106
+ import os
104
107
with open("${ path } ", "w") as f:
105
108
f.seek(${ offset } )
106
109
f.write("""${ contents } """)
107
110
` ;
108
111
109
112
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` ;
111
115
}
112
116
await this . _repl . runCode ( code ) ;
113
117
}
114
118
115
119
// 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
116
120
async writeFile ( path , contents , offset = 0 , modificationTime = null , raw = false ) {
117
- this . _repl . terminalOutput = DEBUG ;
118
-
119
121
if ( raw ) {
120
122
await this . _writeRawFile ( path , contents , offset , modificationTime ) ;
121
123
} else {
122
124
await this . _writeTextFile ( path , contents , offset , modificationTime ) ;
123
125
}
124
-
125
- this . _repl . terminalOutput = true ;
126
126
}
127
127
128
128
async _readRawFile ( path ) {
@@ -178,22 +178,19 @@ with open("${path}", "r") as f:
178
178
// Read a file from the device
179
179
async readFile ( path , raw = false ) {
180
180
let result ;
181
- this . _repl . terminalOutput = DEBUG ;
182
181
183
182
if ( raw ) {
184
183
result = await this . _readRawFile ( path ) ;
185
184
} else {
186
185
result = await this . _readTextFile ( path ) ;
187
186
}
188
187
189
- this . _repl . terminalOutput = true ;
190
188
return result ;
191
189
}
192
190
193
191
// List files using paste mode on the device returning the result as a javascript array
194
192
// We need the file name, whether or not it is a directory, file size and file date
195
193
async listDir ( path ) {
196
- this . _repl . terminalOutput = DEBUG ;
197
194
// Mask sure path has a trailing slash
198
195
if ( path [ path . length - 1 ] != "/" ) {
199
196
path += "/" ;
@@ -222,12 +219,10 @@ for item in contents:
222
219
fileDate : parseInt ( fileDate ) * 1000 ,
223
220
} ) ;
224
221
}
225
- this . _repl . terminalOutput = true ;
226
222
return contents ;
227
223
}
228
224
229
225
async isReadOnly ( ) {
230
- this . _repl . terminalOutput = DEBUG ;
231
226
// MicroPython doesn't have storage, but also doesn't have a CIRCUITPY drive
232
227
let code = `
233
228
try:
@@ -238,29 +233,28 @@ except:
238
233
` ;
239
234
let result = await this . _repl . runCode ( code ) ;
240
235
let isReadOnly = result . match ( "True" ) != null ;
241
- this . _repl . terminalOutput = true ;
242
236
243
237
return isReadOnly ;
244
238
}
245
239
246
240
async makeDir ( path , modificationTime = null ) {
247
241
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
+ ` ;
250
246
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` ;
252
249
}
253
250
await this . _repl . runCode ( code ) ;
254
251
this . _checkReplErrors ( ) ;
255
- this . _repl . terminalOutput = true ;
256
252
}
257
253
258
254
async delete ( path ) {
259
255
await this . _checkReadOnly ( ) ;
260
- this . _repl . terminalOutput = DEBUG ;
261
256
let code = `
262
257
import os
263
-
264
258
stat = os.stat("${ path } ")
265
259
if stat[0] == ${ TYPE_FILE } :
266
260
os.remove("${ path } ")
@@ -269,22 +263,20 @@ else:
269
263
` ;
270
264
await this . _repl . runCode ( code ) ;
271
265
this . _checkReplErrors ( ) ;
272
- this . _repl . terminalOutput = true ;
273
266
}
274
267
275
268
async move ( oldPath , newPath ) {
276
269
await this . _checkReadOnly ( ) ;
277
270
// we need to check if the new path already exists
278
271
// Return true on success and false on failure
279
272
280
- this . _repl . terminalOutput = DEBUG ;
281
273
let code = `
282
274
import os
283
275
os.rename("${ oldPath } ", "${ newPath } ")
284
276
` ;
285
277
await this . _repl . runCode ( code ) ;
286
278
let error = this . _checkReplErrors ( ) ;
287
- this . _repl . terminalOutput = true ;
279
+
288
280
return ! error ;
289
281
}
290
282
}
@@ -786,7 +778,7 @@ export class REPL {
786
778
this . terminalOutput = DEBUG ;
787
779
788
780
await this . getToPrompt ( ) ;
789
- let result = this . execRawMode ( code + LINE_ENDING_LF , codeTimeoutMs ) ;
781
+ let result = await this . execRawMode ( code + LINE_ENDING_LF , codeTimeoutMs ) ;
790
782
this . terminalOutput = true ;
791
783
return result ;
792
784
}
0 commit comments