Skip to content

Commit 333e270

Browse files
committed
Install: Use a python based method to get conda env info
1 parent b754667 commit 333e270

1 file changed

Lines changed: 54 additions & 61 deletions

File tree

mhkit/package/+mhkit/+conda/parse_info.m

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,69 +24,62 @@
2424
%
2525
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2626

27-
[status, cmd_out] = mhkit.sys(sprintf('conda run -n %s conda info', env_name));
27+
% Initialize result struct
28+
result = struct();
29+
30+
% 1. Verify the environment exists in conda's environment list
31+
[status, env_list] = mhkit.sys('conda env list');
2832
if status ~= 0
29-
logger.error('Failed to execute conda info');
33+
logger.error('Failed to get conda environment list');
34+
result = struct();
35+
return;
3036
end
31-
32-
% Initialize the output struct
33-
result = struct();
34-
35-
% Define the fields we want to extract
36-
fields_to_find = {
37-
'active environment',
38-
'active env location',
39-
'python version',
40-
'platform'
41-
};
42-
43-
disp('Parsing conda info output...');
44-
disp(cmd_out);
45-
46-
% Split the input into lines
47-
lines = regexp(cmd_out, '\n', 'split');
48-
49-
% Process each line
50-
for i = 1:length(lines)
51-
line = strtrim(lines{i});
52-
53-
% Skip empty lines
54-
if isempty(line)
55-
continue;
56-
end
57-
58-
% Check if line contains any of our desired fields
59-
for j = 1:length(fields_to_find)
60-
field = fields_to_find{j};
61-
% Extract key part (before the colon)
62-
parts = regexp(line, ':', 'split');
63-
if ~isempty(parts)
64-
line_key = strtrim(parts{1});
65-
if strcmpi(line_key, field) % Case-insensitive exact match
66-
% Get the value
67-
value = strjoin(parts(2:end), ':'); % Rejoin in case value contains colons
68-
value = strtrim(value);
69-
70-
% Convert field name to valid MATLAB field name
71-
field_name = regexprep(lower(field), '\s+', '_');
72-
73-
% Store in struct
74-
result.(field_name) = value;
75-
% fprintf('Matched field "%s" with value "%s"\n', field, value);
76-
break;
77-
end
78-
end
79-
end
37+
38+
% Check if our environment name exists in the list
39+
if ~contains(env_list, env_name)
40+
logger.error('Environment "%s" not found in conda environment list', env_name);
41+
result = struct();
42+
return;
8043
end
81-
82-
% Verify all fields were found
83-
fprintf('\nFound fields:\n');
84-
for j = 1:length(fields_to_find)
85-
field_name = regexprep(lower(fields_to_find{j}), '\s+', '_');
86-
if isfield(result, field_name)
87-
fprintf(' %s: %s\n', field_name, result.(field_name));
88-
else
89-
fprintf(' WARNING: Field "%s" not found!\n', fields_to_find{j});
90-
end
44+
45+
result.active_environment = env_name;
46+
47+
% 2. Get Python version directly from environment
48+
[status, python_version_raw] = mhkit.sys(sprintf('conda run -n %s python --version', env_name));
49+
if status ~= 0
50+
logger.error('Failed to get Python version from environment %s', env_name);
51+
result = struct();
52+
return;
53+
end
54+
version_match = regexp(python_version_raw, 'Python\s+(\d+\.\d+\.\d+)', 'tokens');
55+
if isempty(version_match)
56+
logger.error('Could not parse Python version from: %s', python_version_raw);
57+
result = struct();
58+
return;
59+
end
60+
result.python_version = version_match{1}{1};
61+
62+
% 3. Get environment location directly from Python
63+
[status, env_location] = mhkit.sys(sprintf('conda run -n %s python -c "import sys; print(sys.prefix)"', env_name));
64+
if status ~= 0
65+
logger.error('Failed to get environment location from environment %s', env_name);
66+
result = struct();
67+
return;
68+
end
69+
result.active_env_location = strip(env_location);
70+
71+
% 4. Get platform directly from Python
72+
[status, platform_info] = mhkit.sys(sprintf('conda run -n %s python -c "import platform; print(platform.system().lower() + ''-'' + platform.machine())"', env_name));
73+
if status ~= 0
74+
logger.error('Failed to get platform info from environment %s', env_name);
75+
result = struct();
76+
return;
9177
end
78+
result.platform = strip(platform_info);
79+
80+
% Log success
81+
logger.info('✓ Retrieved environment info for %s:', env_name);
82+
logger.info(' Python version: %s', result.python_version);
83+
logger.info(' Location: %s', result.active_env_location);
84+
logger.info(' Platform: %s', result.platform);
9285
end

0 commit comments

Comments
 (0)