From a1262b124e303b4ea2b09ea07c4c056de522011e Mon Sep 17 00:00:00 2001 From: Christophe Gouiran Date: Sat, 7 Nov 2020 15:01:06 +0100 Subject: [PATCH] Add handling of Arch and Arch-based distributions (#600) * Add handling of arch and arch based distributions * Update src/alire/os_linux/alire-platform.adb Co-authored-by: Alejandro R Mosteo * Improve former Arch based Linux distributions detection (#600) * Check first id then id_like key (#600) * Update alire-platform.adb Add a debug trace when falling back to id_like key * Fix line too long style error (#600) * Handle the case when id_like is a space separated items list (#600) * More robust regular expression (#600) * Simplify processing of id and id_key values (#600) * Easy_Graph is not available in Official Arch repositories (#600) Co-authored-by: Alejandro R Mosteo --- src/alire/alire-platforms.ads | 3 +- src/alire/alire-utils-tools.adb | 4 +- src/alire/os_linux/alire-platform.adb | 94 ++++++++++++++++++++------- 3 files changed, 76 insertions(+), 25 deletions(-) diff --git a/src/alire/alire-platforms.ads b/src/alire/alire-platforms.ads index 9c993ad6..9eac1cbe 100644 --- a/src/alire/alire-platforms.ads +++ b/src/alire/alire-platforms.ads @@ -16,6 +16,7 @@ package Alire.Platforms with Preelaborate is type Distributions is (Debian, Ubuntu, Msys2, + Arch, Distro_Unknown); subtype Known_Distributions is @@ -31,7 +32,7 @@ package Alire.Platforms with Preelaborate is Distro_Manager : constant array (Distributions) of Package_Managers := (Debian | Ubuntu => Apt, - Msys2 => Pacman, + Msys2 | Arch => Pacman, Distro_Unknown => Packager_Unknown); type Toolchains is (System, diff --git a/src/alire/alire-utils-tools.adb b/src/alire/alire-utils-tools.adb index 4aa9e851..46eaee58 100644 --- a/src/alire/alire-utils-tools.adb +++ b/src/alire/alire-utils-tools.adb @@ -57,10 +57,10 @@ package body Alire.Utils.Tools is -- Cannot have package for an unknown distribution return ""; - when Msys2 | Debian | Ubuntu => + when Msys2 | Debian | Ubuntu | Arch => return (case Tool is when Easy_Graph => - (if Distribution /= Msys2 + (if Distribution /= Msys2 and Distribution /= Arch then "libgraph-easy-perl" else ""), when Git | Tar | Unzip | Curl => Exec_For_Tool (Tool), diff --git a/src/alire/os_linux/alire-platform.adb b/src/alire/os_linux/alire-platform.adb index 2d76936f..bce6b763 100644 --- a/src/alire/os_linux/alire-platform.adb +++ b/src/alire/os_linux/alire-platform.adb @@ -1,5 +1,6 @@ with Alire.OS_Lib.Subprocess; with Alire.Utils; +with GNAT.Regpat; package body Alire.Platform is @@ -34,30 +35,79 @@ package body Alire.Platform is use Utils; Release : constant Utils.String_Vector := Subprocess.Checked_Spawn_And_Capture - ("cat", Empty_Vector & "/etc/os-release"); + ("cat", Empty_Vector & "/etc/os-release"); + + function Get_Os_Release_Value_For_Key (Key : String) + return Alire.Platforms.Distributions is + + use GNAT.Regpat; + + -- Regexp accepting lines not starting with '#' like: + -- key=value + -- key='value' + -- key='value1 value2' + -- key="value" + -- key="value1 value2" + Regexp : constant Pattern_Matcher := + Compile ("^" & Key & "=[""']?([^""']+)[""']?"); + Matches : Match_Array (1 .. 1); + + begin + for Line of Release loop + declare + Normalized : constant String := + To_Lower_Case (Line); + + Values : String_Vector; + begin + Match (Regexp, Normalized, Matches); + if Matches (1) /= No_Match then + -- Generate Values from space separated items + Values := + Split ( + Normalized + (Matches (1).First .. Matches (1).Last), ' '); + + for Value of Values loop + begin + return Platforms.Distributions'Value + (Value); + exception + when others => + null; -- Not a known distro. + end; + end loop; + end if; + exception + when others => + null; -- Not a known distro. + end; + end loop; + + return Distro_Unknown; + + end Get_Os_Release_Value_For_Key; + begin - for Line of Release loop - declare - Normalized : constant String := - To_Lower_Case (Replace (Line, " ", "")); - begin - if Starts_With (Normalized, "id=") then - Cached_Distro := - Platforms.Distributions'Value (Tail (Normalized, '=')); - Distro_Cached := True; - return Cached_Distro; - end if; - exception - when others => - exit; -- Not a known distro. - end; - end loop; - - Trace.Debug ("Found unsupported distro: " & Release (1)); - - Cached_Distro := Distro_Unknown; + -- First try with id key + Cached_Distro := + Get_Os_Release_Value_For_Key (Key => "id"); + + -- If no supported distribution found, fallback to id_like key + if Cached_Distro = Distro_Unknown then + Trace.Debug + ("Unknown distro for key 'id', falling back to 'id_like'"); + Cached_Distro := + Get_Os_Release_Value_For_Key (Key => "id_like"); + end if; + + -- Still an unsupported distribution ? + if Cached_Distro = Distro_Unknown then + Trace.Debug ("Found unsupported distro: " & Release (1)); + end if; + Distro_Cached := True; - return Distro_Unknown; + return Cached_Distro; end; end if; exception -- 2.39.5