Skip to content

Commit

Permalink
Ensure conda envs exists in testing (#321)
Browse files Browse the repository at this point in the history
* ensure conda envs exists in testing

* Remove cleanup

* Pin older miniconda

* Add extra steps

* Check conda activate

* activate conda

* Get system info

* Update to latest

* Remove clean

* print conda env paths

* bash formatting

* error when venv doesn't exist

* add python version

* check paths

* add extra safeguard

* undo: add extra safeguard

* Use the python version of the conda environment as an override for the installed conda python version

* Use the Python version from the environment variable

* remove debug step
  • Loading branch information
tonybaloney authored Jan 8, 2025
1 parent 948cfd9 commit 9b58edb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/dotnet-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: conda-incubator/setup-miniconda@v3
- uses: conda-incubator/setup-miniconda@v3.1.0
id: setup-conda
with:
auto-update-conda: true
python-version: ${{ matrix.python-version }}
python-version: "${{ matrix.python }}"
activate-environment: csnakes_test
environment-file: src/Conda.Tests/python/environment.yml
- name: cleanup conda-incubator/setup-miniconda
run: conda clean --all --yes
- name: check conda export
run: |
conda env export --name csnakes_test
conda info
- name: Setup Python
id: installpython
uses: actions/setup-python@v5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
using CSnakes.Runtime.Locators;
using Microsoft.Extensions.Logging;
using System.Runtime.InteropServices;

namespace CSnakes.Runtime.EnvironmentManagement;
#pragma warning disable CS9113 // Parameter is unread. There for future use.
internal class CondaEnvironmentManagement(ILogger logger, string name, bool ensureExists, CondaLocator conda, string? environmentSpecPath) : IEnvironmentManagement
internal class CondaEnvironmentManagement(ILogger logger, string name, bool ensureEnvironment, CondaLocator conda, string? environmentSpecPath, string? pythonEnvironmentVersion) : IEnvironmentManagement
#pragma warning restore CS9113 // Parameter is unread.
{
ILogger IEnvironmentManagement.Logger => logger;

public void EnsureEnvironment(PythonLocationMetadata pythonLocation)
{
if (!ensureExists)
if (!ensureEnvironment)
return;


var fullPath = Path.GetFullPath(GetPath());
if (!Directory.Exists(fullPath))
{
logger.LogError("Cannot find conda environment at {fullPath}.", fullPath);

throw new InvalidOperationException($"Cannot find conda environment at {fullPath}.");

// TODO: Automate the creation of the conda environments.
//var result = conda.ExecuteCondaShellCommand($"env create -n {name} -f {environmentSpecPath}");
//if (!result)
Expand All @@ -38,4 +41,19 @@ public string GetPath()
// TODO: Conda environments are not always in the same location. Resolve the path correctly.
return Path.Combine(conda.CondaHome, "envs", name);
}

public virtual string GetExtraPackagePath(PythonLocationMetadata location)
{
var envLibPath = string.Empty;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
envLibPath = Path.Combine(GetPath(), "Lib", "site-packages");
else
{
Version version = string.IsNullOrEmpty(pythonEnvironmentVersion) ? location.Version : ServiceCollectionExtensions.ParsePythonVersion(pythonEnvironmentVersion);
string suffix = location.FreeThreaded ? "t" : "";
// If the environment has a different version to the version in conda.
envLibPath = Path.Combine(GetPath(), "lib", $"python{version.Major}.{version.Minor}{suffix}", "site-packages");
}
return envLibPath;
}
}
2 changes: 1 addition & 1 deletion src/CSnakes.Runtime/IPythonEnvironmentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IPythonEnvironmentBuilder
/// <param name="environmentSpecPath">The path to the conda environment specification file (environment.yml), used if ensureEnvironment = true.</param>
/// <param name="ensureEnvironment">Indicates whether to create the conda environment if it doesn't exist (not yet supported).</param>
/// <returns>The current instance of the <see cref="IPythonEnvironmentBuilder"/>.</returns>
IPythonEnvironmentBuilder WithCondaEnvironment(string name, string? environmentSpecPath = null, bool ensureEnvironment = false);
IPythonEnvironmentBuilder WithCondaEnvironment(string name, string? environmentSpecPath = null, bool ensureEnvironment = false, string? pythonVersion = null);

/// <summary>
/// Sets the home directory for the Python environment being built.
Expand Down
7 changes: 2 additions & 5 deletions src/CSnakes.Runtime/PythonEnvironmentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ public IPythonEnvironmentBuilder WithVirtualEnvironment(string path, bool ensure
return this;
}

public IPythonEnvironmentBuilder WithCondaEnvironment(string name, string? environmentSpecPath = null, bool ensureEnvironment = false)
public IPythonEnvironmentBuilder WithCondaEnvironment(string name, string? environmentSpecPath = null, bool ensureEnvironment = false, string? pythonVersion = null)
{
if (ensureEnvironment)
throw new InvalidOperationException("Automated Conda environment creation not yet supported. Conda environments must be created manually.");

Services.AddSingleton<IEnvironmentManagement>(
sp => {
try
{
var condaLocator = sp.GetRequiredService<CondaLocator>();
var logger = sp.GetRequiredService<ILogger<CondaEnvironmentManagement>>();
var condaEnvManager = new CondaEnvironmentManagement(logger, name, ensureEnvironment, condaLocator, environmentSpecPath);
var condaEnvManager = new CondaEnvironmentManagement(logger, name, ensureEnvironment, condaLocator, environmentSpecPath, pythonVersion);
return condaEnvManager;
}
catch (InvalidOperationException)
Expand Down
4 changes: 2 additions & 2 deletions src/Conda.Tests/CondaTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CondaTestBase : IDisposable
public CondaTestBase()
{
string condaEnv = Environment.GetEnvironmentVariable("CONDA") ?? string.Empty;

string? pythonVersion = Environment.GetEnvironmentVariable("PYTHON_VERSION");
if (string.IsNullOrEmpty(condaEnv))
{
if (OperatingSystem.IsWindows())
Expand All @@ -28,7 +28,7 @@ public CondaTestBase()
pb.WithHome(Path.Join(Environment.CurrentDirectory, "python"));

pb.FromConda(condaBinPath)
.WithCondaEnvironment("csnakes_test", environmentSpecPath);
.WithCondaEnvironment("csnakes_test", environmentSpecPath, true, pythonVersion);

services.AddLogging(builder => builder.AddXUnit());
})
Expand Down

0 comments on commit 9b58edb

Please sign in to comment.