def ls(file="", recursive=False, long=False): """ List command """ if useful.isfile(file): LsDisplayer("", False, long).show(file) elif useful.isdir(file): scandir(file, "*", recursive, LsDisplayer(file, True, long)) elif file == "": scandir(".", "*", recursive, LsDisplayer(".", True, long)) else: path, pattern = useful.split(file) scandir(path, pattern, recursive, LsDisplayer(path, False, long))
def grep(file, text, recursive=False, ignorecase=False, regexp=False): """ Grep command """ from re import search def __search(text, content, ignorecase, regexp): if ignorecase: content = content.lower() text = text.lower() if regexp: if search(text, content): return True else: if content.find(text) != -1: return True return False def __grep(text, filename, ignorecase, regexp, width, height, count): lineNumber = 1 with open(filename, "r", encoding="latin-1") as f: while 1: line = f.readline() if line: if __search(text, line, ignorecase, regexp): line = line.replace("\t", " ") message = "%s:%d:%s" % (filename, lineNumber, line) message = message.rstrip()[:width] count = printPart(message, width, height, count) if count == None: print("") return None lineNumber += 1 else: break return count if useful.isfile(file): filenames = [file] else: path, pattern = useful.split(file) directories, filenames = scandir(path, pattern, recursive) height, width = useful.getScreenSize() count = 1 for filename in filenames: count = __grep(text, filename, ignorecase, regexp, width, height, count) if count == None: break
def cp(source, destination, recursive=False, quiet=False): """ Copy file command """ filenames = [] directories = [] if useful.isfile(source): copyfile(source, destination, quiet) else: if useful.isdir(source): path = source pattern = "*" else: path, pattern = useful.split(source) directories, filenames = scandir(path, pattern, recursive) for src in filenames: dst = destination + "/" + src[len(path):] copyfile(src, dst, quiet)
def rmdir(directory, recursive=False, force=False, quiet=False, simulate=False): """ Remove directory """ if recursive == False: removedir(directory, force=force, quiet=quiet, simulate=simulate) else: directories = [directory] d = directory while 1: parts = useful.split(d) if parts[1] == "" or parts[0] == "": break directories.append(parts[0]) d = parts[0] for d in directories: if useful.exists(d) and d != ".": removedir(d, force=force, quiet=quiet, simulate=simulate)
def rm(file, recursive=False, quiet=False, force=False, simulate=False): """ Remove file command """ filenames = [] directories = [] if useful.isfile(file): path = file rmfile(file, force=force, quiet=quiet, simulate=simulate) else: if useful.isdir(file): if recursive: directories.append(file) path = file pattern = "*" else: path = None pattern = None else: path, pattern = useful.split(file) if path == None: print("Cannot rm '%s'" % file) else: dirs, filenames = scandir(path, pattern, recursive) directories += dirs for filename in filenames: rmfile(filename, force=force, quiet=quiet, simulate=simulate) if recursive: directories.sort() directories.reverse() for directory in directories: rmdir(directory, recursive=recursive, force=force, quiet=quiet, simulate=simulate)
async def STOR(self): """ Ftp command STOR """ await self.sendResponse(150, b"Start receive file") self.log(b"Ftp receive file '%s'"%(self.root + self.path)) filename = self.root + self.path if useful.ismicropython(): try: self.writeFile(filename, self.pasvsocket) except Exception as err: self.logDebug(useful.exception(err)) directory, file = useful.split(useful.tostrings(filename)) useful.makedir(directory, True) self.writeFile(filename, self.pasvsocket) else: with open(filename, "wb") as file: data = b" " while len(data) > 0: data = self.pasvsocket.recv(1440) file.write(data) data = b"" await self.sendResponse(226, b"End receive file") self.closePasv()
def copyfile(src, dst, quiet): """ Copy file """ dst = dst.replace("//", "/") dst = dst.replace("//", "/") dstdir, dstfile = useful.split(dst) try: if not useful.exists(dstdir): if dstdir != "." and dstdir != "": mkdir(dstdir, recursive=True, quiet=quiet) src_file = open(src, 'rb') dst_file = open(dst, 'wb') if quiet == False: print("cp '%s' -> '%s'" % (src, dst)) while True: buf = src_file.read(256) if len(buf) > 0: dst_file.write(buf) if len(buf) < 256: break src_file.close() dst_file.close() except: print("Cannot cp '%s' -> '%s'" % (src, dst))
def find(file): """ Find a file in directories """ path, pattern = useful.split(file) directories, filenames = scandir(path, pattern, True) for filename in filenames: print(filename)