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
1=== modified file 'ubuntu-device-flash/main.go'
2--- ubuntu-device-flash/main.go 2014-03-10 17:33:08 +0000
3+++ ubuntu-device-flash/main.go 2014-03-18 14:56:18 +0000
4@@ -91,8 +91,8 @@
5 log.Fatal(err)
6 }
7 var image ubuntuimage.Image
8- if args.Revision == 0 {
9- image, err = deviceChannel.GetLatestImage()
10+ if args.Revision <= 0 {
11+ image, err = deviceChannel.GetRelativeImage(args.Revision)
12 } else {
13 image, err = deviceChannel.GetImage(args.Revision)
14 }
15
16=== modified file 'ubuntu-emulator/create.go'
17--- ubuntu-emulator/create.go 2014-02-23 03:29:15 +0000
18+++ ubuntu-emulator/create.go 2014-03-18 14:56:18 +0000
19@@ -83,8 +83,8 @@
20 return err
21 }
22 var image ubuntuimage.Image
23- if createCmd.Revision == 0 {
24- image, err = deviceChannel.GetLatestImage()
25+ if createCmd.Revision <= 0 {
26+ image, err = deviceChannel.GetRelativeImage(createCmd.Revision)
27 } else {
28 image, err = deviceChannel.GetImage(createCmd.Revision)
29 }
30
31=== modified file 'ubuntuimage/channels.go'
32--- ubuntuimage/channels.go 2014-03-10 13:39:53 +0000
33+++ ubuntuimage/channels.go 2014-03-18 14:56:18 +0000
34@@ -26,7 +26,10 @@
35 "net/http"
36 )
37
38-const channelsPath = "/channels.json"
39+const (
40+ channelsPath = "/channels.json"
41+ FULL_IMAGE = "full"
42+)
43
44 func NewChannels(server string) (channels Channels, err error) {
45 resp, err := http.Get(server + channelsPath)
46@@ -46,7 +49,7 @@
47 return deviceChannel, fmt.Errorf("Channel %s not found on server %s", channel, server)
48 } else if _, found := channels[channel].Devices[device]; !found {
49 return deviceChannel, fmt.Errorf("Device %s not found on server %s channel %s",
50- device, server, channel)
51+ device, server, channel)
52 }
53 channelUri := server + channels[channel].Devices[device].Index
54 resp, err := http.Get(channelUri)
55@@ -60,28 +63,42 @@
56 return deviceChannel, fmt.Errorf("Cannot parse channel information for device on %s", channelUri)
57 }
58 deviceChannel.Alias = channels[channel].Alias
59+ order := func(i1, i2 *Image) bool {
60+ return i1.Version > i2.Version
61+ }
62+ ImageBy(order).ImageSort(deviceChannel.Images)
63 return deviceChannel, err
64 }
65
66-func (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-
79 func (deviceChannel *DeviceChannel) GetImage(revision int) (image Image, err error) {
80 for _, image := range deviceChannel.Images {
81- if image.Type == "full" && image.Version == revision {
82+ if image.Type == FULL_IMAGE && image.Version == revision {
83 return image, nil
84 }
85 }
86 //If we reached this point, that means we haven't found the image we were looking for.
87 return image, fmt.Errorf("Failed to locate image %d", revision)
88 }
89+
90+func (deviceChannel *DeviceChannel) GetRelativeImage(revision int) (image Image, err error) {
91+ var steps int
92+ if revision < 0 {
93+ revision = -revision
94+ }
95+ for _, image := range deviceChannel.Images {
96+ if image.Type != FULL_IMAGE {
97+ continue
98+ }
99+ if steps == revision {
100+ return image, nil
101+ }
102+ steps++
103+ }
104+ //If we reached this point, that means we haven't found the image we were looking for.
105+ if revision == 0 {
106+ err = errors.New("Failed to locate latest image information")
107+ } else {
108+ err = fmt.Errorf("Failed to locate relative image to latest - %d", revision)
109+ }
110+ return Image{}, err
111+}
112
113=== modified file 'ubuntuimage/channels_test.go'
114--- ubuntuimage/channels_test.go 2014-03-10 13:42:49 +0000
115+++ ubuntuimage/channels_test.go 2014-03-18 14:56:18 +0000
116@@ -295,10 +295,10 @@
117 c.Assert(channelData, NotNil)
118 c.Assert(channelData.Alias, Equals, "touch/trusty")
119 c.Assert(channelData.Images, NotNil)
120- image, err := channelData.GetLatestImage()
121+ image, err := channelData.GetRelativeImage(0)
122 c.Assert(err, IsNil)
123 c.Check(image.Version, Equals, 166)
124- c.Check(image.Type, Equals, "full")
125+ c.Check(image.Type, Equals, FULL_IMAGE)
126 c.Check(len(image.Files), Equals, 3)
127 }
128
129@@ -314,7 +314,7 @@
130 c.Assert(channelData.Alias, Equals, "touch/trusty")
131 c.Assert(channelData.Images, NotNil)
132 expectedErr := errors.New("Failed to locate latest image information")
133- _, err = channelData.GetLatestImage()
134+ _, err = channelData.GetRelativeImage(0)
135 c.Assert(err, DeepEquals, expectedErr)
136 }
137
138@@ -329,8 +329,38 @@
139 image, err := channelData.GetImage(166)
140 c.Assert(err, IsNil)
141 c.Check(image.Version, Equals, 166)
142- c.Check(image.Type, Equals, "full")
143- c.Check(len(image.Files), Equals, 3)
144+ c.Check(image.Type, Equals, FULL_IMAGE)
145+ c.Check(len(image.Files), Equals, 3)
146+}
147+
148+func (s *DeviceChannelsSuite) TestGetSpecificRelativeImageForChannel(c *C) {
149+ device := "mako"
150+ channel := "touch/devel"
151+ channelData, err := s.channels.GetDeviceChannel(s.ts.URL, channel, device)
152+ c.Assert(err, IsNil)
153+ c.Assert(channelData, NotNil)
154+ c.Assert(channelData.Alias, Equals, "touch/trusty")
155+ c.Assert(channelData.Images, NotNil)
156+ image, err := channelData.GetRelativeImage(-1)
157+ c.Assert(err, IsNil)
158+ c.Check(image.Version, Equals, 150)
159+ c.Check(image.Type, Equals, FULL_IMAGE)
160+ c.Check(len(image.Files), Equals, 3)
161+}
162+
163+func (s *DeviceChannelsSuite) TestFailGetRelativeImageForChannel(c *C) {
164+ device := "mako"
165+ channel := "touch/devel"
166+ channelData, err := s.channels.GetDeviceChannel(s.ts.URL, channel, device)
167+ c.Assert(err, IsNil)
168+ c.Assert(channelData, NotNil)
169+ c.Assert(channelData.Alias, Equals, "touch/trusty")
170+ c.Assert(channelData.Images, NotNil)
171+ rev := -5
172+ _, err = channelData.GetRelativeImage(rev)
173+ expectedErr := fmt.Errorf("Failed to locate relative image to latest - %d", -rev)
174+ _, err = channelData.GetRelativeImage(rev)
175+ c.Assert(err, DeepEquals, expectedErr)
176 }
177
178 func (s *DeviceChannelsSuite) TestFailGetSpecificImageForChannel(c *C) {
179
180=== modified file 'ubuntuimage/sort.go'
181--- ubuntuimage/sort.go 2014-01-07 15:19:28 +0000
182+++ ubuntuimage/sort.go 2014-03-18 14:56:18 +0000
183@@ -55,3 +55,36 @@
184 func (s *fileSorter) Less(i, j int) bool {
185 return s.by(&s.files[i], &s.files[j])
186 }
187+
188+// By is the type of a "less" function that defines the ordering of its File arguments.
189+type ImageBy func(p1, p2 *Image) bool
190+
191+// Sort is a method on the function type, By, that sorts the argument slice according to the function.
192+func (by ImageBy) ImageSort(images []Image) {
193+ is := &imageSorter{
194+ images: images,
195+ by: by,
196+ }
197+ sort.Sort(is)
198+}
199+
200+// fileSorter joins a By function and a slice of File to be sorted.
201+type imageSorter struct {
202+ images []Image
203+ by func(p1, p2 *Image) bool // Closure used in the Less method.
204+}
205+
206+// Len is part of sort.Interface.
207+func (s *imageSorter) Len() int {
208+ return len(s.images)
209+}
210+
211+// Swap is part of sort.Interface.
212+func (s *imageSorter) Swap(i, j int) {
213+ s.images[i], s.images[j] = s.images[j], s.images[i]
214+}
215+
216+// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
217+func (s *imageSorter) Less(i, j int) bool {
218+ return s.by(&s.images[i], &s.images[j])
219+}

Subscribers

People subscribed via source and target branches