Skip to content

Commit 760bfef

Browse files
robert-hhdpgeorge
authored andcommitted
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.
1 parent 22cd7fd commit 760bfef

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

micropython/upysh/upysh.py

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ def __repr__(self):
88
return ""
99

1010
def __call__(self, path="."):
11-
l = os.listdir(path)
11+
l = list(os.ilistdir(path))
1212
l.sort()
1313
for f in l:
14-
st = os.stat("%s/%s" % (path, f))
15-
if st[0] & 0x4000: # stat.S_IFDIR
16-
print(" <dir> %s" % f)
17-
else:
18-
print("% 8d %s" % (st[6], f))
14+
if f[1] == 0x4000: # stat.S_IFDIR
15+
print(" <dir> %s" % f[0])
16+
for f in l:
17+
if f[1] != 0x4000:
18+
if len(f) > 3:
19+
print("% 9d %s" % (f[3], f[0]))
20+
else:
21+
print(" %s" % f[0])
22+
try:
23+
st = os.statvfs(path)
24+
print("\n{:,d}k free".format(st[1] * st[3] // 1024))
25+
except:
26+
pass
1927

2028

2129
class PWD:
@@ -34,17 +42,6 @@ def __call__(self):
3442
return self.__repr__()
3543

3644

37-
pwd = PWD()
38-
ls = LS()
39-
clear = CLEAR()
40-
41-
cd = os.chdir
42-
mkdir = os.mkdir
43-
mv = os.rename
44-
rm = os.remove
45-
rmdir = os.rmdir
46-
47-
4845
def head(f, n=10):
4946
with open(f) as f:
5047
for i in range(n):
@@ -58,6 +55,22 @@ def cat(f):
5855
head(f, 1 << 30)
5956

6057

58+
def cp(s, t):
59+
try:
60+
if os.stat(t)[0] & 0x4000: # is directory
61+
t = t.rstrip("/") + "/" + s
62+
except OSError:
63+
pass
64+
buf = bytearray(512)
65+
buf_mv = memoryview(buf)
66+
with open(s, "rb") as s, open(t, "wb") as t:
67+
while True:
68+
n = s.readinto(buf)
69+
if n <= 0:
70+
break
71+
t.write(buf_mv[:n])
72+
73+
6174
def newfile(path):
6275
print("Type file contents line by line, finish with EOF (Ctrl+D).")
6376
with open(path, "w") as f:
@@ -70,6 +83,19 @@ def newfile(path):
7083
f.write("\n")
7184

7285

86+
def rm(d, recursive=False): # Remove file or tree
87+
try:
88+
if (os.stat(d)[0] & 0x4000) and recursive: # Dir
89+
for f in os.ilistdir(d):
90+
if f[0] != "." and f[0] != "..":
91+
rm("/".join((d, f[0]))) # File or Dir
92+
os.rmdir(d)
93+
else: # File
94+
os.remove(d)
95+
except:
96+
print("rm of '%s' failed" % d)
97+
98+
7399
class Man:
74100
def __repr__(self):
75101
return """
@@ -79,12 +105,20 @@ def __repr__(self):
79105
To see this help text again, type "man".
80106
81107
upysh commands:
82-
pwd, cd("new_dir"), ls, ls(...), head(...), cat(...)
83-
newfile(...), mv("old", "new"), rm(...), mkdir(...), rmdir(...),
84-
clear
108+
clear, ls, ls(...), head(...), cat(...), newfile(...)
109+
cp('src', 'dest'), mv('old', 'new'), rm(...)
110+
pwd, cd(...), mkdir(...), rmdir(...)
85111
"""
86112

87113

88114
man = Man()
115+
pwd = PWD()
116+
ls = LS()
117+
clear = CLEAR()
118+
119+
cd = os.chdir
120+
mkdir = os.mkdir
121+
mv = os.rename
122+
rmdir = os.rmdir
89123

90124
print(man)

0 commit comments

Comments
 (0)