upgrade.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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, errOld := pkgb.NewCompleteVersion(i.LocalVersion)
  51. new, errNew := pkgb.NewCompleteVersion(i.RemoteVersion)
  52. var left, right string
  53. f := func(name string) (output string) {
  54. if alpmConf.Options&alpm.ConfColor == 0 {
  55. return name
  56. }
  57. var hash = 5381
  58. for i := 0; i < len(name); i++ {
  59. hash = int(name[i]) + ((hash << 5) + (hash))
  60. }
  61. return fmt.Sprintf("\x1b[1;%dm%s\x1b[0m", hash%6+31, name)
  62. }
  63. fmt.Print(yellowFg(fmt.Sprintf("%2d ", len(u)+start-k-1)))
  64. fmt.Print(f(i.Repository), "/", boldWhiteFg(i.Name))
  65. if errOld != nil {
  66. left = redFg("Invalid Version")
  67. } else {
  68. if old.Version == new.Version {
  69. left = string(old.Version) + "-" + redFg(string(old.Pkgrel))
  70. } else {
  71. left = redFg(string(old.Version)) + "-" + string(old.Pkgrel)
  72. }
  73. }
  74. if errNew != nil {
  75. right = redFg("Invalid Version")
  76. } else {
  77. if old.Version == new.Version {
  78. right = string(new.Version) + "-" + greenFg(string(new.Pkgrel))
  79. } else {
  80. right = boldGreenFg(string(new.Version)) + "-" + string(new.Pkgrel)
  81. }
  82. }
  83. w := 70 - len(i.Repository) - len(i.Name) + len(left)
  84. fmt.Printf(fmt.Sprintf("%%%ds", w),
  85. fmt.Sprintf("%s -> %s\n", left, right))
  86. }
  87. }
  88. // upList returns lists of packages to upgrade from each source.
  89. func upList() (aurUp upSlice, repoUp upSlice, err error) {
  90. local, remote, _, remoteNames, err := filterPackages()
  91. if err != nil {
  92. return
  93. }
  94. repoC := make(chan upSlice)
  95. aurC := make(chan upSlice)
  96. errC := make(chan error)
  97. fmt.Println(boldCyanFg("::"), boldFg("Searching databases for updates..."))
  98. go func() {
  99. repoUpList, err := upRepo(local)
  100. errC <- err
  101. repoC <- repoUpList
  102. }()
  103. fmt.Println(boldCyanFg("::"), boldFg("Searching AUR for updates..."))
  104. go func() {
  105. aurUpList, err := upAUR(remote, remoteNames)
  106. errC <- err
  107. aurC <- aurUpList
  108. }()
  109. var i = 0
  110. loop:
  111. for {
  112. select {
  113. case repoUp = <-repoC:
  114. i++
  115. case aurUp = <-aurC:
  116. i++
  117. case err := <-errC:
  118. if err != nil {
  119. fmt.Println(err)
  120. }
  121. default:
  122. if i == 2 {
  123. close(repoC)
  124. close(aurC)
  125. close(errC)
  126. break loop
  127. }
  128. }
  129. }
  130. return
  131. }
  132. func upDevel(remote []alpm.Package, packageC chan upgrade, done chan bool) {
  133. for _, e := range savedInfo {
  134. if e.needsUpdate() {
  135. found := false
  136. var pkg alpm.Package
  137. for _, r := range remote {
  138. if r.Name() == e.Package {
  139. found = true
  140. pkg = r
  141. }
  142. }
  143. if found {
  144. if pkg.ShouldIgnore() {
  145. fmt.Print(yellowFg("Warning: "))
  146. fmt.Printf("%s ignoring package upgrade (%s => %s)\n", pkg.Name(), pkg.Version(), "git")
  147. } else {
  148. packageC <- upgrade{e.Package, "devel", e.SHA[0:6], "git"}
  149. }
  150. } else {
  151. removeVCSPackage([]string{e.Package})
  152. }
  153. }
  154. }
  155. done <- true
  156. }
  157. // upAUR gathers foreign packages and checks if they have new versions.
  158. // Output: Upgrade type package list.
  159. func upAUR(remote []alpm.Package, remoteNames []string) (toUpgrade upSlice, err error) {
  160. var j int
  161. var routines int
  162. var routineDone int
  163. packageC := make(chan upgrade)
  164. done := make(chan bool)
  165. if config.Devel {
  166. routines++
  167. go upDevel(remote, packageC, done)
  168. fmt.Println(boldCyanFg("::"), boldFg("Checking development packages..."))
  169. }
  170. for i := len(remote); i != 0; i = j {
  171. //Split requests so AUR RPC doesn't get mad at us.
  172. j = i - config.RequestSplitN
  173. if j < 0 {
  174. j = 0
  175. }
  176. routines++
  177. go func(local []alpm.Package, remote []string) {
  178. qtemp, err := rpc.Info(remote)
  179. if err != nil {
  180. fmt.Println(err)
  181. done <- true
  182. return
  183. }
  184. // For each item in query: Search equivalent in foreign.
  185. // We assume they're ordered and are returned ordered
  186. // and will only be missing if they don't exist in AUR.
  187. max := len(qtemp) - 1
  188. var missing, x int
  189. for i := range local {
  190. x = i - missing
  191. if x > max {
  192. break
  193. } else if qtemp[x].Name == local[i].Name() {
  194. if (config.TimeUpdate && (int64(qtemp[x].LastModified) > local[i].BuildDate().Unix())) ||
  195. (alpm.VerCmp(local[i].Version(), qtemp[x].Version) < 0) {
  196. if local[i].ShouldIgnore() {
  197. fmt.Print(yellowFg("Warning: "))
  198. fmt.Printf("%s ignoring package upgrade (%s => %s)\n", local[i].Name(), local[i].Version(), qtemp[x].Version)
  199. } else {
  200. packageC <- upgrade{qtemp[x].Name, "aur", local[i].Version(), qtemp[x].Version}
  201. }
  202. }
  203. continue
  204. } else {
  205. missing++
  206. }
  207. }
  208. done <- true
  209. }(remote[j:i], remoteNames[j:i])
  210. }
  211. for {
  212. select {
  213. case pkg := <-packageC:
  214. for _, w := range toUpgrade {
  215. if w.Name == pkg.Name {
  216. continue
  217. }
  218. }
  219. toUpgrade = append(toUpgrade, pkg)
  220. case <-done:
  221. routineDone++
  222. if routineDone == routines {
  223. err = nil
  224. return
  225. }
  226. }
  227. }
  228. }
  229. // upRepo gathers local packages and checks if they have new versions.
  230. // Output: Upgrade type package list.
  231. func upRepo(local []alpm.Package) (upSlice, error) {
  232. dbList, err := alpmHandle.SyncDbs()
  233. if err != nil {
  234. return nil, err
  235. }
  236. slice := upSlice{}
  237. for _, pkg := range local {
  238. newPkg := pkg.NewVersion(dbList)
  239. if newPkg != nil {
  240. if pkg.ShouldIgnore() {
  241. fmt.Print(yellowFg("Warning: "))
  242. fmt.Printf("%s ignoring package upgrade (%s => %s)\n", pkg.Name(), pkg.Version(), newPkg.Version())
  243. } else {
  244. slice = append(slice, upgrade{pkg.Name(), newPkg.DB().Name(), pkg.Version(), newPkg.Version()})
  245. }
  246. }
  247. }
  248. return slice, nil
  249. }
  250. //Contains returns whether e is present in s
  251. func containsInt(s []int, e int) bool {
  252. for _, a := range s {
  253. if a == e {
  254. return true
  255. }
  256. }
  257. return false
  258. }
  259. // RemoveIntListFromList removes all src's elements that are present in target
  260. func removeIntListFromList(src, target []int) []int {
  261. max := len(target)
  262. for i := 0; i < max; i++ {
  263. if containsInt(src, target[i]) {
  264. target = append(target[:i], target[i+1:]...)
  265. max--
  266. i--
  267. }
  268. }
  269. return target
  270. }
  271. // upgradePkgs handles updating the cache and installing updates.
  272. func upgradePkgs(flags []string) error {
  273. aurUp, repoUp, err := upList()
  274. if err != nil {
  275. return err
  276. } else if len(aurUp)+len(repoUp) == 0 {
  277. fmt.Println("\nThere is nothing to do")
  278. return err
  279. }
  280. var repoNums []int
  281. var aurNums []int
  282. sort.Sort(repoUp)
  283. fmt.Println(boldBlueFg("::"), len(aurUp)+len(repoUp), boldWhiteFg("Packages to upgrade."))
  284. repoUp.Print(len(aurUp) + 1)
  285. aurUp.Print(1)
  286. if !config.NoConfirm {
  287. fmt.Println(greenFg("Enter packages you don't want to upgrade."))
  288. fmt.Print("Numbers: ")
  289. reader := bufio.NewReader(os.Stdin)
  290. numberBuf, overflow, err := reader.ReadLine()
  291. if err != nil || overflow {
  292. fmt.Println(err)
  293. return err
  294. }
  295. result := strings.Fields(string(numberBuf))
  296. excludeAur := make([]int, 0)
  297. excludeRepo := make([]int, 0)
  298. for _, numS := range result {
  299. negate := numS[0] == '^'
  300. if negate {
  301. numS = numS[1:]
  302. }
  303. var numbers []int
  304. num, err := strconv.Atoi(numS)
  305. if err != nil {
  306. numbers, err = BuildRange(numS)
  307. if err != nil {
  308. continue
  309. }
  310. } else {
  311. numbers = []int{num}
  312. }
  313. for _, target := range numbers {
  314. if target > len(aurUp)+len(repoUp) || target <= 0 {
  315. continue
  316. } else if target <= len(aurUp) {
  317. target = len(aurUp) - target
  318. if negate {
  319. excludeAur = append(excludeAur, target)
  320. } else {
  321. aurNums = append(aurNums, target)
  322. }
  323. } else {
  324. target = len(aurUp) + len(repoUp) - target
  325. if negate {
  326. excludeRepo = append(excludeRepo, target)
  327. } else {
  328. repoNums = append(repoNums, target)
  329. }
  330. }
  331. }
  332. }
  333. if len(repoNums) == 0 && len(aurNums) == 0 &&
  334. (len(excludeRepo) > 0 || len(excludeAur) > 0) {
  335. if len(repoUp) > 0 {
  336. repoNums = BuildIntRange(0, len(repoUp)-1)
  337. }
  338. if len(aurUp) > 0 {
  339. aurNums = BuildIntRange(0, len(aurUp)-1)
  340. }
  341. }
  342. aurNums = removeIntListFromList(excludeAur, aurNums)
  343. repoNums = removeIntListFromList(excludeRepo, repoNums)
  344. }
  345. arguments := cmdArgs.copy()
  346. arguments.delArg("u", "sysupgrade")
  347. arguments.delArg("y", "refresh")
  348. var repoNames []string
  349. var aurNames []string
  350. if len(repoUp) != 0 {
  351. repoloop:
  352. for i, k := range repoUp {
  353. for _, j := range repoNums {
  354. if j == i {
  355. continue repoloop
  356. }
  357. }
  358. repoNames = append(repoNames, k.Name)
  359. }
  360. }
  361. if len(aurUp) != 0 {
  362. aurloop:
  363. for i, k := range aurUp {
  364. for _, j := range aurNums {
  365. if j == i {
  366. continue aurloop
  367. }
  368. }
  369. aurNames = append(aurNames, k.Name)
  370. }
  371. }
  372. arguments.addTarget(repoNames...)
  373. arguments.addTarget(aurNames...)
  374. err = install(arguments)
  375. return err
  376. }