diff mbox

[Branch,~linaro-image-tools/linaro-image-tools/trunk] Rev 558: [merge] Implement 'copy_files' field support in linaro-media-create.

Message ID 20120904172313.23808.17024.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Paul Sokolovsky Sept. 4, 2012, 5:23 p.m. UTC
Merge authors:
  Paul Sokolovsky (pfalcon)
Related merge proposals:
  https://code.launchpad.net/~pfalcon/linaro-image-tools/copy_files-lmc/+merge/122686
  proposed by: Paul Sokolovsky (pfalcon)
  review: Approve - James Tunnicliffe (dooferlad)
------------------------------------------------------------
revno: 558 [merge]
committer: Paul Sokolovsky <paul.sokolovsky@linaro.org>
branch nick: trunk
timestamp: Tue 2012-09-04 20:22:04 +0300
message:
  [merge] Implement 'copy_files' field support in linaro-media-create.
modified:
  linaro_image_tools/media_create/boards.py
  linaro_image_tools/media_create/tests/test_media_create.py


--
lp:linaro-image-tools
https://code.launchpad.net/~linaro-image-tools/linaro-image-tools/trunk

You are subscribed to branch lp:linaro-image-tools.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-image-tools/linaro-image-tools/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'linaro_image_tools/media_create/boards.py'
--- linaro_image_tools/media_create/boards.py	2012-09-03 08:25:05 +0000
+++ linaro_image_tools/media_create/boards.py	2012-09-04 14:38:45 +0000
@@ -202,11 +202,26 @@ 
         return format
 
     def get_file(self, file_alias):
-        file_name, hwpack_tarfile = self.get_field(file_alias)
-        if file_name is not None:
-            hwpack_tarfile.extract(file_name, self.tempdir)
-            file_name = os.path.join(self.tempdir, file_name)
-        return file_name
+        """Get file(s) from a hwpack.
+        :param file_alias: Property name (not field name) which contains
+                           file reference(s)
+        :return: path to a file or list of paths to files
+        """
+        file_names, hwpack_tarfile = self.get_field(file_alias)
+        if not file_names:
+            return file_names
+        single = False
+        if not isinstance(args, list):
+            single = True
+            file_names = [file_names]
+        out_files = []
+        for f in file_names:
+            hwpack_tarfile.extract(f, self.tempdir)
+            f = os.path.join(self.tempdir, f)
+            out_files.append(f)
+        if single:
+            return out_files[0]
+        return out_files
 
 
 class BoardConfig(object):
@@ -778,8 +793,9 @@ 
         bootloader_parts_dir = os.path.join(chroot_dir, parts_dir)
         cmd_runner.run(['mkdir', '-p', boot_disk]).wait()
         with partition_mounted(boot_partition, boot_disk):
-            if cls.bootloader_file_in_boot_part:
-                with cls.hardwarepack_handler:
+            boot_files = []
+            with cls.hardwarepack_handler:
+                if cls.bootloader_file_in_boot_part:
                     # <legacy v1 support>
                     if cls.bootloader_flavor is not None:
                         default = os.path.join(
@@ -797,8 +813,15 @@ 
                     assert bootloader_bin is not None, (
                         "bootloader binary could not be found")
 
+                    boot_files.append(bootloader_bin)
+
+                copy_files = cls.get_file('boot_copy_files')
+                if copy_files:
+                    boot_files.extend(copy_files)
+
+                for f in boot_files:
                     proc = cmd_runner.run(
-                        ['cp', '-v', bootloader_bin, boot_disk], as_root=True)
+                        ['cp', '-v', f, boot_disk], as_root=True)
                     proc.wait()
 
             cls.make_boot_files(

=== modified file 'linaro_image_tools/media_create/tests/test_media_create.py'
--- linaro_image_tools/media_create/tests/test_media_create.py	2012-08-05 09:22:32 +0000
+++ linaro_image_tools/media_create/tests/test_media_create.py	2012-09-04 13:53:43 +0000
@@ -309,6 +309,37 @@ 
             test_file = hp.get_file('bootloader_file')
             self.assertEquals(data, open(test_file, 'r').read())
 
+    def test_get_file_v3(self):
+        # Test that get_file() works as expected with hwpackv3 and
+        # supports its new file fields.
+        metadata = textwrap.dedent("""\
+        format: 3.0
+        name: ahwpack
+        version: 4
+        architecture: armel
+        origin: linaro
+        bootloaders:
+         u_boot:
+          file: a_file
+          copy_files:
+           - file1
+           - file2
+         uefi:
+          file: b_file
+        """)
+        files = {'FORMAT': '3.0\n', 'metadata': metadata,
+                 'a_file': 'a_file content', 'file1': 'file1 content',
+                 'file2': 'file2 content'}
+        tarball = self.add_to_tarball(files.items())
+        hp = HardwarepackHandler([tarball], bootloader='u_boot')
+        with hp:
+            test_file = hp.get_file('bootloader_file')
+            self.assertEquals(files['a_file'], open(test_file, 'r').read())
+            test_files = hp.get_file('boot_copy_files')
+            self.assertEquals(len(test_files), 2)
+            self.assertEquals(files['file1'], open(test_files[0], 'r').read())
+            self.assertEquals(files['file2'], open(test_files[1], 'r').read())
+
 
 class TestSetMetadata(TestCaseWithFixtures):
 
@@ -2907,6 +2938,21 @@ 
         self.useFixture(MockSomethingFixture(
             self.config, 'make_boot_files', self.save_args))
 
+    def prepare_config_v3(self, config):
+        class c(config):
+            pass
+
+        self.config = c
+        self.config.boot_script = 'boot_script'
+        self.config.hardwarepack_handler = \
+            TestSetMetadata.MockHardwarepackHandler('ahwpack.tar.gz')
+        self.config.hardwarepack_handler.get_format = lambda: '3.0'
+        self.config.hardwarepack_handler.get_file = \
+                            lambda file_alias: ['file1', 'file2']
+        self.popen_fixture = self.useFixture(MockCmdRunnerPopenFixture())
+        self.useFixture(MockSomethingFixture(
+            self.config, 'make_boot_files', self.save_args))
+
     def call_populate_boot(self, config, is_live=False):
         config.populate_boot(
             'chroot_dir', 'rootfs_id', 'boot_partition', 'boot_disk',
@@ -2957,6 +3003,24 @@ 
             expected_calls, self.popen_fixture.mock.commands_executed)
         self.assertEquals(self.expected_args, self.saved_args)
 
+    def test_populate_boot_copy_files(self):
+        self.prepare_config_v3(boards.BoardConfig)
+        self.config.bootloader_flavor = "bootloader_flavor"
+        # Test that copy_files works per spec (puts stuff in boot partition)
+        # even if bootloader not in_boot_part.
+        self.config.bootloader_file_in_boot_part = False
+        self.call_populate_boot(self.config)
+        expected_calls = self.expected_calls[:]
+        expected_calls.insert(2,
+            '%s cp -v file1 '
+            'boot_disk' % sudo_args)
+        expected_calls.insert(3,
+            '%s cp -v file2 '
+            'boot_disk' % sudo_args)
+        self.assertEquals(
+            expected_calls, self.popen_fixture.mock.commands_executed)
+        self.assertEquals(self.expected_args, self.saved_args)
+
     def test_populate_boot_no_bootloader_flavor(self):
         self.prepare_config(boards.BoardConfig)
         self.config.bootloader_file_in_boot_part = True