=== modified file 'linaro-media-create'
@@ -22,7 +22,6 @@
import os
import sys
import tempfile
-import subprocess
from linaro_image_tools import cmd_runner
@@ -43,7 +42,11 @@
unpack_binary_tarball,
)
from linaro_image_tools.media_create import get_args_parser
-from linaro_image_tools.utils import ensure_command, is_arm_host
+from linaro_image_tools.utils import (
+ ensure_command,
+ is_arm_host,
+ verify_file_integrity,
+ )
# Just define the global variables
TMP_DIR = None
@@ -105,15 +108,9 @@
ensure_required_commands(args)
sig_file_list = args.hwpacksigs[:]
- verified_files = []
if args.binarysig is not None:
sig_file_list.append(args.binarysig)
- for sig_file in sig_file_list:
- hash_file = sig_file[0:-len('.asc')]
- cmd_runner.run(['gpg', '--verify', sig_file]).wait()
- sha1sums_out, _ = cmd_runner.run(['sha1sum', '-c', hash_file],
- stdout=subprocess.PIPE).communicate()
- verified_files.extend(sha1sums_out.replace(': OK', '').splitlines())
+ verified_files = verify_file_integrity(sig_file_list)
for verified_file in verified_files:
print 'Hash verification of file %s OK.' % verified_file
=== modified file 'linaro_image_tools/cmd_runner.py'
@@ -36,7 +36,7 @@
def run(args, as_root=False, chroot=None, stdin=None, stdout=None,
- stderr=None):
+ stderr=None, cwd=None):
"""Run the given command as a sub process.
Return a Popen instance.
@@ -60,7 +60,7 @@
as_root = True
if as_root and os.getuid() != 0:
args = SUDO_ARGS + args
- return Popen(args, stdin=stdin, stdout=stdout, stderr=stderr)
+ return Popen(args, stdin=stdin, stdout=stdout, stderr=stderr, cwd=cwd)
class Popen(subprocess.Popen):
=== modified file 'linaro_image_tools/tests/test_pyflakes.py'
@@ -29,8 +29,8 @@
(stdout, stderr) = proc.communicate()
stdout = stdout.splitlines()
stdout.sort()
- expected = ["./linaro_image_tools/utils.py:26: redefinition of "
- "unused 'CommandNotFound' from line 24" ]
+ expected = ["./linaro_image_tools/utils.py:27: redefinition of "
+ "unused 'CommandNotFound' from line 25" ]
self.assertEquals(expected, stdout)
self.assertEquals('', stderr)
=== modified file 'linaro_image_tools/tests/test_utils.py'
@@ -35,12 +35,49 @@
install_package_providing,
preferred_tools_dir,
UnableToFindPackageProvidingCommand,
+ verify_file_integrity,
)
sudo_args = " ".join(cmd_runner.SUDO_ARGS)
+class TestVerifyFileIntegrity(TestCaseWithFixtures):
+
+ filenames_in_shafile = ['verified-file1', 'verified-file2']
+
+ class MockCmdRunnerPopen(object):
+ def __call__(self, cmd, *args, **kwargs):
+ self.returncode = 0
+ return self
+
+ def communicate(self, input=None):
+ self.wait()
+ return ': OK\n'.join(
+ TestVerifyFileIntegrity.filenames_in_shafile) + ': OK\n', ''
+
+ def wait(self):
+ return self.returncode
+
+ def test_verify_files(self):
+ fixture = self.useFixture(MockCmdRunnerPopenFixture())
+ hash_filename = "dummy-file.txt"
+ signature_filename = hash_filename + ".asc"
+ verify_file_integrity([signature_filename])
+ self.assertEqual(
+ ['gpg --verify %s' % signature_filename,
+ 'sha1sum -c %s' % hash_filename],
+ fixture.mock.commands_executed)
+
+ def test_verify_files_returns_files(self):
+ self.useFixture(MockSomethingFixture(cmd_runner, 'Popen',
+ self.MockCmdRunnerPopen()))
+ hash_filename = "dummy-file.txt"
+ signature_filename = hash_filename + ".asc"
+ verified_files = verify_file_integrity([signature_filename])
+ self.assertEqual(self.filenames_in_shafile, verified_files)
+
+
class TestEnsureCommand(TestCaseWithFixtures):
install_pkg_providing_called = False
=== modified file 'linaro_image_tools/utils.py'
@@ -19,6 +19,7 @@
import os
import platform
+import subprocess
try:
from CommandNotFound import CommandNotFound
@@ -28,6 +29,31 @@
from linaro_image_tools import cmd_runner
+def verify_file_integrity(sig_file_list):
+ """Verify a list of signature files.
+
+ The parameter is a list of filenames of gpg signature files which will be
+ verified using gpg. For each of the files it is assumed that there is an
+ sha1 hash file with the same file name minus the '.asc' extension.
+
+ Each of the sha1 files will be checked using sha1sums. All files listed in
+ the sha1 hash file must be found in the same directory as the hash file.
+ """
+ verified_files = []
+ for sig_file in sig_file_list:
+ hash_file = sig_file[0:-len('.asc')]
+ cmd_runner.run(['gpg', '--verify', sig_file]).wait()
+ if os.path.dirname(hash_file) == '':
+ sha_cwd = None
+ else:
+ sha_cwd = os.path.dirname(hash_file)
+ sha1sums_out, _ = cmd_runner.run(['sha1sum', '-c', hash_file],
+ stdout=subprocess.PIPE, cwd=sha_cwd
+ ).communicate()
+ verified_files.extend(sha1sums_out.replace(': OK', '').splitlines())
+ return verified_files
+
+
def install_package_providing(command):
"""Install a package which provides the given command.