|
| 1 | +use std::{collections::HashSet, fs, str::FromStr, sync::Arc}; |
| 2 | + |
| 3 | +use anyhow::{Result, anyhow, bail}; |
| 4 | +use libabbs::{ |
| 5 | + apml::{ |
| 6 | + ApmlContext, |
| 7 | + value::{array::StringArray, union::Union}, |
| 8 | + }, |
| 9 | + tree::{AbbsSourcePackage, AbbsTree}, |
| 10 | +}; |
| 11 | +use regex::Regex; |
| 12 | +use tokio::task::JoinSet; |
| 13 | + |
| 14 | +mod pkgcontents; |
| 15 | +mod pkgsite; |
| 16 | + |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct PkgBreakContext { |
| 19 | + pub abbs: AbbsTree, |
| 20 | + pub http_client: reqwest::Client, |
| 21 | +} |
| 22 | + |
| 23 | +impl PkgBreakContext { |
| 24 | + /// Selects a set of packages to be rebuilt. |
| 25 | + /// |
| 26 | + /// Note that the produced list are not filtered |
| 27 | + /// and may include packages that have been dropped |
| 28 | + /// from the repository. However, it must never include |
| 29 | + /// the trigger package itself. |
| 30 | + pub async fn select( |
| 31 | + self: &Arc<Self>, |
| 32 | + package: &AbbsSourcePackage, |
| 33 | + kind: &str, |
| 34 | + ) -> Result<HashSet<String>> { |
| 35 | + let kind = kind.to_ascii_uppercase(); |
| 36 | + |
| 37 | + let mut result = HashSet::new(); |
| 38 | + let mut jobs = JoinSet::new(); |
| 39 | + |
| 40 | + let spec = fs::read_to_string(package.join("spec"))?; |
| 41 | + let spec_ctx = ApmlContext::eval_source(&spec)?; |
| 42 | + let pkgrebuild = spec_ctx |
| 43 | + .get(&format!("PKGREBUILD__{}", kind)) |
| 44 | + .or_else(|| spec_ctx.get("PKGREBUILD")) |
| 45 | + .map(|val| StringArray::from(val.as_string())) |
| 46 | + .map(|val| { |
| 47 | + val.unwrap() |
| 48 | + .into_iter() |
| 49 | + .map(|dir| Directive::from_str(&dir)) |
| 50 | + .collect() |
| 51 | + }) |
| 52 | + .unwrap_or_else(|| { |
| 53 | + if kind == "ABI" { |
| 54 | + Ok(vec![ |
| 55 | + Directive::PackageDependents(None), |
| 56 | + Directive::LibraryDependents(None), |
| 57 | + ]) |
| 58 | + } else { |
| 59 | + Ok(vec![]) |
| 60 | + } |
| 61 | + })?; |
| 62 | + |
| 63 | + for directive in pkgrebuild { |
| 64 | + // fast-path for pkg directives |
| 65 | + if let Directive::Package(pkg) = &directive { |
| 66 | + result.insert(pkg.to_string()); |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + let ctx = self.clone(); |
| 71 | + let package = package.clone(); |
| 72 | + jobs.spawn(async move { |
| 73 | + ctx.select_directive(&package, &directive).await |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + while let Some(part) = jobs.join_next().await { |
| 78 | + let part = part?; |
| 79 | + let part = part?; |
| 80 | + result.extend(part); |
| 81 | + } |
| 82 | + |
| 83 | + result.remove(package.name()); |
| 84 | + Ok(result) |
| 85 | + } |
| 86 | + |
| 87 | + /// Selects a set of packages to be rebuilt. |
| 88 | + pub async fn select_directive( |
| 89 | + &self, |
| 90 | + package: &AbbsSourcePackage, |
| 91 | + directive: &Directive, |
| 92 | + ) -> Result<HashSet<String>> { |
| 93 | + match directive { |
| 94 | + Directive::LibraryDependents(pkg) => { |
| 95 | + let deps = pkgsite::find_deps( |
| 96 | + &self.http_client, |
| 97 | + &pkg.to_owned() |
| 98 | + .unwrap_or_else(|| package.name().to_string()), |
| 99 | + false, |
| 100 | + ) |
| 101 | + .await?; |
| 102 | + Ok(deps) |
| 103 | + } |
| 104 | + Directive::PackageDependents(pkg) => { |
| 105 | + let deps = pkgsite::find_deps( |
| 106 | + &self.http_client, |
| 107 | + &pkg.to_owned() |
| 108 | + .unwrap_or_else(|| package.name().to_string()), |
| 109 | + true, |
| 110 | + ) |
| 111 | + .await?; |
| 112 | + Ok(deps) |
| 113 | + } |
| 114 | + Directive::PathPattern(regex) => { |
| 115 | + pkgcontents::find_deps(&self.http_client, regex).await |
| 116 | + } |
| 117 | + Directive::Section(section) => { |
| 118 | + let mut result = HashSet::new(); |
| 119 | + for package in self.abbs.section_packages(§ion.into())? { |
| 120 | + for package in package.subpackages()? { |
| 121 | + result.insert(package.name()?); |
| 122 | + } |
| 123 | + } |
| 124 | + Ok(result) |
| 125 | + } |
| 126 | + Directive::Package(pkg) => Ok(HashSet::from([pkg.clone()])), |
| 127 | + Directive::PackagePattern(regex) => { |
| 128 | + let mut result = HashSet::new(); |
| 129 | + for package in self.abbs.all_packages()? { |
| 130 | + for package in package.subpackages()? { |
| 131 | + let name = package.name()?; |
| 132 | + if regex.is_match(&name) { |
| 133 | + result.insert(name); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + Ok(result) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/// A PKGREBUILD selector directive. |
| 144 | +#[derive(Debug, Clone)] |
| 145 | +pub enum Directive { |
| 146 | + /// Shared-library dependents. |
| 147 | + LibraryDependents(Option<String>), |
| 148 | + /// Reverse dependents. |
| 149 | + PackageDependents(Option<String>), |
| 150 | + /// Packages providing files matching the pattern. |
| 151 | + PathPattern(Regex), |
| 152 | + /// Packages in a certain section. |
| 153 | + Section(String), |
| 154 | + /// A certain package. |
| 155 | + Package(String), |
| 156 | + /// Packages matching the pattern. |
| 157 | + PackagePattern(Regex), |
| 158 | +} |
| 159 | + |
| 160 | +impl FromStr for Directive { |
| 161 | + type Err = anyhow::Error; |
| 162 | + |
| 163 | + fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { |
| 164 | + let un = Union::try_from(s)?; |
| 165 | + match un.tag.as_str() { |
| 166 | + "sodep" => Ok(Self::LibraryDependents(un.argument)), |
| 167 | + "revdep" => Ok(Self::PackageDependents(un.argument)), |
| 168 | + "path" => { |
| 169 | + Ok(Self::PathPattern(Regex::new(&un.argument.ok_or_else( |
| 170 | + || anyhow!("path directive must have an argument"), |
| 171 | + )?)?)) |
| 172 | + } |
| 173 | + "section" => Ok(Self::Section(un.argument.ok_or_else(|| { |
| 174 | + anyhow!("section directive must have an argument") |
| 175 | + })?)), |
| 176 | + "pkg" => Ok(Self::Package(un.argument.ok_or_else(|| { |
| 177 | + anyhow!("pkg directive must have an argument") |
| 178 | + })?)), |
| 179 | + "pkgpattern" => { |
| 180 | + Ok(Self::PackagePattern(Regex::new(&un.argument.ok_or_else( |
| 181 | + || anyhow!("pkgpattern directive must have an argument"), |
| 182 | + )?)?)) |
| 183 | + } |
| 184 | + _ => bail!("unsupported tag in PKGREBUILD directive"), |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments