Merge lp:~xavi-garcia-mena/go-unityscopes/utils-config-scopefiles into lp:go-unityscopes/v2

Proposed by Xavi Garcia
Status: Needs review
Proposed branch: lp:~xavi-garcia-mena/go-unityscopes/utils-config-scopefiles
Merge into: lp:go-unityscopes/v2
Prerequisite: lp:~xavi-garcia-mena/go-unityscopes/utils-series
Diff against target: 407 lines (+382/-0)
5 files modified
utils/goscope-security.json (+7/-0)
utils/goscope.ini (+13/-0)
utils/manifest.json (+16/-0)
utils/scopefiles/scopefiles.go (+140/-0)
utils/scopefiles/scopefiles_test.go (+206/-0)
To merge this branch: bzr merge lp:~xavi-garcia-mena/go-unityscopes/utils-config-scopefiles
Reviewer Review Type Date Requested Status
Unity API Team Pending
Review via email: mp+265834@code.launchpad.net

Commit message

Adding package that holds the functionality relative to scope configuration files in order to crosscompile and build click packages.

Description of the change

Adding package that holds the functionality relative to scope configuration files in order to crosscompile and build click packages.

To post a comment you must log in.

Unmerged revisions

72. By Xavi Garcia

Added scopefiles package to deal with the configuration files and how and where config files are located and verified

71. By Xavi Garcia

Added utilites series information to describe the different configurations we can face when crosscompiling go scopes

70. By Xavi Garcia

