Skip to content

Commit 2371135

Browse files
committed
is_removable: correct behavior, added python backend
dotnet was not easily capable of doing this correctly python: simplify
1 parent 221cd89 commit 2371135

File tree

11 files changed

+43
-33
lines changed

11 files changed

+43
-33
lines changed

+stdlib/+dotnet/is_removable.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
%% DOTNET.IS_REMOVABLE detect removable drive
22
%
3+
% This does NOT detect USB flash drives, so we DON'T automatically use it.
4+
%
35
% Ref: https://learn.microsoft.com/en-us/dotnet/api/system.io.drivetype
46

57
function y = is_removable(filepath)

+stdlib/+python/create_symlink.m

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
function ok = create_symlink(target, link)
2-
arguments
3-
target (1,1) string
4-
link (1,1) string
5-
end
62

73
ok = false;
84

9-
if ~stdlib.exists(target) || stdlib.strempty(link) || stdlib.exists(link)
5+
t = py.str(target);
6+
l = py.str(link);
7+
8+
if ~py.os.path.exists(t) || py.os.path.exists(l)
109
return
1110
end
1211

1312
try
14-
py.os.symlink(target, link);
13+
py.os.symlink(t, l);
1514
ok = true;
1615
catch e
1716
warning(e.identifier, "create_symlink(%s, %s) failed: %s", target, link, e.message);

+stdlib/+python/device.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
function i = device(file)
2-
arguments
3-
file (1,1) string
4-
end
52

63
try
74
i = int64(py.os.stat(file).st_dev);

+stdlib/+python/disk_available.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
function i = disk_available(file)
2-
arguments
3-
file (1,1) string
4-
end
52

63
i = disk_usage(file, 'free');
74

+stdlib/+python/disk_capacity.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
function i = disk_capacity(file)
2-
arguments
3-
file (1,1) string
4-
end
52

63
i = disk_usage(file, 'total');
74

+stdlib/+python/filesystem_type.m

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
function t = filesystem_type(file)
2-
arguments
3-
file (1,1) string
4-
end
52

63
t = '';
74

85
% important for heuristic matching
9-
if ~stdlib.exists(file)
6+
p = py.str(file);
7+
if ~py.os.path.exists(p)
108
return
119
end
1210

13-
pr = stdlib.absolute(file);
11+
p = py.os.path.abspath(p);
1412

15-
try %#ok<TRYNC>
16-
for part = py.psutil.disk_partitions(file)
13+
% https://psutil.readthedocs.io/en/stable/index.html?highlight=disk_partitions#psutil.disk_partitions
14+
try
15+
for part = py.psutil.disk_partitions()
1716
prt = part{1};
18-
if startsWith(pr, char(prt.mountpoint))
17+
if p.startswith(prt.mountpoint)
1918
t = char(prt.fstype);
2019
return
2120
end
2221
end
22+
catch e
23+
rethrow(e)
2324
end
2425

2526
end

+stdlib/+python/hard_link_count.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
function c = hard_link_count(file)
2-
arguments
3-
file (1,1) string
4-
end
52

63
try
74
c = double(py.os.stat(file).st_nlink);

+stdlib/+python/inode.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
function i = inode(file)
2-
arguments
3-
file (1,1) string
4-
end
52

63
try
74
i = int64(py.os.stat(file).st_ino);

+stdlib/+python/is_removable.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function y = is_removable(file)
2+
3+
y = false;
4+
5+
% important for heuristic matching
6+
p = py.str(file);
7+
if ~py.os.path.exists(p)
8+
return
9+
end
10+
11+
p = py.os.path.abspath(p);
12+
13+
% https://psutil.readthedocs.io/en/stable/index.html?highlight=disk_partitions#psutil.disk_partitions
14+
try
15+
for part = py.psutil.disk_partitions()
16+
prt = part{1};
17+
if p.startswith(prt.mountpoint)
18+
y = contains(string(prt.opts), ["cdrom", "removable"]);
19+
return
20+
end
21+
end
22+
catch e
23+
rethrow(e)
24+
end
25+
26+
end

+stdlib/+python/is_symlink.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
function y = is_symlink(file)
2-
arguments
3-
file (1,1) string
4-
end
52

63
try
74
y = py.pathlib.Path(file).is_symlink();

0 commit comments

Comments
 (0)