Skip to content

Commit 74c0f98

Browse files
committed
add WSL fcns: has_wsl, wslpath2winpath, winpath2wslpath, wsl_tempfile
1 parent ac5075c commit 74c0f98

File tree

9 files changed

+126
-53
lines changed

9 files changed

+126
-53
lines changed

+stdlib/+fileio/which.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
fpath (1,:) string = getenv('PATH')
88
end
99

10-
import stdlib.fileio.is_exe
11-
import stdlib.fileio.expanduser
12-
1310
names = filename;
1411

1512
if ispc
@@ -26,7 +23,7 @@
2623

2724
if strlength(fileparts(name)) > 0
2825
% has directory part
29-
if is_exe(name)
26+
if stdlib.fileio.is_exe(name)
3027
exe = name;
3128
return
3229
end
@@ -37,7 +34,7 @@
3734
%% path given
3835

3936
if isscalar(fpath)
40-
fpath = split(expanduser(fpath), pathsep).';
37+
fpath = split(stdlib.fileio.expanduser(fpath), pathsep).';
4138
end
4239
fpath = fpath(strlength(fpath)>0);
4340

@@ -50,7 +47,7 @@
5047

5148
for p = fpath
5249
exe = fullfile(p, name);
53-
if is_exe(exe)
50+
if stdlib.fileio.is_exe(exe)
5451
return
5552
end
5653
end

+stdlib/+sys/has_wsl.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function ok = has_wsl()
2+
% HAS_WSL Check if WSL is installed and notionally working
3+
4+
persistent wsl;
5+
6+
if isempty(wsl)
7+
if ispc
8+
[stat, ~] = system("wsl test 1");
9+
wsl = stat == 0;
10+
else
11+
wsl = false;
12+
end
13+
end
14+
15+
ok = wsl;
16+
17+
end

+stdlib/+sys/iswsl.m

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

44
if isempty(wsl)
55
wsl = false;
6-
yeswsl = false;
76
if isunix && ~ismac
87
fid = fopen('/proc/version');
9-
if fid < 1
10-
return
8+
if fid >= 1
9+
v = fscanf(fid,'%s');
10+
fclose(fid);
11+
wsl = contains(v, 'microsoft-standard');
1112
end
12-
v = fscanf(fid,'%s');
13-
fclose(fid);
14-
wsl = contains(v, 'microsoft-standard');
1513
end
1614
end
1715

