FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

added CondaEnvironment and modernized project · losttech/WhichPython@33329f7 · GitHub

Repository navigation

Commit 33329f7

Browse files
committed
added CondaEnvironment and modernized project
1 parent 38cc60b commit 33329f7

5 files changed

Lines changed: 205 additions & 130 deletions

File tree

‎app/WhichPython.App.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<LangVersion>8.0</LangVersion>
7+
<Nullable>enable</Nullable>
68
<RootNamespace>LostTech.WhichPython</RootNamespace>
79
</PropertyGroup>
810

‎app/WhichPythonProgram.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
static class WhichPythonProgram {
77
static void Main() {
88
foreach (var environment in PythonEnvironment.EnumerateEnvironments()
9-
.Concat(PythonEnvironment.EnumerateCondaEnvironments()))
10-
Console.WriteLine($"{environment.LanguageVersion.ToString(2)}-{environment.Architecture?.ToString() ?? "???"} @ {environment.Home}");
9+
.Concat(CondaEnvironment.EnumerateCondaEnvironments())) {
10+
Console.WriteLine($"{environment.LanguageVersion?.ToString(2) ?? "??"}-{environment.Architecture?.ToString() ?? "???"} @ {environment.Home}");
11+
}
1112
}
1213
}
1314
}

‎src/CondaEnvironment.cs‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
namespace LostTech.WhichPython {
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
using System.Threading;
8+
/// <summary>
9+
/// Represents a Python environment, managed by Anaconda
10+
/// </summary>
11+
public sealed class CondaEnvironment: PythonEnvironment {
12+
/// <inheritdoc cref="PythonEnvironment.Home"/> />
13+
#pragma warning disable CS8600, CS8603 // Conda environments always have non-null homes
14+
public new DirectoryInfo Home => (DirectoryInfo)base.Home;
15+
#pragma warning restore CS8600, CS8603 // Conda environments always have non-null homes
16+
/// <summary>
17+
/// Short name of the environment
18+
/// </summary>
19+
public string Name => this.Home.Name;
20+
/// <summary>
21+
/// Checks specified directory
22+
/// </summary>
23+
/// <param name="home"></param>
24+
/// <param name="cancellation"></param>
25+
/// <returns></returns>
26+
public static CondaEnvironment? DetectCondaEnvironment(DirectoryInfo home, CancellationToken cancellation = default) {
27+
if (home is null) throw new ArgumentNullException(nameof(home));
28+
29+
if (!home.Exists)
30+
return null;
31+
32+
int? versionDoublet = TryGetVersionDoublet(home, cancellation);
33+
34+
Version? version = versionDoublet == null
35+
? null
36+
: new Version(versionDoublet.Value / 10, versionDoublet.Value % 10);
37+
38+
string? interpreterPath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
39+
? Path.Combine(home.FullName, "python.exe")
40+
: version == null ? null
41+
: Path.Combine(home.FullName, "bin",
42+
FormattableString.Invariant($"python{version.Major}.{version.Minor}"));
43+
44+
if (interpreterPath is null || !File.Exists(interpreterPath)) return null;
45+
46+
return new CondaEnvironment(
47+
interpreterPath: new FileInfo(interpreterPath),
48+
home: home,
49+
dll: version != null ? new FileInfo(GetDll(home, version)) : null,
50+
version,
51+
architecture: null);
52+
}
53+
54+
public new static IEnumerable<CondaEnvironment> EnumerateCondaEnvironments(
55+
CancellationToken cancellation = default)
56+
=> GetEnvironmentRoots(cancellation)
57+
.SelectMany(home => {
58+
var environment = DetectCondaEnvironment(home, cancellation);
59+
return environment is null
60+
? Array.Empty<CondaEnvironment>()
61+
: new[] {environment};
62+
});
63+
64+
static IEnumerable<DirectoryInfo> GetEnvironmentRoots(CancellationToken cancellation) {
65+
string userHome = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
66+
string condaEnvironmentsFilePath = Path.Combine(userHome, ".conda", "environments.txt");
67+
if (!File.Exists(condaEnvironmentsFilePath))
68+
yield break;
69+
70+
cancellation.ThrowIfCancellationRequested();
71+
72+
string[] pythonHomes;
73+
try {
74+
pythonHomes = File.ReadAllLines(condaEnvironmentsFilePath);
75+
}
76+
catch (FileNotFoundException) { yield break; }
77+
catch (DirectoryNotFoundException) { yield break; }
78+
79+
foreach (string home in pythonHomes) {
80+
if (string.IsNullOrWhiteSpace(home))
81+
continue;
82+
83+
yield return new DirectoryInfo(home);
84+
}
85+
}
86+
87+
CondaEnvironment(FileInfo interpreterPath, DirectoryInfo home, FileInfo? dll, Version? languageVersion, Architecture? architecture) : base(interpreterPath, home, dll, languageVersion, architecture) { }
88+
}
89+
}

‎src/LostTech.WhichPython.csproj‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<LangVersion>latest</LangVersion>
5+
<LangVersion>8.0</LangVersion>
6+
<Nullable>enable</Nullable>
67
<PackageId>WhichPython</PackageId>
7-
<Version>0.2.0</Version>
8+
<Version>0.3.0</Version>
89
<Authors>Lost Tech LLC</Authors>
910
<Product>Which Python</Product>
1011
<PackageLicenseUrl>https://raw.githubusercontent.com/losttech/WhichPython/master/LICENSE</PackageLicenseUrl>
@@ -14,7 +15,11 @@
1415
</PropertyGroup>
1516

1617
<ItemGroup>
17-
<PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
18+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" />
1823
</ItemGroup>
1924

2025
</Project>

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL