pacman.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. package pacman
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "github.com/jguer/go-alpm"
  7. "github.com/jguer/yay/config"
  8. )
  9. // Query describes a Repository search.
  10. type Query []Result
  11. // Result describes a pkg.
  12. type Result struct {
  13. Name string
  14. Repository string
  15. Version string
  16. Description string
  17. Group string
  18. Installed bool
  19. }
  20. // Search handles repo searches. Creates a RepoSearch struct.
  21. func Search(pkgInputN []string) (s Query, n int, err error) {
  22. localDb, err := config.AlpmHandle.LocalDb()
  23. if err != nil {
  24. return
  25. }
  26. dbList, err := config.AlpmHandle.SyncDbs()
  27. if err != nil {
  28. return
  29. }
  30. var installed bool
  31. dbS := dbList.Slice()
  32. // BottomUp functions
  33. initL := func(len int) int {
  34. return len - 1
  35. }
  36. compL := func(len int, i int) bool {
  37. return i > 0
  38. }
  39. finalL := func(i int) int {
  40. return i - 1
  41. }
  42. // TopDown functions
  43. if config.YayConf.SortMode == config.TopDown {
  44. initL = func(len int) int {
  45. return 0
  46. }
  47. compL = func(len int, i int) bool {
  48. return i < len
  49. }
  50. finalL = func(i int) int {
  51. return i + 1
  52. }
  53. }
  54. lenDbs := len(dbS)
  55. for f := initL(lenDbs); compL(lenDbs, f); f = finalL(f) {
  56. pkgS := dbS[f].PkgCache().Slice()
  57. lenPkgs := len(pkgS)
  58. for i := initL(lenPkgs); compL(lenPkgs, i); i = finalL(i) {
  59. match := true
  60. for _, pkgN := range pkgInputN {
  61. if !(strings.Contains(pkgS[i].Name(), pkgN) || strings.Contains(strings.ToLower(pkgS[i].Description()), pkgN)) {
  62. match = false
  63. break
  64. }
  65. }
  66. if match {
  67. installed = false
  68. if r, _ := localDb.PkgByName(pkgS[i].Name()); r != nil {
  69. installed = true
  70. }
  71. n++
  72. s = append(s, Result{
  73. Name: pkgS[i].Name(),
  74. Description: pkgS[i].Description(),
  75. Version: pkgS[i].Version(),
  76. Repository: dbS[f].Name(),
  77. Group: strings.Join(pkgS[i].Groups().Slice(), ","),
  78. Installed: installed,
  79. })
  80. }
  81. }
  82. }
  83. return
  84. }
  85. //PrintSearch receives a RepoSearch type and outputs pretty text.
  86. func (s Query) PrintSearch() {
  87. for i, res := range s {
  88. var toprint string
  89. if config.YayConf.SearchMode == config.NumberMenu {
  90. if config.YayConf.SortMode == config.BottomUp {
  91. toprint += fmt.Sprintf("%d ", len(s)-i-1)
  92. } else {
  93. toprint += fmt.Sprintf("%d ", i)
  94. }
  95. } else if config.YayConf.SearchMode == config.Minimal {
  96. fmt.Println(res.Name)
  97. continue
  98. }
  99. toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m",
  100. res.Repository, res.Name, res.Version)
  101. if len(res.Group) != 0 {
  102. toprint += fmt.Sprintf("(%s) ", res.Group)
  103. }
  104. if res.Installed {
  105. toprint += fmt.Sprintf("\x1b[32;40mInstalled\x1b[0m")
  106. }
  107. toprint += "\n" + res.Description
  108. fmt.Println(toprint)
  109. }
  110. }
  111. // PackageSlices separates an input slice into aur and repo slices
  112. func PackageSlices(toCheck []string) (aur []string, repo []string, err error) {
  113. dbList, err := config.AlpmHandle.SyncDbs()
  114. if err != nil {
  115. return
  116. }
  117. for _, pkg := range toCheck {
  118. found := false
  119. for _, db := range dbList.Slice() {
  120. _, err = db.PkgByName(pkg)
  121. if err == nil {
  122. found = true
  123. repo = append(repo, pkg)
  124. break
  125. }
  126. }
  127. if !found {
  128. if _, errdb := dbList.PkgCachebyGroup(pkg); errdb == nil {
  129. repo = append(repo, pkg)
  130. } else {
  131. aur = append(aur, pkg)
  132. }
  133. }
  134. }
  135. err = nil
  136. return
  137. }
  138. // BuildDependencies finds packages, on the second run
  139. // compares with a baselist and avoids searching those
  140. func BuildDependencies(baselist []string) func(toCheck []string, isBaseList bool, last bool) (repo []string, notFound []string) {
  141. localDb, _ := config.AlpmHandle.LocalDb()
  142. dbList, _ := config.AlpmHandle.SyncDbs()
  143. f := func(c rune) bool {
  144. return c == '>' || c == '<' || c == '=' || c == ' '
  145. }
  146. return func(toCheck []string, isBaseList bool, close bool) (repo []string, notFound []string) {
  147. if close {
  148. return
  149. }
  150. Loop:
  151. for _, dep := range toCheck {
  152. if !isBaseList {
  153. for _, base := range baselist {
  154. if base == dep {
  155. continue Loop
  156. }
  157. }
  158. }
  159. if _, erp := localDb.PkgCache().FindSatisfier(dep); erp == nil {
  160. continue
  161. } else if pkg, erp := dbList.FindSatisfier(dep); erp == nil {
  162. repo = append(repo, pkg.Name())
  163. } else {
  164. field := strings.FieldsFunc(dep, f)
  165. notFound = append(notFound, field[0])
  166. }
  167. }
  168. return
  169. }
  170. }
  171. // DepSatisfier receives a string slice, returns a slice of packages found in
  172. // repos and one of packages not found in repos. Leaves out installed packages.
  173. func DepSatisfier(toCheck []string) (repo []string, notFound []string, err error) {
  174. localDb, err := config.AlpmHandle.LocalDb()
  175. if err != nil {
  176. return
  177. }
  178. dbList, err := config.AlpmHandle.SyncDbs()
  179. if err != nil {
  180. return
  181. }
  182. f := func(c rune) bool {
  183. return c == '>' || c == '<' || c == '=' || c == ' '
  184. }
  185. for _, dep := range toCheck {
  186. if _, erp := localDb.PkgCache().FindSatisfier(dep); erp == nil {
  187. continue
  188. } else if pkg, erp := dbList.FindSatisfier(dep); erp == nil {
  189. repo = append(repo, pkg.Name())
  190. } else {
  191. field := strings.FieldsFunc(dep, f)
  192. notFound = append(notFound, field[0])
  193. }
  194. }
  195. err = nil
  196. return
  197. }
  198. // CleanRemove sends a full removal command to pacman with the pkgName slice
  199. func CleanRemove(pkgName []string) (err error) {
  200. if len(pkgName) == 0 {
  201. return nil
  202. }
  203. err = config.PassToPacman("-Rsnc", pkgName, []string{"--noutil.Conf.rm"})
  204. return err
  205. }
  206. // ForeignPackages returns a map of foreign packages, with their version and date as values.
  207. func ForeignPackages() (foreign map[string]*struct {
  208. Version string
  209. Date int64
  210. }, n int, err error) {
  211. localDb, err := config.AlpmHandle.LocalDb()
  212. if err != nil {
  213. return
  214. }
  215. dbList, err := config.AlpmHandle.SyncDbs()
  216. if err != nil {
  217. return
  218. }
  219. foreign = make(map[string]*struct {
  220. Version string
  221. Date int64
  222. })
  223. // Find foreign packages in system
  224. for _, pkg := range localDb.PkgCache().Slice() {
  225. // Change to more effective method
  226. found := false
  227. for _, db := range dbList.Slice() {
  228. _, err = db.PkgByName(pkg.Name())
  229. if err == nil {
  230. found = true
  231. break
  232. }
  233. }
  234. if !found {
  235. foreign[pkg.Name()] = &struct {
  236. Version string
  237. Date int64
  238. }{pkg.Version(), pkg.InstallDate().Unix()}
  239. n++
  240. }
  241. }
  242. return
  243. }
  244. // Statistics returns statistics about packages installed in system
  245. func Statistics() (info struct {
  246. Totaln int
  247. Expln int
  248. TotalSize int64
  249. }, err error) {
  250. var tS int64 // TotalSize
  251. var nPkg int
  252. var ePkg int
  253. localDb, err := config.AlpmHandle.LocalDb()
  254. if err != nil {
  255. return
  256. }
  257. for _, pkg := range localDb.PkgCache().Slice() {
  258. tS += pkg.ISize()
  259. nPkg++
  260. if pkg.Reason() == 0 {
  261. ePkg++
  262. }
  263. }
  264. info = struct {
  265. Totaln int
  266. Expln int
  267. TotalSize int64
  268. }{
  269. nPkg, ePkg, tS,
  270. }
  271. return
  272. }
  273. // BiggestPackages prints the name of the ten biggest packages in the system.
  274. func BiggestPackages() {
  275. localDb, err := config.AlpmHandle.LocalDb()
  276. if err != nil {
  277. return
  278. }
  279. pkgCache := localDb.PkgCache()
  280. pkgS := pkgCache.SortBySize().Slice()
  281. if len(pkgS) < 10 {
  282. return
  283. }
  284. for i := 0; i < 10; i++ {
  285. fmt.Printf("%s: \x1B[0;33m%dMB\x1B[0m\n", pkgS[i].Name(), pkgS[i].ISize()/(1024*1024))
  286. }
  287. // Could implement size here as well, but we just want the general idea
  288. }
  289. // HangingPackages returns a list of packages installed as deps
  290. // and unneeded by the system
  291. func HangingPackages() (hanging []string, err error) {
  292. localDb, err := config.AlpmHandle.LocalDb()
  293. if err != nil {
  294. return
  295. }
  296. f := func(pkg alpm.Package) error {
  297. if pkg.Reason() != alpm.PkgReasonDepend {
  298. return nil
  299. }
  300. requiredby := pkg.ComputeRequiredBy()
  301. if len(requiredby) == 0 {
  302. hanging = append(hanging, pkg.Name())
  303. fmt.Printf("%s: \x1B[0;33m%dMB\x1B[0m\n", pkg.Name(), pkg.ISize()/(1024*1024))
  304. }
  305. return nil
  306. }
  307. err = localDb.PkgCache().ForEach(f)
  308. return
  309. }
  310. // SliceHangingPackages returns a list of packages installed as deps
  311. // and unneeded by the system from a provided list of package names.
  312. func SliceHangingPackages(pkgS []string) (hanging []string) {
  313. localDb, err := config.AlpmHandle.LocalDb()
  314. if err != nil {
  315. return
  316. }
  317. big:
  318. for _, pkgName := range pkgS {
  319. for _, hangN := range hanging {
  320. if hangN == pkgName {
  321. continue big
  322. }
  323. }
  324. pkg, err := localDb.PkgByName(pkgName)
  325. if err == nil {
  326. if pkg.Reason() != alpm.PkgReasonDepend {
  327. continue
  328. }
  329. requiredby := pkg.ComputeRequiredBy()
  330. if len(requiredby) == 0 {
  331. hanging = append(hanging, pkgName)
  332. fmt.Printf("%s: \x1B[0;33m%dMB\x1B[0m\n", pkg.Name(), pkg.ISize()/(1024*1024))
  333. }
  334. }
  335. }
  336. return
  337. }
  338. // GetPkgbuild downloads pkgbuild from the ABS.
  339. func GetPkgbuild(pkgN string, path string) (err error) {
  340. dbList, err := config.AlpmHandle.SyncDbs()
  341. if err != nil {
  342. return
  343. }
  344. for _, db := range dbList.Slice() {
  345. pkg, err := db.PkgByName(pkgN)
  346. if err == nil {
  347. var url string
  348. if db.Name() == "core" || db.Name() == "extra" {
  349. url = "https://projects.archlinux.org/svntogit/packages.git/snapshot/packages/" + pkg.Name() + ".tar.gz"
  350. } else if db.Name() == "community" {
  351. url = "https://projects.archlinux.org/svntogit/community.git/snapshot/community-packages/" + pkg.Name() + ".tar.gz"
  352. } else {
  353. return fmt.Errorf("Not in standard repositories")
  354. }
  355. fmt.Printf("\x1b[1;32m==>\x1b[1;33m %s \x1b[1;32mfound in ABS.\x1b[0m\n", pkgN)
  356. errD := config.DownloadAndUnpack(url, path, true)
  357. return errD
  358. }
  359. }
  360. return fmt.Errorf("package not found")
  361. }
  362. //CreatePackageList appends Repo packages to completion cache
  363. func CreatePackageList(out *os.File) (err error) {
  364. dbList, err := config.AlpmHandle.SyncDbs()
  365. if err != nil {
  366. return
  367. }
  368. p := func(pkg alpm.Package) error {
  369. fmt.Print(pkg.Name())
  370. out.WriteString(pkg.Name())
  371. if config.YayConf.Shell == "fish" {
  372. fmt.Print("\t" + pkg.DB().Name() + "\n")
  373. out.WriteString("\t" + pkg.DB().Name() + "\n")
  374. } else {
  375. fmt.Print("\n")
  376. out.WriteString("\n")
  377. }
  378. return nil
  379. }
  380. f := func(db alpm.Db) error {
  381. db.PkgCache().ForEach(p)
  382. return nil
  383. }
  384. dbList.ForEach(f)
  385. return nil
  386. }