+stdlib/+sys/winpath2wslpath.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function wsl_path = winpath2wslpath(win_path)
2+
%% WINPATH2WSLPATH Convert Windows path to WSL path
3+
% input format like \\wsl$\Ubuntu\home\username\...
4+
arguments
5+
win_path (1,1) string {mustBeNonzeroLengthText}
6+
end
7+
8+
assert(stdlib.sys.has_wsl(), "stdlib:sys:winpath2wslpath:EnvironmentError", mfilename() + "only supported on Windows Matlab with WSL")
9+
10+
[stat, wsl_path] = system("wsl wslpath " + strrep(win_path, '\', '/'));
11+
12+
assert(stat == 0, "stdlib:sys:winpath2wslpath:IOError", "could not convert wslpath " + wsl_path)
13+
14+
wsl_path = strip(string(wsl_path));
15+
16+
end

+stdlib/+sys/wsl_tempfile.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function path = wsl_tempfile()
2+
%% Windows Subsystem for Linux (WSL) temporary file from Windows Matlab
3+
4+
assert(stdlib.sys.has_wsl(), "stdlib:sys:wsl_tempfile:EnvironmentError", mfilename() + "only supported on Windows Matlab with WSL")
5+
6+
[stat, path] = system("wsl mktemp -u");
7+
8+
assert(stat == 0, "stdlib:sys:wsl_tempfile:IOError", "could not get wsl mktemp " + path)
9+
10+
path = strip(string(path));
11+
12+
end

+stdlib/+sys/wslpath2winpath.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function win_path = wslpath2winpath(wsl_path)
2+
%% WSLPATH2WINPATH Convert a WSL path to a Windows path
3+
% output format is like \\wsl$\Ubuntu\home\username\...
4+
arguments
5+
wsl_path (1,1) string {mustBeNonzeroLengthText}
6+
end
7+
8+
assert(stdlib.sys.has_wsl(), "stdlib:sys:wslpath2winpath:EnvironmentError", mfilename() + "only supported on Windows Matlab with WSL")
9+
10+
[stat, win_path] = system("wsl wslpath -w " + wsl_path);
11+
12+
assert(stat == 0, "stdlib:sys:wslpath2winpath:IOError", "could not convert wslpath " + win_path)
13+
14+
win_path = strip(string(win_path));
15+
16+
end

+stdlib/+test/TestFileImpure.m

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
function test_expanduser(tc)
66
import stdlib.fileio.expanduser
7+
78
tc.verifyFalse(startsWith(expanduser('~/foo'), "~"))
89
tc.verifyFalse(any(startsWith(expanduser(["~/abc", "~/123"]), "~")))
910

@@ -21,7 +22,6 @@ function test_makedir(tc)
2122
end
2223

2324
function test_which_name(tc)
24-
import stdlib.fileio.which
2525

2626
if ismac
2727
n = "ls";
@@ -30,50 +30,13 @@ function test_which_name(tc)
3030
end
3131
%% which: Matlab in environment variable PATH
3232
% MacOS Matlab does not source .zshrc so Matlab is not on internal Matlab PATH
33-
tc.verifyNotEmpty(which(n))
33+
tc.verifyNotEmpty(stdlib.fileio.which(n))
3434

3535
end
3636

37-
function test_is_exe_which_wsl(tc)
38-
import stdlib.fileio.is_exe
39-
import stdlib.fileio.which
40-
import stdlib.sys.subprocess_run
41-
42-
tc.assumeTrue(ispc, "Windows only")
43-
44-
wsl = which("wsl");
45-
tc.assumeNotEmpty(wsl, "did not find Windows Subsystem for Linux")
46-
47-
[ret, cc] = system("wsl which cc");
48-
tc.assumeEqual(ret, 0, "could not find WSL C compiler")
49-
tc.assumeNotEmpty(cc, "did not find WSL C compiler")
50-
51-
cwd = fileparts(mfilename('fullpath'));
52-
src = "main.c";
53-
[~, out] = fileparts(tempname);
54-
55-
oldcwd = pwd;
56-
cd(cwd)
57-
ret = system("wsl cc " + src + " -o" + out);
58-
59-
tc.assumeEqual(ret, 0, "failed to compile " + src)
60-
tc.assumeTrue(isfile(out), "cc failed to produce output file " + out)
61-
62-
tc.verifyTrue(is_exe(out), "is_exe() failed to detect WSL executable " + out)
63-
64-
wsl_exe = which(out);
65-
tc.verifyNotEmpty(wsl_exe, "which() failed to detect WSL executable " + out)
66-
67-
delete(out)
68-
69-
cd(oldcwd)
70-
71-
end
7237

7338
function test_is_exe_which_fullpath(tc)
7439
import matlab.unittest.constraints.IsFile
75-
import stdlib.fileio.which
76-
import stdlib.fileio.is_exe
7740

7841
n = "matlab";
7942
%% is_exe test
@@ -83,9 +46,9 @@ function test_is_exe_which_fullpath(tc)
8346
else
8447
fp = p;
8548
end
86-
tc.verifyTrue(is_exe(fp))
49+
tc.verifyTrue(stdlib.fileio.is_exe(fp))
8750
%% which: test absolute path
88-
exe = which(p);
51+
exe = stdlib.fileio.which(p);
8952

9053
if ispc
9154
tc.verifyTrue(endsWith(exe, ".exe"))

+stdlib/+test/TestSys.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function test_octave(tc)
5555
function test_wsl(tc)
5656
import matlab.unittest.constraints.IsOfClass
5757
tc.verifyThat(stdlib.sys.iswsl, IsOfClass('logical'))
58+
tc.verifyThat(stdlib.sys.has_wsl, IsOfClass('logical'))
5859
end
5960

6061
function test_isinteractive(tc)

+stdlib/+test/TestWSL.m

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
classdef TestWSL < matlab.unittest.TestCase
2+
3+
methods (Test)
4+
5+
function test_is_exe_which_wsl(tc)
6+
7+
tc.assumeTrue(ispc, "Windows only")
8+
tc.assumeTrue(stdlib.sys.has_wsl(), "did not find Windows Subsystem for Linux")
9+
10+
[ret, cc] = system("wsl which cc");
11+
tc.assumeEqual(ret, 0, "could not find WSL C compiler")
12+
tc.assumeNotEmpty(cc, "did not find WSL C compiler")
13+
14+
cwd = fileparts(mfilename('fullpath'));
15+
src = stdlib.sys.winpath2wslpath(fullfile(cwd, "main.c"));
16+
17+
[ret, out_wsl] = system("wsl mktemp -u");
18+
tc.assumeEqual(ret, 0, "could not get WSL tempfile")
19+
out_wsl = strtrim(out_wsl);
20+
21+
ret = system("wsl cc " + src + " -o" + out_wsl);
22+
23+
tc.assumeEqual(ret, 0, "failed to compile " + src)
24+
out = stdlib.sys.wslpath2winpath(out_wsl);
25+
tc.assumeTrue(isfile(out), "cc failed to produce output file " + out)
26+
27+
tc.verifyTrue(stdlib.fileio.is_exe(out), "is_exe() failed to detect WSL executable " + out)
28+
29+
wsl_exe = stdlib.fileio.which(out);
30+
tc.verifyNotEmpty(wsl_exe, "which() failed to detect WSL executable " + out)
31+
32+
end
33+
34+
35+
function test_wsl_path(tc)
36+
37+
tc.assumeTrue(ispc, "Windows only")
38+
tc.assumeTrue(stdlib.sys.has_wsl(), "did not find Windows Subsystem for Linux")
39+
40+
wsl_temp = stdlib.sys.wsl_tempfile();
41+
tc.verifyNotEmpty(wsl_temp, "could not get WSL tempfile")
42+
43+
wsl_path = stdlib.sys.wslpath2winpath(wsl_temp);
44+
tc.verifyNotEmpty(wsl_path, "could not convert WSL path to Windows path")
45+
46+
win_path = stdlib.sys.winpath2wslpath(wsl_path);
47+
tc.verifyNotEmpty(win_path, "could not convert Windows path to WSL path")
48+
49+
end
50+
51+
end
52+
53+
end

0 commit comments

Comments
 (0)