Merge lp:~elopio/snapcraft/fix1477638-format_strings1 into lp:~snappy-dev/snapcraft/core

Proposed by Leo Arias
Status: Superseded
Proposed branch: lp:~elopio/snapcraft/fix1477638-format_strings1
Merge into: lp:~snappy-dev/snapcraft/core
Diff against target: 919 lines (+244/-110)
16 files modified
integration-tests/units/jobs.pxu (+4/-4)
snapcraft/__init__.py (+17/-7)
snapcraft/cmds.py (+29/-24)
snapcraft/common.py (+20/-10)
snapcraft/dirs.py (+3/-3)
snapcraft/main.py (+8/-0)
snapcraft/plugin.py (+17/-11)
snapcraft/plugins/ant_project.py (+7/-2)
snapcraft/plugins/copy.py (+5/-1)
snapcraft/plugins/ubuntu.py (+10/-4)
snapcraft/tests/__init__.py (+3/-3)
snapcraft/tests/test_cmds.py (+46/-8)
snapcraft/tests/test_copy_plugin.py (+15/-8)
snapcraft/tests/test_plugin.py (+17/-4)
snapcraft/tests/test_yaml.py (+30/-14)
snapcraft/yaml.py (+13/-7)
To merge this branch: bzr merge lp:~elopio/snapcraft/fix1477638-format_strings1
Reviewer Review Type Date Requested Status
Snappy Developers Pending
Review via email: mp+265702@code.launchpad.net

Commit message

Cleanups to prepare for changing the format strings safely.

To post a comment you must log in.
111. By Leo Arias

Add the missing file.

112. By Leo Arias

Moved the plugindir cleanup to the base test case.

113. By Leo Arias

