Merge lp:~chipaca/snappy/dynamicExclusion into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by John Lenton on 2015-07-29
Status: Merged
Approved by: Sergio Schvezov on 2015-07-29
Approved revision: 618
Merged at revision: 619
Proposed branch: lp:~chipaca/snappy/dynamicExclusion
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Prerequisite: lp:~chipaca/snappy/delayed-service-start
Diff against target: 116 lines (+77/-2)
2 files modified
snappy/build.go (+55/-2)
snappy/build_test.go (+22/-0)
To merge this branch: bzr merge lp:~chipaca/snappy/dynamicExclusion
Reviewer Review Type Date Requested Status
Sergio Schvezov 2015-07-29 Approve on 2015-07-29
Review via email: mp+266200@code.launchpad.net

This proposal supersedes a proposal from 2015-07-29.

Commit Message

Introducing .snapignore, which is made of lines of regexps and not globs.

To post a comment you must log in.
Zygmunt Krynicki (zyga) wrote :

11:58 < zyga> Chipaca: one more suggestiong
11:58 < zyga> *on
11:58 < zyga> Chipaca: perhaps remove the built-in list and create the .snapingore file with that list if missing
11:58 < zyga> Chipaca: it's better than having two, one secred that nobody knows of, and one in the file
11:59 < Chipaca> hmm
11:59 < Chipaca> i see your point
12:00 < Chipaca> can you make that comment on the merge?
12:00 < Chipaca> i think it needs sergio or mvo to comment
12:00 < zyga> sure

Sergio Schvezov (sergiusens) wrote :

looks good, just a couple of comments

review: Needs Fixing
John Lenton (chipaca) :
Sergio Schvezov (sergiusens) wrote :

> 11:58 < zyga> Chipaca: one more suggestiong
> 11:58 < zyga> *on
> 11:58 < zyga> Chipaca: perhaps remove the built-in list and create the
> .snapingore file with that list if missing
> 11:58 < zyga> Chipaca: it's better than having two, one secred that nobody
> knows of, and one in the file
> 11:59 < Chipaca> hmm
> 11:59 < Chipaca> i see your point
> 12:00 < Chipaca> can you make that comment on the merge?
> 12:00 < Chipaca> i think it needs sergio or mvo to comment
> 12:00 < zyga> sure

Only if explicit, maybe something like

snappy build --skeleton (which doesn't really assemble the package) but inserts a:
.snapignore # base
meta/.. # template

lp:~chipaca/snappy/dynamicExclusion updated on 2015-07-29
618. By John Lenton on 2015-07-29

addressed issues found in review

John Lenton (chipaca) wrote :

ok, in a different branch then.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snappy/build.go'
2--- snappy/build.go 2015-07-23 11:05:37 +0000
3+++ snappy/build.go 2015-07-29 13:13:40 +0000
4@@ -51,7 +51,7 @@
5 //
6 // Please resist the temptation of optimizing the regexp by grouping
7 // things by hand. People will find it unreadable enough as it is.
8-var shouldExclude = regexp.MustCompile(strings.Join([]string{
9+var shouldExcludeDefault = regexp.MustCompile(strings.Join([]string{
10 `\.snap$`, // added
11 `\.click$`,
12 `^\..*\.sw.$`,
13@@ -82,8 +82,61 @@
14 `^_MTN$`,
15 `^_darcs$`,
16 `^{arch}$`,
17+ `^\.snapignore$`,
18 }, "|")).MatchString
19
20+// fake static function variables
21+type keep struct {
22+ basedir string
23+ exclude func(string) bool
24+}
25+
26+func (k *keep) shouldExclude(basedir string, file string) bool {
27+ if basedir == k.basedir {
28+ if k.exclude == nil {
29+ return false
30+ }
31+
32+ return k.exclude(file)
33+ }
34+
35+ k.basedir = basedir
36+ k.exclude = nil
37+
38+ snapignore, err := os.Open(filepath.Join(basedir, ".snapignore"))
39+ if err != nil {
40+ return false
41+ }
42+
43+ scanner := bufio.NewScanner(snapignore)
44+ var lines []string
45+ for scanner.Scan() {
46+ line := scanner.Text()
47+ if _, err := regexp.Compile(line); err != nil {
48+ // not a regexp
49+ line = regexp.QuoteMeta(line)
50+ }
51+ lines = append(lines, line)
52+ }
53+
54+ fullRegex := strings.Join(lines, "|")
55+ exclude, err := regexp.Compile(fullRegex)
56+ if err == nil {
57+ k.exclude = exclude.MatchString
58+
59+ return k.exclude(file)
60+ }
61+
62+ // can't happen; can't even find a way to trigger it in testing.
63+ panic(fmt.Sprintf("|-composition of valid regexps is invalid?!? Please report this bug: %#v", fullRegex))
64+}
65+
66+var shouldExcludeDynamic = new(keep).shouldExclude
67+
68+func shouldExclude(basedir string, file string) bool {
69+ return shouldExcludeDefault(file) || shouldExcludeDynamic(basedir, file)
70+}
71+
72 // small helper that return the architecture or "multi" if its multiple arches
73 func debArchitecture(m *packageYaml) string {
74 switch len(m.Architectures) {
75@@ -375,7 +428,7 @@
76 return errin
77 }
78
79- if shouldExclude(filepath.Base(path)) {
80+ if shouldExclude(sourceDir, filepath.Base(path)) {
81 if info.IsDir() {
82 return filepath.SkipDir
83 }
84
85=== modified file 'snappy/build_test.go'
86--- snappy/build_test.go 2015-06-08 16:26:25 +0000
87+++ snappy/build_test.go 2015-07-29 13:13:40 +0000
88@@ -356,6 +356,28 @@
89 c.Check(string(out), Matches, `(?m)Only in \S+: \.bzr`)
90 }
91
92+func (s *SnapTestSuite) TestExcludeDynamicFalseIfNoSnapignore(c *C) {
93+ basedir := c.MkDir()
94+ c.Check(shouldExcludeDynamic(basedir, "foo"), Equals, false)
95+}
96+
97+func (s *SnapTestSuite) TestExcludeDynamicWorksIfSnapignore(c *C) {
98+ basedir := c.MkDir()
99+ c.Assert(ioutil.WriteFile(filepath.Join(basedir, ".snapignore"), []byte("foo\nb.r\n"), 0644), IsNil)
100+ c.Check(shouldExcludeDynamic(basedir, "foo"), Equals, true)
101+ c.Check(shouldExcludeDynamic(basedir, "bar"), Equals, true)
102+ c.Check(shouldExcludeDynamic(basedir, "bzr"), Equals, true)
103+ c.Check(shouldExcludeDynamic(basedir, "baz"), Equals, false)
104+}
105+
106+func (s *SnapTestSuite) TestExcludeDynamicWeirdRegexps(c *C) {
107+ basedir := c.MkDir()
108+ c.Assert(ioutil.WriteFile(filepath.Join(basedir, ".snapignore"), []byte("*hello\n"), 0644), IsNil)
109+ // note “*hello” is not a valid regexp, so will be taken literally (not globbed!)
110+ c.Check(shouldExcludeDynamic(basedir, "ahello"), Equals, false)
111+ c.Check(shouldExcludeDynamic(basedir, "*hello"), Equals, true)
112+}
113+
114 func (s *SnapTestSuite) TestBuildSimpleOutputDir(c *C) {
115 sourceDir := makeExampleSnapSourceDir(c, `name: hello
116 version: 1.0.1

Subscribers

People subscribed via source and target branches