Merge lp:~sergiusens/goget-ubuntu-touch/relative into lp:goget-ubuntu-touch

Proposed by Sergio Schvezov
Status: Merged
Approved by: Manuel de la Peña
Approved revision: 29
Merged at revision: 26
Proposed branch: lp:~sergiusens/goget-ubuntu-touch/relative
Merge into: lp:goget-ubuntu-touch
Diff against target: 219 lines (+105/-25)
5 files modified
ubuntu-device-flash/main.go (+2/-2)
ubuntu-emulator/create.go (+2/-2)
ubuntuimage/channels.go (+33/-16)
ubuntuimage/channels_test.go (+35/-5)
ubuntuimage/sort.go (+33/-0)
To merge this branch: bzr merge lp:~sergiusens/goget-ubuntu-touch/relative
Reviewer Review Type Date Requested Status
Manuel de la Peña (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+211523@code.launchpad.net

Commit message

Adding relative image support

Description of the change

This allows flashing with --revision <= 0 for relative versions the the latest one.

I could filter by full when I order them and make the code look simpler; but I want to leave the possibility open for delta images.

To post a comment you must log in.
27. By Sergio Schvezov

Adding option to emulator

28. By Sergio Schvezov

Adding error test

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
29. By Sergio Schvezov

Making "full" a constant

Revision history for this message
Manuel de la Peña (mandel) wrote :

Loosk good, thx for adding the constants.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ubuntu-device-flash/main.go'
--- ubuntu-device-flash/main.go 2014-03-10 17:33:08 +0000
+++ ubuntu-device-flash/main.go 2014-03-18 14:56:18 +0000
@@ -91,8 +91,8 @@
91 log.Fatal(err)91 log.Fatal(err)
92 }92 }
93 var image ubuntuimage.Image93 var image ubuntuimage.Image
94 if args.Revision == 0 {94 if args.Revision <= 0 {
95 image, err = deviceChannel.GetLatestImage()95 image, err = deviceChannel.GetRelativeImage(args.Revision)
96 } else {96 } else {
97 image, err = deviceChannel.GetImage(args.Revision)97 image, err = deviceChannel.GetImage(args.Revision)
98 }98 }
9999
=== modified file 'ubuntu-emulator/create.go'
--- ubuntu-emulator/create.go 2014-02-23 03:29:15 +0000
+++ ubuntu-emulator/create.go 2014-03-18 14:56:18 +0000
@@ -83,8 +83,8 @@
83 return err83 return err
84 }84 }
85 var image ubuntuimage.Image85 var image ubuntuimage.Image
86 if createCmd.Revision == 0 {86 if createCmd.Revision <= 0 {
87 image, err = deviceChannel.GetLatestImage()87 image, err = deviceChannel.GetRelativeImage(createCmd.Revision)
88 } else {88 } else {
89 image, err = deviceChannel.GetImage(createCmd.Revision)89 image, err = deviceChannel.GetImage(createCmd.Revision)
90 }90 }
9191
=== modified file 'ubuntuimage/channels.go'
--- ubuntuimage/channels.go 2014-03-10 13:39:53 +0000
+++ ubuntuimage/channels.go 2014-03-18 14:56:18 +0000
@@ -26,7 +26,10 @@
26 "net/http"26 "net/http"
27)27)
2828
29const channelsPath = "/channels.json"29const (
30 channelsPath = "/channels.json"
31 FULL_IMAGE = "full"
32)
3033
31func NewChannels(server string) (channels Channels, err error) {34func NewChannels(server string) (channels Channels, err error) {
32 resp, err := http.Get(server + channelsPath)35 resp, err := http.Get(server + channelsPath)
@@ -46,7 +49,7 @@
46 return deviceChannel, fmt.Errorf("Channel %s not found on server %s", channel, server)49 return deviceChannel, fmt.Errorf("Channel %s not found on server %s", channel, server)
47 } else if _, found := channels[channel].Devices[device]; !found {50 } else if _, found := channels[channel].Devices[device]; !found {
48 return deviceChannel, fmt.Errorf("Device %s not found on server %s channel %s",51 return deviceChannel, fmt.Errorf("Device %s not found on server %s channel %s",
49 device, server, channel)52 device, server, channel)
50 }53 }
51 channelUri := server + channels[channel].Devices[device].Index54 channelUri := server + channels[channel].Devices[device].Index
52 resp, err := http.Get(channelUri)55 resp, err := http.Get(channelUri)
@@ -60,28 +63,42 @@
60 return deviceChannel, fmt.Errorf("Cannot parse channel information for device on %s", channelUri)63 return deviceChannel, fmt.Errorf("Cannot parse channel information for device on %s", channelUri)
61 }64 }
62 deviceChannel.Alias = channels[channel].Alias65 deviceChannel.Alias = channels[channel].Alias
66 order := func(i1, i2 *Image) bool {
67 return i1.Version > i2.Version
68 }
69 ImageBy(order).ImageSort(deviceChannel.Images)
63 return deviceChannel, err70 return deviceChannel, err
64}71}
6572
66func (deviceChannel *DeviceChannel) GetLatestImage() (image Image, err error) {
67 var latestImage Image
68 for _, image := range deviceChannel.Images {
69 if image.Type == "full" && image.Version > latestImage.Version {
70 latestImage = image
71 }
72 }
73 if latestImage.Version == 0 {
74 err = errors.New("Failed to locate latest image information")
75 }
76 return latestImage, err
77}
78
79func (deviceChannel *DeviceChannel) GetImage(revision int) (image Image, err error) {73func (deviceChannel *DeviceChannel) GetImage(revision int) (image Image, err error) {
80 for _, image := range deviceChannel.Images {74 for _, image := range deviceChannel.Images {
81 if image.Type == "full" && image.Version == revision {75 if image.Type == FULL_IMAGE && image.Version == revision {
82 return image, nil76 return image, nil
83 }77 }
84 }78 }
85 //If we reached this point, that means we haven't found the image we were looking for.79 //If we reached this point, that means we haven't found the image we were looking for.
86 return image, fmt.Errorf("Failed to locate image %d", revision)80 return image, fmt.Errorf("Failed to locate image %d", revision)
87}81}
82
83func (deviceChannel *DeviceChannel) GetRelativeImage(revision int) (image Image, err error) {
84 var steps int
85 if revision < 0 {
86 revision = -revision
87 }
88 for _, image := range deviceChannel.Images {
89 if image.Type != FULL_IMAGE {
90 continue
91 }
92 if steps == revision {
93 return image, nil
94 }
95 steps++
96 }
97 //If we reached this point, that means we haven't found the image we were looking for.
98 if revision == 0 {
99 err = errors.New("Failed to locate latest image information")
100 } else {
101 err = fmt.Errorf("Failed to locate relative image to latest - %d", revision)
102 }
103 return Image{}, err
104}
88105
=== modified file 'ubuntuimage/channels_test.go'
--- ubuntuimage/channels_test.go 2014-03-10 13:42:49 +0000
+++ ubuntuimage/channels_test.go 2014-03-18 14:56:18 +0000
@@ -295,10 +295,10 @@
295 c.Assert(channelData, NotNil)295 c.Assert(channelData, NotNil)
296 c.Assert(channelData.Alias, Equals, "touch/trusty")296 c.Assert(channelData.Alias, Equals, "touch/trusty")
297 c.Assert(channelData.Images, NotNil)297 c.Assert(channelData.Images, NotNil)
298 image, err := channelData.GetLatestImage()298 image, err := channelData.GetRelativeImage(0)
299 c.Assert(err, IsNil)299 c.Assert(err, IsNil)
300 c.Check(image.Version, Equals, 166)300 c.Check(image.Version, Equals, 166)
301 c.Check(image.Type, Equals, "full")301 c.Check(image.Type, Equals, FULL_IMAGE)
302 c.Check(len(image.Files), Equals, 3)302 c.Check(len(image.Files), Equals, 3)
303}303}
304304
@@ -314,7 +314,7 @@
314 c.Assert(channelData.Alias, Equals, "touch/trusty")314 c.Assert(channelData.Alias, Equals, "touch/trusty")
315 c.Assert(channelData.Images, NotNil)315 c.Assert(channelData.Images, NotNil)
316 expectedErr := errors.New("Failed to locate latest image information")316 expectedErr := errors.New("Failed to locate latest image information")
317 _, err = channelData.GetLatestImage()317 _, err = channelData.GetRelativeImage(0)
318 c.Assert(err, DeepEquals, expectedErr)318 c.Assert(err, DeepEquals, expectedErr)
319}319}
320320
@@ -329,8 +329,38 @@
329 image, err := channelData.GetImage(166)329 image, err := channelData.GetImage(166)
330 c.Assert(err, IsNil)330 c.Assert(err, IsNil)
331 c.Check(image.Version, Equals, 166)331 c.Check(image.Version, Equals, 166)
332 c.Check(image.Type, Equals, "full")332 c.Check(image.Type, Equals, FULL_IMAGE)
333 c.Check(len(image.Files), Equals, 3)333 c.Check(len(image.Files), Equals, 3)
334}
335
336func (s *DeviceChannelsSuite) TestGetSpecificRelativeImageForChannel(c *C) {
337 device := "mako"
338 channel := "touch/devel"
339 channelData, err := s.channels.GetDeviceChannel(s.ts.URL, channel, device)
340 c.Assert(err, IsNil)
341 c.Assert(channelData, NotNil)
342 c.Assert(channelData.Alias, Equals, "touch/trusty")
343 c.Assert(channelData.Images, NotNil)
344 image, err := channelData.GetRelativeImage(-1)
345 c.Assert(err, IsNil)
346 c.Check(image.Version, Equals, 150)
347 c.Check(image.Type, Equals, FULL_IMAGE)
348 c.Check(len(image.Files), Equals, 3)
349}
350
351func (s *DeviceChannelsSuite) TestFailGetRelativeImageForChannel(c *C) {
352 device := "mako"
353 channel := "touch/devel"
354 channelData, err := s.channels.GetDeviceChannel(s.ts.URL, channel, device)
355 c.Assert(err, IsNil)
356 c.Assert(channelData, NotNil)
357 c.Assert(channelData.Alias, Equals, "touch/trusty")
358 c.Assert(channelData.Images, NotNil)
359 rev := -5
360 _, err = channelData.GetRelativeImage(rev)
361 expectedErr := fmt.Errorf("Failed to locate relative image to latest - %d", -rev)
362 _, err = channelData.GetRelativeImage(rev)
363 c.Assert(err, DeepEquals, expectedErr)
334}364}
335365
336func (s *DeviceChannelsSuite) TestFailGetSpecificImageForChannel(c *C) {366func (s *DeviceChannelsSuite) TestFailGetSpecificImageForChannel(c *C) {
337367
=== modified file 'ubuntuimage/sort.go'
--- ubuntuimage/sort.go 2014-01-07 15:19:28 +0000
+++ ubuntuimage/sort.go 2014-03-18 14:56:18 +0000
@@ -55,3 +55,36 @@
55func (s *fileSorter) Less(i, j int) bool {55func (s *fileSorter) Less(i, j int) bool {
56 return s.by(&s.files[i], &s.files[j])56 return s.by(&s.files[i], &s.files[j])
57}57}
58
59// By is the type of a "less" function that defines the ordering of its File arguments.
60type ImageBy func(p1, p2 *Image) bool
61
62// Sort is a method on the function type, By, that sorts the argument slice according to the function.
63func (by ImageBy) ImageSort(images []Image) {
64 is := &imageSorter{
65 images: images,
66 by: by,
67 }
68 sort.Sort(is)
69}
70
71// fileSorter joins a By function and a slice of File to be sorted.
72type imageSorter struct {
73 images []Image
74 by func(p1, p2 *Image) bool // Closure used in the Less method.
75}
76
77// Len is part of sort.Interface.
78func (s *imageSorter) Len() int {
79 return len(s.images)
80}
81
82// Swap is part of sort.Interface.
83func (s *imageSorter) Swap(i, j int) {
84 s.images[i], s.images[j] = s.images[j], s.images[i]
85}
86
87// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
88func (s *imageSorter) Less(i, j int) bool {
89 return s.by(&s.images[i], &s.images[j])
90}

Subscribers

People subscribed via source and target branches