query.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "sync"
  7. alpm "github.com/jguer/go-alpm"
  8. rpc "github.com/mikkeloscar/aur"
  9. )
  10. // Query is a collection of Results
  11. type aurQuery []rpc.Pkg
  12. // Query holds the results of a repository search.
  13. type repoQuery []alpm.Package
  14. func (q aurQuery) Len() int {
  15. return len(q)
  16. }
  17. func (q aurQuery) Less(i, j int) bool {
  18. var result bool
  19. switch config.SortBy {
  20. case "votes":
  21. result = q[i].NumVotes > q[j].NumVotes
  22. case "popularity":
  23. result = q[i].Popularity > q[j].Popularity
  24. case "name":
  25. result = lessRunes([]rune(q[i].Name), []rune(q[j].Name))
  26. case "base":
  27. result = lessRunes([]rune(q[i].PackageBase), []rune(q[j].PackageBase))
  28. case "submitted":
  29. result = q[i].FirstSubmitted < q[j].FirstSubmitted
  30. case "modified":
  31. result = q[i].LastModified < q[j].LastModified
  32. case "id":
  33. result = q[i].ID < q[j].ID
  34. case "baseid":
  35. result = q[i].PackageBaseID < q[j].PackageBaseID
  36. }
  37. if config.SortMode == BottomUp {
  38. return !result
  39. }
  40. return result
  41. }
  42. func (q aurQuery) Swap(i, j int) {
  43. q[i], q[j] = q[j], q[i]
  44. }
  45. // FilterPackages filters packages based on source and type from local repository.
  46. func filterPackages() (local []alpm.Package, remote []alpm.Package,
  47. localNames []string, remoteNames []string, err error) {
  48. localDb, err := alpmHandle.LocalDb()
  49. if err != nil {
  50. return
  51. }
  52. dbList, err := alpmHandle.SyncDbs()
  53. if err != nil {
  54. return
  55. }
  56. f := func(k alpm.Package) error {
  57. found := false
  58. // For each DB search for our secret package.
  59. _ = dbList.ForEach(func(d alpm.Db) error {
  60. if found {
  61. return nil
  62. }
  63. _, err := d.PkgByName(k.Name())
  64. if err == nil {
  65. found = true
  66. local = append(local, k)
  67. localNames = append(localNames, k.Name())
  68. }
  69. return nil
  70. })
  71. if !found {
  72. remote = append(remote, k)
  73. remoteNames = append(remoteNames, k.Name())
  74. }
  75. return nil
  76. }
  77. err = localDb.PkgCache().ForEach(f)
  78. return
  79. }
  80. // NarrowSearch searches AUR and narrows based on subarguments
  81. func narrowSearch(pkgS []string, sortS bool) (aurQuery, error) {
  82. var r []rpc.Pkg
  83. var err error
  84. var usedIndex int
  85. if len(pkgS) == 0 {
  86. return nil, nil
  87. }
  88. for i, word := range pkgS {
  89. r, err = rpc.Search(word)
  90. if err == nil {
  91. usedIndex = i
  92. break
  93. }
  94. }
  95. if err != nil {
  96. return nil, err
  97. }
  98. if len(pkgS) == 1 {
  99. if sortS {
  100. sort.Sort(aurQuery(r))
  101. }
  102. return r, err
  103. }
  104. var aq aurQuery
  105. var n int
  106. for _, res := range r {
  107. match := true
  108. for i, pkgN := range pkgS {
  109. if usedIndex == i {
  110. continue
  111. }
  112. if !(strings.Contains(res.Name, pkgN) || strings.Contains(strings.ToLower(res.Description), pkgN)) {
  113. match = false
  114. break
  115. }
  116. }
  117. if match {
  118. n++
  119. aq = append(aq, res)
  120. }
  121. }
  122. if sortS {
  123. sort.Sort(aq)
  124. }
  125. return aq, err
  126. }
  127. // SyncSearch presents a query to the local repos and to the AUR.
  128. func syncSearch(pkgS []string) (err error) {
  129. aq, aurErr := narrowSearch(pkgS, true)
  130. pq, _, err := queryRepo(pkgS)
  131. if err != nil {
  132. return err
  133. }
  134. if config.SortMode == BottomUp {
  135. aq.printSearch(1)
  136. pq.printSearch()
  137. } else {
  138. pq.printSearch()
  139. aq.printSearch(1)
  140. }
  141. if aurErr != nil {
  142. fmt.Printf("Error during AUR search: %s\n", aurErr)
  143. fmt.Println("Showing Repo packags only")
  144. }
  145. return nil
  146. }
  147. // SyncInfo serves as a pacman -Si for repo packages and AUR packages.
  148. func syncInfo(pkgS []string) (err error) {
  149. var info []*rpc.Pkg
  150. aurS, repoS, err := packageSlices(pkgS)
  151. if err != nil {
  152. return
  153. }
  154. if len(aurS) != 0 {
  155. noDb := make([]string, 0, len(aurS))
  156. for _, pkg := range aurS {
  157. _, name := splitDbFromName(pkg)
  158. noDb = append(noDb, name)
  159. }
  160. info, err = aurInfo(noDb)
  161. if err != nil {
  162. fmt.Println(err)
  163. }
  164. }
  165. // Repo always goes first
  166. if len(repoS) != 0 {
  167. arguments := cmdArgs.copy()
  168. arguments.delTarget(aurS...)
  169. err = passToPacman(arguments)
  170. if err != nil {
  171. return
  172. }
  173. }
  174. if len(aurS) != 0 {
  175. for _, pkg := range info {
  176. PrintInfo(pkg)
  177. }
  178. }
  179. return
  180. }
  181. // Search handles repo searches. Creates a RepoSearch struct.
  182. func queryRepo(pkgInputN []string) (s repoQuery, n int, err error) {
  183. dbList, err := alpmHandle.SyncDbs()
  184. if err != nil {
  185. return
  186. }
  187. // BottomUp functions
  188. initL := func(len int) int {
  189. if config.SortMode == TopDown {
  190. return 0
  191. }
  192. return len - 1
  193. }
  194. compL := func(len int, i int) bool {
  195. if config.SortMode == TopDown {
  196. return i < len
  197. }
  198. return i > -1
  199. }
  200. finalL := func(i int) int {
  201. if config.SortMode == TopDown {
  202. return i + 1
  203. }
  204. return i - 1
  205. }
  206. dbS := dbList.Slice()
  207. lenDbs := len(dbS)
  208. for f := initL(lenDbs); compL(lenDbs, f); f = finalL(f) {
  209. pkgS := dbS[f].PkgCache().Slice()
  210. lenPkgs := len(pkgS)
  211. for i := initL(lenPkgs); compL(lenPkgs, i); i = finalL(i) {
  212. match := true
  213. for _, pkgN := range pkgInputN {
  214. if !(strings.Contains(pkgS[i].Name(), pkgN) || strings.Contains(strings.ToLower(pkgS[i].Description()), pkgN)) {
  215. match = false
  216. break
  217. }
  218. }
  219. if match {
  220. n++
  221. s = append(s, pkgS[i])
  222. }
  223. }
  224. }
  225. return
  226. }
  227. // PackageSlices separates an input slice into aur and repo slices
  228. func packageSlices(toCheck []string) (aur []string, repo []string, err error) {
  229. dbList, err := alpmHandle.SyncDbs()
  230. if err != nil {
  231. return
  232. }
  233. for _, _pkg := range toCheck {
  234. db, name := splitDbFromName(_pkg)
  235. found := false
  236. if db == "aur" {
  237. aur = append(aur, _pkg)
  238. continue
  239. } else if db != "" {
  240. repo = append(repo, _pkg)
  241. continue
  242. }
  243. _ = dbList.ForEach(func(db alpm.Db) error {
  244. _, err := db.PkgByName(name)
  245. if err == nil {
  246. found = true
  247. return fmt.Errorf("")
  248. }
  249. return nil
  250. })
  251. if !found {
  252. _, errdb := dbList.PkgCachebyGroup(name)
  253. found = errdb == nil
  254. }
  255. if found {
  256. repo = append(repo, _pkg)
  257. } else {
  258. aur = append(aur, _pkg)
  259. }
  260. }
  261. return
  262. }
  263. // HangingPackages returns a list of packages installed as deps
  264. // and unneeded by the system
  265. func hangingPackages() (hanging []string, err error) {
  266. localDb, err := alpmHandle.LocalDb()
  267. if err != nil {
  268. return
  269. }
  270. f := func(pkg alpm.Package) error {
  271. if pkg.Reason() != alpm.PkgReasonDepend {
  272. return nil
  273. }
  274. requiredby := pkg.ComputeRequiredBy()
  275. if len(requiredby) == 0 {
  276. hanging = append(hanging, pkg.Name())
  277. fmt.Println(pkg.Name() + ": " + magenta(human(pkg.ISize())))
  278. }
  279. return nil
  280. }
  281. err = localDb.PkgCache().ForEach(f)
  282. return
  283. }
  284. // Statistics returns statistics about packages installed in system
  285. func statistics() (info struct {
  286. Totaln int
  287. Expln int
  288. TotalSize int64
  289. }, err error) {
  290. var tS int64 // TotalSize
  291. var nPkg int
  292. var ePkg int
  293. localDb, err := alpmHandle.LocalDb()
  294. if err != nil {
  295. return
  296. }
  297. for _, pkg := range localDb.PkgCache().Slice() {
  298. tS += pkg.ISize()
  299. nPkg++
  300. if pkg.Reason() == 0 {
  301. ePkg++
  302. }
  303. }
  304. info = struct {
  305. Totaln int
  306. Expln int
  307. TotalSize int64
  308. }{
  309. nPkg, ePkg, tS,
  310. }
  311. return
  312. }
  313. // Queries the aur for information about specified packages.
  314. // All packages should be queried in a single rpc request except when the number
  315. // of packages exceeds the number set in config.RequestSplitN.
  316. // If the number does exceed config.RequestSplitN multiple rpc requests will be
  317. // performed concurrently.
  318. func aurInfo(names []string) ([]*rpc.Pkg, error) {
  319. info := make([]*rpc.Pkg, 0, len(names))
  320. seen := make(map[string]int)
  321. var mux sync.Mutex
  322. var wg sync.WaitGroup
  323. var err error
  324. missing := make([]string, 0, len(names))
  325. orphans := make([]string, 0, len(names))
  326. outOfDate := make([]string, 0, len(names))
  327. makeRequest := func(n, max int) {
  328. defer wg.Done()
  329. tempInfo, requestErr := rpc.Info(names[n:max])
  330. if err != nil {
  331. return
  332. }
  333. if requestErr != nil {
  334. err = requestErr
  335. return
  336. }
  337. mux.Lock()
  338. for _, _i := range tempInfo {
  339. i := _i
  340. info = append(info, &i)
  341. }
  342. mux.Unlock()
  343. }
  344. for n := 0; n < len(names); n += config.RequestSplitN {
  345. max := min(len(names), n+config.RequestSplitN)
  346. wg.Add(1)
  347. go makeRequest(n, max)
  348. }
  349. wg.Wait()
  350. if err != nil {
  351. return info, err
  352. }
  353. for k, pkg := range info {
  354. seen[pkg.Name] = k
  355. }
  356. for _, name := range names {
  357. i, ok := seen[name]
  358. if !ok {
  359. missing = append(missing, name)
  360. continue
  361. }
  362. pkg := info[i]
  363. if pkg.Maintainer == "" {
  364. orphans = append(orphans, name)
  365. }
  366. if pkg.OutOfDate != 0 {
  367. outOfDate = append(outOfDate, name)
  368. }
  369. }
  370. if len(missing) > 0 {
  371. fmt.Print(bold(red(arrow + " Missing AUR Packages:")))
  372. for _, name := range missing {
  373. fmt.Print(" " + bold(magenta(name)))
  374. }
  375. fmt.Println()
  376. }
  377. if len(orphans) > 0 {
  378. fmt.Print(bold(red(arrow + " Orphaned AUR Packages:")))
  379. for _, name := range orphans {
  380. fmt.Print(" " + bold(magenta(name)))
  381. }
  382. fmt.Println()
  383. }
  384. if len(outOfDate) > 0 {
  385. fmt.Print(bold(red(arrow + " Out Of Date AUR Packages:")))
  386. for _, name := range outOfDate {
  387. fmt.Print(" " + bold(magenta(name)))
  388. }
  389. fmt.Println()
  390. }
  391. return info, nil
  392. }