Merge lp:~mabac/linaro-image-tools/chdir-for-hashverify into lp:linaro-image-tools/11.11

Proposed by Mattias Backman
Status: Merged
Approved by: James Westby
Approved revision: 362
Merged at revision: 360
Proposed branch: lp:~mabac/linaro-image-tools/chdir-for-hashverify
Merge into: lp:linaro-image-tools/11.11
Diff against target: 175 lines (+73/-13)
5 files modified
linaro-media-create (+6/-9)
linaro_image_tools/cmd_runner.py (+2/-2)
linaro_image_tools/tests/test_pyflakes.py (+2/-2)
linaro_image_tools/tests/test_utils.py (+37/-0)
linaro_image_tools/utils.py (+26/-0)
To merge this branch: bzr merge lp:~mabac/linaro-image-tools/chdir-for-hashverify
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+65221@code.launchpad.net

Description of the change

Hi,

This change makes sure that the sha1sums command in linaro-media-create is run from the directory of the hash file to be verified.

This is needed for instance for FetchImage which will pass the full path of the hash file to l-m-c. I have found no way to make sha1sums check for the files in anything but the current working dir.

The change is implemented by adding a WorkingDirChanger object that acts as a context manager (the thing with the __enter__ and __exit__ methods which should make sure that the old wd is restored just after the call to sha1sums.

Thanks,

Mattias

To post a comment you must log in.
Revision history for this message
James Westby (james-w) wrote :

Hi Mattias,

Are you aware of the cwd argument to cmd_runner.run (via subprocess)?

Would that be suitable for changing the directory here?

Thanks,

James

Revision history for this message
Mattias Backman (mabac) wrote :

On Mon, Jun 20, 2011 at 10:21 PM, James Westby
<email address hidden> wrote:
> Hi Mattias,
>
> Are you aware of the cwd argument to cmd_runner.run (via subprocess)?
>
> Would that be suitable for changing the directory here?

That's exactly what I need. Thank you for pointing me to it!

359. By Mattias Backman

Add cwd parameter to cmd_runner.run() and use it for the sha1 verification in linaro-media-create.

360. By Mattias Backman

Move file verification from main into a utils function.

361. By Mattias Backman

Add basic tests for the file verification.

362. By Mattias Backman

Adjust pyflakes test to reflect that a source line has been added.

Revision history for this message
Mattias Backman (mabac) wrote :

Now, this uses the subprocess cwd parameter instead. I also moved most of the signature verification code into utils and added two basic tests for it.

Revision history for this message
James Westby (james-w) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'linaro-media-create'
2--- linaro-media-create 2011-06-17 12:47:16 +0000
3+++ linaro-media-create 2011-06-21 09:23:27 +0000
4@@ -22,7 +22,6 @@
5 import os
6 import sys
7 import tempfile
8-import subprocess
9
10 from linaro_image_tools import cmd_runner
11
12@@ -43,7 +42,11 @@
13 unpack_binary_tarball,
14 )
15 from linaro_image_tools.media_create import get_args_parser
16-from linaro_image_tools.utils import ensure_command, is_arm_host
17+from linaro_image_tools.utils import (
18+ ensure_command,
19+ is_arm_host,
20+ verify_file_integrity,
21+ )
22
23 # Just define the global variables
24 TMP_DIR = None
25@@ -105,15 +108,9 @@
26 ensure_required_commands(args)
27
28 sig_file_list = args.hwpacksigs[:]
29- verified_files = []
30 if args.binarysig is not None:
31 sig_file_list.append(args.binarysig)
32- for sig_file in sig_file_list:
33- hash_file = sig_file[0:-len('.asc')]
34- cmd_runner.run(['gpg', '--verify', sig_file]).wait()
35- sha1sums_out, _ = cmd_runner.run(['sha1sum', '-c', hash_file],
36- stdout=subprocess.PIPE).communicate()
37- verified_files.extend(sha1sums_out.replace(': OK', '').splitlines())
38+ verified_files = verify_file_integrity(sig_file_list)
39 for verified_file in verified_files:
40 print 'Hash verification of file %s OK.' % verified_file
41
42
43=== modified file 'linaro_image_tools/cmd_runner.py'
44--- linaro_image_tools/cmd_runner.py 2011-04-04 10:38:07 +0000
45+++ linaro_image_tools/cmd_runner.py 2011-06-21 09:23:27 +0000
46@@ -36,7 +36,7 @@
47
48
49 def run(args, as_root=False, chroot=None, stdin=None, stdout=None,
50- stderr=None):
51+ stderr=None, cwd=None):
52 """Run the given command as a sub process.
53
54 Return a Popen instance.
55@@ -60,7 +60,7 @@
56 as_root = True
57 if as_root and os.getuid() != 0:
58 args = SUDO_ARGS + args
59- return Popen(args, stdin=stdin, stdout=stdout, stderr=stderr)
60+ return Popen(args, stdin=stdin, stdout=stdout, stderr=stderr, cwd=cwd)
61
62
63 class Popen(subprocess.Popen):
64
65=== modified file 'linaro_image_tools/tests/test_pyflakes.py'
66--- linaro_image_tools/tests/test_pyflakes.py 2011-05-10 15:43:32 +0000
67+++ linaro_image_tools/tests/test_pyflakes.py 2011-06-21 09:23:27 +0000
68@@ -29,8 +29,8 @@
69 (stdout, stderr) = proc.communicate()
70 stdout = stdout.splitlines()
71 stdout.sort()
72- expected = ["./linaro_image_tools/utils.py:26: redefinition of "
73- "unused 'CommandNotFound' from line 24" ]
74+ expected = ["./linaro_image_tools/utils.py:27: redefinition of "
75+ "unused 'CommandNotFound' from line 25" ]
76 self.assertEquals(expected, stdout)
77 self.assertEquals('', stderr)
78
79
80=== modified file 'linaro_image_tools/tests/test_utils.py'
81--- linaro_image_tools/tests/test_utils.py 2011-03-24 18:41:59 +0000
82+++ linaro_image_tools/tests/test_utils.py 2011-06-21 09:23:27 +0000
83@@ -35,12 +35,49 @@
84 install_package_providing,
85 preferred_tools_dir,
86 UnableToFindPackageProvidingCommand,
87+ verify_file_integrity,
88 )
89
90
91 sudo_args = " ".join(cmd_runner.SUDO_ARGS)
92
93
94+class TestVerifyFileIntegrity(TestCaseWithFixtures):
95+
96+ filenames_in_shafile = ['verified-file1', 'verified-file2']
97+
98+ class MockCmdRunnerPopen(object):
99+ def __call__(self, cmd, *args, **kwargs):
100+ self.returncode = 0
101+ return self
102+
103+ def communicate(self, input=None):
104+ self.wait()
105+ return ': OK\n'.join(
106+ TestVerifyFileIntegrity.filenames_in_shafile) + ': OK\n', ''
107+
108+ def wait(self):
109+ return self.returncode
110+
111+ def test_verify_files(self):
112+ fixture = self.useFixture(MockCmdRunnerPopenFixture())
113+ hash_filename = "dummy-file.txt"
114+ signature_filename = hash_filename + ".asc"
115+ verify_file_integrity([signature_filename])
116+ self.assertEqual(
117+ ['gpg --verify %s' % signature_filename,
118+ 'sha1sum -c %s' % hash_filename],
119+ fixture.mock.commands_executed)
120+
121+ def test_verify_files_returns_files(self):
122+ self.useFixture(MockSomethingFixture(cmd_runner, 'Popen',
123+ self.MockCmdRunnerPopen()))
124+ hash_filename = "dummy-file.txt"
125+ signature_filename = hash_filename + ".asc"
126+ verified_files = verify_file_integrity([signature_filename])
127+ self.assertEqual(self.filenames_in_shafile, verified_files)
128+
129+
130 class TestEnsureCommand(TestCaseWithFixtures):
131
132 install_pkg_providing_called = False
133
134=== modified file 'linaro_image_tools/utils.py'
135--- linaro_image_tools/utils.py 2011-05-10 15:42:59 +0000
136+++ linaro_image_tools/utils.py 2011-06-21 09:23:27 +0000
137@@ -19,6 +19,7 @@
138
139 import os
140 import platform
141+import subprocess
142
143 try:
144 from CommandNotFound import CommandNotFound
145@@ -28,6 +29,31 @@
146 from linaro_image_tools import cmd_runner
147
148
149+def verify_file_integrity(sig_file_list):
150+ """Verify a list of signature files.
151+
152+ The parameter is a list of filenames of gpg signature files which will be
153+ verified using gpg. For each of the files it is assumed that there is an
154+ sha1 hash file with the same file name minus the '.asc' extension.
155+
156+ Each of the sha1 files will be checked using sha1sums. All files listed in
157+ the sha1 hash file must be found in the same directory as the hash file.
158+ """
159+ verified_files = []
160+ for sig_file in sig_file_list:
161+ hash_file = sig_file[0:-len('.asc')]
162+ cmd_runner.run(['gpg', '--verify', sig_file]).wait()
163+ if os.path.dirname(hash_file) == '':
164+ sha_cwd = None
165+ else:
166+ sha_cwd = os.path.dirname(hash_file)
167+ sha1sums_out, _ = cmd_runner.run(['sha1sum', '-c', hash_file],
168+ stdout=subprocess.PIPE, cwd=sha_cwd
169+ ).communicate()
170+ verified_files.extend(sha1sums_out.replace(': OK', '').splitlines())
171+ return verified_files
172+
173+
174 def install_package_providing(command):
175 """Install a package which provides the given command.
176

Subscribers

People subscribed via source and target branches