-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconda.m
212 lines (156 loc) · 3.94 KB
/
conda.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
% conda.m
% a simple MATLAB utility to control conda
% environments from within MATLAB
%
% usage
%
% conda env list
% conda activate $ENV
%
% Srinivas Gorur-Shandilya
classdef conda
properties
end
methods
end
methods (Static)
function varargout = conda(varargin)
varargout = {};
if strcmp(varargin{1},'init')
conda.init()
return
end
if nargin == 2
if strcmp(varargin{1},'env') && strcmp(varargin{2},'list')
conda.getenv();
return
elseif strcmp(varargin{1},'activate')
conda.setenv(varargin{2})
return
else
error('Unknown argument syntax')
end
else
error('Unknown argument syntax')
end
end
function init()
try
getpref('condalab','base_path');
catch
str = input('Enter the path to your conda installation: \n','s');
if contains(str,['condabin' filesep 'conda'])
str = strrep(str,['condabin' filesep 'conda'],'condabin');
else
str = strrep(str,[filesep 'conda'],'');
end
setpref('condalab','base_path',str)
end
conda.addBaseCondaPath()
end
function addBaseCondaPath()
try
condalab_base_path = getpref('condalab','base_path');
catch
conda.init()
condalab_base_path = getpref('condalab','base_path');
end
[e,~] = system('which conda');
if e == 0
% conda is somewhere on the path, so do nothing
return
end
P = strsplit(getenv('PATH'),pathsep);
add_to_path = true;
for i = 1:length(P)
if strcmp(P{i},condalab_base_path)
add_to_path = false;
end
end
if add_to_path
setenv('PATH',[condalab_base_path pathsep getenv('PATH') ]);
end
end
function varargout = getenv()
conda.addBaseCondaPath;
[~,envs] = system('conda env list');
envs = strsplit(envs,'\n');
p = strsplit(getenv('PATH'),pathsep);
% remove the asterix because it always is on root
for i = length(envs):-1:1
envs{i} = strrep(envs{i},'*',' ');
if isempty(envs{i})
continue
end
if strcmp(envs{i}(1),'#')
continue
end
this_env = strsplit(envs{i});
env_names{i} = this_env{1};
env_paths{i} = this_env{2};
active_path = 0;
for j = 1:length(env_paths)
this_env_path = [env_paths{j} filesep 'bin'];
if any(strcmp(this_env_path,p))
active_path = j;
end
end
end
if nargout
varargout{1} = env_names;
varargout{2} = env_paths;
varargout{3} = active_path;
else
fprintf('\n')
for i = 1:length(env_names)
if isempty(env_names{i})
continue
end
if active_path == i
disp([pad(['*' env_names{i}],15) ' ' env_paths{i}])
else
disp([pad(env_names{i},15) ' ' env_paths{i}])
end
end
end
end % end getenv
function setenv(env)
conda.addBaseCondaPath;
[~,envs] = system(['conda env list']);
envs = strsplit(envs,'\n');
[env_names, env_paths] = conda.getenv;
% check that envs exists in the list
assert(any(strcmp(env_names,env)), 'env you want to activate is not valid')
p = getenv('PATH');
% delete every conda env path from the path
p = strsplit(p,pathsep);
rm_this = false(length(p),1);
for i = 1:length(p)
% remove "bin" from the end
this_path = strtrim(strrep(p{i}, [filesep 'bin'],''));
if any(strcmp(this_path,env_paths))
rm_this(i) = true;
end
end
p(rm_this) = [];
% add the path of the env we want to switch to. On Windows the
% executable lives in the main env directory, on *NIX in the bin
% subfolder.
if ~ispc
this_env_path = [env_paths{strcmp(env_names,env)} filesep 'bin'];
else
this_env_path = env_paths{strcmp(env_names,env)};
end
p = [this_env_path p];
p = strjoin(p,pathsep);
% append the base path to this, because apparently
% conda decides to change everything every 2 months
p = [p pathsep getpref('condalab','base_path')];
setenv('PATH', p);
end % setenv
% asks the python interpreter where it is located
function test()
system(['python ' fileparts(which(mfilename)) filesep 'test.py']);
end
end
end % end classdef