kopia lustrzana https://github.com/micropython/micropython-lib
micropython/upysh: Add the cp() function and improve ls and rm.
- cp() copies a file. If the target is a directory, the file is copied into that directory. It uses a small buffer, so it's not fast. - ls uses ilistdir and creates a sorted output with directories listed as the first group. - rm optionally deletes recursive, if the target is a directory.pull/419/merge
rodzic
22cd7fdd64
commit
760bfefd9d
|
@ -8,14 +8,22 @@ class LS:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def __call__(self, path="."):
|
def __call__(self, path="."):
|
||||||
l = os.listdir(path)
|
l = list(os.ilistdir(path))
|
||||||
l.sort()
|
l.sort()
|
||||||
for f in l:
|
for f in l:
|
||||||
st = os.stat("%s/%s" % (path, f))
|
if f[1] == 0x4000: # stat.S_IFDIR
|
||||||
if st[0] & 0x4000: # stat.S_IFDIR
|
print(" <dir> %s" % f[0])
|
||||||
print(" <dir> %s" % f)
|
for f in l:
|
||||||
else:
|
if f[1] != 0x4000:
|
||||||
print("% 8d %s" % (st[6], f))
|
if len(f) > 3:
|
||||||
|
print("% 9d %s" % (f[3], f[0]))
|
||||||
|
else:
|
||||||
|
print(" %s" % f[0])
|
||||||
|
try:
|
||||||
|
st = os.statvfs(path)
|
||||||
|
print("\n{:,d}k free".format(st[1] * st[3] // 1024))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class PWD:
|
class PWD:
|
||||||
|
@ -34,17 +42,6 @@ class CLEAR:
|
||||||
return self.__repr__()
|
return self.__repr__()
|
||||||
|
|
||||||
|
|
||||||
pwd = PWD()
|
|
||||||
ls = LS()
|
|
||||||
clear = CLEAR()
|
|
||||||
|
|
||||||
cd = os.chdir
|
|
||||||
mkdir = os.mkdir
|
|
||||||
mv = os.rename
|
|
||||||
rm = os.remove
|
|
||||||
rmdir = os.rmdir
|
|
||||||
|
|
||||||
|
|
||||||
def head(f, n=10):
|
def head(f, n=10):
|
||||||
with open(f) as f:
|
with open(f) as f:
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
|
@ -58,6 +55,22 @@ def cat(f):
|
||||||
head(f, 1 << 30)
|
head(f, 1 << 30)
|
||||||
|
|
||||||
|
|
||||||
|
def cp(s, t):
|
||||||
|
try:
|
||||||
|
if os.stat(t)[0] & 0x4000: # is directory
|
||||||
|
t = t.rstrip("/") + "/" + s
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
buf = bytearray(512)
|
||||||
|
buf_mv = memoryview(buf)
|
||||||
|
with open(s, "rb") as s, open(t, "wb") as t:
|
||||||
|
while True:
|
||||||
|
n = s.readinto(buf)
|
||||||
|
if n <= 0:
|
||||||
|
break
|
||||||
|
t.write(buf_mv[:n])
|
||||||
|
|
||||||
|
|
||||||
def newfile(path):
|
def newfile(path):
|
||||||
print("Type file contents line by line, finish with EOF (Ctrl+D).")
|
print("Type file contents line by line, finish with EOF (Ctrl+D).")
|
||||||
with open(path, "w") as f:
|
with open(path, "w") as f:
|
||||||
|
@ -70,6 +83,19 @@ def newfile(path):
|
||||||
f.write("\n")
|
f.write("\n")
|
||||||
|
|
||||||
|
|
||||||
|
def rm(d, recursive=False): # Remove file or tree
|
||||||
|
try:
|
||||||
|
if (os.stat(d)[0] & 0x4000) and recursive: # Dir
|
||||||
|
for f in os.ilistdir(d):
|
||||||
|
if f[0] != "." and f[0] != "..":
|
||||||
|
rm("/".join((d, f[0]))) # File or Dir
|
||||||
|
os.rmdir(d)
|
||||||
|
else: # File
|
||||||
|
os.remove(d)
|
||||||
|
except:
|
||||||
|
print("rm of '%s' failed" % d)
|
||||||
|
|
||||||
|
|
||||||
class Man:
|
class Man:
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return """
|
return """
|
||||||
|
@ -79,12 +105,20 @@ from upysh import *
|
||||||
To see this help text again, type "man".
|
To see this help text again, type "man".
|
||||||
|
|
||||||
upysh commands:
|
upysh commands:
|
||||||
pwd, cd("new_dir"), ls, ls(...), head(...), cat(...)
|
clear, ls, ls(...), head(...), cat(...), newfile(...)
|
||||||
newfile(...), mv("old", "new"), rm(...), mkdir(...), rmdir(...),
|
cp('src', 'dest'), mv('old', 'new'), rm(...)
|
||||||
clear
|
pwd, cd(...), mkdir(...), rmdir(...)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
man = Man()
|
man = Man()
|
||||||
|
pwd = PWD()
|
||||||
|
ls = LS()
|
||||||
|
clear = CLEAR()
|
||||||
|
|
||||||
|
cd = os.chdir
|
||||||
|
mkdir = os.mkdir
|
||||||
|
mv = os.rename
|
||||||
|
rmdir = os.rmdir
|
||||||
|
|
||||||
print(man)
|
print(man)
|
||||||
|
|
Ładowanie…
Reference in New Issue