Merge lp:~sergiusens/snapcraft/1496381 into lp:~snappy-dev/snapcraft/core

Proposed by Sergio Schvezov on 2015-09-16
Status: Merged
Approved by: John Lenton on 2015-09-17
Approved revision: 168
Merged at revision: 170
Proposed branch: lp:~sergiusens/snapcraft/1496381
Merge into: lp:~snappy-dev/snapcraft/core
Diff against target: 87 lines (+26/-19)
3 files modified
snapcraft/cmds.py (+9/-1)
snapcraft/tests/test_wiki.py (+3/-3)
snapcraft/wiki.py (+14/-15)
To merge this branch: bzr merge lp:~sergiusens/snapcraft/1496381
Reviewer Review Type Date Requested Status
John Lenton 2015-09-16 Approve on 2015-09-17
Daniel Holbach (community) Approve on 2015-09-17
Review via email: mp+271299@code.launchpad.net

Commit Message

Fetch from the wiki only once

Description of the Change

Based on Chipaca's example code

To post a comment you must log in.
Sergio Schvezov (sergiusens) wrote :

I'm just noticing this is a useless branch :-)

Sergio Schvezov (sergiusens) wrote :

there we go

Daniel Holbach (dholbach) wrote :

gcc -Wall -Wextra -Werror -pedantic -o test ./test.c -I/tmp/1496381/examples/downloader-with-wiki-parts/parts/curl/install/include -I/tmp/1496381/examples/downloader-with-wiki-parts/parts/curl/install/usr/include -I/tmp/1496381/examples/downloader-with-wiki-parts/parts/curl/install/include/x86_64-linux-gnu -I/tmp/1496381/examples/downloader-with-wiki-parts/parts/curl/install/usr/include/x86_64-linux-gnu -L/tmp/1496381/examples/downloader-with-wiki-parts/parts/curl/install/lib -L/tmp/1496381/examples/downloader-with-wiki-parts/parts/curl/install/usr/lib -L/tmp/1496381/examples/downloader-with-wiki-parts/parts/curl/install/lib/x86_64-linux-gnu -L/tmp/1496381/examples/downloader-with-wiki-parts/parts/curl/install/usr/lib/x86_64-linux-gnu -lcurl -lssl -lcrypto -lssl -lcrypto -lz
make install DESTDIR=/tmp/1496381/examples/downloader-with-wiki-parts/parts/main/install
install -d -m755 /tmp/1496381/examples/downloader-with-wiki-parts/parts/main/install/bin/
install -m755 ./test /tmp/1496381/examples/downloader-with-wiki-parts/parts/main/install/bin/test
Staging main
Snapping main
Traceback (most recent call last):
  File "../../bin/snapcraft", line 33, in <module>
    snapcraft.main.main()
  File "/tmp/1496381/snapcraft/main.py", line 77, in main
    args.func(args)
  File "/tmp/1496381/snapcraft/cmds.py", line 94, in assemble
    snap(args)
  File "/tmp/1496381/snapcraft/cmds.py", line 82, in snap
    config = _load_config()
  File "/tmp/1496381/snapcraft/cmds.py", line 299, in _load_config
    return snapcraft.yaml.Config()
  File "/tmp/1496381/snapcraft/yaml.py", line 99, in __init__
    self._compute_part_dependencies(after_requests)
  File "/tmp/1496381/snapcraft/yaml.py", line 130, in _compute_part_dependencies
    wiki_part = w.get_part(dep)
  File "/tmp/1496381/snapcraft/wiki.py", line 59, in get_part
    return self.wiki_parts[name]
AttributeError: 'Wiki' object has no attribute 'wiki_parts'
daniel@daydream:/tmp/1496381/examples/downloader-with-wiki-parts$

review: Needs Fixing
lp:~sergiusens/snapcraft/1496381 updated on 2015-09-16
168. By Sergio Schvezov on 2015-09-16

Fetch from the wiki only once

Daniel Holbach (dholbach) wrote :

Good work!

review: Approve
John Lenton (chipaca) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snapcraft/cmds.py'
2--- snapcraft/cmds.py 2015-09-14 09:48:23 +0000
3+++ snapcraft/cmds.py 2015-09-16 21:02:24 +0000
4@@ -41,6 +41,9 @@
5 '''
6
7
8+_config = None
9+
10+
11 def init(args):
12 if os.path.exists("snapcraft.yaml"):
13 logger.error('snapcraft.yaml already exists!')
14@@ -295,8 +298,13 @@
15
16
17 def _load_config():
18+ global _config
19+ if _config:
20+ return _config
21+
22 try:
23- return snapcraft.yaml.Config()
24+ _config = snapcraft.yaml.Config()
25+ return _config
26 except snapcraft.yaml.SnapcraftYamlFileError as e:
27 logger.error(
28 'Could not find {}. Are you sure you are in the right directory?\n'
29
30=== modified file 'snapcraft/tests/test_wiki.py'
31--- snapcraft/tests/test_wiki.py 2015-09-15 15:06:54 +0000
32+++ snapcraft/tests/test_wiki.py 2015-09-16 21:02:24 +0000
33@@ -42,9 +42,9 @@
34 def test_get_part(self):
35 w = snapcraft.wiki.Wiki()
36
37+ self.assertEqual(w.get_part('part1'), {'type': 'go'})
38+ self.assertEqual(w.get_part('part2'), None)
39+
40 self.mock_requests.assert_called_once_with(
41 'https://wiki.ubuntu.com/Snappy/Parts',
42 params={'action': 'raw'})
43-
44- self.assertEqual(w.get_part('part1'), {'type': 'go'})
45- self.assertEqual(w.get_part('part2'), None)
46
47=== modified file 'snapcraft/wiki.py'
48--- snapcraft/wiki.py 2015-09-15 14:45:57 +0000
49+++ snapcraft/wiki.py 2015-09-16 21:02:24 +0000
50@@ -28,23 +28,22 @@
51
52
53 class Wiki:
54-
55- def __init__(self):
56- self.wiki_parts = self._fetch()
57+ wiki_parts = None
58
59 def _fetch(self):
60- raw_content = requests.get(PARTS_URI, params=PARTS_URI_PARAMS)
61- content = raw_content.text.strip()
62-
63- if content.startswith(_WIKI_OPEN):
64- content = content[len(_WIKI_OPEN):].strip()
65- if content.endswith(_WIKI_CLOSE):
66- content = content[:-len(_WIKI_CLOSE)]
67-
68- return yaml.load(content)
69+ if self.wiki_parts is None:
70+ raw_content = requests.get(PARTS_URI, params=PARTS_URI_PARAMS)
71+ content = raw_content.text.strip()
72+
73+ if content.startswith(_WIKI_OPEN):
74+ content = content[len(_WIKI_OPEN):].strip()
75+ if content.endswith(_WIKI_CLOSE):
76+ content = content[:-len(_WIKI_CLOSE)]
77+
78+ self.wiki_parts = yaml.load(content)
79
80 def get_part(self, name):
81- try:
82+ self._fetch()
83+
84+ if name in self.wiki_parts:
85 return self.wiki_parts[name]
86- except KeyError:
87- return None

Subscribers

People subscribed via source and target branches