Added utils directory with tools for building. Added common test utils package. Added utils command package to run system commands

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'utils/goscope-security.json'
2--- utils/goscope-security.json 1970-01-01 00:00:00 +0000
3+++ utils/goscope-security.json 2015-07-24 14:23:18 +0000
4@@ -0,0 +1,7 @@
5+{
6+ "template": "ubuntu-scope-network",
7+ "policy_groups": [
8+ ],
9+ "policy_version": 1.3
10+}
11+
12
13=== added file 'utils/goscope.ini'
14--- utils/goscope.ini 1970-01-01 00:00:00 +0000
15+++ utils/goscope.ini 2015-07-24 14:23:18 +0000
16@@ -0,0 +1,13 @@
17+[ScopeConfig]
18+ScopeRunner=./%SCOPE_NAME% --runtime %R --scope %S
19+DisplayName=Goscope
20+Description=This is test scope developed in Go
21+Author=Canonical Ltd.
22+Art=
23+Icon=images/goscope-card.png
24+SearchHint=Search Goscope
25+
26+[Appearance]
27+PageHeader.Logo=images/goscope_logo.png
28+PageHeader.Background=color:///#FFFFFF
29+Background=color:///#FFFFFF
30
31=== added file 'utils/manifest.json'
32--- utils/manifest.json 1970-01-01 00:00:00 +0000
33+++ utils/manifest.json 2015-07-24 14:23:18 +0000
34@@ -0,0 +1,16 @@
35+{
36+ "description": "Test Go scope",
37+ "framework": "ubuntu-sdk-15.04",
38+ "architecture": "armhf",
39+ "hooks": {
40+ "goscope": {
41+ "scope": "%SCOPE_NAME%",
42+ "apparmor": "goscope-security.json"
43+ }
44+ },
45+ "icon": "icon",
46+ "maintainer": "Canonical Content Partners <canonical-content-partners@canonical.com>",
47+ "name": "%SCOPE_NAME%",
48+ "title": "Go scope",
49+ "version": "1.0.0"
50+}
51
52=== added directory 'utils/scopefiles'
53=== added file 'utils/scopefiles/scopefiles.go'
54--- utils/scopefiles/scopefiles.go 1970-01-01 00:00:00 +0000
55+++ utils/scopefiles/scopefiles.go 2015-07-24 14:23:18 +0000
56@@ -0,0 +1,140 @@
57+package scopefiles
58+
59+import (
60+ "fmt"
61+ "go/build"
62+ "io/ioutil"
63+ "os"
64+ "path/filepath"
65+ "strings"
66+)
67+
68+// PathExists returns whether the given file or directory exists or not
69+func PathExists(path string) bool {
70+ _, err := os.Stat(path)
71+ if err == nil {
72+ return true
73+ }
74+ if os.IsNotExist(err) {
75+ return false
76+ }
77+ return true
78+}
79+
80+// FindSourceDirectory finds in the system the directory where to find the source files for the series package.
81+func FindSourceDirectory() string {
82+ pack, err := build.Import("launchpad.net/go-unityscopes/v2/utils/series", "", build.FindOnly)
83+ if err != nil {
84+ fmt.Println("ERROR: package launchpad.net/go-unityscopes/v2/utils/series was not installed under GOPATH")
85+ os.Exit(2)
86+ }
87+ return pack.Dir + string(filepath.Separator) + ".." + string(filepath.Separator)
88+}
89+
90+// PrepareFile copies a file from the source path to the destination modifying the word "%SCOPE_NAME% for the given scope name.
91+func PrepareFile(sourcePath, finalPath, scopeName string) error {
92+ buff, err := ioutil.ReadFile(sourcePath)
93+ if err != nil {
94+ return fmt.Errorf("Error opening file: %s, Error is: %s", sourcePath, err.Error())
95+ }
96+
97+ strContent := string(buff)
98+ newContent := strings.Replace(strContent, "%SCOPE_NAME%", scopeName, -1)
99+ f, err := os.Create(finalPath)
100+ if err != nil {
101+ return fmt.Errorf("Error creating file: %s Error is: %s", finalPath, err.Error())
102+ }
103+ f.WriteString(newContent)
104+ f.Close()
105+ return nil
106+}
107+
108+// PrepareScopeFiles creates all the necessary files to build a scope under the current path.
109+// Parameters:
110+// baseDir: Base directory (will take current directory if empty)
111+// scopeName: the scope name
112+// developerName: the developer name
113+//
114+// Returns: nil on success, a non nil error containing the failure information otherwise.
115+func PrepareScopeFiles(baseDir, scopeName, developerName string) error {
116+ auxBaseDir := baseDir + string(filepath.Separator)
117+ if baseDir == "" {
118+ auxBaseDir = "." + string(filepath.Separator)
119+ } else {
120+ if !PathExists(baseDir) {
121+ return fmt.Errorf("Base directory %s does not exist", baseDir)
122+ }
123+ }
124+
125+ scopeDir := scopeName + "." + developerName
126+ fmt.Println("Removing directory [" + scopeDir + "]...")
127+ err := os.RemoveAll(scopeDir)
128+ if err != nil {
129+ return err
130+ }
131+
132+ fmt.Println("Creating directory [" + auxBaseDir + scopeDir + "]...")
133+ err = os.Mkdir(auxBaseDir+scopeDir, 0777)
134+ if err != nil {
135+ return err
136+ }
137+
138+ fmt.Println("Creating directory [" + auxBaseDir + scopeDir + "/" + scopeDir + "]...")
139+ err = os.Mkdir(auxBaseDir+scopeDir+string(filepath.Separator)+scopeDir, 0777)
140+ if err != nil {
141+ return err
142+ }
143+
144+ sourceDir := FindSourceDirectory()
145+ fmt.Println("Preparing manifest file...")
146+ err = PrepareFile(sourceDir+"manifest.json", auxBaseDir+scopeDir+string(filepath.Separator)+"manifest.json", scopeDir)
147+ if err != nil {
148+ return err
149+ }
150+
151+ fmt.Println("Preparing ini file...")
152+ iniFilePath := fmt.Sprintf("%s/%s/%s_%s.ini", auxBaseDir+scopeDir, scopeDir, scopeDir, scopeName)
153+ err = PrepareFile(sourceDir+"goscope.ini", iniFilePath, auxBaseDir+scopeDir)
154+ if err != nil {
155+ return err
156+ }
157+
158+ fmt.Println("Preparing security file...")
159+ securityFilePath := fmt.Sprintf("%s/goscope-security.json", auxBaseDir+scopeDir)
160+ err = PrepareFile(sourceDir+"goscope-security.json", securityFilePath, auxBaseDir+scopeDir)
161+ return err
162+}
163+
164+// CheckBuildDirectory verifies that the build directory have the expected contents.
165+func CheckBuildDirectory(baseDir, scopeName, developerName string) error {
166+ auxBaseDir := baseDir + string(filepath.Separator)
167+ if baseDir == "" {
168+ auxBaseDir = "." + string(filepath.Separator)
169+ } else {
170+ if !PathExists(baseDir) {
171+ return fmt.Errorf("Base directory %s does not exist", baseDir)
172+ }
173+ }
174+ scopeFullName := scopeName + "." + developerName
175+ sep := string(filepath.Separator)
176+ internalDir := auxBaseDir + scopeFullName + sep + scopeFullName
177+ checkDirs := []string{internalDir,
178+ auxBaseDir + scopeFullName,
179+ }
180+ checkFiles := []string{auxBaseDir + scopeFullName + sep + "manifest.json",
181+ auxBaseDir + scopeFullName + sep + "goscope-security.json",
182+ auxBaseDir + scopeFullName + sep + scopeFullName + sep + fmt.Sprintf("%s_%s.ini", scopeFullName, scopeName),
183+ }
184+ for _, path := range checkDirs {
185+ if !PathExists(path) {
186+ return fmt.Errorf("Directory %s does not exist, use the prepare-scope tool to create all needed files", path)
187+ }
188+ }
189+
190+ for _, path := range checkFiles {
191+ if !PathExists(path) {
192+ return fmt.Errorf("File %s does not exist, use the prepare-scope tool to create all needed files", path)
193+ }
194+ }
195+ return nil
196+}
197
198=== added file 'utils/scopefiles/scopefiles_test.go'
199--- utils/scopefiles/scopefiles_test.go 1970-01-01 00:00:00 +0000
200+++ utils/scopefiles/scopefiles_test.go 2015-07-24 14:23:18 +0000
201@@ -0,0 +1,206 @@
202+package scopefiles_test
203+
204+import (
205+ . "gopkg.in/check.v1"
206+ "io/ioutil"
207+ "launchpad.net/go-unityscopes/v2/utils/scopefiles"
208+ "os"
209+ "path/filepath"
210+ "strings"
211+ "testing"
212+)
213+
214+type S struct{}
215+
216+func init() {
217+ Suite(&S{})
218+}
219+
220+func TestAll(t *testing.T) {
221+ TestingT(t)
222+}
223+
224+func (s *S) TestDirExists(c *C) {
225+ c.Check(scopefiles.PathExists("/tmp"), Equals, true)
226+}
227+
228+func (s *S) TestDirNotExists(c *C) {
229+ c.Check(scopefiles.PathExists("/tmpppppppppppppp"), Equals, false)
230+}
231+
232+func (s *S) TestFileExists(c *C) {
233+ // create temp directory
234+ dir, err := ioutil.TempDir("/tmp", "scopefiles_test")
235+ c.Assert(err, IsNil)
236+ c.Assert(dir, Not(Equals), "")
237+
238+ // create temp file
239+ file, err := ioutil.TempFile(dir, "test")
240+ c.Assert(err, IsNil)
241+ c.Assert(file, Not(Equals), nil)
242+ c.Check(scopefiles.PathExists(file.Name()), Equals, true)
243+
244+ err = os.RemoveAll(dir)
245+ c.Assert(err, IsNil)
246+}
247+
248+func (s *S) TestFileNotExists(c *C) {
249+ // create temp directory
250+ dir, err := ioutil.TempDir("/tmp", "scopefiles_test")
251+ c.Assert(err, IsNil)
252+ c.Assert(dir, Not(Equals), "")
253+
254+ c.Check(scopefiles.PathExists(dir+"/the-dir-is-empty"), Equals, false)
255+
256+ err = os.RemoveAll(dir)
257+ c.Assert(err, IsNil)
258+}
259+
260+func (s *S) TestFindSourceDirectoryAndFiles(c *C) {
261+ source := scopefiles.FindSourceDirectory()
262+ c.Check(source, Not(Equals), "")
263+
264+ _, err := os.Stat(source + string(filepath.Separator) + "manifest.json")
265+ c.Assert(err, IsNil)
266+
267+ _, err = os.Stat(source + string(filepath.Separator) + "goscope.ini")
268+ c.Assert(err, IsNil)
269+
270+ _, err = os.Stat(source + string(filepath.Separator) + "goscope-security.json")
271+ c.Assert(err, IsNil)
272+}
273+
274+func (s *S) TestModifyFile(c *C) {
275+ source := scopefiles.FindSourceDirectory()
276+ c.Check(source, Not(Equals), "")
277+
278+ scopefiles.PrepareFile(source+string(filepath.Separator)+"goscope.ini", "./testFile", "%%GO_SCOPE%%")
279+ _, err := os.Stat("./testFile")
280+ c.Assert(err, IsNil)
281+
282+ buff, err := ioutil.ReadFile("./testFile")
283+ c.Assert(err, IsNil)
284+
285+ c.Check(strings.Contains(string(buff), "%SCOPE_NAME%"), Equals, false)
286+ c.Check(strings.Contains(string(buff), "%%GO_SCOPE%%"), Equals, true)
287+
288+ err = os.Remove("./testFile")
289+ c.Assert(err, IsNil)
290+}
291+
292+func (s *S) TestPrepareScopeFilesBadDir(c *C) {
293+ err := scopefiles.PrepareScopeFiles("/tmpppppppppppp", "test", "developer")
294+ c.Assert(err, Not(Equals), nil)
295+ c.Check(err.Error(), Equals, "Base directory /tmpppppppppppp does not exist")
296+}
297+
298+func (s *S) TestPrepareScopeFiles(c *C) {
299+ // create temp directory
300+ dir, err := ioutil.TempDir("/tmp", "scopefiles_test")
301+ c.Assert(err, IsNil)
302+ c.Assert(dir, Not(Equals), "")
303+
304+ scopeDir := "test.developer"
305+ sep := string(filepath.Separator)
306+ c.Check(scopefiles.PathExists(dir+sep+scopeDir), Equals, false)
307+ c.Check(scopefiles.PathExists(dir+sep+scopeDir+sep+scopeDir), Equals, false)
308+
309+ err = scopefiles.PrepareScopeFiles(dir, "test", "developer")
310+ c.Assert(err, IsNil)
311+
312+ c.Check(scopefiles.PathExists(dir+sep+scopeDir), Equals, true)
313+ c.Check(scopefiles.PathExists(dir+sep+scopeDir+sep+scopeDir), Equals, true)
314+ c.Check(scopefiles.PathExists(dir+sep+scopeDir+sep+"manifest.json"), Equals, true)
315+ c.Check(scopefiles.PathExists(dir+sep+scopeDir+sep+"goscope-security.json"), Equals, true)
316+ c.Check(scopefiles.PathExists(dir+sep+scopeDir+sep+scopeDir+sep+"test.developer_test.ini"), Equals, true)
317+
318+ err = os.RemoveAll(dir)
319+ c.Assert(err, IsNil)
320+}
321+
322+func (s *S) TestCheckBuildDirectory(c *C) {
323+ // create temp directory
324+ dir, err := ioutil.TempDir("/tmp", "scopefiles_test")
325+ c.Assert(err, IsNil)
326+ c.Assert(dir, Not(Equals), "")
327+
328+ err = scopefiles.CheckBuildDirectory(dir, "test", "developer")
329+ c.Assert(err, Not(Equals), nil)
330+ c.Check(err.Error(), Equals, "Directory "+dir+"/test.developer/test.developer does not exist, use the prepare-scope tool to create all needed files")
331+
332+ err = scopefiles.PrepareScopeFiles(dir, "test", "developer")
333+ c.Assert(err, IsNil)
334+
335+ err = scopefiles.CheckBuildDirectory(dir, "test", "developer")
336+ c.Check(err, IsNil)
337+
338+ err = os.RemoveAll(dir)
339+ c.Assert(err, IsNil)
340+}
341+
342+func (s *S) TestCheckBuildDirectoryBadDir(c *C) {
343+ // create temp directory
344+ dir, err := ioutil.TempDir("/tmp", "scopefiles_test")
345+ c.Assert(err, IsNil)
346+ c.Assert(dir, Not(Equals), "")
347+
348+ err = scopefiles.PrepareScopeFiles(dir, "test", "developer")
349+ c.Assert(err, IsNil)
350+
351+ err = scopefiles.CheckBuildDirectory("/tmppppppp", "test", "developer")
352+ c.Assert(err, Not(Equals), nil)
353+ c.Check(err.Error(), Equals, "Base directory /tmppppppp does not exist")
354+
355+ err = os.RemoveAll(dir)
356+ c.Assert(err, IsNil)
357+}
358+
359+func (s *S) TestCheckBuildDirectoryEraseManifestFile(c *C) {
360+
361+ sep := string(filepath.Separator)
362+ filePaths := []string{"test.developer" + sep + "manifest.json",
363+ "test.developer" + sep + "goscope-security.json",
364+ "test.developer" + sep + "test.developer" + sep + "test.developer_test.ini",
365+ }
366+
367+ for _, filePath := range filePaths {
368+ // create temp directory
369+ dir, err := ioutil.TempDir("/tmp", "scopefiles_test")
370+ c.Assert(err, IsNil)
371+ c.Assert(dir, Not(Equals), "")
372+
373+ err = scopefiles.PrepareScopeFiles(dir, "test", "developer")
374+ c.Assert(err, IsNil)
375+
376+ err = os.Remove(dir + sep + filePath)
377+ c.Assert(err, IsNil)
378+
379+ err = scopefiles.CheckBuildDirectory(dir, "test", "developer")
380+ c.Check(err, Not(Equals), nil)
381+ c.Check(err.Error(), Equals, "File "+dir+sep+filePath+" does not exist, use the prepare-scope tool to create all needed files")
382+
383+ err = os.RemoveAll(dir)
384+ c.Assert(err, IsNil)
385+ }
386+}
387+
388+func (s *S) TestCheckBuildDirectoryCurrentDirectory(c *C) {
389+ err := scopefiles.CheckBuildDirectory("", "test", "developer")
390+ c.Assert(err, Not(Equals), nil)
391+ c.Check(err.Error(), Equals, "Directory ./test.developer/test.developer does not exist, use the prepare-scope tool to create all needed files")
392+
393+ err = scopefiles.PrepareScopeFiles("", "test", "developer")
394+ c.Assert(err, IsNil)
395+
396+ err = scopefiles.CheckBuildDirectory("", "test", "developer")
397+ c.Check(err, IsNil)
398+
399+ err = os.RemoveAll("test.developer")
400+ c.Assert(err, IsNil)
401+}
402+
403+func (s *S) TestPermissionErrors(c *C) {
404+ err := scopefiles.PrepareScopeFiles("/usr", "test", "developer")
405+ c.Assert(err, Not(Equals), nil)
406+ c.Check(err.Error(), Equals, "mkdir /usr/test.developer: permission denied")
407+}

Subscribers

People subscribed via source and target branches

to all changes: