Skip to content

Commit

Permalink
Fix semver parsing in APT and From_Output
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Jun 21, 2024
1 parent 27cb6ee commit 331b10e
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 51 deletions.
10 changes: 5 additions & 5 deletions alire.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ commit = "56bbdc008e16996b6f76e443fd0165a240de1b13"

[pins.dirty_booleans]
url = "https://github.com/mosteo/dirty_booleans"
branch = "alire"
commit = "05c40d88ecfe109e575ec8b21dd6ffa2e61df1dc"

[pins.diskflags]
url = "https://github.com/mosteo/diskflags"
branch = "alire"
commit = "60729edf31816aca0036b13b2794c39a9bd0172e"

[pins.gnatcoll]
url = "https://github.com/alire-project/gnatcoll-core.git"
Expand All @@ -77,23 +77,23 @@ commit = "9a9c660f9c6f27f5ef75417e7fac7061dff14d78"

[pins.semantic_versioning]
url = "https://github.com/alire-project/semantic_versioning"
commit = "cc2148cf9c8934fb557b5ae49a3f7947194fa7ee"
commit = "4861e32bd8a2f0df038d3ecc9a72b6381e7a34cc"

[pins.simple_logging]
url = "https://github.com/alire-project/simple_logging"
commit = "3505dc645f3eef6799a486aae223d37e88cfc4d5"

[pins.si_units]
url = "https://github.com/mosteo/si_units"
branch = "alire"
commit = "9329d2591b82440ccc859a53f1380ac07ea4194d"

[pins.stopwatch]
url = "https://github.com/mosteo/stopwatch"
commit = "f607a63b714f09bbf6126de9851cbc21cf8666c9"

[pins.toml_slicer]
url = "https://github.com/mosteo/toml_slicer"
branch = "alire"
commit = "3e5cbdb5673b85a1da6344a41764ef1cbafe3289"

# To disable version updating, export ALR_VERSION_DONT_PATCH with any value

Expand Down
2 changes: 1 addition & 1 deletion scripts/version-patcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ fi
$bin "$@"

echo "Resulting version file:"
cat src/alire/alire-version.ads | grep Current_Str
cat src/alire/alire-version.ads | grep "Current_Str : constant String"
29 changes: 14 additions & 15 deletions src/alire/alire-externals-from_output.adb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ with Alire.OS_Lib.Subprocess;
with Alire.Paths;
with Alire.Releases;
with Alire.TOML_Keys;
with Alire.Utils.Regex;
with Alire.Utils.TTY;

with Semantic_Versioning;
Expand Down Expand Up @@ -48,20 +49,13 @@ package body Alire.Externals.From_Output is
end if;

declare
use GNAT.Regpat;
Matches : Match_Array (1 .. Match_Count'Last);
-- Although we should have at most one match, it turns out that the
-- match won't necessarily be on the first position in the array, as
-- it depends on the number of () expressions.

Lines : AAA.Strings.Vector;
Status : constant Integer :=
OS_Lib.Subprocess.Unchecked_Spawn_And_Capture
(This.Command.First_Element,
This.Command.Tail,
Lines);
Output : constant String :=
Lines.Flatten ("" & ASCII.LF);
Output : constant String := Lines.Flatten ("" & ASCII.LF);
-- ASCII.LF is used by Regpat for new lines
begin
if Status /= 0 then
Expand All @@ -73,13 +67,13 @@ package body Alire.Externals.From_Output is

Trace.Debug
("Looking for external in version string '" & Output & "'");
Match (This.Regexp, Output, Matches);

for I in Matches'Range loop
if Matches (I) /= No_Match then
declare
Version : constant String :=
Utils.Regex.First_Match (+This.Regstr, Output);
begin
if Version /= "" then
declare
Version : constant String :=
Output (Matches (I).First .. Matches (I).Last);
Path : constant Any_Path :=
OS_Lib.Subprocess.Locate_In_Path
(This.Command.First_Element);
Expand All @@ -89,16 +83,21 @@ package body Alire.Externals.From_Output is

Result.Insert
(Index.Crate (Name, Index.Query_Mem_Only).Base
.Retagging (Semantic_Versioning.Parse (Version))
.Retagging (Semantic_Versioning.Parse
(Version, Relaxed => True))
.Providing (This.Provides)
.Replacing (Origins.New_External ("path " & Path))
.Replacing (Notes => "Detected at " -- length is 12
& Shorten
(String (Path),
Max_Description_Length - 12)));
end;
else
Trace.Debug
("Failed to match a version using regexp '"
& (+This.Regstr) & "' on output: " & Version);
end if;
end loop;
end;
end;

return Result;
Expand Down
71 changes: 42 additions & 29 deletions src/alire/alire-externals-from_system.adb
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,49 @@ package body Alire.Externals.From_System is
else
for Candidate of Origin.Value.Packages loop
Trace.Detail ("Looking for system package: " & Candidate);
declare
Detector : constant System.Deployer'Class :=
System.Platform_Deployer (Candidate);
Result : constant System.Version_Outcomes.Outcome :=
Detector.Detect;
begin
if Result.Success then
Trace.Detail ("Success with system package: "
& Candidate);

-- A system package may be found more than once if
-- transitional and proper package names are given
-- for detection of the same system package.
declare
Release : constant Alire.Releases.Release
:= Index.Crate (Name, Index.Query_Mem_Only).Base
.Retagging (Result.Value)
.Providing (This.Provides)
.Replacing (Origins.New_System (Candidate))
.Replacing (Notes => "Provided by system package:"
& " " & Candidate);
begin
if Releases.Contains (Release) then
Trace.Debug ("Not readding same system package: "
& Release.Milestone.Image);
else
Releases.Insert (Release);
end if;
end;
end if;
declare
Detector : constant System.Deployer'Class :=
System.Platform_Deployer (Candidate);
Result : constant System.Version_Outcomes.Outcome :=
Detector.Detect;
begin
if Result.Success then
Trace.Detail ("Success with system package: "
& Candidate);

-- A system package may be found more than once if
-- transitional and proper package names are given
-- for detection of the same system package.
declare
Release : constant Alire.Releases.Release
:= Index.Crate (Name,
Index.Query_Mem_Only)
.Base
.Retagging (Result.Value)
.Providing (This.Provides)
.Replacing (Origins.New_System (Candidate))
.Replacing
(Notes => "Provided by system package:"
& " " & Candidate);
begin
if Releases.Contains (Release) then
Trace.Debug
("Not readding same system package: "
& Release.Milestone.Image);
else
Releases.Insert (Release);
end if;
end;
end if;
end;
exception
when E : others =>
Trace.Debug
("Error attempting version detection of system "
& "package [" & Candidate & "] for crate: "
& (+Name));
Log_Exception (E);
end;
end loop;
end if;
Expand Down
13 changes: 13 additions & 0 deletions src/alire/alire-origins-deployers-system-apt.adb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ package body Alire.Origins.Deployers.System.Apt is
Trace.Debug
("Unexpected version format, could not identify version");
end if;
exception
-- We do not really want to disturb users for a problem
-- introduced externally by some new package version in the
-- underlying distro. This will make the problem harder to
-- detect, but eventually someone should notice that a package
-- is not being detected as intended.
when Constraint_Error | Semantic_Versioning.Malformed_Input =>
Trace.Debug
("Unexpected error while parsing version from: "
& Match & " in line " & Line & " in pkg "
& This.Base.Package_Name);
return Version_Outcomes.Outcome_Failure
("could not be detected", Report => False);
end;
end if;
end loop;
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
description = "Sample crate"
name = "crate1"
licenses = []
maintainers = ["[email protected]"]
maintainers-logins = ["someone"]

[[external]]
kind = "version-output"
version-command = ["echo", "1.1abc"] # Should result in 1.1.0+abc
version-regexp = "(.*)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
description = "Sample crate"
name = "crate2"
licenses = []
maintainers = ["[email protected]"]
maintainers-logins = ["someone"]

[[external]]
kind = "version-output"
version-command = ["echo", "1.1.abc"] # Should result in 1.1.0+.abc
version-regexp = "(.*)"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = "1.2"
18 changes: 18 additions & 0 deletions testsuite/tests/externals/bad-versions/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Check that external version detector doesn't crash on problematic versions we
have been seeing in the wild.
"""

from drivers.alr import run_alr
from drivers.asserts import assert_substring


def check_version(crate_name, version: str):
assert_substring(version,
run_alr("show", crate_name, "--external-detect").out)


check_version("crate1", "1.1.0+abc")
check_version("crate2", "1.1.0+.abc")

print("SUCCESS")
5 changes: 5 additions & 0 deletions testsuite/tests/externals/bad-versions/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
driver: python-script
build_mode: both
indexes:
my_index:
in_fixtures: false

0 comments on commit 331b10e

Please sign in to comment.