Fixed the imports.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'integration-tests/units/jobs.pxu'
--- integration-tests/units/jobs.pxu 2015-07-22 21:05:20 +0000
+++ integration-tests/units/jobs.pxu 2015-07-23 17:20:28 +0000
@@ -3,7 +3,7 @@
3estimated_duration: 0.13estimated_duration: 0.1
4command:4command:
5 set -x5 set -x
6 OUTPUT=$(${SNAPCRAFT} pull)6 OUTPUT=$(${SNAPCRAFT} pull 2>&1)
7 test $? = 1 || exit 17 test $? = 1 || exit 1
8 echo $OUTPUT | grep "Could not find snapcraft\.yaml\."8 echo $OUTPUT | grep "Could not find snapcraft\.yaml\."
99
@@ -13,7 +13,7 @@
13command:13command:
14 set -x14 set -x
15 cp -rT $PLAINBOX_PROVIDER_DATA/assemble .15 cp -rT $PLAINBOX_PROVIDER_DATA/assemble .
16 OUTPUT=$(${SNAPCRAFT} assemble)16 OUTPUT=$(${SNAPCRAFT} assemble 2>&1)
17 test $? = 1 || exit 117 test $? = 1 || exit 1
18 echo $OUTPUT | grep "Missing snappy metadata file"18 echo $OUTPUT | grep "Missing snappy metadata file"
1919
@@ -117,7 +117,7 @@
117command:117command:
118 set -x118 set -x
119 cp -rT $PLAINBOX_PROVIDER_DATA/conflicts .119 cp -rT $PLAINBOX_PROVIDER_DATA/conflicts .
120 OUTPUT=$(${SNAPCRAFT} stage)120 OUTPUT=$(${SNAPCRAFT} stage 2>&1)
121 test $? = 1 || exit 1121 test $? = 1 || exit 1
122 echo $OUTPUT | grep "Error: parts p1 and p2 have the following files in common: bin/test" # squished output by bash122 echo $OUTPUT | grep "Error: parts p1 and p2 have the following files in common: bin/test" # squished output by bash
123123
@@ -140,7 +140,7 @@
140 set -x140 set -x
141 cp -rT $PLAINBOX_PROVIDER_DATA/dependencies .141 cp -rT $PLAINBOX_PROVIDER_DATA/dependencies .
142 sed -i "s/p1:/p1:\n after: [p3]/" snapcraft.yaml142 sed -i "s/p1:/p1:\n after: [p3]/" snapcraft.yaml
143 OUTPUT=$(${SNAPCRAFT} pull)143 OUTPUT=$(${SNAPCRAFT} pull 2>&1)
144 test $? = 1 || exit 1144 test $? = 1 || exit 1
145 echo $OUTPUT | grep -i "circular dependency"145 echo $OUTPUT | grep -i "circular dependency"
146146
147147
=== modified file 'snapcraft/__init__.py'
--- snapcraft/__init__.py 2015-07-22 18:59:28 +0000
+++ snapcraft/__init__.py 2015-07-23 17:20:28 +0000
@@ -14,11 +14,16 @@
14# You should have received a copy of the GNU General Public License14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import logging
17import os18import os
18import snapcraft.common
19import subprocess19import subprocess
20import urllib.parse20import urllib.parse
2121
22import snapcraft.common
23
24
25logger = logging.getLogger(__name__)
26
2227
23class BasePlugin:28class BasePlugin:
2429
@@ -53,7 +58,7 @@
53 if cwd is None:58 if cwd is None:
54 cwd = self.builddir59 cwd = self.builddir
55 if True:60 if True:
56 print(' '.join(cmd))61 logger.info(' '.join(cmd))
57 return snapcraft.common.run(cmd, cwd=cwd, **kwargs)62 return snapcraft.common.run(cmd, cwd=cwd, **kwargs)
5863
59 def isurl(self, url):64 def isurl(self, url):
@@ -71,7 +76,8 @@
7176
72 def pull_git(self, source, source_tag=None, source_branch=None):77 def pull_git(self, source, source_tag=None, source_branch=None):
73 if source_tag and source_branch:78 if source_tag and source_branch:
74 snapcraft.common.fatal("You can't specify both source-tag and source-branch for a git source (part '%s')." % self.name)79 logger.error("You can't specify both source-tag and source-branch for a git source (part '%s')." % self.name)
80 snapcraft.common.fatal()
7581
76 if os.path.exists(os.path.join(self.sourcedir, ".git")):82 if os.path.exists(os.path.join(self.sourcedir, ".git")):
77 refspec = 'HEAD'83 refspec = 'HEAD'
@@ -120,11 +126,13 @@
120 elif source.startswith("git:"):126 elif source.startswith("git:"):
121 source_type = 'git'127 source_type = 'git'
122 elif self.isurl(source):128 elif self.isurl(source):
123 snapcraft.common.fatal("Unrecognized source '%s' for part '%s'." % (source, self.name))129 logger.error("Unrecognized source '%s' for part '%s'." % (source, self.name))
130 snapcraft.common.fatal()
124131
125 if source_type == 'bzr':132 if source_type == 'bzr':
126 if source_branch:133 if source_branch:
127 snapcraft.common.fatal("You can't specify source-branch for a bzr source (part '%s')." % self.name)134 logger.error("You can't specify source-branch for a bzr source (part '%s')." % self.name)
135 snapcraft.common.fatal()
128 if not self.pull_bzr(source, source_tag=source_tag):136 if not self.pull_bzr(source, source_tag=source_tag):
129 return False137 return False
130 if not self.run(['cp', '-Trfa', self.sourcedir, self.builddir]):138 if not self.run(['cp', '-Trfa', self.sourcedir, self.builddir]):
@@ -136,9 +144,11 @@
136 return False144 return False
137 elif source_type == 'tar':145 elif source_type == 'tar':
138 if source_branch:146 if source_branch:
139 snapcraft.common.fatal("You can't specify source-branch for a tar source (part '%s')." % self.name)147 logger.error("You can't specify source-branch for a tar source (part '%s')." % self.name)
148 snapcraft.common.fatal()
140 if source_tag:149 if source_tag:
141 snapcraft.common.fatal("You can't specify source-tag for a tar source (part '%s')." % self.name)150 logger.error("You can't specify source-tag for a tar source (part '%s')." % self.name)
151 snapcraft.common.fatal()
142 if not self.pull_tarball(source):152 if not self.pull_tarball(source):
143 return False153 return False
144 if not self.extract_tarball(source):154 if not self.extract_tarball(source):
145155
=== modified file 'snapcraft/cmds.py'
--- snapcraft/cmds.py 2015-07-22 21:05:20 +0000
+++ snapcraft/cmds.py 2015-07-23 17:20:28 +0000
@@ -15,21 +15,27 @@
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import glob17import glob
18import logging
18import os19import os
19import shlex20import shlex
20import snapcraft.common
21import snapcraft.plugin
22import snapcraft.yaml
23import subprocess21import subprocess
24import sys22import sys
25import tempfile23import tempfile
26import time24import time
25
27import yaml26import yaml
2827
28import snapcraft.plugin
29import snapcraft.yaml
30from snapcraft import common
31
32
33logger = logging.getLogger(__name__)
34
2935
30def init(args):36def init(args):
31 if os.path.exists("snapcraft.yaml"):37 if os.path.exists("snapcraft.yaml"):
32 snapcraft.common.log("snapcraft.yaml already exists!", file=sys.stderr)38 logger.error('snapcraft.yaml already exists!')
33 sys.exit(1)39 sys.exit(1)
34 yaml = 'parts:\n'40 yaml = 'parts:\n'
35 for part_name in args.part:41 for part_name in args.part:
@@ -41,23 +47,22 @@
41 yaml = yaml.strip()47 yaml = yaml.strip()
42 with open('snapcraft.yaml', mode='w+') as f:48 with open('snapcraft.yaml', mode='w+') as f:
43 f.write(yaml)49 f.write(yaml)
44 snapcraft.common.log("Wrote the following as snapcraft.yaml.")50 logger.info('Wrote the following as snapcraft.yaml:\n{}'.format(yaml))
45 print()
46 print(yaml)
47 sys.exit(0)51 sys.exit(0)
4852
4953
50def shell(args):54def shell(args):
51 config = snapcraft.yaml.Config()55 config = snapcraft.yaml.Config()
52 snapcraft.common.env = config.stage_env()56 common.env = config.stage_env()
53 userCommand = args.userCommand57 userCommand = args.userCommand
54 if not userCommand:58 if not userCommand:
55 userCommand = ['/usr/bin/env', 'PS1=\[\e[1;32m\]snapcraft:\w\$\[\e[0m\] ', '/bin/bash', '--norc']59 userCommand = ['/usr/bin/env', 'PS1=\[\e[1;32m\]snapcraft:\w\$\[\e[0m\] ', '/bin/bash', '--norc']
56 snapcraft.common.run(userCommand)60 common.run(userCommand)
5761
5862
59def wrap_exe(relexepath):63def wrap_exe(relexepath):
60 exepath = os.path.join(snapcraft.common.snapdir, relexepath)64 snapdir = common.get_snapdir()
65 exepath = os.path.join(snapdir, relexepath)
61 wrappath = exepath + '.wrapper'66 wrappath = exepath + '.wrapper'
6267
63 try:68 try:
@@ -65,13 +70,13 @@
65 except Exception:70 except Exception:
66 pass71 pass
6772
68 script = "#!/bin/sh\n%s\nexec %s $*" % (snapcraft.common.assemble_env().replace(snapcraft.common.snapdir, "$SNAP_APP_PATH"), '"$SNAP_APP_PATH/%s"' % relexepath)73 script = "#!/bin/sh\n%s\nexec %s $*" % (common.assemble_env().replace(snapdir, "$SNAP_APP_PATH"), '"$SNAP_APP_PATH/%s"' % relexepath)
69 with open(wrappath, 'w+') as f:74 with open(wrappath, 'w+') as f:
70 f.write(script)75 f.write(script)
7176
72 os.chmod(wrappath, 0o755)77 os.chmod(wrappath, 0o755)
7378
74 return os.path.relpath(wrappath, snapcraft.common.snapdir)79 return os.path.relpath(wrappath, snapdir)
7580
7681
77def snap(args):82def snap(args):
@@ -81,17 +86,17 @@
81 config = snapcraft.yaml.Config()86 config = snapcraft.yaml.Config()
8287
83 if 'snappy-metadata' in config.data:88 if 'snappy-metadata' in config.data:
84 snapcraft.common.run(89 common.run(
85 ['cp', '-arvT', config.data['snappy-metadata'], snapcraft.common.snapdir + '/meta'])90 ['cp', '-arvT', config.data['snappy-metadata'], common.get_snapdir() + '/meta'])
86 if not os.path.exists('snap/meta/package.yaml'):91 if not os.path.exists('snap/meta/package.yaml'):
87 snapcraft.common.log("Missing snappy metadata file 'meta/package.yaml'. Try specifying 'snappy-metadata' in snapcraft.yaml, pointing to a meta directory in your source tree.")92 logger.error("Missing snappy metadata file 'meta/package.yaml'. Try specifying 'snappy-metadata' in snapcraft.yaml, pointing to a meta directory in your source tree.")
88 sys.exit(1)93 sys.exit(1)
8994
90 # wrap all included commands95 # wrap all included commands
91 with open("snap/meta/package.yaml", 'r') as f:96 with open("snap/meta/package.yaml", 'r') as f:
92 package = yaml.load(f)97 package = yaml.load(f)
9398
94 snapcraft.common.env = config.snap_env()99 common.env = config.snap_env()
95100
96 def replace_cmd(execparts, cmd):101 def replace_cmd(execparts, cmd):
97 newparts = [cmd] + execparts[1:]102 newparts = [cmd] + execparts[1:]
@@ -125,7 +130,7 @@
125def assemble(args):130def assemble(args):
126 args.cmd = 'snap'131 args.cmd = 'snap'
127 snap(args)132 snap(args)
128 snapcraft.common.run(['snappy', 'build', snapcraft.common.snapdir])133 common.run(['snappy', 'build', common.get_snapdir()])
129134
130135
131def run(args):136def run(args):
@@ -136,7 +141,7 @@
136 os.makedirs(qemudir)141 os.makedirs(qemudir)
137 except FileExistsError:142 except FileExistsError:
138 pass143 pass
139 snapcraft.common.run(144 common.run(
140 ['sudo', 'ubuntu-device-flash', 'core', '--developer-mode', '--enable-ssh', '15.04', '-o', qemu_img],145 ['sudo', 'ubuntu-device-flash', 'core', '--developer-mode', '--enable-ssh', '15.04', '-o', qemu_img],
141 cwd=qemudir)146 cwd=qemudir)
142 qemu = subprocess.Popen(147 qemu = subprocess.Popen(
@@ -186,7 +191,7 @@
186 for otherPartName in partsFiles:191 for otherPartName in partsFiles:
187 common = partFiles & partsFiles[otherPartName]192 common = partFiles & partsFiles[otherPartName]
188 if common:193 if common:
189 snapcraft.common.log("Error: parts %s and %s have the following files in common:\n %s" % (otherPartName, part.names()[0], '\n '.join(sorted(common))))194 logger.error('Error: parts %s and %s have the following files in common:\n %s' % (otherPartName, part.names()[0], '\n '.join(sorted(common))))
190 return False195 return False
191196
192 # And add our files to the list197 # And add our files to the list
@@ -201,9 +206,9 @@
201206
202 cmds = [args.cmd]207 cmds = [args.cmd]
203208
204 if cmds[0] in snapcraft.common.commandOrder:209 if cmds[0] in common.COMMAND_ORDER:
205 forceCommand = cmds[0]210 forceCommand = cmds[0]
206 cmds = snapcraft.common.commandOrder[0:snapcraft.common.commandOrder.index(cmds[0]) + 1]211 cmds = common.COMMAND_ORDER[0:common.COMMAND_ORDER.index(cmds[0]) + 1]
207212
208 config = snapcraft.yaml.Config()213 config = snapcraft.yaml.Config()
209214
@@ -214,7 +219,7 @@
214 if subprocess.call(['dpkg-query', '-s', checkpkg], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) != 0:219 if subprocess.call(['dpkg-query', '-s', checkpkg], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) != 0:
215 newPackages.append(checkpkg)220 newPackages.append(checkpkg)
216 if newPackages:221 if newPackages:
217 print("Installing required packages on the host system: " + ", ".join(newPackages))222 logger.info('Installing required packages on the host system: ' + ', '.join(newPackages))
218 subprocess.call(['sudo', 'apt-get', '-y', 'install'] + newPackages, stdout=subprocess.DEVNULL)223 subprocess.call(['sudo', 'apt-get', '-y', 'install'] + newPackages, stdout=subprocess.DEVNULL)
219224
220 for part in config.all_parts:225 for part in config.all_parts:
@@ -229,8 +234,8 @@
229 if not check_for_collisions(config.all_parts):234 if not check_for_collisions(config.all_parts):
230 sys.exit(1)235 sys.exit(1)
231236
232 snapcraft.common.env = config.build_env_for_part(part)237 common.env = config.build_env_for_part(part)
233 force = forceAll or cmd == forceCommand238 force = forceAll or cmd == forceCommand
234 if not getattr(part, cmd)(force=force):239 if not getattr(part, cmd)(force=force):
235 snapcraft.common.log("Failed doing %s for %s!" % (cmd, part.names()[0]))240 logger.error('Failed doing %s for %s!' % (cmd, part.names()[0]))
236 sys.exit(1)241 sys.exit(1)
237242
=== modified file 'snapcraft/common.py'
--- snapcraft/common.py 2015-07-14 21:15:07 +0000
+++ snapcraft/common.py 2015-07-23 17:20:28 +0000
@@ -21,6 +21,11 @@
21import sys21import sys
22import tempfile22import tempfile
2323
24
25COMMAND_ORDER = ["pull", "build", "stage", "snap"]
26_DEFAULT_PLUGINDIR = '/usr/share/snapcraft/plugins'
27_plugindir = _DEFAULT_PLUGINDIR
28
24env = []29env = []
2530
2631
@@ -39,17 +44,22 @@
39 return subprocess.call(['/bin/sh', f.name] + cmd, **kwargs) == 044 return subprocess.call(['/bin/sh', f.name] + cmd, **kwargs) == 0
4045
4146
42def log(msg, file=None):
43 print('\033[01m' + msg + '\033[0m', file=None)
44
45
46def fatal(msg):47def fatal(msg):
47 log(msg, file=sys.stderr)
48 sys.exit(1)48 sys.exit(1)
4949
5050
51commandOrder = ["pull", "build", "stage", "snap"]51def get_stagedir():
52stagedir = os.path.join(os.getcwd(), "stage")52 return os.path.join(os.getcwd(), 'stage')
53snapdir = os.path.join(os.getcwd(), "snap")53
5454
55plugindir = '/usr/share/snapcraft/plugins'55def get_snapdir():
56 return os.path.join(os.getcwd(), 'snap')
57
58
59def set_plugindir(plugindir):
60 global _plugindir
61 _plugindir = plugindir
62
63
64def get_plugindir():
65 return _plugindir
5666
=== modified file 'snapcraft/dirs.py'
--- snapcraft/dirs.py 2015-07-20 13:53:07 +0000
+++ snapcraft/dirs.py 2015-07-23 17:20:28 +0000
@@ -19,11 +19,11 @@
1919
20def setup_dirs():20def setup_dirs():
21 """21 """
22 Ensure that snapcraft.common.plugindir are setup correctly22 Ensure that snapcraft.common plugindir is setup correctly
23 and support running out of a development snapshot23 and support running out of a development snapshot
24 """24 """
25 import snapcraft.common25 from snapcraft import common
26 topdir = os.path.abspath(os.path.join(__file__, "..", ".."))26 topdir = os.path.abspath(os.path.join(__file__, "..", ".."))
27 # only change the default if we are running from a checkout27 # only change the default if we are running from a checkout
28 if os.path.exists(os.path.join(topdir, "setup.py")):28 if os.path.exists(os.path.join(topdir, "setup.py")):
29 snapcraft.common.plugindir = os.path.join(topdir, 'plugins')29 common.set_plugindir(os.path.join(topdir, 'plugins'))
3030
=== modified file 'snapcraft/main.py'
--- snapcraft/main.py 2015-07-17 17:19:04 +0000
+++ snapcraft/main.py 2015-07-23 17:20:28 +0000
@@ -16,12 +16,20 @@
16# along with this program. If not, see <http://www.gnu.org/licenses/>.16# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
18import argparse18import argparse
19import logging
19import sys20import sys
2021
21import snapcraft.cmds22import snapcraft.cmds
2223
2324
25_COLOR_BOLD = '\033[1m'
26_COLOR_END = '\033[0m'
27
28
24def main():29def main():
30 logging.basicConfig(
31 format=_COLOR_BOLD + '%(message)s' + _COLOR_END, level=logging.INFO)
32
25 root_parser = argparse.ArgumentParser()33 root_parser = argparse.ArgumentParser()
26 subparsers = root_parser.add_subparsers(dest='cmd')34 subparsers = root_parser.add_subparsers(dest='cmd')
2735
2836
=== modified file 'snapcraft/plugin.py'
--- snapcraft/plugin.py 2015-07-22 18:59:28 +0000
+++ snapcraft/plugin.py 2015-07-23 17:20:28 +0000
@@ -16,12 +16,18 @@
1616
17import glob17import glob
18import importlib18import importlib
19import logging
19import os20import os
20import snapcraft
21import snapcraft.common
22import sys21import sys
22
23import yaml23import yaml
2424
25import snapcraft
26from snapcraft import common
27
28
29logger = logging.getLogger(__name__)
30
2531
26def is_local_plugin(name):32def is_local_plugin(name):
27 return name.startswith("x-")33 return name.startswith("x-")
@@ -31,7 +37,7 @@
31 if is_local_plugin(name):37 if is_local_plugin(name):
32 return os.path.abspath(os.path.join('parts', 'plugins'))38 return os.path.abspath(os.path.join('parts', 'plugins'))
33 else:39 else:
34 return snapcraft.common.plugindir40 return common.get_plugindir()
3541
3642
37class PluginError(Exception):43class PluginError(Exception):
@@ -69,7 +75,7 @@
69 def _load_config(self, name):75 def _load_config(self, name):
70 configPath = os.path.join(plugindir(name), name + ".yaml")76 configPath = os.path.join(plugindir(name), name + ".yaml")
71 if not os.path.exists(configPath):77 if not os.path.exists(configPath):
72 snapcraft.common.log("Unknown plugin %s" % name, file=sys.stderr)78 logger.error("Unknown plugin %s" % name)
73 raise PluginError()79 raise PluginError()
74 with open(configPath, 'r') as fp:80 with open(configPath, 'r') as fp:
75 self.config = yaml.load(fp) or {}81 self.config = yaml.load(fp) or {}
@@ -86,7 +92,7 @@
86 setattr(options, attrname, properties[opt])92 setattr(options, attrname, properties[opt])
87 else:93 else:
88 if opt_parameters.get('required', False):94 if opt_parameters.get('required', False):
89 snapcraft.common.log("Required field %s missing on part %s" % (opt, name), file=sys.stderr)95 logger.error("Required field %s missing on part %s" % (opt, name))
90 raise PluginError()96 raise PluginError()
91 setattr(options, attrname, None)97 setattr(options, attrname, None)
9298
@@ -147,13 +153,13 @@
147 return self.part_names153 return self.part_names
148154
149 def notify_stage(self, stage, hint=''):155 def notify_stage(self, stage, hint=''):
150 snapcraft.common.log(stage + " " + self.part_names[0] + hint)156 logger.info(stage + " " + self.part_names[0] + hint)
151157
152 def is_dirty(self, stage):158 def is_dirty(self, stage):
153 try:159 try:
154 with open(self.statefile, 'r') as f:160 with open(self.statefile, 'r') as f:
155 lastStep = f.read()161 lastStep = f.read()
156 return snapcraft.common.commandOrder.index(stage) > snapcraft.common.commandOrder.index(lastStep)162 return common.COMMAND_ORDER.index(stage) > common.COMMAND_ORDER.index(lastStep)
157 except Exception:163 except Exception:
158 return True164 return True
159165
@@ -197,7 +203,7 @@
197 return True203 return True
198204
199 self.notify_stage("Staging")205 self.notify_stage("Staging")
200 snapcraft.common.run(['cp', '-arT', self.installdir, self.stagedir])206 common.run(['cp', '-arT', self.installdir, self.stagedir])
201 self.mark_done('stage')207 self.mark_done('stage')
202 return True208 return True
203209
@@ -213,9 +219,9 @@
213 snapDirs, snap_files = self.collect_snap_files(includes, excludes)219 snapDirs, snap_files = self.collect_snap_files(includes, excludes)
214220
215 if snapDirs:221 if snapDirs:
216 snapcraft.common.run(['mkdir', '-p'] + list(snapDirs), cwd=self.stagedir)222 common.run(['mkdir', '-p'] + list(snapDirs), cwd=self.stagedir)
217 if snap_files:223 if snap_files:
218 snapcraft.common.run(['cp', '-a', '--parent'] + list(snap_files) + [self.snapdir], cwd=self.stagedir)224 common.run(['cp', '-a', '--parent'] + list(snap_files) + [self.snapdir], cwd=self.stagedir)
219225
220 self.mark_done('snap')226 self.mark_done('snap')
221 return True227 return True
@@ -274,6 +280,6 @@
274def load_plugin(part_name, plugin_name, properties={}, load_code=True):280def load_plugin(part_name, plugin_name, properties={}, load_code=True):
275 part = PluginHandler(plugin_name, part_name, properties, load_code=load_code)281 part = PluginHandler(plugin_name, part_name, properties, load_code=load_code)
276 if not part.is_valid():282 if not part.is_valid():
277 snapcraft.common.log("Could not load part %s" % plugin_name, file=sys.stderr)283 logger.error("Could not load part %s" % plugin_name)
278 sys.exit(1)284 sys.exit(1)
279 return part285 return part
280286
=== modified file 'snapcraft/plugins/ant_project.py'
--- snapcraft/plugins/ant_project.py 2015-07-22 18:23:16 +0000
+++ snapcraft/plugins/ant_project.py 2015-07-23 17:20:28 +0000
@@ -15,10 +15,15 @@
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import glob17import glob
18import logging
18import os19import os
20import sys
21
19import snapcraft22import snapcraft
20import snapcraft.common23import snapcraft.common
21import sys24
25
26logger = logging.getLogger(__name__)
2227
2328
24class AntProjectPlugin(snapcraft.BasePlugin):29class AntProjectPlugin(snapcraft.BasePlugin):
@@ -31,7 +36,7 @@
31 return False36 return False
32 files = glob.glob(os.path.join(self.builddir, 'target', '*.jar'))37 files = glob.glob(os.path.join(self.builddir, 'target', '*.jar'))
33 if not files:38 if not files:
34 snapcraft.common.log('Could not find any built jar files for part %s' % self.name)39 logger.error('Could not find any built jar files for part %s' % self.name)
35 sys.exit(1)40 sys.exit(1)
36 jardir = os.path.join(self.installdir, 'jar')41 jardir = os.path.join(self.installdir, 'jar')
37 return self.run(['mkdir', '-p', jardir]) and \42 return self.run(['mkdir', '-p', jardir]) and \
3843
=== modified file 'snapcraft/plugins/copy.py'
--- snapcraft/plugins/copy.py 2015-07-22 14:55:48 +0000
+++ snapcraft/plugins/copy.py 2015-07-23 17:20:28 +0000
@@ -14,11 +14,15 @@
14# You should have received a copy of the GNU General Public License14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import logging
17import os18import os
1819
19import snapcraft20import snapcraft
2021
2122
23logger = logging.getLogger(__name__)
24
25
22class CopyPlugin(snapcraft.BasePlugin):26class CopyPlugin(snapcraft.BasePlugin):
2327
24 def build(self):28 def build(self):
@@ -26,7 +30,7 @@
26 for src in sorted(self.options.files):30 for src in sorted(self.options.files):
27 dst = self.options.files[src]31 dst = self.options.files[src]
28 if not os.path.lexists(src):32 if not os.path.lexists(src):
29 snapcraft.common.log("WARNING: file '%s' missing" % src)33 logger.warning("WARNING: file '%s' missing" % src)
30 res = False34 res = False
31 continue35 continue
32 dst = os.path.join(self.installdir, dst)36 dst = os.path.join(self.installdir, dst)
3337
=== modified file 'snapcraft/plugins/ubuntu.py'
--- snapcraft/plugins/ubuntu.py 2015-07-22 18:23:16 +0000
+++ snapcraft/plugins/ubuntu.py 2015-07-23 17:20:28 +0000
@@ -14,12 +14,18 @@
14# You should have received a copy of the GNU General Public License14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import apt17import logging
18import os18import os
19import snapcraft.common
20import subprocess19import subprocess
21import sys20import sys
2221
22import apt
23
24import snapcraft.common
25
26
27logger = logging.getLogger(__name__)
28
2329
24class UbuntuPlugin(snapcraft.BasePlugin):30class UbuntuPlugin(snapcraft.BasePlugin):
2531
@@ -32,7 +38,7 @@
32 else:38 else:
33 # User didn't specify a package, use the part name39 # User didn't specify a package, use the part name
34 if name == 'ubuntu':40 if name == 'ubuntu':
35 snapcraft.common.log("Part %s needs either a package option or a name" % name)41 logger.error('Part %s needs either a package option or a name' % name)
36 sys.exit(1)42 sys.exit(1)
37 self.included_packages.append(name)43 self.included_packages.append(name)
3844
@@ -83,7 +89,7 @@
83 for p in packages:89 for p in packages:
84 if p not in alldeps:90 if p not in alldeps:
85 exit = True91 exit = True
86 snapcraft.common.log("Package %s not recognized" % p, file=sys.stderr)92 logger.error('Package %s not recognized' % p, file=sys.stderr)
87 if exit:93 if exit:
88 sys.exit(1)94 sys.exit(1)
8995
9096
=== modified file 'snapcraft/tests/__init__.py'
--- snapcraft/tests/__init__.py 2015-07-22 15:03:34 +0000
+++ snapcraft/tests/__init__.py 2015-07-23 17:20:28 +0000
@@ -16,7 +16,6 @@
1616
17import fixtures17import fixtures
1818
19import snapcraft.dirs
20from snapcraft.tests import fixture_setup19from snapcraft.tests import fixture_setup
2120
2221
@@ -24,5 +23,6 @@
2423
25 def setUp(self):24 def setUp(self):
26 super().setUp()25 super().setUp()
27 snapcraft.dirs.setup_dirs()26 temp_cwd_fixture = fixture_setup.TempCWD()
28 self.useFixture(fixture_setup.TempCWD())27 self.useFixture(temp_cwd_fixture)
28 self.path = temp_cwd_fixture.path
2929
=== modified file 'snapcraft/tests/test_cmds.py'
--- snapcraft/tests/test_cmds.py 2015-07-15 09:00:46 +0000
+++ snapcraft/tests/test_cmds.py 2015-07-23 17:20:28 +0000
@@ -14,18 +14,23 @@
14# You should have received a copy of the GNU General Public License14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import logging
17import os18import os
18import tempfile19import tempfile
19from unittest import mock20from unittest import mock
2021
21from snapcraft.cmds import check_for_collisions22import fixtures
23
24from snapcraft import cmds
22from snapcraft.tests import TestCase25from snapcraft.tests import TestCase
2326
2427
25class TestCommands(TestCase):28class TestCommands(TestCase):
2629
27 @mock.patch('snapcraft.common.log')30 def test_check_for_collisions(self):
28 def test_check_for_collisions(self, logmock):31 fake_logger = fixtures.FakeLogger(level=logging.ERROR)
32 self.useFixture(fake_logger)
33
29 tmpdirObject = tempfile.TemporaryDirectory()34 tmpdirObject = tempfile.TemporaryDirectory()
30 self.addCleanup(tmpdirObject.cleanup)35 self.addCleanup(tmpdirObject.cleanup)
31 tmpdir = tmpdirObject.name36 tmpdir = tmpdirObject.name
@@ -52,8 +57,41 @@
52 open(part3.installdir + '/1', mode='w').close()57 open(part3.installdir + '/1', mode='w').close()
53 open(part3.installdir + '/a/2', mode='w').close()58 open(part3.installdir + '/a/2', mode='w').close()
5459
55 self.assertTrue(check_for_collisions([part1, part2]))60 self.assertTrue(cmds.check_for_collisions([part1, part2]))
56 self.assertFalse(logmock.called)61 self.assertEqual('', fake_logger.output)
5762
58 self.assertFalse(check_for_collisions([part1, part2, part3]))63 self.assertFalse(cmds.check_for_collisions([part1, part2, part3]))
59 logmock.assert_called_with("Error: parts part2 and part3 have the following files in common:\n 1\n a/2")64 self.assertEqual(
65 'Error: parts part2 and part3 have the following files in common:\n'
66 ' 1\n'
67 ' a/2\n',
68 fake_logger.output)
69
70
71class InitTestCase(TestCase):
72
73 def test_init_with_existing_snapcraft_yaml_must_fail(self):
74 fake_logger = fixtures.FakeLogger(level=logging.ERROR)
75 self.useFixture(fake_logger)
76
77 open('snapcraft.yaml', 'w').close()
78
79 with self.assertRaises(SystemExit) as raised:
80 cmds.init('dummy args')
81 self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
82 self.assertEqual(
83 'snapcraft.yaml already exists!\n', fake_logger.output)
84
85 def test_init_without_parts_must_write_snapcraft_yaml(self):
86 fake_logger = fixtures.FakeLogger(level=logging.INFO)
87 self.useFixture(fake_logger)
88
89 snap_without_parts = type('obj', (object, ), {'part': []})
90 with self.assertRaises(SystemExit) as raised:
91 cmds.init(snap_without_parts)
92
93 self.assertEqual(raised.exception.code, 0, 'Wrong exit code returned.')
94 self.assertEqual(
95 'Wrote the following as snapcraft.yaml:\n'
96 'parts:\n',
97 fake_logger.output)
6098
=== modified file 'snapcraft/tests/test_copy_plugin.py'
--- snapcraft/tests/test_copy_plugin.py 2015-07-22 21:05:20 +0000
+++ snapcraft/tests/test_copy_plugin.py 2015-07-23 17:20:28 +0000
@@ -14,11 +14,11 @@
14# You should have received a copy of the GNU General Public License14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import logging
17import os.path18import os.path
18from unittest.mock import (19from unittest.mock import Mock
19 Mock,20
20 patch,21import fixtures
21)
2222
23from snapcraft.plugins.copy import CopyPlugin23from snapcraft.plugins.copy import CopyPlugin
24from snapcraft.tests import TestCase24from snapcraft.tests import TestCase
@@ -43,12 +43,17 @@
43 }43 }
44 open("zzz", "w").close()44 open("zzz", "w").close()
45 c = CopyPlugin("copy", self.mock_options)45 c = CopyPlugin("copy", self.mock_options)
46 with patch("snapcraft.common.log") as mock_log:46
47 res = c.build()47 fake_logger = fixtures.FakeLogger(level=logging.WARNING)
48 self.assertFalse(res)48 self.useFixture(fake_logger)
49 mock_log.assert_called_with("WARNING: file 'src' missing")49
50 res = c.build()
51 self.assertFalse(res)
52 self.assertEqual("WARNING: file 'src' missing\n", fake_logger.output)
5053
51 def test_copy_plugin_copies(self):54 def test_copy_plugin_copies(self):
55 self.useFixture(fixtures.FakeLogger())
56
52 self.mock_options.files = {57 self.mock_options.files = {
53 "src": "dst",58 "src": "dst",
54 }59 }
@@ -59,6 +64,8 @@
59 self.assertTrue(os.path.exists(os.path.join(self.dst_prefix, "dst")))64 self.assertTrue(os.path.exists(os.path.join(self.dst_prefix, "dst")))
6065
61 def test_copy_plugin_creates_prefixes(self):66 def test_copy_plugin_creates_prefixes(self):
67 self.useFixture(fixtures.FakeLogger())
68
62 self.mock_options.files = {69 self.mock_options.files = {
63 "src": "dir/dst",70 "src": "dir/dst",
64 }71 }
6572
=== modified file 'snapcraft/tests/test_plugin.py'
--- snapcraft/tests/test_plugin.py 2015-07-22 21:05:20 +0000
+++ snapcraft/tests/test_plugin.py 2015-07-23 17:20:28 +0000
@@ -14,25 +14,29 @@
14# You should have received a copy of the GNU General Public License14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import logging
17import os18import os
18import sys19import sys
19import tempfile20import tempfile
2021
22import fixtures
21from unittest.mock import (23from unittest.mock import (
22 Mock,24 Mock,
23 patch,25 patch,
24)26)
2527
28import snapcraft.tests.mock_plugin
26from snapcraft.plugin import PluginHandler, PluginError29from snapcraft.plugin import PluginHandler, PluginError
27from snapcraft.tests import TestCase30from snapcraft.tests import TestCase
2831
29import snapcraft.tests.mock_plugin
30
3132
32class TestPlugin(TestCase):33class TestPlugin(TestCase):
3334
35 def get_test_plugin(self):
36 return PluginHandler('mock', 'mock-part', {}, load_config=False, load_code=False)
37
34 def test_is_dirty(self):38 def test_is_dirty(self):
35 p = PluginHandler("mock", "mock-part", {}, load_config=False, load_code=False)39 p = self.get_test_plugin()
36 p.statefile = tempfile.NamedTemporaryFile().name40 p.statefile = tempfile.NamedTemporaryFile().name
37 self.addCleanup(os.remove, p.statefile)41 self.addCleanup(os.remove, p.statefile)
38 p.code = Mock()42 p.code = Mock()
@@ -45,7 +49,7 @@
45 self.assertFalse(p.code.pull.called)49 self.assertFalse(p.code.pull.called)
4650
47 def test_collect_snap_files(self):51 def test_collect_snap_files(self):
48 p = PluginHandler("mock", "mock-part", {}, load_config=False, load_code=False)52 p = self.get_test_plugin()
4953
50 tmpdirObject = tempfile.TemporaryDirectory()54 tmpdirObject = tempfile.TemporaryDirectory()
51 self.addCleanup(tmpdirObject.cleanup)55 self.addCleanup(tmpdirObject.cleanup)
@@ -92,6 +96,15 @@
92 set(['1', '1/1a', '1/1a/1b', '2', '2/2a']),96 set(['1', '1/1a', '1/1a/1b', '2', '2/2a']),
93 set()))97 set()))
9498
99 def test_notify_stage_must_log_information(self):
100 fake_logger = fixtures.FakeLogger(level=logging.INFO)
101 self.useFixture(fake_logger)
102
103 plugin = self.get_test_plugin()
104 plugin.notify_stage('test stage')
105
106 self.assertEqual('test stage mock-part\n', fake_logger.output)
107
95 def test_local_plugins(self):108 def test_local_plugins(self):
96 """Ensure local plugins are loaded from parts/plugins"""109 """Ensure local plugins are loaded from parts/plugins"""
97 def mock_import_modules(module_name):110 def mock_import_modules(module_name):
98111
=== modified file 'snapcraft/tests/test_yaml.py'
--- snapcraft/tests/test_yaml.py 2015-07-17 17:00:18 +0000
+++ snapcraft/tests/test_yaml.py 2015-07-23 17:20:28 +0000
@@ -14,15 +14,18 @@
14# You should have received a copy of the GNU General Public License14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import logging
17import os18import os
18import tempfile19import tempfile
19import unittest20import unittest
20from unittest.mock import (21
21 patch,22import fixtures
23
24from snapcraft import (
25 common,
26 dirs
22)27)
23
24from snapcraft.yaml import Config28from snapcraft.yaml import Config
25
26from snapcraft.tests import TestCase29from snapcraft.tests import TestCase
2730
2831
@@ -46,16 +49,27 @@
46 "package": "fswebcam",49 "package": "fswebcam",
47 })50 })
4851
49 @patch("snapcraft.common.log")52 def test_config_raises_on_missing_snapcraft_yaml(self):
50 def test_config_raises_on_missing_snapcraft_yaml(self, mock_log):53 fake_logger = fixtures.FakeLogger(level=logging.ERROR)
54 self.useFixture(fake_logger)
55
51 # no snapcraft.yaml56 # no snapcraft.yaml
52 with self.assertRaises(SystemExit):57 with self.assertRaises(SystemExit) as raised:
53 Config()58 Config()
54 mock_log.assert_called_with("""Could not find snapcraft.yaml. Are you sure you're in the right directory?59
55To start a new project, use 'snapcraft init'""")60 self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
5661 self.assertEqual(
57 @patch("snapcraft.common.log")62 "Could not find snapcraft.yaml. Are you sure you're in the right directory?\n"
58 def test_config_loop(self, mock_log):63 "To start a new project, use 'snapcraft init'\n",
64 fake_logger.output)
65
66 def test_config_loop(self):
67 self.addCleanup(common.set_plugindir, common.get_plugindir())
68 dirs.setup_dirs()
69
70 fake_logger = fixtures.FakeLogger(level=logging.ERROR)
71 self.useFixture(fake_logger)
72
59 self.make_snapcraft_yaml("""parts:73 self.make_snapcraft_yaml("""parts:
60 p1:74 p1:
61 plugin: ubuntu75 plugin: ubuntu
@@ -64,6 +78,8 @@
64 plugin: ubuntu78 plugin: ubuntu
65 after: [p1]79 after: [p1]
66""")80""")
67 with self.assertRaises(SystemExit):81 with self.assertRaises(SystemExit) as raised:
68 Config()82 Config()
69 mock_log.assert_called_with("Circular dependency chain!")83
84 self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
85 self.assertEqual('Circular dependency chain!\n', fake_logger.output)
7086
=== modified file 'snapcraft/yaml.py'
--- snapcraft/yaml.py 2015-07-22 13:57:19 +0000
+++ snapcraft/yaml.py 2015-07-23 17:20:28 +0000
@@ -14,11 +14,17 @@
14# You should have received a copy of the GNU General Public License14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import snapcraft.common17import logging
18import snapcraft.plugin
19import sys18import sys
19
20import yaml20import yaml
2121
22import snapcraft.plugin
23from snapcraft import common
24
25
26logger = logging.getLogger(__name__)
27
2228
23class Config:29class Config:
2430
@@ -31,7 +37,7 @@
31 with open("snapcraft.yaml", 'r') as fp:37 with open("snapcraft.yaml", 'r') as fp:
32 self.data = yaml.load(fp)38 self.data = yaml.load(fp)
33 except FileNotFoundError:39 except FileNotFoundError:
34 snapcraft.common.log("Could not find snapcraft.yaml. Are you sure you're in the right directory?\nTo start a new project, use 'snapcraft init'")40 logger.error("Could not find snapcraft.yaml. Are you sure you're in the right directory?\nTo start a new project, use 'snapcraft init'")
35 sys.exit(1)41 sys.exit(1)
36 self.build_tools = self.data.get('build-tools', [])42 self.build_tools = self.data.get('build-tools', [])
3743
@@ -75,7 +81,7 @@
75 foundIt = True81 foundIt = True
76 break82 break
77 if not foundIt:83 if not foundIt:
78 snapcraft.common.log("Could not find part name %s" % dep)84 logger.error("Could not find part name %s" % dep)
79 sys.exit(1)85 sys.exit(1)
8086
81 # Now sort them (this is super inefficient, but easy-ish to follow)87 # Now sort them (this is super inefficient, but easy-ish to follow)
@@ -92,7 +98,7 @@
92 topPart = part98 topPart = part
93 break99 break
94 if not topPart:100 if not topPart:
95 snapcraft.common.log("Circular dependency chain!")101 logger.error("Circular dependency chain!")
96 sys.exit(1)102 sys.exit(1)
97 sortedParts = [topPart] + sortedParts103 sortedParts = [topPart] + sortedParts
98 self.all_parts.remove(topPart)104 self.all_parts.remove(topPart)
@@ -132,7 +138,7 @@
132 return env138 return env
133139
134 def stage_env(self):140 def stage_env(self):
135 root = snapcraft.common.stagedir141 root = common.get_stagedir()
136 env = []142 env = []
137143
138 env += self.runtime_env(root)144 env += self.runtime_env(root)
@@ -143,7 +149,7 @@
143 return env149 return env
144150
145 def snap_env(self):151 def snap_env(self):
146 root = snapcraft.common.snapdir152 root = common.get_snapdir()
147 env = []153 env = []
148154
149 env += self.runtime_env(root)155 env += self.runtime_env(root)

Subscribers

People subscribed via source and target branches

to all changes: