query.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. if i := strings.Index(_pkg, "/"); i != -1 {
  198. _pkg = _pkg[i+1:]
  199. }
  200. pkg := getNameFromDep(_pkg)
  201. _, errdb := dbList.FindSatisfier(_pkg)
  202. found := errdb == nil
  203. if !found {
  204. _, errdb = dbList.PkgCachebyGroup(_pkg)
  205. found = errdb == nil
  206. }
  207. if found {
  208. repo = append(repo, pkg)
  209. } else {
  210. aur = append(aur, pkg)
  211. }
  212. }
  213. return
  214. }
  215. // HangingPackages returns a list of packages installed as deps
  216. // and unneeded by the system
  217. func hangingPackages() (hanging []string, err error) {
  218. localDb, err := alpmHandle.LocalDb()
  219. if err != nil {
  220. return
  221. }
  222. f := func(pkg alpm.Package) error {
  223. if pkg.Reason() != alpm.PkgReasonDepend {
  224. return nil
  225. }
  226. requiredby := pkg.ComputeRequiredBy()
  227. if len(requiredby) == 0 {
  228. hanging = append(hanging, pkg.Name())
  229. fmt.Println(pkg.Name() + ": " + magenta(human(pkg.ISize())))
  230. }
  231. return nil
  232. }
  233. err = localDb.PkgCache().ForEach(f)
  234. return
  235. }
  236. // Statistics returns statistics about packages installed in system
  237. func statistics() (info struct {
  238. Totaln int
  239. Expln int
  240. TotalSize int64
  241. }, err error) {
  242. var tS int64 // TotalSize
  243. var nPkg int
  244. var ePkg int
  245. localDb, err := alpmHandle.LocalDb()
  246. if err != nil {
  247. return
  248. }
  249. for _, pkg := range localDb.PkgCache().Slice() {
  250. tS += pkg.ISize()
  251. nPkg++
  252. if pkg.Reason() == 0 {
  253. ePkg++
  254. }
  255. }
  256. info = struct {
  257. Totaln int
  258. Expln int
  259. TotalSize int64
  260. }{
  261. nPkg, ePkg, tS,
  262. }
  263. return
  264. }
  265. func min(a, b int) int {
  266. if a < b {
  267. return a
  268. }
  269. return b
  270. }
  271. func max(a, b int) int {
  272. if a < b {
  273. return b
  274. }
  275. return a
  276. }
  277. // Queries the aur for information about specified packages.
  278. // All packages should be queried in a single rpc request except when the number
  279. // of packages exceeds the number set in config.RequestSplitN.
  280. // If the number does exceed config.RequestSplitN multiple rpc requests will be
  281. // performed concurrently.
  282. func aurInfo(names []string) ([]rpc.Pkg, error) {
  283. info := make([]rpc.Pkg, 0, len(names))
  284. seen := make(map[string]int)
  285. var mux sync.Mutex
  286. var wg sync.WaitGroup
  287. var err error
  288. missing := make([]string, 0, len(names))
  289. orphans := make([]string, 0, len(names))
  290. outOfDate := make([]string, 0, len(names))
  291. makeRequest := func(n, max int) {
  292. tempInfo, requestErr := rpc.Info(names[n:max])
  293. if err != nil {
  294. wg.Done()
  295. return
  296. }
  297. if requestErr != nil {
  298. //return info, err
  299. err = requestErr
  300. wg.Done()
  301. return
  302. }
  303. mux.Lock()
  304. info = append(info, tempInfo...)
  305. mux.Unlock()
  306. wg.Done()
  307. }
  308. for n := 0; n < len(names); n += config.RequestSplitN {
  309. max := min(len(names), n+config.RequestSplitN)
  310. wg.Add(1)
  311. go makeRequest(n, max)
  312. }
  313. wg.Wait()
  314. if err != nil {
  315. return info, err
  316. }
  317. for k, pkg := range info {
  318. seen[pkg.Name] = k
  319. }
  320. for _, name := range names {
  321. i, ok := seen[name]
  322. if !ok {
  323. missing = append(missing, name)
  324. continue
  325. }
  326. pkg := info[i]
  327. if pkg.Maintainer == "" {
  328. orphans = append(orphans, name)
  329. }
  330. if pkg.OutOfDate != 0 {
  331. outOfDate = append(outOfDate, name)
  332. }
  333. }
  334. if len(missing) > 0 {
  335. fmt.Print(bold(red(arrow + " Missing AUR Packages:")))
  336. for _, name := range missing {
  337. fmt.Print(" " + bold(magenta(name)))
  338. }
  339. fmt.Println()
  340. }
  341. if len(orphans) > 0 {
  342. fmt.Print(bold(red(arrow + " Orphaned AUR Packages:")))
  343. for _, name := range orphans {
  344. fmt.Print(" " + bold(magenta(name)))
  345. }
  346. fmt.Println()
  347. }
  348. if len(outOfDate) > 0 {
  349. fmt.Print(bold(red(arrow + " Out Of Date AUR Packages:")))
  350. for _, name := range outOfDate {
  351. fmt.Print(" " + bold(magenta(name)))
  352. }
  353. fmt.Println()
  354. }
  355. return info, nil
  356. }