Skip to content

Commit 9f286a7

Browse files
committed
inode,device via python
1 parent 552d240 commit 9f286a7

File tree

9 files changed

+72
-44
lines changed

9 files changed

+72
-44
lines changed

+stdlib/device.m

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,10 @@
88

99
i = [];
1010

11-
if ispc() && stdlib.has_dotnet()
12-
h = NET.addAssembly('System.Management');
13-
14-
r = stdlib.root_name(path);
15-
queryLogicalDisk = sprintf('SELECT VolumeSerialNumber FROM Win32_LogicalDisk WHERE Caption = ''%s''', r);
16-
17-
% Create a ManagementObjectSearcher instance
18-
searcher = System.Management.ManagementObjectSearcher(queryLogicalDisk);
19-
20-
% Get the collection of ManagementObject instances
21-
logicalDisks = searcher.Get();
22-
23-
% Get the first result (should be at most one for a drive letter)
24-
logicalDiskEnumerator = logicalDisks.GetEnumerator();
25-
if logicalDiskEnumerator.MoveNext()
26-
logicalDisk = logicalDiskEnumerator.Current;
27-
28-
% Get the VolumeSerialNumber property
29-
% This property is a string in WMI, representing a 32-bit hexadecimal integer.
30-
i = hex2dec(char(logicalDisk.GetPropertyValue('VolumeSerialNumber')));
31-
end
32-
33-
delete(h)
11+
if stdlib.has_python()
12+
i = uint64(py.pathlib.Path(path).stat().st_dev);
13+
elseif ispc() && stdlib.has_dotnet()
14+
i = device_dotnet(path);
3415
elseif stdlib.isoctave()
3516
[s, err] = stat(path);
3617
if err == 0

+stdlib/has_python.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function y = has_python()
2+
3+
try
4+
pe = pyenv();
5+
catch e
6+
if strcmp(e.identifier, 'Octave:undefined-function')
7+
y = false;
8+
return
9+
else
10+
rethrow(e);
11+
end
12+
end
13+
14+
y = ~isempty(pe.Version);
15+
16+
end

+stdlib/inode.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99

1010
i = [];
1111

12-
if stdlib.isoctave()
12+
if stdlib.has_python()
13+
i = uint64(py.pathlib.Path(path).stat().st_ino);
14+
elseif isunix() && stdlib.has_java() && stdlib.java_api() >= 11
15+
% Java 1.8 is buggy in some corner cases, so we require at least 11.
16+
opt = javaMethod("values", "java.nio.file.LinkOption");
17+
i = javaMethod("getAttribute", "java.nio.file.Files", javaPathObject(path), "unix:ino", opt);
18+
elseif stdlib.isoctave()
1319
[s, err] = stat(path);
1420
if err == 0
1521
i = s.ino;
1622
end
17-
elseif isunix() && stdlib.java_api() >= 11
18-
% Java 1.8 is buggy in some corner cases, so we require at least 11.
19-
opt = javaMethod("values", "java.nio.file.LinkOption");
20-
i = java.nio.file.Files.getAttribute(javaPathObject(path), "unix:ino", opt);
2123
end
2224

2325
end

+stdlib/java_api.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
function api = java_api()
55

6+
api = [];
7+
68
v = stdlib.java_version();
9+
if strempty(v), return, end
710

811
% major version is first number before "."
912

1013
a = strsplit(v, '.');
11-
if(isempty(a))
12-
api = [];
13-
return
14-
end
14+
if(isempty(a)), return, end
1515

1616
if a{1} == "1"
1717
api = str2double(a{2});

+stdlib/java_version.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
v = javaMethod("getProperty", "java.lang.System", "java.version");
1414

15-
try %#ok<*TRYNC>
15+
try %#ok<*TRYNC>
1616
v = string(v);
1717
end
1818

19+
end
20+
1921
%!assert(!isempty(java_version()))

+stdlib/private/device_dotnet.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function i = device_dotnet(path)
2+
3+
4+
h = NET.addAssembly('System.Management');
5+
6+
r = stdlib.root_name(path);
7+
queryLogicalDisk = sprintf('SELECT VolumeSerialNumber FROM Win32_LogicalDisk WHERE Caption = ''%s''', r);
8+
9+
% Create a ManagementObjectSearcher instance
10+
searcher = System.Management.ManagementObjectSearcher(queryLogicalDisk);
11+
12+
% Get the collection of ManagementObject instances
13+
logicalDisks = searcher.Get();
14+
15+
% Get the first result (should be at most one for a drive letter)
16+
logicalDiskEnumerator = logicalDisks.GetEnumerator();
17+
if logicalDiskEnumerator.MoveNext()
18+
logicalDisk = logicalDiskEnumerator.Current;
19+
20+
% Get the VolumeSerialNumber property
21+
% This property is a string in WMI, representing a 32-bit hexadecimal integer.
22+
i = hex2dec(char(logicalDisk.GetPropertyValue('VolumeSerialNumber')));
23+
end
24+
25+
delete(h)
26+
27+
end

+stdlib/python_version.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
%% PYTHON_VERSION get the Python version used by MATLAB
2+
13
function v = python_version()
24

35
pe = pyenv();
46
v = pe.Version;
5-
7+
68
end

test/TestDisk.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,21 @@ function test_filesystem_type(tc, Ps)
5252

5353
function test_device(tc)
5454

55-
tc.assumeTrue(ispc() || stdlib.has_java())
55+
tc.assumeTrue(stdlib.has_python() || (isunix() && stdlib.has_java() && stdlib.java_api() >= 11))
5656

5757
if ispc()
5858
tc.verifyGreaterThan(stdlib.device(pwd()), 0)
5959
else
60-
tc.assumeGreaterThanOrEqual(stdlib.java_api(), 11)
61-
6260
tc.verifyEqual(stdlib.device("."), stdlib.device(pwd()))
6361
end
6462
end
6563

64+
65+
function test_inode(tc)
66+
tc.assumeTrue(stdlib.has_python() || (~ispc() && stdlib.has_java() && stdlib.java_api() >= 11))
67+
68+
tc.verifyEqual(stdlib.inode("."), stdlib.inode(pwd()))
69+
end
70+
6671
end
6772
end

test/TestJava.m

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@
77

88
methods (Test, TestTags=["java", "unix"])
99

10-
function test_inode(tc)
11-
tc.assumeFalse(ispc(), "not for Windows")
12-
tc.assumeGreaterThanOrEqual(stdlib.java_api(), 11)
13-
14-
tc.verifyEqual(stdlib.inode("."), stdlib.inode(pwd()))
15-
end
16-
1710

1811
function test_owner(tc, Ps)
1912

0 commit comments

Comments
 (0)