upgrade.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "unicode"
  10. alpm "github.com/jguer/go-alpm"
  11. rpc "github.com/mikkeloscar/aur"
  12. pkgb "github.com/mikkeloscar/gopkgbuild"
  13. )
  14. // upgrade type describes a system upgrade.
  15. type upgrade struct {
  16. Name string
  17. Repository string
  18. LocalVersion string
  19. RemoteVersion string
  20. }
  21. // upSlice is a slice of Upgrades
  22. type upSlice []upgrade
  23. func (u upSlice) Len() int { return len(u) }
  24. func (u upSlice) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
  25. func (u upSlice) Less(i, j int) bool {
  26. iRunes := []rune(u[i].Repository)
  27. jRunes := []rune(u[j].Repository)
  28. max := len(iRunes)
  29. if max > len(jRunes) {
  30. max = len(jRunes)
  31. }
  32. for idx := 0; idx < max; idx++ {
  33. ir := iRunes[idx]
  34. jr := jRunes[idx]
  35. lir := unicode.ToLower(ir)
  36. ljr := unicode.ToLower(jr)
  37. if lir != ljr {
  38. return lir > ljr
  39. }
  40. // the lowercase runes are the same, so compare the original
  41. if ir != jr {
  42. return ir > jr
  43. }
  44. }
  45. return false
  46. }
  47. // Print prints the details of the packages to upgrade.
  48. func (u upSlice) Print(start int) {
  49. for k, i := range u {
  50. old, err := pkgb.NewCompleteVersion(i.LocalVersion)
  51. if err != nil {
  52. fmt.Println(i.Name, err)
  53. }
  54. new, err := pkgb.NewCompleteVersion(i.RemoteVersion)
  55. if err != nil {
  56. fmt.Println(i.Name, err)
  57. }
  58. f := func(name string) (color int) {
  59. var hash = 5381
  60. for i := 0; i < len(name); i++ {
  61. hash = int(name[i]) + ((hash << 5) + (hash))
  62. }
  63. return hash%6 + 31
  64. }
  65. fmt.Printf("\x1b[33m%-2d\x1b[0m ", len(u)+start-k-1)
  66. fmt.Printf("\x1b[1;%dm%s\x1b[0m/\x1b[1;39m%-25s\t\t\x1b[0m", f(i.Repository), i.Repository, i.Name)
  67. if old.Version != new.Version {
  68. fmt.Printf("\x1b[31m%18s\x1b[0m-%d -> \x1b[1;32m%s\x1b[0m-%d\x1b[0m",
  69. old.Version, old.Pkgrel,
  70. new.Version, new.Pkgrel)
  71. } else {
  72. fmt.Printf("\x1b[0m%18s-\x1b[31m%d\x1b[0m -> %s-\x1b[32m%d\x1b[0m",
  73. old.Version, old.Pkgrel,
  74. new.Version, new.Pkgrel)
  75. }
  76. print("\n")
  77. }
  78. }
  79. // upList returns lists of packages to upgrade from each source.
  80. func upList() (aurUp upSlice, repoUp upSlice, err error) {
  81. local, remote, _, remoteNames, err := filterPackages()
  82. if err != nil {
  83. return
  84. }
  85. repoC := make(chan upSlice)
  86. aurC := make(chan upSlice)
  87. errC := make(chan error)
  88. fmt.Println("\x1b[1;36;1m::\x1b[0m\x1b[1m Searching databases for updates...\x1b[0m")
  89. go func() {
  90. repoUpList, err := upRepo(local)
  91. errC <- err
  92. repoC <- repoUpList
  93. }()
  94. fmt.Println("\x1b[1;36;1m::\x1b[0m\x1b[1m Searching AUR for updates...\x1b[0m")
  95. go func() {
  96. aurUpList, err := upAUR(remote, remoteNames)
  97. errC <- err
  98. aurC <- aurUpList
  99. }()
  100. var i = 0
  101. loop:
  102. for {
  103. select {
  104. case repoUp = <-repoC:
  105. i++
  106. case aurUp = <-aurC:
  107. i++
  108. case err := <-errC:
  109. if err != nil {
  110. fmt.Println(err)
  111. }
  112. default:
  113. if i == 2 {
  114. close(repoC)
  115. close(aurC)
  116. close(errC)
  117. break loop
  118. }
  119. }
  120. }
  121. return
  122. }
  123. func isIgnored(pkg alpm.Package) bool {
  124. for _, p := range alpmConf.IgnorePkg {
  125. if p == pkg.Name() {
  126. fmt.Printf("\x1b[33mwarning:\x1b[0m %s (ignored pkg) ignoring upgrade (%s)\n", pkg.Name(), pkg.Version())
  127. return true
  128. }
  129. }
  130. for _, g := range alpmConf.IgnoreGroup {
  131. for _, pg := range pkg.Groups().Slice() {
  132. if g == pg {
  133. fmt.Printf("\x1b[33mwarning:\x1b[0m %s (ignored pkg) ignoring upgrade (%s)\n", pkg.Name(), pkg.Version())
  134. return true
  135. }
  136. }
  137. }
  138. return false
  139. }
  140. // upAUR gathers foreign packages and checks if they have new versions.
  141. // Output: Upgrade type package list.
  142. func upAUR(remote []alpm.Package, remoteNames []string) (toUpgrade upSlice, err error) {
  143. var j int
  144. var routines int
  145. var routineDone int
  146. packageC := make(chan upgrade)
  147. done := make(chan bool)
  148. for i := len(remote); i != 0; i = j {
  149. //Split requests so AUR RPC doesn't get mad at us.
  150. j = i - config.RequestSplitN
  151. if j < 0 {
  152. j = 0
  153. }
  154. routines++
  155. go func(local []alpm.Package, remote []string) {
  156. qtemp, err := rpc.Info(remoteNames)
  157. if err != nil {
  158. fmt.Println(err)
  159. done <- true
  160. return
  161. }
  162. // For each item in query: Search equivalent in foreign.
  163. // We assume they're ordered and are returned ordered
  164. // and will only be missing if they don't exist in AUR.
  165. max := len(qtemp) - 1
  166. var missing, x int
  167. for i := range local {
  168. if isIgnored(local[i]) {
  169. continue
  170. }
  171. x = i - missing
  172. if x > max {
  173. break
  174. } else if qtemp[x].Name == local[i].Name() {
  175. if (config.TimeUpdate && (int64(qtemp[x].LastModified) > local[i].BuildDate().Unix())) ||
  176. (alpm.VerCmp(local[i].Version(), qtemp[x].Version) < 0) {
  177. packageC <- upgrade{qtemp[x].Name, "aur", local[i].Version(), qtemp[x].Version}
  178. }
  179. continue
  180. } else {
  181. missing++
  182. }
  183. }
  184. done <- true
  185. }(remote[j:i], remoteNames[j:i])
  186. }
  187. for {
  188. select {
  189. case pkg := <-packageC:
  190. toUpgrade = append(toUpgrade, pkg)
  191. case <-done:
  192. routineDone++
  193. if routineDone == routines {
  194. err = nil
  195. return
  196. }
  197. }
  198. }
  199. }
  200. // upRepo gathers local packages and checks if they have new versions.
  201. // Output: Upgrade type package list.
  202. func upRepo(local []alpm.Package) (upSlice, error) {
  203. dbList, err := alpmHandle.SyncDbs()
  204. if err != nil {
  205. return nil, err
  206. }
  207. slice := upSlice{}
  208. for _, pkg := range local {
  209. if isIgnored(pkg) {
  210. continue
  211. }
  212. newPkg := pkg.NewVersion(dbList)
  213. if newPkg != nil {
  214. slice = append(slice, upgrade{pkg.Name(), newPkg.DB().Name(), pkg.Version(), newPkg.Version()})
  215. }
  216. }
  217. return slice, nil
  218. }
  219. // upgradePkgs handles updating the cache and installing updates.
  220. func upgradePkgs(flags []string) error {
  221. aurUp, repoUp, err := upList()
  222. if err != nil {
  223. return err
  224. } else if len(aurUp)+len(repoUp) == 0 {
  225. fmt.Println("\nthere is nothing to do")
  226. return err
  227. }
  228. var repoNums []int
  229. var aurNums []int
  230. sort.Sort(repoUp)
  231. fmt.Printf("\x1b[1;34;1m:: \x1b[0m\x1b[1m%d Packages to upgrade.\x1b[0m\n", len(aurUp)+len(repoUp))
  232. repoUp.Print(len(aurUp))
  233. aurUp.Print(0)
  234. if !config.NoConfirm {
  235. fmt.Print("\x1b[32mEnter packages you don't want to upgrade.\x1b[0m\nNumbers: ")
  236. reader := bufio.NewReader(os.Stdin)
  237. numberBuf, overflow, err := reader.ReadLine()
  238. if err != nil || overflow {
  239. fmt.Println(err)
  240. return err
  241. }
  242. result := strings.Fields(string(numberBuf))
  243. for _, numS := range result {
  244. num, err := strconv.Atoi(numS)
  245. if err != nil {
  246. continue
  247. }
  248. if num > len(aurUp)+len(repoUp)-1 || num < 0 {
  249. continue
  250. } else if num < len(aurUp) {
  251. num = len(aurUp) - num - 1
  252. aurNums = append(aurNums, num)
  253. } else {
  254. num = len(aurUp) + len(repoUp) - num - 1
  255. repoNums = append(repoNums, num)
  256. }
  257. }
  258. }
  259. if len(repoUp) != 0 {
  260. var repoNames []string
  261. repoloop:
  262. for i, k := range repoUp {
  263. for _, j := range repoNums {
  264. if j == i {
  265. continue repoloop
  266. }
  267. }
  268. repoNames = append(repoNames, k.Name)
  269. }
  270. err := passToPacman("-S", repoNames, append(flags, "--noconfirm"))
  271. if err != nil {
  272. fmt.Println("Error upgrading repo packages.")
  273. }
  274. }
  275. if len(aurUp) != 0 {
  276. var aurNames []string
  277. aurloop:
  278. for i, k := range aurUp {
  279. for _, j := range aurNums {
  280. if j == i {
  281. continue aurloop
  282. }
  283. }
  284. aurNames = append(aurNames, k.Name)
  285. }
  286. aurInstall(aurNames, flags)
  287. }
  288. return nil
  289. }
  290. func develUpgrade(foreign map[string]alpm.Package, flags []string) error {
  291. fmt.Println(" Checking development packages...")
  292. develUpdates := checkUpdates(foreign)
  293. if len(develUpdates) != 0 {
  294. for _, q := range develUpdates {
  295. fmt.Printf("\x1b[1m\x1b[32m==>\x1b[33;1m %s\x1b[0m\n", q)
  296. }
  297. // Install updated packages
  298. if !continueTask("Proceed with upgrade?", "nN") {
  299. return nil
  300. }
  301. err := aurInstall(develUpdates, flags)
  302. if err != nil {
  303. fmt.Println(err)
  304. }
  305. }
  306. return nil
  307. }