Skip to content

Commit

Permalink
feat: dependency parse
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraZiling committed Apr 17, 2024
1 parent 2742278 commit 3ee61ce
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/PipManager/Models/AppConfigModels/EnvironmentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ public EnvironmentItem()
{
}

public EnvironmentItem(string pipVersion, string pythonPath, string pythonVersion)
public EnvironmentItem(string pipVersion, string pythonPath, string pythonVersion, string pythonDllPath)
{
PipVersion = pipVersion;
PythonPath = pythonPath;
PythonVersion = pythonVersion;
PythonDllPath = pythonDllPath;
}

[JsonProperty("pipVersion")] public string? PipVersion { get; set; }
[JsonProperty("pythonPath")] public string? PythonPath { get; set; }
[JsonProperty("pythonVersion")] public string? PythonVersion { get; set; }
[JsonProperty("pythonDllPath")] public string? PythonDllPath { get; set; }
}
1 change: 1 addition & 0 deletions src/PipManager/PipManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2365.46" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="pythonnet" Version="3.1.0-preview2024-04-07" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
Expand Down
16 changes: 10 additions & 6 deletions src/PipManager/Services/Configuration/ConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,17 @@ public string FindPythonPathByPipDir(string pipDir)
public EnvironmentItem? GetEnvironmentItem(string pythonPath)
{
var pythonVersion = FileVersionInfo.GetVersionInfo(pythonPath).FileVersion!;
var pythonDirectory = Directory.GetParent(pythonPath)!.FullName;
var pipDirectory = GetPipDirectories(pythonDirectory);

if (pipDirectory == null)
var pythonDirectory = Directory.GetParent(pythonPath)!;
var pipDirectory = GetPipDirectories(pythonDirectory.FullName);
var pythonDllName = $"{pythonDirectory.Name.ToLower().Replace(".", "")}.dll";
var pythonDllPath = pythonDirectory.GetFiles(pythonDllName, SearchOption.AllDirectories).FirstOrDefault();
if (pythonDllPath == null || pipDirectory == null)
{
return null;
}

var pipVersion = GetPipVersionInInitFile().Match(File.ReadAllText(Path.Combine(pipDirectory, @"__init__.py"))).Groups[1].Value;
return new EnvironmentItem(pipVersion, pythonPath, pythonVersion);
return new EnvironmentItem(pipVersion, pythonPath, pythonVersion, pythonDllPath.FullName);
}

public EnvironmentItem? GetEnvironmentItemFromCommand(string command, string arguments)
Expand Down Expand Up @@ -137,9 +138,12 @@ public string FindPythonPathByPipDir(string pipDir)
}
pipVersion = pipVersion.Trim();
var pythonPath = FindPythonPathByPipDir(pipDir.Trim());
var pythonDirectory = Directory.GetParent(pythonPath)!;
var pythonDllName = $"{pythonDirectory.Name.ToLower().Replace(".", "")}.dll";
var pythonDllPath = pythonDirectory.GetFiles(pythonDllName, SearchOption.AllDirectories).FirstOrDefault();
pythonVersion = pythonVersion.Trim();
proc.Close();
return pipDir.Length > 0 ? new EnvironmentItem(pipVersion, pythonPath, pythonVersion) : null;
return pipDir.Length > 0 && pythonDllPath != null ? new EnvironmentItem(pipVersion, pythonPath, pythonVersion, pythonDllPath.FullName) : null;
}

public void RefreshAllEnvironmentVersions()
Expand Down
32 changes: 32 additions & 0 deletions src/PipManager/Services/Environment/EnvironmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
using Python.Runtime;
using Wpf.Ui.Controls;
using Path = System.IO.Path;

Expand Down Expand Up @@ -222,6 +223,37 @@ public ActionResponse PurgeEnvironmentCache(EnvironmentItem environmentItem)
return [.. packages.OrderBy(x => x.Name)];
}

public ParseRequirementsResponse ParseRequirements(IEnumerable<string> requirements)
{
var parsedRequirements = new ParseRequirementsResponse { Success = true, Requirements = [] };
Runtime.PythonDLL = configurationService.AppConfig.CurrentEnvironment!.PythonDllPath!;
PythonEngine.Initialize();
using (Py.GIL())
{
try
{
dynamic packagingModule = Py.Import("packaging.requirements");
dynamic requirementClass = packagingModule.Requirement;
foreach (var requirement in requirements)
{
dynamic requirementParser = requirementClass(requirement);
parsedRequirements.Requirements.Add(new ParsedRequirement
{
Name = requirementParser.name.ToString(),
Specifier = requirementParser.specifier.ToString()
});
}
}
catch (PythonException ex)
{
Log.Error(ex.Message);
}

}

return parsedRequirements;
}

public async Task<GetVersionsResponse> GetVersions(string packageName)
{
try
Expand Down
1 change: 1 addition & 0 deletions src/PipManager/Services/Environment/IEnvironmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IEnvironmentService

public Task<GetVersionsResponse> GetVersions(string packageName);
public bool TryKillProcess();
public ParseRequirementsResponse ParseRequirements(IEnumerable<string> requirements);

public ActionResponse Install(string packageName, DataReceivedEventHandler consoleOutputCallback, string[]? extraParameters = null);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace PipManager.Services.Environment.Response;

public class ParseRequirementsResponse
{
public bool Success { get; init; }
public List<ParsedRequirement>? Requirements { get; init; }
}

public class ParsedRequirement
{
public required string Name { get; init; }
public required string Specifier { get; init; }
}
17 changes: 15 additions & 2 deletions src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
using PipManager.Models.Action;
using System.IO;
using System.Windows.Shapes;
using Microsoft.Extensions.Configuration;
using PipManager.Models.Action;
using PipManager.Services.Action;
using PipManager.Services.Configuration;
using PipManager.Services.Environment;
using Python.Runtime;
using Serilog;
using Wpf.Ui.Controls;

namespace PipManager.ViewModels.Pages.Lab;

public partial class LabViewModel(IActionService actionService)
public partial class LabViewModel(IActionService actionService, IEnvironmentService environmentService)
: ObservableObject, INavigationAware
{
private bool _isInitialized;

[RelayCommand]
private void ParseTest()
{
var parsed = environmentService.ParseRequirements(["requests", "numpy"]);
parsed.Requirements.ForEach(item => Log.Information(item.Specifier));

Check warning on line 23 in src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 23 in src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 23 in src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 23 in src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 23 in src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 23 in src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

[RelayCommand]
private void ActionTest()
{
Expand Down
1 change: 1 addition & 0 deletions src/PipManager/Views/Pages/Lab/LabPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
mc:Ignorable="d">

<Grid>
<Button Content="Test" Command="{Binding ViewModel.ParseTestCommand}"></Button>
</Grid>
</Page>

0 comments on commit 3ee61ce

Please sign in to comment.