184 lines
5.0 KiB
MoonBit
184 lines
5.0 KiB
MoonBit
///|
|
|
/// Where to look up a package attribute on Hydra for a given channel.
|
|
#derive(Eq, Debug)
|
|
pub(all) struct Channel {
|
|
name : String
|
|
project : String
|
|
jobset : String
|
|
job_prefix : String // "" for nixpkgs/trunk, "nixpkgs." for nixos release jobsets
|
|
releases_url : String // channel base URL for store-paths.xz
|
|
}
|
|
|
|
///|
|
|
/// Map a channel name to a Hydra jobset.
|
|
/// nixos-unstable / nixpkgs-unstable / unstable -> hydra nixpkgs/trunk
|
|
/// nixos-YY.MM -> hydra nixos/release-YY.MM
|
|
pub fn Channel::parse(name : String) -> Channel raise @store.NixError {
|
|
if name == "nixos-unstable" ||
|
|
name == "nixpkgs-unstable" ||
|
|
name == "unstable" {
|
|
return {
|
|
name,
|
|
project: "nixpkgs",
|
|
jobset: "trunk",
|
|
job_prefix: "",
|
|
releases_url: "https://channels.nixos.org/\{name}",
|
|
}
|
|
}
|
|
if name.has_prefix("nixos-") {
|
|
let ver = name[6:].to_owned()
|
|
let ok = ver.length() == 5 &&
|
|
ver[2:3].to_owned() == "." &&
|
|
ver.to_array().iter().all(fn(c) { c == '.' || (c >= '0' && c <= '9') })
|
|
if ok {
|
|
return {
|
|
name,
|
|
project: "nixos",
|
|
jobset: "release-\{ver}",
|
|
job_prefix: "nixpkgs.",
|
|
releases_url: "https://channels.nixos.org/\{name}",
|
|
}
|
|
}
|
|
}
|
|
raise Parse(
|
|
"unknown channel '\{name}' (expected nixos-unstable, nixpkgs-unstable or nixos-YY.MM)",
|
|
)
|
|
}
|
|
|
|
///|
|
|
/// Hydra URL returning the JSON of the latest successful build of `attr`.
|
|
pub fn Channel::job_url(
|
|
self : Channel,
|
|
attr : String,
|
|
system : String,
|
|
) -> String raise @store.NixError {
|
|
if attr.length() == 0 {
|
|
raise Parse("empty package attribute")
|
|
}
|
|
for c in attr {
|
|
let ok = (c >= 'a' && c <= 'z') ||
|
|
(c >= 'A' && c <= 'Z') ||
|
|
(c >= '0' && c <= '9') ||
|
|
c == '_' ||
|
|
c == '-' ||
|
|
c == '.' ||
|
|
c == '+'
|
|
if !ok {
|
|
raise Parse("invalid character '\{c}' in package attribute '\{attr}'")
|
|
}
|
|
}
|
|
"https://hydra.nixos.org/job/\{self.project}/\{self.jobset}/\{self.job_prefix}\{attr}.\{system}/latest-finished"
|
|
}
|
|
|
|
///|
|
|
/// Extract `(store path, nixname)` of the `out` output from Hydra's build JSON.
|
|
pub fn parse_hydra_build(
|
|
text : String,
|
|
) -> (String, String) raise @store.NixError {
|
|
let j = @json.parse(text) catch {
|
|
_ => raise Parse("Hydra response is not valid JSON")
|
|
}
|
|
guard j is Object(top) else { raise Parse("Hydra response: expected object") }
|
|
if top.get("error") is Some(e) {
|
|
raise Parse("Hydra error: \{e.stringify()}")
|
|
}
|
|
if top.get("finished") is Some(f) && f.stringify() != "1" {
|
|
raise Verify("Hydra build not finished")
|
|
}
|
|
if top.get("buildstatus") is Some(s) && s.stringify() != "0" {
|
|
raise Verify(
|
|
"Hydra build was not successful (buildstatus \{s.stringify()})",
|
|
)
|
|
}
|
|
guard top.get("buildoutputs") is Some(Object(outs)) else {
|
|
raise Parse("Hydra response: missing buildoutputs")
|
|
}
|
|
guard outs.get("out") is Some(Object(out)) else {
|
|
raise Parse("Hydra response: no 'out' output")
|
|
}
|
|
guard out.get("path") is Some(String(path)) else {
|
|
raise Parse("Hydra response: no path for 'out'")
|
|
}
|
|
let name = match top.get("nixname") {
|
|
Some(String(n)) => n
|
|
_ => ""
|
|
}
|
|
ignore(@store.StorePath::parse(path))
|
|
(path, name)
|
|
}
|
|
|
|
///|
|
|
/// Parse the text of `store-paths.xz` (one store path per line).
|
|
pub fn parse_store_paths(
|
|
text : String,
|
|
) -> Array[@store.StorePath] raise @store.NixError {
|
|
let out : Array[@store.StorePath] = []
|
|
for line in text.split("\n") {
|
|
if line.length() == 0 {
|
|
continue
|
|
}
|
|
out.push(@store.StorePath::parse(line.to_owned()))
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
/// Substring search over `name-version` strings of the channel's store paths.
|
|
/// Returns distinct names sorted by (exact match first, then length, then
|
|
/// alphabetical) with the number of distinct store paths for that name (the
|
|
/// same name appears several times because the channel covers all platforms).
|
|
pub fn search(
|
|
paths : Array[@store.StorePath],
|
|
query : String,
|
|
) -> Array[(String, Int32)] {
|
|
let q = query.to_lower()
|
|
let counts : Map[String, Int32] = Map([])
|
|
for p in paths {
|
|
if p.name.to_lower().contains(q) {
|
|
counts[p.name] = counts.get_or_default(p.name, 0) + 1
|
|
}
|
|
}
|
|
let res : Array[(String, Int32)] = []
|
|
for k, v in counts {
|
|
res.push((k, v))
|
|
}
|
|
res.sort_by(fn(a, b) {
|
|
let ka = rank(a.0, q)
|
|
let kb = rank(b.0, q)
|
|
if ka != kb {
|
|
ka - kb
|
|
} else if a.0.length() != b.0.length() {
|
|
a.0.length() - b.0.length()
|
|
} else {
|
|
a.0.compare(b.0)
|
|
}
|
|
})
|
|
res
|
|
}
|
|
|
|
///|
|
|
/// 0 = pname equals query, 1 = starts with query, 2 = elsewhere.
|
|
fn rank(name : String, q : String) -> Int32 {
|
|
let l = name.to_lower()
|
|
// pname = up to the first '-' followed by a digit
|
|
let chars = l.to_array()
|
|
let mut cut = chars.length()
|
|
for i in 0..<chars.length() {
|
|
if chars[i] == '-' && i + 1 < chars.length() {
|
|
let n = chars[i + 1]
|
|
if n >= '0' && n <= '9' {
|
|
cut = i
|
|
break
|
|
}
|
|
}
|
|
}
|
|
let pname = l[0:cut].to_owned()
|
|
if pname == q {
|
|
0
|
|
} else if l.has_prefix(q) {
|
|
1
|
|
} else {
|
|
2
|
|
}
|
|
}
|