From 3eb2eccbe52e9285e7fb687a8dbaee99abbfa00e Mon Sep 17 00:00:00 2001 From: Greg Villicana Date: Sun, 30 Jan 2022 16:00:34 -0800 Subject: [PATCH 1/2] Fix Pypi Dev version parsing --- .../pip/IPyPiClient.cs | 2 +- .../pip/PipComponentDetector.cs | 4 ++-- .../pip/PythonVersion.cs | 8 ++++++++ .../PythonVersionTests.cs | 7 +++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.ComponentDetection.Detectors/pip/IPyPiClient.cs b/src/Microsoft.ComponentDetection.Detectors/pip/IPyPiClient.cs index 96f8265d4..86077e896 100644 --- a/src/Microsoft.ComponentDetection.Detectors/pip/IPyPiClient.cs +++ b/src/Microsoft.ComponentDetection.Detectors/pip/IPyPiClient.cs @@ -210,7 +210,7 @@ public async Task>> GetRele } catch (ArgumentException ae) { - Logger.LogError($"Component {release.Key} : {JsonConvert.SerializeObject(release.Value)} could not be added to the sorted list of pip components. Error details follow:"); + Logger.LogError($"Component {release.Key} : {JsonConvert.SerializeObject(release.Value)} could not be added to the sorted list of pip components for spec={spec.Name}. Usually this happens with unexpected PyPi version formats (e.g. prerelease/dev versions). Error details follow:"); Logger.LogException(ae, true); continue; } diff --git a/src/Microsoft.ComponentDetection.Detectors/pip/PipComponentDetector.cs b/src/Microsoft.ComponentDetection.Detectors/pip/PipComponentDetector.cs index 0bf5d371d..eb1a63811 100644 --- a/src/Microsoft.ComponentDetection.Detectors/pip/PipComponentDetector.cs +++ b/src/Microsoft.ComponentDetection.Detectors/pip/PipComponentDetector.cs @@ -21,7 +21,7 @@ public class PipComponentDetector : FileComponentDetector public override IEnumerable SupportedComponentTypes { get; } = new[] { ComponentType.Pip }; - public override int Version { get; } = 4; + public override int Version { get; } = 5; [Import] public IPythonCommandService PythonCommandService { get; set; } @@ -68,7 +68,7 @@ protected override async Task OnFileFound(ProcessRequest processRequest, IDictio .Select(tuple => new DetectedComponent(tuple.Item2)) .ToList() .ForEach(gitComponent => singleFileComponentRecorder.RegisterUsage(gitComponent, isExplicitReferencedDependency: true)); -} + } catch (Exception e) { Logger.LogFailedReadingFile(file.Location, e); diff --git a/src/Microsoft.ComponentDetection.Detectors/pip/PythonVersion.cs b/src/Microsoft.ComponentDetection.Detectors/pip/PythonVersion.cs index e829c8830..9fb9a63a0 100644 --- a/src/Microsoft.ComponentDetection.Detectors/pip/PythonVersion.cs +++ b/src/Microsoft.ComponentDetection.Detectors/pip/PythonVersion.cs @@ -28,6 +28,8 @@ public class PythonVersion : IComparable public int? PostNumber { get; set; } + public string DevLabel { get; set; } + public int? DevNumber { get; set; } public bool Floating { get; set; } = false; @@ -81,6 +83,12 @@ public PythonVersion(string version) PostNumber = postRelease2; } + if (groups["dev_l"].Success) + { + DevLabel = groups["dev_l"].Value; + DevNumber = 0; + } + if (groups["dev_n"].Success && int.TryParse(groups["dev_n"].Value, out int devNumber)) { DevNumber = devNumber; diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs index 718c9b9a0..6827bd472 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs @@ -32,6 +32,7 @@ public void TestPythonVersionComplexComparisons() // This is a list of versions supplied by PEP440 for testing (minus local versions) var versions = new List { + "1.0.dev", "1.0.dev456", "1.0a1", "1.0a2.dev456", @@ -41,16 +42,22 @@ public void TestPythonVersionComplexComparisons() "1.0b2", "1.0b2.post345.dev456", "1.0b2.post345", + "1.0rc1.dev", "1.0rc1.dev456", "1.0rc1", "1.0", "1.0.post456.dev34", "1.0.post456", + "1.1.dev", "1.1.dev1", + "1.1", }.Select(x => new PythonVersion(x)).ToList(); for (int i = 1; i < versions.Count; i++) { + var verA = versions[i - 1]; + var verB = versions[i]; + var temp = verA < verB; Assert.IsTrue(versions[i - 1] < versions[i]); } } From 1fa1771d095fa1312a6e6a1df2901603cd3aac97 Mon Sep 17 00:00:00 2001 From: Greg Villicana Date: Sun, 30 Jan 2022 16:15:01 -0800 Subject: [PATCH 2/2] nit --- .../PythonVersionTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs index 6827bd472..89f399baa 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs @@ -55,9 +55,6 @@ public void TestPythonVersionComplexComparisons() for (int i = 1; i < versions.Count; i++) { - var verA = versions[i - 1]; - var verB = versions[i]; - var temp = verA < verB; Assert.IsTrue(versions[i - 1] < versions[i]); } }