query.go 7.7 KB

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