upgrade.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. // Print prints the details of the packages to upgrade.
  65. func (u upSlice) Print(start int) {
  66. for k, i := range u {
  67. left, right := getVersionDiff(i.LocalVersion, i.RemoteVersion)
  68. fmt.Print(magenta(fmt.Sprintf("%2d ", len(u)+start-k-1)))
  69. fmt.Print(bold(colourHash(i.Repository)), "/", cyan(i.Name))
  70. w := 70 - len(i.Repository) - len(i.Name) + len(left)
  71. fmt.Printf(fmt.Sprintf("%%%ds", w),
  72. fmt.Sprintf("%s -> %s\n", left, right))
  73. }
  74. }
  75. // upList returns lists of packages to upgrade from each source.
  76. func upList(dt *depTree) (aurUp upSlice, repoUp upSlice, err error) {
  77. local, remote, _, remoteNames, err := filterPackages()
  78. if err != nil {
  79. return
  80. }
  81. repoC := make(chan upSlice)
  82. aurC := make(chan upSlice)
  83. errC := make(chan error)
  84. fmt.Println(bold(cyan("::") + " Searching databases for updates..."))
  85. go func() {
  86. repoUpList, err := upRepo(local)
  87. errC <- err
  88. repoC <- repoUpList
  89. }()
  90. fmt.Println(bold(cyan("::") + " Searching AUR for updates..."))
  91. go func() {
  92. aurUpList, err := upAUR(remote, remoteNames, dt)
  93. errC <- err
  94. aurC <- aurUpList
  95. }()
  96. var i = 0
  97. loop:
  98. for {
  99. select {
  100. case repoUp = <-repoC:
  101. i++
  102. case aurUp = <-aurC:
  103. i++
  104. case err := <-errC:
  105. if err != nil {
  106. fmt.Println(err)
  107. }
  108. default:
  109. if i == 2 {
  110. close(repoC)
  111. close(aurC)
  112. close(errC)
  113. break loop
  114. }
  115. }
  116. }
  117. return
  118. }
  119. func upDevel(remote []alpm.Package, packageC chan upgrade, done chan bool) {
  120. for vcsName, e := range savedInfo {
  121. if e.needsUpdate() {
  122. found := false
  123. var pkg alpm.Package
  124. for _, r := range remote {
  125. if r.Name() == vcsName {
  126. found = true
  127. pkg = r
  128. }
  129. }
  130. if found {
  131. if pkg.ShouldIgnore() {
  132. fmt.Print(magenta("Warning: "))
  133. fmt.Printf("%s ignoring package upgrade (%s => %s)\n", cyan(pkg.Name()), pkg.Version(), "git")
  134. } else {
  135. packageC <- upgrade{pkg.Name(), "devel", pkg.Version(), "latest-commit"}
  136. }
  137. } else {
  138. removeVCSPackage([]string{vcsName})
  139. }
  140. }
  141. }
  142. done <- true
  143. }
  144. // upAUR gathers foreign packages and checks if they have new versions.
  145. // Output: Upgrade type package list.
  146. func upAUR(remote []alpm.Package, remoteNames []string, dt *depTree) (toUpgrade upSlice, err error) {
  147. var routines int
  148. var routineDone int
  149. packageC := make(chan upgrade)
  150. done := make(chan bool)
  151. if config.Devel {
  152. routines++
  153. go upDevel(remote, packageC, done)
  154. fmt.Println(bold(cyan("::") + " Checking development packages..."))
  155. }
  156. routines++
  157. go func(remote []alpm.Package, remoteNames []string, dt *depTree) {
  158. for _, pkg := range remote {
  159. aurPkg, ok := dt.Aur[pkg.Name()]
  160. if !ok {
  161. continue
  162. }
  163. if (config.TimeUpdate && (int64(aurPkg.LastModified) > pkg.BuildDate().Unix())) ||
  164. (alpm.VerCmp(pkg.Version(), aurPkg.Version) < 0) {
  165. if pkg.ShouldIgnore() {
  166. left, right := getVersionDiff(pkg.Version(), aurPkg.Version)
  167. fmt.Print(magenta("Warning: "))
  168. fmt.Printf("%s ignoring package upgrade (%s => %s)\n", cyan(pkg.Name()), left, right)
  169. } else {
  170. packageC <- upgrade{aurPkg.Name, "aur", pkg.Version(), aurPkg.Version}
  171. }
  172. }
  173. }
  174. done <- true
  175. }(remote, remoteNames, dt)
  176. if routineDone == routines {
  177. err = nil
  178. return
  179. }
  180. for {
  181. select {
  182. case pkg := <-packageC:
  183. for _, w := range toUpgrade {
  184. if w.Name == pkg.Name {
  185. continue
  186. }
  187. }
  188. toUpgrade = append(toUpgrade, pkg)
  189. case <-done:
  190. routineDone++
  191. if routineDone == routines {
  192. err = nil
  193. return
  194. }
  195. }
  196. }
  197. }
  198. // upRepo gathers local packages and checks if they have new versions.
  199. // Output: Upgrade type package list.
  200. func upRepo(local []alpm.Package) (upSlice, error) {
  201. dbList, err := alpmHandle.SyncDbs()
  202. if err != nil {
  203. return nil, err
  204. }
  205. slice := upSlice{}
  206. for _, pkg := range local {
  207. newPkg := pkg.NewVersion(dbList)
  208. if newPkg != nil {
  209. if pkg.ShouldIgnore() {
  210. fmt.Print(magenta("Warning: "))
  211. fmt.Printf("%s ignoring package upgrade (%s => %s)\n", pkg.Name(), pkg.Version(), newPkg.Version())
  212. } else {
  213. slice = append(slice, upgrade{pkg.Name(), newPkg.DB().Name(), pkg.Version(), newPkg.Version()})
  214. }
  215. }
  216. }
  217. return slice, nil
  218. }
  219. //Contains returns whether e is present in s
  220. func containsInt(s []int, e int) bool {
  221. for _, a := range s {
  222. if a == e {
  223. return true
  224. }
  225. }
  226. return false
  227. }
  228. // RemoveIntListFromList removes all src's elements that are present in target
  229. func removeIntListFromList(src, target []int) []int {
  230. max := len(target)
  231. for i := 0; i < max; i++ {
  232. if containsInt(src, target[i]) {
  233. target = append(target[:i], target[i+1:]...)
  234. max--
  235. i--
  236. }
  237. }
  238. return target
  239. }
  240. // upgradePkgs handles updating the cache and installing updates.
  241. func upgradePkgs(dt *depTree) (stringSet, stringSet, error) {
  242. repoNames := make(stringSet)
  243. aurNames := make(stringSet)
  244. aurUp, repoUp, err := upList(dt)
  245. if err != nil {
  246. return repoNames, aurNames, err
  247. } else if len(aurUp)+len(repoUp) == 0 {
  248. return repoNames, aurNames, err
  249. }
  250. sort.Sort(repoUp)
  251. fmt.Println(bold(blue("::")), len(aurUp)+len(repoUp), bold("Packages to upgrade."))
  252. repoUp.Print(len(aurUp) + 1)
  253. aurUp.Print(1)
  254. if config.NoConfirm {
  255. for _, up := range repoUp {
  256. repoNames.set(up.Name)
  257. }
  258. for _, up := range aurUp {
  259. aurNames.set(up.Name)
  260. }
  261. return repoNames, aurNames, nil
  262. }
  263. fmt.Println(bold(green(arrow + " Packages to not upgrade (eg: 1 2 3, 1-3, ^4 or repo name)")))
  264. fmt.Print(bold(green(arrow + " ")))
  265. reader := bufio.NewReader(os.Stdin)
  266. numberBuf, overflow, err := reader.ReadLine()
  267. if err != nil {
  268. return nil, nil, err
  269. }
  270. if overflow {
  271. return nil, nil, fmt.Errorf("Input too long")
  272. }
  273. //upgrade menu asks you which packages to NOT upgrade so in this case
  274. //include and exclude are kind of swaped
  275. //include, exclude, other := parseNumberMenu(string(numberBuf))
  276. include, exclude, otherInclude, otherExclude := parseNumberMenu(string(numberBuf))
  277. isInclude := len(exclude) == 0 && len(otherExclude) == 0
  278. for i, pkg := range repoUp {
  279. if isInclude && otherInclude.get(pkg.Repository) {
  280. continue
  281. }
  282. if isInclude && !include.get(len(repoUp)-i+len(aurUp)) {
  283. repoNames.set(pkg.Name)
  284. }
  285. if !isInclude && (exclude.get(len(repoUp)-i+len(aurUp)) || otherExclude.get(pkg.Repository)) {
  286. repoNames.set(pkg.Name)
  287. }
  288. }
  289. for i, pkg := range aurUp {
  290. if isInclude && otherInclude.get(pkg.Repository) {
  291. continue
  292. }
  293. if isInclude && !include.get(len(aurUp)-i) {
  294. aurNames.set(pkg.Name)
  295. }
  296. if !isInclude && (exclude.get(len(aurUp)-i) || otherExclude.get(pkg.Repository)) {
  297. aurNames.set(pkg.Name)
  298. }
  299. }
  300. return repoNames, aurNames, err
  301. }