// https://github.com/scientifichackers/ampy/blob/master/ampy/files.py#L88 export const ls = (args = { directory: "/", recursive: false, includeSha256: false }) => { const { directory, recursive, includeSha256 } = args const finalDir = directory.startsWith("/") ? directory : "/" + directory let command = ` try: import os except ImportError: import uos as os ` if (includeSha256) { command += ` import sys import ubinascii import uhashlib def get_file_stats(filename): stat = os.stat(filename) size = stat[6] mtime = stat[8] hasher = uhashlib.sha256() with open(filename, 'rb') as infile: while True: result = infile.read(32) if result == b'': break hasher.update(result) sha256 = ubinascii.hexlify(hasher.digest()) return (size, mtime, sha256.decode()) ` } else { command += ` def get_file_stats(filename): stat = os.stat(filename) size = stat[6] mtime = stat[8] return (size, mtime, '') ` } if (recursive) { command += ` def listdir(directory): result = set() def _listdir(dir_or_file): try: # if its a directory, then it should provide some children. children = os.listdir(dir_or_file) except OSError: # probably a file. run stat() to confirm. file_stats = get_file_stats(dir_or_file) result.add((dir_or_file, False) + file_stats) else: # probably a directory, add to result if empty. result.add((dir_or_file, True, 0, 0, '')) if children: # queue the children to be dealt with in next iteration. for child in children: # create the full path. if dir_or_file == '/': next = dir_or_file + child else: next = dir_or_file + '/' + child _listdir(next) _listdir(directory) return sorted(result) ` } else { command += ` def listdir(directory): files = os.ilistdir(directory) out = [] for (filename, filetype, inode, _) in files: fn_full = "/" + filename if directory == '/' else directory + '/' + filename isdir = filetype == 0x4000 if isdir: out.append((fn_full, isdir, 0, 0, '')) else: file_stats = get_file_stats(fn_full) out.append((fn_full, isdir) + file_stats) return sorted(out) ` } command += ` for (filename, isdir, size, mtime, sha256) in listdir('${finalDir}'): print("%s | %s | %s | %s | %s" % (filename, 'd' if isdir else 'f', size, mtime, sha256)) # ` return command } export const manyPrints = (lines = 200) => { let ret = '' for (let i = 0; i < lines; i++) { ret += `print(${i})\n` } return ret } export const getFile = (filename: string) => { return ` import sys import ubinascii with open('${filename}', 'rb') as infile: while True: result = infile.read(32) if result == b'': break len = sys.stdout.write(ubinascii.hexlify(result)) #` // this # is needed, else we get an error (SyntaxError: invalid syntax) } export const getFileHash = (filename: string) => { return ` import sys import ubinascii import uhashlib hasher = uhashlib.sha256() with open('${filename}', 'rb') as infile: while True: result = infile.read(32) if result == b'': break hasher.update(result) sys.stdout.write(ubinascii.hexlify(hasher.digest())) ` // this # is needed, else we get an error (SyntaxError: invalid syntax) } export const stat = (path: string) => { return ` try: import os except ImportError: import uos as os try: s = os.stat('${path}') print('%s | %s' % ('f' if s[0] == 32768 else 'd', s[6])) except: print('x') #` } export const isFileTheSame = (filename: string, fileSize: number, sha256Hash: string) => { return ` try: import os except ImportError: import uos as os import sys import ubinascii import uhashlib def getHash(): hasher = uhashlib.sha256() with open('${filename}', 'rb') as infile: while True: result = infile.read(32) if result == b'': break hasher.update(result) return ubinascii.hexlify(hasher.digest()) def is_same(): try: s = os.stat('${filename}') except OSError: print('0') return if ${fileSize} != s[6]: print('0') else: hash = getHash().decode() print("1" if hash == '${sha256Hash}' else "0") is_same() #` } export const deleteEverythingRecurive = (path: string) => { return ` try: import os except ImportError: import uos as os def rmdir(directory): os.chdir(directory) for f in os.listdir(): try: os.remove(f) except OSError: pass for f in os.listdir(): rmdir(f) os.chdir('..') try: os.rmdir(directory) except OSError: pass rmdir('${path}') ` }