| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,8 @@ | |||
| 3 | 3 | <PropertyGroup> | |
| 4 | 4 | <OutputType>Exe</OutputType> | |
| 5 | 5 | <TargetFramework>netcoreapp2.1</TargetFramework> | |
| 6 | + <LangVersion>8.0</LangVersion> | ||
| 7 | + <Nullable>enable</Nullable> | ||
| 6 | 8 | <RootNamespace>LostTech.WhichPython</RootNamespace> | |
| 7 | 9 | </PropertyGroup> | |
| 8 | 10 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,8 +6,9 @@ | |||
| 6 | 6 | static class WhichPythonProgram { | |
| 7 | 7 | static void Main() { | |
| 8 | 8 | 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 | + } | ||
| 11 | 12 | } | |
| 12 | 13 | } | |
| 13 | 14 | } | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,9 +2,10 @@ | |||
| 2 | 2 | ||
| 3 | 3 | <PropertyGroup> | |
| 4 | 4 | <TargetFramework>netstandard2.0</TargetFramework> | |
| 5 | - <LangVersion>latest</LangVersion> | ||
| 5 | + <LangVersion>8.0</LangVersion> | ||
| 6 | + <Nullable>enable</Nullable> | ||
| 6 | 7 | <PackageId>WhichPython</PackageId> | |
| 7 | - <Version>0.2.0</Version> | ||
| 8 | + <Version>0.3.0</Version> | ||
| 8 | 9 | <Authors>Lost Tech LLC</Authors> | |
| 9 | 10 | <Product>Which Python</Product> | |
| 10 | 11 | <PackageLicenseUrl>https://raw.githubusercontent.com/losttech/WhichPython/master/LICENSE</PackageLicenseUrl> | |
@@ -14,7 +15,11 @@ | |||
| 14 | 15 | </PropertyGroup> | |
| 15 | 16 | ||
| 16 | 17 | <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" /> | ||
| 18 | 23 | </ItemGroup> | |
| 19 | 24 | ||
| 20 | 25 | </Project> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments