|
| 1 | +with Ada.Characters.Latin_1; |
| 2 | +with AAA.Strings; use AAA.Strings; |
| 3 | + |
| 4 | +with Alire.Errors; |
| 5 | +with Alire.OS_Lib.Subprocess; |
| 6 | +-- with Alire.Utils.Regex; |
| 7 | + |
| 8 | +package body Alire.Origins.Deployers.System.Portage is |
| 9 | + |
| 10 | + package L1 renames Ada.Characters.Latin_1; |
| 11 | + package Subprocess renames Alire.OS_Lib.Subprocess; |
| 12 | + |
| 13 | + ----------------------- |
| 14 | + -- Already_Installed -- |
| 15 | + ----------------------- |
| 16 | + |
| 17 | + overriding function Already_Installed (This : Deployer) return Boolean |
| 18 | + is |
| 19 | + -- The following call is faster than using emerge. |
| 20 | + Output : constant AAA.Strings.Vector := |
| 21 | + Subprocess.Checked_Spawn_And_Capture |
| 22 | + ("eix", |
| 23 | + Empty_Vector & "-ce" & This.Base.Package_Name, |
| 24 | + -- Check the category (A), |
| 25 | + -- whether it is installed (I) and |
| 26 | + -- match against the exact string (e). |
| 27 | + -- i.e. "app-editors/zed" |
| 28 | + Valid_Exit_Codes => (1, 0), -- returns 1 when not found |
| 29 | + Err_To_Out => True); |
| 30 | + |
| 31 | + Indicator : constant String := "[I]"; |
| 32 | + Line : constant String := AAA.Strings.First_Element (Output); |
| 33 | + begin |
| 34 | + Trace.Detail ("Already_Installed: " & This.Base.Package_Name); |
| 35 | + |
| 36 | + if Line'Length >= Indicator'Length then |
| 37 | + return |
| 38 | + Line (Line'First .. Line'First + Indicator'Length - 1) = Indicator; |
| 39 | + end if; |
| 40 | + |
| 41 | + return False; |
| 42 | + end Already_Installed; |
| 43 | + |
| 44 | + ------------------------- |
| 45 | + -- To_Semantic_Version -- |
| 46 | + ------------------------- |
| 47 | + |
| 48 | + function To_Semantic_Version (Gentoo_Version : String) return String is |
| 49 | + Tmp : String := Gentoo_Version; |
| 50 | + begin |
| 51 | + for C in Tmp'Range loop |
| 52 | + -- Format: 0.0.0_release-rc9 |
| 53 | + if Tmp (C) = L1.Low_Line then |
| 54 | + Tmp (C) := L1.Hyphen; |
| 55 | + |
| 56 | + Trace.Debug ("To_Semantic_Version: " & Tmp); |
| 57 | + |
| 58 | + return Tmp; |
| 59 | + end if; |
| 60 | + end loop; |
| 61 | + |
| 62 | + Trace.Debug ("To_Semantic_Version: " & Tmp); |
| 63 | + |
| 64 | + return Tmp; |
| 65 | + end To_Semantic_Version; |
| 66 | + |
| 67 | + ------------ |
| 68 | + -- Detect -- |
| 69 | + ------------ |
| 70 | + |
| 71 | + overriding |
| 72 | + function Detect (This : Deployer) return Version_Outcomes.Outcome |
| 73 | + is |
| 74 | + function Split_Version (Gentoo_Package_Version : String) return String is |
| 75 | + begin |
| 76 | + for Index in Gentoo_Package_Version'Range loop |
| 77 | + if Gentoo_Package_Version (Index) = L1.Hyphen then |
| 78 | + return Gentoo_Package_Version |
| 79 | + (Index + 1 .. Gentoo_Package_Version'Last); |
| 80 | + end if; |
| 81 | + end loop; |
| 82 | + |
| 83 | + return ""; |
| 84 | + end Split_Version; |
| 85 | + pragma Unreferenced (Split_Version); |
| 86 | + |
| 87 | + PortageQ_Output : constant AAA.Strings.Vector := |
| 88 | + Subprocess.Checked_Spawn_And_Capture |
| 89 | + ("portageq", |
| 90 | + Empty_Vector & "best_visible" & "/" & |
| 91 | + This.Base.Package_Name, |
| 92 | + Valid_Exit_Codes => (0, 1), -- Returned when not found |
| 93 | + Err_To_Out => True); |
| 94 | + -- portageq should *always* produce zero or one line result. |
| 95 | + |
| 96 | + Output : constant AAA.Strings.Vector := |
| 97 | + Subprocess.Checked_Spawn_And_Capture |
| 98 | + ("qatom", |
| 99 | + Empty_Vector & "-F%{PVR}" & |
| 100 | + First_Element (PortageQ_Output), |
| 101 | + Valid_Exit_Codes => (0, 1), -- Returned when not found |
| 102 | + Err_To_Out => True); |
| 103 | + Gentoo_Version : constant String := First_Element (Output); |
| 104 | + |
| 105 | + -- Regexp : constant String := |
| 106 | + -- "[0-9]+(\.[0-9]+)*[a-z](_[a-z]+[0-9]?)*(-r[0-9]*)*"; |
| 107 | + -- From the pms.pdf. |
| 108 | + begin |
| 109 | + if Gentoo_Version /= "" then |
| 110 | + Trace.Debug ("Detect: " & This.Base.Package_Name & " - " & |
| 111 | + Gentoo_Version & " - " & To_Semantic_Version (Gentoo_Version) & |
| 112 | + " => " & Semantic_Versioning.Parse |
| 113 | + (To_Semantic_Version (Gentoo_Version), |
| 114 | + Relaxed => True).Image); |
| 115 | + |
| 116 | + return |
| 117 | + Version_Outcomes.New_Result |
| 118 | + (Semantic_Versioning.Parse |
| 119 | + (To_Semantic_Version (Gentoo_Version), Relaxed => True)); |
| 120 | + end if; |
| 121 | + |
| 122 | + Trace.Debug ("System deployer could not detect: " & This.Base.Image); |
| 123 | + return Version_Outcomes.Outcome_Failure ("could not be detected", |
| 124 | + Report => False); |
| 125 | + end Detect; |
| 126 | + |
| 127 | + ------------- |
| 128 | + -- Install -- |
| 129 | + ------------- |
| 130 | + |
| 131 | + overriding |
| 132 | + function Install (This : Deployer) return Outcome is |
| 133 | + begin |
| 134 | + Trace.Debug ("Install: " & This.Base.Package_Name); |
| 135 | + |
| 136 | + Subprocess.Checked_Spawn |
| 137 | + ("sudo", Empty_Vector & |
| 138 | + "emerge" & |
| 139 | + "-av" & |
| 140 | + This.Base.Package_Name); |
| 141 | + |
| 142 | + return Outcome_Success; |
| 143 | + exception |
| 144 | + when E : others => |
| 145 | + return Alire.Errors.Get (E); |
| 146 | + end Install; |
| 147 | + |
| 148 | +end Alire.Origins.Deployers.System.Portage; |
0 commit comments