| 1 | module main |
| 2 | |
| 3 | import nspkg |
| 4 | import strings |
| 5 | |
| 6 | // terms_match is the classic match: every term is in the name or in the |
| 7 | // description, case ignored. no terms match everything, as pacman's -Ss |
| 8 | // with no target lists everything |
| 9 | fn terms_match(name string, desc string, terms []string) bool { |
| 10 | n, d := name.to_lower(), desc.to_lower() |
| 11 | for term in terms { |
| 12 | t := term.to_lower() |
| 13 | if !n.contains(t) && !d.contains(t) { |
| 14 | return false |
| 15 | } |
| 16 | } |
| 17 | return true |
| 18 | } |
| 19 | |
| 20 | // match_classic is the classic match of a libalpm package |
| 21 | fn match_classic(pkg &nspkg.Package, config ConfigSearch) bool { |
| 22 | return terms_match(pkg.get_name(), pkg.get_desc(), config.query) |
| 23 | } |
| 24 | |
| 25 | fn jw_if_match(config ConfigSearch, needle string, haystack string) ?f64 { |
| 26 | score := strings.jaro_winkler_similarity(needle, haystack) |
| 27 | if score >= config.thold { |
| 28 | return score |
| 29 | } |
| 30 | return none |
| 31 | } |
| 32 | |
| 33 | // match_fuzzy_name needs every term to match the name, and scores the name by |
| 34 | // the average of the terms |
| 35 | fn match_fuzzy_name(pkg &nspkg.Package, config ConfigSearch) ?f64 { |
| 36 | if config.query.len == 0 { |
| 37 | return none |
| 38 | } |
| 39 | name := pkg.get_name().to_lower() |
| 40 | mut total := 0.0 |
| 41 | for term in config.query { |
| 42 | score := jw_if_match(config, term.to_lower(), name) or { return none } |
| 43 | total += score |
| 44 | } |
| 45 | return total / f64(config.query.len) |
| 46 | } |
| 47 | |
| 48 | // fuzzy_desc_hit is true when one word of the description matches the term |
| 49 | fn fuzzy_desc_hit(config ConfigSearch, term string, words []string) bool { |
| 50 | for h in words { |
| 51 | if _ := jw_if_match(config, term, h) { |
| 52 | return true |
| 53 | } |
| 54 | } |
| 55 | return false |
| 56 | } |
| 57 | |
| 58 | fn match_fuzzy_desc(pkg &nspkg.Package, config ConfigSearch) bool { |
| 59 | if config.query.len == 0 { |
| 60 | return false |
| 61 | } |
| 62 | words := pkg.get_desc().to_lower().split(' ').filter(|word| word.runes().len > 1) |
| 63 | for term in config.query { |
| 64 | if !fuzzy_desc_hit(config, term.to_lower(), words) { |
| 65 | return false |
| 66 | } |
| 67 | } |
| 68 | return true |
| 69 | } |
| 70 | |
| 71 | struct Scored { |
| 72 | pkg &nspkg.Package |
| 73 | score f64 |
| 74 | } |
| 75 | |
| 76 | fn build_matches(pkgs []&nspkg.Package, config ConfigSearch) []&nspkg.Package { |
| 77 | return if config.fuzzy { |
| 78 | mut m_name := []Scored{cap: pkgs.len} |
| 79 | for pkg in pkgs { |
| 80 | if s := match_fuzzy_name(pkg, config) { |
| 81 | m_name << Scored{pkg, s} |
| 82 | } |
| 83 | } |
| 84 | // strong name matches lead; desc-only matches trail them. a package |
| 85 | // listed for its name is not listed again for its description: the |
| 86 | // names matched are a set, one lookup per package instead of a scan |
| 87 | // of the matches (one call reads one db, where names are unique) |
| 88 | m_name.sort(a.score > b.score) |
| 89 | m_name_pkg := m_name.map(|pkg| pkg.pkg) |
| 90 | mut by_name := map[string]bool{} |
| 91 | for pkg in m_name_pkg { |
| 92 | by_name[pkg.get_name()] = true |
| 93 | } |
| 94 | m_desc_pkg := pkgs.filter(|pkg| !by_name[pkg.get_name()] && match_fuzzy_desc(pkg, config)) |
| 95 | // one new list, the name matches then the description ones: neither |
| 96 | // of the two it is made of is written to |
| 97 | mut matches := []&nspkg.Package{cap: m_name_pkg.len + m_desc_pkg.len} |
| 98 | matches << m_name_pkg |
| 99 | matches << m_desc_pkg |
| 100 | matches |
| 101 | } else { |
| 102 | pkgs.filter(|pkg| match_classic(pkg, config)) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // pkgbuild_matches is the classic match for AUR results, which only have a name and |
| 107 | // a description |
| 108 | fn pkgbuild_matches(pkg PackagePkgbuild, terms []string) bool { |
| 109 | return terms_match(pkg.name, pkg.description, terms) |
| 110 | } |
| 111 | |
| 112 | // filtered is true when a filter flag asks for installed packages with a property |
| 113 | fn (config ConfigSearch) filtered() bool { |
| 114 | return config.deps || config.explicit || config.foreign || config.native |
| 115 | || config.unrequired || config.upgrades |
| 116 | } |
| 117 | |
| 118 | // GroupRef is one group of a db, as the bare group listing collects it: a |
| 119 | // sync db's, or the local db's (repo 'local') with -l |
| 120 | struct GroupRef { |
| 121 | repo string |
| 122 | name string |
| 123 | } |
| 124 | |
| 125 | // group_source is how a listing names a db: the local one is 'local', as |
| 126 | // the installed packages are everywhere else |
| 127 | fn group_source(handle &nspkg.Handle, db &nspkg.DB) string { |
| 128 | if localdb := handle.get_localdb() { |
| 129 | if voidptr(localdb) == voidptr(db) { |
| 130 | return 'local' |
| 131 | } |
| 132 | } |
| 133 | return db.get_name() |
| 134 | } |
| 135 | |
| 136 | // sync_groups lists every group of the dbs, db by db |
| 137 | fn sync_groups(handle &nspkg.Handle, dbs []&nspkg.DB) []GroupRef { |
| 138 | mut out := []GroupRef{} |
| 139 | for db in dbs { |
| 140 | mut node := db.get_groupcache() or { continue } |
| 141 | for node != unsafe { nil } { |
| 142 | if group := node.get_group() { |
| 143 | out << GroupRef{group_source(handle, db), group.get_name()} |
| 144 | } |
| 145 | node = node.next |
| 146 | } |
| 147 | } |
| 148 | return out |
| 149 | } |
| 150 | |
| 151 | // scope_repo narrows a search to one repo when its first name is one, like |
| 152 | // 'vpac s -r extra zsh': the dbs left, and the query without the repo |
| 153 | fn scope_repo(dbs []&nspkg.DB, query []string) ([]&nspkg.DB, []string) { |
| 154 | if query.len > 1 && dbs.any(it.get_name() == query[0]) { |
| 155 | return dbs.filter(it.get_name() == query[0]), query[1..] |
| 156 | } |
| 157 | return dbs, query |
| 158 | } |
| 159 | |
| 160 | // collect_group_members lists the members of the named groups of dbs: the |
| 161 | // sync dbs, or the local db for pacman's -Qg. the filters look at the |
| 162 | // installed packages: a member that is not installed cannot pass them, and |
| 163 | // sync_dbs is what the sync-comparing ones compare with |
| 164 | fn collect_group_members(mut listing Listing, handle &nspkg.Handle, dbs []&nspkg.DB, sync_dbs []&nspkg.DB, config ConfigSearch) ! { |
| 165 | for name in config.query { |
| 166 | mut found := false |
| 167 | // a group several dbs carry lists each member once, from the first |
| 168 | // db that has it, as alpm_find_group_pkgs does |
| 169 | mut seen := map[string]bool{} |
| 170 | for db in dbs { |
| 171 | group := db.get_group(name) or { continue } |
| 172 | found = true |
| 173 | for pkg in group.packages.pkgs() { |
| 174 | if seen[pkg.get_name()] { |
| 175 | continue |
| 176 | } |
| 177 | seen[pkg.get_name()] = true |
| 178 | local := handle.find_local_pkg(pkg.get_name()) |
| 179 | if config.installed && local == none { |
| 180 | continue |
| 181 | } |
| 182 | if config.filtered() { |
| 183 | l := local or { continue } |
| 184 | if !pkg_matches_filter(l, config, sync_dbs) { |
| 185 | continue |
| 186 | } |
| 187 | } |
| 188 | listing.add_member(pkg, group_source(handle, db), name) |
| 189 | } |
| 190 | } |
| 191 | // as pacman's -Sg: one missing group fails the run, the rest list |
| 192 | if !found { |
| 193 | target_error('group "${name}" not found') |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // collect_search lists the matches of the query: the installed packages, the |
| 199 | // sync dbs and the pkgbuild repos, each when the config asks for it |
| 200 | fn collect_search(mut listing Listing, mut ui TransUI, mut handle &nspkg.Handle, config ConfigSearch) ! { |
| 201 | if config.local { |
| 202 | mut pkgcache := local_pkgcache(handle)! |
| 203 | |
| 204 | sync_dbs := filter_sync_dbs(mut ui, mut handle, config)! |
| 205 | installed := pkgcache.pkgs().filter(|pkg| pkg_matches_filter(pkg, config, sync_dbs)) |
| 206 | for pkg in build_matches(installed, config) { |
| 207 | listing.add_pkg(pkg, 'local') |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if config.remote { |
| 212 | mut scope := config |
| 213 | dbs, query := scope_repo(init_repos(mut handle)!, config.query) |
| 214 | scope.query = query |
| 215 | if scope.sync_dbs { |
| 216 | check_ask_db_lock(mut ui) |
| 217 | update_repos(mut handle, false)! |
| 218 | } else { |
| 219 | check_sync_fresh(mut ui, mut handle)! |
| 220 | } |
| 221 | |
| 222 | for db in dbs { |
| 223 | mut pkgcache := pkgcache_of(handle, db)! |
| 224 | for pkg in build_matches(pkgcache.pkgs(), scope) { |
| 225 | if scope.installed && handle.find_local_pkg(pkg.get_name()) == none { |
| 226 | continue |
| 227 | } |
| 228 | listing.add_pkg(pkg, db.get_name()) |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if config.pkgbuild { |
| 234 | mut brepos := pkgbuild_repos() |
| 235 | if brepos.len == 0 { |
| 236 | return error('no pkgbuild repo in the config: add (repo #:name "aur" #:type "pkgbuild" #:src "https://aur.archlinux.org")') |
| 237 | } |
| 238 | // a first name that is a pkgbuild repo narrows the search to it |
| 239 | mut bquery := config.query.clone() |
| 240 | if bquery.len > 1 { |
| 241 | known := brepos.map(it.name) |
| 242 | if bquery[0] in known { |
| 243 | brepos = brepos.filter(it.name == bquery[0]) |
| 244 | bquery = bquery[1..] |
| 245 | } |
| 246 | } |
| 247 | // each pkgbuild repo is searched on its own site, and its results |
| 248 | // carry the repo's name |
| 249 | needle := if bquery.len > 0 { bquery[0] } else { '' } |
| 250 | rest := if bquery.len > 1 { bquery[1..] } else { []string{} } |
| 251 | for repo in brepos { |
| 252 | pkgs := get_pkgbuild_packages(repo.servers[0].trim_right('/'), needle) or { return err } |
| 253 | for pkg in pkgs { |
| 254 | if pkgbuild_matches(pkg, rest) { |
| 255 | if config.installed && handle.find_local_pkg(pkg.name) == none { |
| 256 | continue |
| 257 | } |
| 258 | listing.add_pkgbuild(pkg, repo.name) |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // search collects the matches, then prints them. a search that fails part |
| 266 | // way prints no partial table, but its quiet listing still names what was |
| 267 | // found before the error, the way a stream of names does |
| 268 | fn search(config ConfigSearch) ! { |
| 269 | if config.filtered() && !config.groups && (config.remote || config.pkgbuild) { |
| 270 | return error('the filters only apply to installed packages: use them with -l, not -r or -a') |
| 271 | } |
| 272 | // the groups are the installed packages' or the sync dbs', pacman's -Qg |
| 273 | // or -Sg: asked for both, one of the two would be dropped without a word |
| 274 | if config.groups && config.local && config.remote { |
| 275 | return error('search -g lists the groups of the installed packages (-l) or of the sync dbs (-r), not both at once') |
| 276 | } |
| 277 | mut ui := new_trans_ui(noconfirm: config.noconfirm) |
| 278 | mut handle := init_handle(ui)! |
| 279 | defer { handle.release() or { went_on('release the nspkg handle', err) } } |
| 280 | defer { ui.stop() } |
| 281 | |
| 282 | fields := parse_fields(config.fields, cfg_search_fields)! |
| 283 | mut listing := Listing{ |
| 284 | mode: if config.quiet { PrintMode.minimal } else { PrintMode.default } |
| 285 | fields: fields |
| 286 | } |
| 287 | |
| 288 | if config.groups { |
| 289 | mut scope := config |
| 290 | mut dbs := []&nspkg.DB{} |
| 291 | mut sync_dbs := []&nspkg.DB{} |
| 292 | if config.local { |
| 293 | // pacman's -Qg: the groups of the installed packages, read from |
| 294 | // the local db. the sync dbs come in only for a filter that |
| 295 | // compares with them |
| 296 | dbs << handle.get_localdb() or { |
| 297 | return error('could not open the local database: ${err.msg()}') |
| 298 | } |
| 299 | sync_dbs = filter_sync_dbs(mut ui, mut handle, scope)! |
| 300 | } else { |
| 301 | // the same repo scoping narrows the group listing |
| 302 | scoped, query := scope_repo(init_repos(mut handle)!, config.query) |
| 303 | dbs = scoped.clone() |
| 304 | scope.query = query.clone() |
| 305 | // the sync-comparing filters (--native, --foreign, --upgrades) reach |
| 306 | // into these dbs through pkg_matches_filter, so they run the same |
| 307 | // freshness check the installed-package listing does. the dbs are |
| 308 | // registered above already, and libalpm refuses a second |
| 309 | // registration, so the check is all that is left of |
| 310 | // filter_sync_dbs here |
| 311 | if scope.foreign || scope.native || scope.upgrades { |
| 312 | check_sync_fresh(mut ui, mut handle)! |
| 313 | } |
| 314 | sync_dbs = dbs.clone() |
| 315 | } |
| 316 | if scope.query.len == 0 && config.groups_level >= 2 { |
| 317 | // -gg: every group with its members, pacman's 'group package' |
| 318 | // lines; each group is listed once, whichever dbs carry it |
| 319 | mut names := []string{} |
| 320 | for g in sync_groups(handle, dbs) { |
| 321 | if g.name !in names { |
| 322 | names << g.name |
| 323 | } |
| 324 | } |
| 325 | scope.query = names |
| 326 | collect_group_members(mut listing, handle, dbs, sync_dbs, scope)! |
| 327 | if ipc_on() || config.quiet { |
| 328 | listing.print() |
| 329 | } else { |
| 330 | for e in listing.entries { |
| 331 | println('${e.group} ${name_style(e.name)}') |
| 332 | } |
| 333 | } |
| 334 | return |
| 335 | } |
| 336 | if scope.query.len == 0 { |
| 337 | // no names: every group of every db, one line each |
| 338 | groups := sync_groups(handle, dbs) |
| 339 | if ipc_on() { |
| 340 | emit(GroupsEvent{ |
| 341 | groups: groups |
| 342 | }) |
| 343 | return |
| 344 | } |
| 345 | for g in groups { |
| 346 | println(table_text([row(g.repo, g.name)], ' ', ' ')) |
| 347 | } |
| 348 | return |
| 349 | } |
| 350 | collect_group_members(mut listing, handle, dbs, sync_dbs, scope) or { |
| 351 | listing.print_found() |
| 352 | return err |
| 353 | } |
| 354 | listing.print() |
| 355 | return |
| 356 | } |
| 357 | |
| 358 | collect_search(mut listing, mut ui, mut handle, config) or { |
| 359 | listing.print_found() |
| 360 | return err |
| 361 | } |
| 362 | listing.print() |
| 363 | } |
| 364 | |