upgrade.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "sort"
  7. "unicode"
  8. alpm "github.com/jguer/go-alpm"
  9. pkgb "github.com/mikkeloscar/gopkgbuild"
  10. )
  11. // upgrade type describes a system upgrade.
  12. type upgrade struct {
  13. Name string
  14. Repository string
  15. LocalVersion string
  16. RemoteVersion string
  17. }
  18. // upSlice is a slice of Upgrades
  19. type upSlice []upgrade
  20. func (u upSlice) Len() int { return len(u) }
  21. func (u upSlice) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
  22. func (u upSlice) Less(i, j int) bool {
  23. iRunes := []rune(u[i].Repository)
  24. jRunes := []rune(u[j].Repository)
  25. max := len(iRunes)
  26. if max > len(jRunes) {
  27. max = len(jRunes)
  28. }
  29. for idx := 0; idx < max; idx++ {
  30. ir := iRunes[idx]
  31. jr := jRunes[idx]
  32. lir := unicode.ToLower(ir)
  33. ljr := unicode.ToLower(jr)
  34. if lir != ljr {
  35. return lir > ljr
  36. }
  37. // the lowercase runes are the same, so compare the original
  38. if ir != jr {
  39. return ir > jr
  40. }
  41. }
  42. return false
  43. }
  44. func getVersionDiff(oldVersion, newversion string) (left, right string) {
  45. old, errOld := pkgb.NewCompleteVersion(oldVersion)
  46. new, errNew := pkgb.NewCompleteVersion(newversion)
  47. if errOld != nil {
  48. left = red("Invalid Version")
  49. }
  50. if errNew != nil {
  51. right = red("Invalid Version")
  52. }
  53. if errOld == nil && errNew == nil {
  54. if old.Version == new.Version {
  55. left = string(old.Version) + "-" + red(string(old.Pkgrel))
  56. right = string(new.Version) + "-" + green(string(new.Pkgrel))
  57. } else {
  58. left = red(string(old.Version)) + "-" + string(old.Pkgrel)
  59. right = bold(green(string(new.Version))) + "-" + string(new.Pkgrel)
  60. }
  61. }
  62. return
  63. }
  64. // upList returns lists of packages to upgrade from each source.
  65. func upList(dt *depTree) (aurUp upSlice, repoUp upSlice, err error) {
  66. local, remote, _, remoteNames, err := filterPackages()
  67. if err != nil {
  68. return
  69. }
  70. repoC := make(chan upSlice)
  71. aurC := make(chan upSlice)
  72. errC := make(chan error)
  73. fmt.Println(bold(cyan("::") + " Searching databases for updates..."))
  74. go func() {
  75. repoUpList, err := upRepo(local)
  76. errC <- err
  77. repoC <- repoUpList
  78. }()
  79. fmt.Println(bold(cyan("::") + " Searching AUR for updates..."))
  80. go func() {
  81. aurUpList, err := upAUR(remote, remoteNames, dt)
  82. errC <- err
  83. aurC <- aurUpList
  84. }()
  85. var i = 0
  86. loop:
  87. for {
  88. select {
  89. case repoUp = <-repoC:
  90. i++
  91. case aurUp = <-aurC:
  92. i++
  93. case err := <-errC:
  94. if err != nil {
  95. fmt.Println(err)
  96. }
  97. default:
  98. if i == 2 {
  99. close(repoC)
  100. close(aurC)
  101. close(errC)
  102. break loop
  103. }
  104. }
  105. }
  106. return
  107. }
  108. func upDevel(remote []alpm.Package, packageC chan upgrade, done chan bool) {
  109. for vcsName, e := range savedInfo {
  110. if e.needsUpdate() {
  111. found := false
  112. var pkg alpm.Package
  113. for _, r := range remote {
  114. if r.Name() == vcsName {
  115. found = true
  116. pkg = r
  117. }
  118. }
  119. if found {
  120. if pkg.ShouldIgnore() {
  121. fmt.Print(magenta("Warning: "))
  122. fmt.Printf("%s ignoring package upgrade (%s => %s)\n", cyan(pkg.Name()), pkg.Version(), "git")
  123. } else {
  124. packageC <- upgrade{pkg.Name(), "devel", pkg.Version(), "latest-commit"}
  125. }
  126. } else {
  127. removeVCSPackage([]string{vcsName})
  128. }
  129. }
  130. }
  131. done <- true
  132. }
  133. // upAUR gathers foreign packages and checks if they have new versions.
  134. // Output: Upgrade type package list.
  135. func upAUR(remote []alpm.Package, remoteNames []string, dt *depTree) (toUpgrade upSlice, err error) {
  136. var routines int
  137. var routineDone int
  138. packageC := make(chan upgrade)
  139. done := make(chan bool)
  140. if config.Devel {
  141. routines++
  142. go upDevel(remote, packageC, done)
  143. fmt.Println(bold(cyan("::") + " Checking development packages..."))
  144. }
  145. routines++
  146. go func(remote []alpm.Package, remoteNames []string, dt *depTree) {
  147. for _, pkg := range remote {
  148. aurPkg, ok := dt.Aur[pkg.Name()]
  149. if !ok {
  150. continue
  151. }
  152. if (config.TimeUpdate && (int64(aurPkg.LastModified) > pkg.BuildDate().Unix())) ||
  153. (alpm.VerCmp(pkg.Version(), aurPkg.Version) < 0) {
  154. if pkg.ShouldIgnore() {
  155. left, right := getVersionDiff(pkg.Version(), aurPkg.Version)
  156. fmt.Print(magenta("Warning: "))
  157. fmt.Printf("%s ignoring package upgrade (%s => %s)\n", cyan(pkg.Name()), left, right)
  158. } else {
  159. packageC <- upgrade{aurPkg.Name, "aur", pkg.Version(), aurPkg.Version}
  160. }
  161. }
  162. }
  163. done <- true
  164. }(remote, remoteNames, dt)
  165. if routineDone == routines {
  166. err = nil
  167. return
  168. }
  169. for {
  170. select {
  171. case pkg := <-packageC:
  172. for _, w := range toUpgrade {
  173. if w.Name == pkg.Name {
  174. continue
  175. }
  176. }
  177. toUpgrade = append(toUpgrade, pkg)
  178. case <-done:
  179. routineDone++
  180. if routineDone == routines {
  181. err = nil
  182. return
  183. }
  184. }
  185. }
  186. }
  187. // upRepo gathers local packages and checks if they have new versions.
  188. // Output: Upgrade type package list.
  189. func upRepo(local []alpm.Package) (upSlice, error) {
  190. dbList, err := alpmHandle.SyncDbs()
  191. if err != nil {
  192. return nil, err
  193. }
  194. slice := upSlice{}
  195. for _, pkg := range local {
  196. newPkg := pkg.NewVersion(dbList)
  197. if newPkg != nil {
  198. if pkg.ShouldIgnore() {
  199. fmt.Print(magenta("Warning: "))
  200. fmt.Printf("%s ignoring package upgrade (%s => %s)\n", pkg.Name(), pkg.Version(), newPkg.Version())
  201. } else {
  202. slice = append(slice, upgrade{pkg.Name(), newPkg.DB().Name(), pkg.Version(), newPkg.Version()})
  203. }
  204. }
  205. }
  206. return slice, nil
  207. }
  208. //Contains returns whether e is present in s
  209. func containsInt(s []int, e int) bool {
  210. for _, a := range s {
  211. if a == e {
  212. return true
  213. }
  214. }
  215. return false
  216. }
  217. // RemoveIntListFromList removes all src's elements that are present in target
  218. func removeIntListFromList(src, target []int) []int {
  219. max := len(target)
  220. for i := 0; i < max; i++ {
  221. if containsInt(src, target[i]) {
  222. target = append(target[:i], target[i+1:]...)
  223. max--
  224. i--
  225. }
  226. }
  227. return target
  228. }
  229. // upgradePkgs handles updating the cache and installing updates.
  230. func upgradePkgs(dt *depTree) (stringSet, stringSet, error) {
  231. repoNames := make(stringSet)
  232. aurNames := make(stringSet)
  233. aurUp, repoUp, err := upList(dt)
  234. if err != nil {
  235. return repoNames, aurNames, err
  236. } else if len(aurUp)+len(repoUp) == 0 {
  237. return repoNames, aurNames, err
  238. }
  239. sort.Sort(repoUp)
  240. fmt.Println(bold(blue("::")), len(aurUp)+len(repoUp), bold("Packages to upgrade."))
  241. repoUp.Print(len(aurUp) + 1)
  242. aurUp.Print(1)
  243. if config.NoConfirm {
  244. for _, up := range repoUp {
  245. repoNames.set(up.Name)
  246. }
  247. for _, up := range aurUp {
  248. aurNames.set(up.Name)
  249. }
  250. return repoNames, aurNames, nil
  251. }
  252. fmt.Println(bold(green(arrow + " Packages to not upgrade (eg: 1 2 3, 1-3, ^4 or repo name)")))
  253. fmt.Print(bold(green(arrow + " ")))
  254. reader := bufio.NewReader(os.Stdin)
  255. numberBuf, overflow, err := reader.ReadLine()
  256. if err != nil {
  257. return nil, nil, err
  258. }
  259. if overflow {
  260. return nil, nil, fmt.Errorf("Input too long")
  261. }
  262. //upgrade menu asks you which packages to NOT upgrade so in this case
  263. //include and exclude are kind of swaped
  264. //include, exclude, other := parseNumberMenu(string(numberBuf))
  265. include, exclude, otherInclude, otherExclude := parseNumberMenu(string(numberBuf))
  266. isInclude := len(exclude) == 0 && len(otherExclude) == 0
  267. for i, pkg := range repoUp {
  268. if isInclude && otherInclude.get(pkg.Repository) {
  269. continue
  270. }
  271. if isInclude && !include.get(len(repoUp)-i+len(aurUp)) {
  272. repoNames.set(pkg.Name)
  273. }
  274. if !isInclude && (exclude.get(len(repoUp)-i+len(aurUp)) || otherExclude.get(pkg.Repository)) {
  275. repoNames.set(pkg.Name)
  276. }
  277. }
  278. for i, pkg := range aurUp {
  279. if isInclude && otherInclude.get(pkg.Repository) {
  280. continue
  281. }
  282. if isInclude && !include.get(len(aurUp)-i) {
  283. aurNames.set(pkg.Name)
  284. }
  285. if !isInclude && (exclude.get(len(aurUp)-i) || otherExclude.get(pkg.Repository)) {
  286. aurNames.set(pkg.Name)
  287. }
  288. }
  289. return repoNames, aurNames, err
  290. }