Merge lp:~salgado/linaro-image-tools/port-populate_boot into lp:linaro-image-tools/11.11

Proposed by Guilherme Salgado
Status: Merged
Merged at revision: 182
Proposed branch: lp:~salgado/linaro-image-tools/port-populate_boot
Merge into: lp:linaro-image-tools/11.11
Diff against target: 468 lines (+252/-92)
4 files modified
linaro-media-create (+21/-88)
media_create/cmd_runner.py (+5/-4)
media_create/populate_boot.py (+128/-0)
media_create/tests/test_media_create.py (+98/-0)
To merge this branch: bzr merge lp:~salgado/linaro-image-tools/port-populate_boot
Reviewer Review Type Date Requested Status
Michael Hudson-Doyle (community) Approve
Review via email: mp+42670@code.launchpad.net

Description of the change

This branch ports the meat of populate_boot to python.

There are a few things left to port in that function, but this branch is
already quite big.

It can be reviewed as one big diff or two incremental ones:

  r175 refactors populate_boot into smaller shell functions
  r178 ports that to python, and even makes a few improvements

To post a comment you must log in.
Revision history for this message
Martin Ohlsson (martin-ohlson) wrote :

>=== modified file 'media_create/cmd_runner.py'
>--- media_create/cmd_runner.py 2010-12-01 19:08:25 +0000
>+++ media_create/cmd_runner.py 2010-12-03 19:01:21 +0000
>@@ -1,12 +1,13 @@
> import subprocess
>
>-def run(args, as_root=False):
>+def run(args, as_root=False, stdout=None):

Would it be possible to add stdin as a parameter to this function so the output from one command can be connected to the input of another? E.g. check_device() connects several commands using a pipe.

This implies also that stdout of the subprocess is returned in some way.

/Martin

Revision history for this message
Guilherme Salgado (salgado) wrote :

On Fri, 2010-12-03 at 19:57 +0000, Martin Ohlsson wrote:
> >=== modified file 'media_create/cmd_runner.py'
> >--- media_create/cmd_runner.py 2010-12-01 19:08:25 +0000
> >+++ media_create/cmd_runner.py 2010-12-03 19:01:21 +0000
> >@@ -1,12 +1,13 @@
> > import subprocess
> >
> >-def run(args, as_root=False):
> >+def run(args, as_root=False, stdout=None):
>
> Would it be possible to add stdin as a parameter to this function so the output from one command can be connected to the input of another? E.g. check_device() connects several commands using a pipe.

I suppose that's possible, but I wonder if there is a real advantage in
running each of the commands individually and pipe their stdin/stdout in
Python, or would this be a case we'd be better off by letting the shell
do that (i.e. run them all as a single subprocess.call(...,
shell=True))?

>
> This implies also that stdout of the subprocess is returned in some way.

I'm not sure I understand what you mean here.

Revision history for this message
Martin Ohlsson (martin-ohlson) wrote :

You're right, it would probably mess up the simplicity of the cmd_runner to make it handle everything. More complex cases might be better handled locally for those rare situations.

Revision history for this message
Michael Hudson-Doyle (mwhudson) wrote :

I haven't checked every last detail for correctness, but I approve of the overall structure. Let me know if you want me to check all the arguments to each invocation of mkImage -- it can be done, but is probably a paper and pencil job.

I wonder if it would be easier to read if populate_boot took --options rather than positional arguments. I guess it depends how you see the code developing.

Thanks for working on this, linaro-media-create just makes me want to cry and this helps :-)

review: Approve
Revision history for this message
Guilherme Salgado (salgado) wrote :

On Mon, 2010-12-06 at 22:21 +0000, Michael Hudson-Doyle wrote:
> Review: Approve
> I haven't checked every last detail for correctness, but I approve of
> the overall structure. Let me know if you want me to check all the
> arguments to each invocation of mkImage -- it can be done, but is
> probably a paper and pencil job.

I don't think that's necessary; I've checked it many times myself.

>
> I wonder if it would be easier to read if populate_boot took --options
> rather than positional arguments. I guess it depends how you see the
> code developing.

It'd probably be more readable, but soon enough we won't need any of
that as everything will be python, so I'd rather stick with whatever is
simpler until we get there.

>
> Thanks for working on this, linaro-media-create just makes me want to
> cry and this helps :-)

You're certainly not alone there.

Thanks for the review!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'linaro-media-create'
--- linaro-media-create 2010-11-30 12:35:27 +0000
+++ linaro-media-create 2010-12-03 19:01:21 +0000
@@ -535,9 +535,9 @@
535 echo ""535 echo ""
536 536
537 if [ "$IS_LIVE" -eq 1 ]; then537 if [ "$IS_LIVE" -eq 1 ]; then
538 parts_dir=casper538 parts_dir="${DIR}/binary/casper"
539 else539 else
540 parts_dir=boot540 parts_dir="${DIR}/binary/boot"
541 fi541 fi
542 542
543 mkdir -p "${BOOT_DISK}"543 mkdir -p "${BOOT_DISK}"
@@ -548,92 +548,9 @@
548 "${BOOT_DISK}"548 "${BOOT_DISK}"
549 fi549 fi
550550
551 case "$DEVIMAGE" in551 python -m media_create.populate_boot "$DEVIMAGE" "$SUB_ARCH" "$LOAD_ADDR" \
552 beagle|igep)552 "$parts_dir" "$BOOT_DISK" "$TMP_DIR" "$BOOT_SCRIPT" \
553 if [ "$DEVIMAGE" = "beagle" ]; then553 "${DEVICE:-$IMAGE_FILE}"
554 sudo cp -v binary/usr/lib/x-loader-omap/MLO "${BOOT_DISK}"
555 fi
556 sync
557 echo "done"
558
559 sudo mkimage -A arm -O linux -T kernel -C none -a 0x80008000 \
560 -e 0x80008000 -n Linux \
561 -d "${DIR}/binary/${parts_dir}"/vmlinuz-*-linaro-omap \
562 "${BOOT_DISK}/uImage"
563 sudo mkimage -A arm -O linux -T ramdisk -C none -a 0 \
564 -e 0 -n initramfs \
565 -d "${DIR}/binary/${parts_dir}"/initrd.img-*-linaro-omap \
566 "${BOOT_DISK}/uInitrd"
567 sudo mkimage -A arm -O linux -T script -C none -a 0 \
568 -e 0 -n "boot script" -d "${TMP_DIR}/boot.cmd" \
569 "${BOOT_DISK}/boot.scr"
570 sudo cp -v "${BOOT_DISK}/boot.scr" "${BOOT_DISK}/boot.ini"
571 ;;
572
573 panda)
574 sudo cp -v binary/usr/lib/x-loader-omap4/MLO "${BOOT_DISK}"
575 sync
576 echo "done"
577
578 sudo mkimage -A arm -O linux -T kernel -C none -a 0x80008000 \
579 -e 0x80008000 -n Linux \
580 -d "${DIR}/binary/${parts_dir}"/vmlinuz-*-omap4 \
581 "${BOOT_DISK}/uImage"
582 sudo mkimage -A arm -O linux -T ramdisk -C none -a 0 \
583 -e 0 -n initramfs \
584 -d "${DIR}/binary/${parts_dir}"/initrd.img-*-omap4 \
585 "${BOOT_DISK}/uInitrd"
586 sudo mkimage -A arm -O linux -T script -C none -a 0 \
587 -e 0 -n "boot script" -d "${TMP_DIR}/boot.cmd" \
588 "${BOOT_DISK}/boot.scr"
589 sudo cp -v "${BOOT_DISK}/boot.scr" "${BOOT_DISK}/boot.ini"
590 ;;
591
592 ux500)
593 sudo mkimage -A arm -O linux -T kernel -C none -a 0x00008000 \
594 -e 0x00008000 -n Linux \
595 -d "${DIR}/binary/${parts_dir}"/vmlinuz-*-ux500 \
596 "${BOOT_DISK}/uImage"
597 sudo mkimage -A arm -O linux -T ramdisk -C none -a 0 \
598 -e 0 -n initramfs \
599 -d "${DIR}/binary/${parts_dir}"/initrd.img-*-ux500 \
600 "${BOOT_DISK}/uInitrd"
601 sudo mkimage -A arm -O linux -T script -C none -a 0 \
602 -e 0 -n "boot script" -d "${TMP_DIR}/boot.cmd" \
603 "${BOOT_DISK}/flash.scr"
604 ;;
605
606 vexpress)
607 sudo mkimage -A arm -O linux -T kernel -C none -a "$KERNEL_ADDR" \
608 -e "$KERNEL_ADDR" -n Linux \
609 -d "${DIR}/binary/${parts_dir}"/vmlinuz-*-linaro-vexpress \
610 "${BOOT_DISK}/uImage"
611 sudo mkimage -A arm -O linux -T ramdisk -C none -a "$INITRD_ADDR" \
612 -e "$INITRD_ADDR" -n initramfs \
613 -d "${DIR}/binary/${parts_dir}"/initrd.img-*-linaro-vexpress \
614 "${BOOT_DISK}/uInitrd"
615 ;;
616
617 mx51evk)
618 sudo dd if=binary/usr/lib/u-boot/mx51evk/u-boot.imx of="${DEVICE:-$IMAGE_FILE}" \
619 bs=1024 seek=1 conv=notrunc
620 sudo mkimage -A arm -O linux -T kernel -C none -a 0x90008000 \
621 -e 0x90008000 -n Linux \
622 -d "${DIR}/binary/${parts_dir}"/vmlinuz-*-linaro-mx51 \
623 "${BOOT_DISK}/uImage"
624 sudo mkimage -A arm -O linux -T ramdisk -C none -a 0 \
625 -e 0 -n initramfs \
626 -d "${DIR}/binary/${parts_dir}"/initrd.img-*-linaro-mx51 \
627 "${BOOT_DISK}/uInitrd"
628 sudo mkimage -A arm -O linux -T script -C none -a 0 \
629 -e 0 -n "boot script" -d "${TMP_DIR}/boot.cmd" \
630 "${BOOT_DISK}/boot.scr"
631 ;;
632 *)
633 echo "Internal error; missing support for $DEVIMAGE" >&2
634 exit 1
635 ;;
636 esac
637554
638 sync555 sync
639 sync556 sync
@@ -798,6 +715,8 @@
798 ;;715 ;;
799esac716esac
800717
718# This whole thing will be moved into config files that will be specific for
719# each board.
801if [ "$DEVIMAGE" ]; then720if [ "$DEVIMAGE" ]; then
802 case "$DEVIMAGE" in721 case "$DEVIMAGE" in
803 beagle|igep)722 beagle|igep)
@@ -807,6 +726,9 @@
807 fi726 fi
808 KERNEL_ADDR="0x80000000"727 KERNEL_ADDR="0x80000000"
809 INITRD_ADDR="0x81600000"728 INITRD_ADDR="0x81600000"
729 LOAD_ADDR="0x80008000"
730 SUB_ARCH="linaro-omap"
731 BOOT_SCRIPT="boot.scr"
810 boot_args_options="$boot_args_options earlyprintk fixrtc nocompcache vram=12M omapfb.debug=y omapfb.mode=dvi:1280x720MR-16@60"732 boot_args_options="$boot_args_options earlyprintk fixrtc nocompcache vram=12M omapfb.debug=y omapfb.mode=dvi:1280x720MR-16@60"
811 ;;733 ;;
812 panda)734 panda)
@@ -816,6 +738,9 @@
816 fi738 fi
817 KERNEL_ADDR="0x80200000"739 KERNEL_ADDR="0x80200000"
818 INITRD_ADDR="0x81600000"740 INITRD_ADDR="0x81600000"
741 LOAD_ADDR="0x80008000"
742 SUB_ARCH="omap4"
743 BOOT_SCRIPT="boot.scr"
819 boot_args_options="$boot_args_options earlyprintk fixrtc nocompcache vram=32M omapfb.debug=y omapfb.vram=0:8M mem=463M ip=none"744 boot_args_options="$boot_args_options earlyprintk fixrtc nocompcache vram=32M omapfb.debug=y omapfb.vram=0:8M mem=463M ip=none"
820 ;;745 ;;
821 ux500)746 ux500)
@@ -825,6 +750,9 @@
825 fi750 fi
826 KERNEL_ADDR="0x00100000"751 KERNEL_ADDR="0x00100000"
827 INITRD_ADDR="0x08000000"752 INITRD_ADDR="0x08000000"
753 LOAD_ADDR="0x00008000"
754 SUB_ARCH="ux500"
755 BOOT_SCRIPT="flash.scr"
828 boot_args_options="$boot_args_options earlyprintk rootdelay=1 fixrtc nocompcache"756 boot_args_options="$boot_args_options earlyprintk rootdelay=1 fixrtc nocompcache"
829 boot_args_options="$boot_args_options mem=96M@0 mem_modem=32M@96M mem=44M@128M pmem=22M@172M mem=30M@194M mem_mali=32M@224M pmem_hwb=54M@256M hwmem=48M@302M mem=152M@360M"757 boot_args_options="$boot_args_options mem=96M@0 mem_modem=32M@96M mem=44M@128M pmem=22M@172M mem=30M@194M mem_mali=32M@224M pmem_hwb=54M@256M hwmem=48M@302M mem=152M@360M"
830 mmc_option="1:1"758 mmc_option="1:1"
@@ -836,6 +764,9 @@
836 fi764 fi
837 KERNEL_ADDR="0x90000000"765 KERNEL_ADDR="0x90000000"
838 INITRD_ADDR="0x90800000"766 INITRD_ADDR="0x90800000"
767 LOAD_ADDR="0x90008000"
768 SUB_ARCH="linaro-mx51"
769 BOOT_SCRIPT="boot.scr"
839 MMC_PART_OFFSET=1770 MMC_PART_OFFSET=1
840 mmc_option="0:2"771 mmc_option="0:2"
841 ;;772 ;;
@@ -846,6 +777,8 @@
846 fi777 fi
847 KERNEL_ADDR="0x60008000"778 KERNEL_ADDR="0x60008000"
848 INITRD_ADDR="0x81000000"779 INITRD_ADDR="0x81000000"
780 LOAD_ADDR=$KERNEL_ADDR
781 SUB_ARCH="linaro-vexpress"
849 # ARM Boot Monitor is used to load u-boot, uImage etc. into flash and782 # ARM Boot Monitor is used to load u-boot, uImage etc. into flash and
850 # only allows for FAT16783 # only allows for FAT16
851 FAT_SIZE=16784 FAT_SIZE=16
852785
=== modified file 'media_create/cmd_runner.py'
--- media_create/cmd_runner.py 2010-12-01 19:08:25 +0000
+++ media_create/cmd_runner.py 2010-12-03 19:01:21 +0000
@@ -1,12 +1,13 @@
1import subprocess1import subprocess
22
33
4def run(args, as_root=False):4def run(args, as_root=False, stdout=None):
5 """Run the given command as a sub process.5 """Run the given command as a sub process.
66
7 :param command: A list or tuple containing the command to run and the7 :param command: A list or tuple containing the command to run and the
8 arguments that should be passed to it.8 arguments that should be passed to it.
9 :param as_root: Should the given command be run as root (with sudo)?9 :param as_root: Should the given command be run as root (with sudo)?
10 :param stdout: Same as subprocess.Popen().
10 """11 """
11 assert isinstance(args, (list, tuple)), (12 assert isinstance(args, (list, tuple)), (
12 "The command to run must be a list or tuple, found: %s" % type(args))13 "The command to run must be a list or tuple, found: %s" % type(args))
@@ -15,15 +16,15 @@
15 if as_root:16 if as_root:
16 args = args[:]17 args = args[:]
17 args.insert(0, 'sudo')18 args.insert(0, 'sudo')
18 return_value = do_run(args)19 return_value = do_run(args, stdout=stdout)
19 if return_value != 0:20 if return_value != 0:
20 raise SubcommandNonZeroReturnValue(args, return_value)21 raise SubcommandNonZeroReturnValue(args, return_value)
21 return return_value22 return return_value
2223
2324
24def do_run(args):25def do_run(args, stdout=None):
25 """A wrapper around subprocess.call() to make testing easier."""26 """A wrapper around subprocess.call() to make testing easier."""
26 return subprocess.call(args)27 return subprocess.call(args, stdout=stdout)
2728
2829
29class SubcommandNonZeroReturnValue(Exception):30class SubcommandNonZeroReturnValue(Exception):
3031
=== added file 'media_create/populate_boot.py'
--- media_create/populate_boot.py 1970-01-01 00:00:00 +0000
+++ media_create/populate_boot.py 2010-12-03 19:01:21 +0000
@@ -0,0 +1,128 @@
1import glob
2import sys
3
4from media_create import cmd_runner
5
6
7def _run_mkimage(img_type, load_addr, entry_point, name, img_data, img,
8 stdout=None):
9 cmd = ['mkimage',
10 '-A', 'arm',
11 '-O', 'linux',
12 '-T', img_type,
13 '-C', 'none',
14 '-a', load_addr,
15 '-e', load_addr,
16 '-n', name,
17 '-d', img_data,
18 img]
19 return cmd_runner.run(cmd, as_root=True, stdout=stdout)
20
21
22def _get_file_matching(regex):
23 """Return a file whose path matches the given regex.
24
25 If zero or more than one files match, raise a ValueError.
26 """
27 files = glob.glob(regex)
28 if len(files) == 1:
29 return files[0]
30 elif len(files) == 0:
31 raise ValueError(
32 "No files found matching '%s'; can't continue" % regex)
33 else:
34 # TODO: Could ask the user to chosse which file to use instead of
35 # raising an exception.
36 raise ValueError("Too many files matching '%s' found." % regex)
37
38
39def make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk):
40 img_data = _get_file_matching(
41 '%s/vmlinuz-*-%s' % (uboot_parts_dir, sub_arch))
42 img = '%s/uImage' % boot_disk
43 return _run_mkimage(
44 'kernel', load_addr, load_addr, 'Linux', img_data, img)
45
46
47def make_uInitrd(uboot_parts_dir, sub_arch, boot_disk):
48 img_data = _get_file_matching(
49 '%s/initrd.img-*-%s' % (uboot_parts_dir, sub_arch))
50 img = '%s/uInitrd' % boot_disk
51 return _run_mkimage('ramdisk', '0', '0', 'initramfs', img_data, img)
52
53
54def make_boot_script(boot_script, tmp_dir):
55 img_data = '%s/boot.cmd' % tmp_dir
56 return _run_mkimage(
57 'script', '0', '0', 'boot script', img_data, boot_script)
58
59
60def install_mx51evk_boot_loader(imx_file, boot_device_or_file):
61 cmd_runner.run([
62 "dd",
63 "if=%s" % imx_file,
64 "of=%s" % boot_device_or_file,
65 "bs=1024",
66 "seek=1",
67 "conv=notrunc"], as_root=True)
68
69
70def install_omap_boot_loader(mlo_file, boot_disk):
71 cmd_runner.run(["cp", "-v", mlo_file, boot_disk], as_root=True)
72 # XXX: Is this really needed?
73 cmd_runner.run(["sync"])
74
75
76def make_boot_ini(boot_script, boot_disk):
77 cmd_runner.run(
78 ["cp", "-v", boot_script, "%s/boot.ini" % boot_disk], as_root=True)
79
80
81def populate_boot(board, sub_arch, load_addr, uboot_parts_dir, boot_disk,
82 tmp_dir, boot_script_name, boot_device_or_file):
83
84 boot_script = "%(boot_disk)s/%(boot_script_name)s" % (
85 dict(boot_disk=boot_disk, boot_script_name=boot_script_name))
86
87 # TODO: Once linaro-media-create is fully ported to python, we should
88 # split this into several board-specific functions that are defined
89 # somewhere else and just called here.
90 if board in ["beagle", "panda"]:
91 mlo_file = "binary/usr/lib/x-loader-omap/MLO"
92 if board == "panda":
93 mlo_file = "binary/usr/lib/x-loader-omap4/MLO"
94 install_omap_boot_loader(mlo_file, boot_disk)
95 make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
96 make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
97 make_boot_script(boot_script, tmp_dir)
98 make_boot_ini(boot_script, boot_disk)
99
100 elif board == "igep":
101 make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
102 make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
103 make_boot_script(boot_script, tmp_dir)
104 make_boot_ini(boot_script, boot_disk)
105
106 elif board == "ux500":
107 make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
108 make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
109 make_boot_script(boot_script, tmp_dir)
110
111 elif board == "vexpress":
112 make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
113 make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
114
115 elif board == "mx51evk":
116 install_mx51evk_boot_loader(
117 "binary/usr/lib/u-boot/mx51evk/u-boot.imx", boot_device_or_file)
118 make_uImage(load_addr, uboot_parts_dir, sub_arch, boot_disk)
119 make_uInitrd(uboot_parts_dir, sub_arch, boot_disk)
120 make_boot_script(boot_script, tmp_dir)
121
122 else:
123 raise AssertionError(
124 "Internal error; missing support for board: %s" % board)
125
126
127if __name__ == "__main__":
128 sys.exit(populate_boot(*sys.argv[1:]))
0129
=== modified file 'media_create/tests/test_media_create.py'
--- media_create/tests/test_media_create.py 2010-12-03 18:48:55 +0000
+++ media_create/tests/test_media_create.py 2010-12-03 19:01:21 +0000
@@ -1,7 +1,10 @@
1from contextlib import contextmanager1from contextlib import contextmanager
2import os2import os
3import random
4import string
3import subprocess5import subprocess
4import sys6import sys
7import tempfile
58
6from testtools import TestCase9from testtools import TestCase
710
@@ -11,6 +14,14 @@
11from media_create import cmd_runner14from media_create import cmd_runner
12from media_create import ensure_command15from media_create import ensure_command
1316
17from media_create import populate_boot
18from media_create.populate_boot import (
19 make_boot_script,
20 make_uImage,
21 make_uInitrd,
22 _get_file_matching,
23 _run_mkimage,
24 )
14from media_create.remove_binary_dir import remove_binary_dir25from media_create.remove_binary_dir import remove_binary_dir
15from media_create.unpack_binary_tarball import unpack_binary_tarball26from media_create.unpack_binary_tarball import unpack_binary_tarball
1627
@@ -18,6 +29,7 @@
18 CreateTempDirFixture,29 CreateTempDirFixture,
19 CreateTarballFixture,30 CreateTarballFixture,
20 MockDoRunFixture,31 MockDoRunFixture,
32 MockSomethingFixture,
21 )33 )
2234
2335
@@ -135,3 +147,89 @@
135 def test_do_run(self):147 def test_do_run(self):
136 return_code = cmd_runner.do_run('true')148 return_code = cmd_runner.do_run('true')
137 self.assertEqual(0, return_code)149 self.assertEqual(0, return_code)
150
151
152class TestPopulateBoot(TestCaseWithFixtures):
153
154 def _mock_get_file_matching(self):
155 self.useFixture(MockSomethingFixture(
156 populate_boot, '_get_file_matching',
157 lambda regex: regex))
158
159 def _mock_do_run(self):
160 fixture = MockDoRunFixture()
161 self.useFixture(fixture)
162 return fixture
163
164 def test_make_uImage(self):
165 self._mock_get_file_matching()
166 fixture = self._mock_do_run()
167 make_uImage('load_addr', 'parts_dir', 'sub_arch', 'boot_disk')
168 expected = [
169 'sudo', 'mkimage', '-A', 'arm', '-O', 'linux', '-T', 'kernel',
170 '-C', 'none', '-a', 'load_addr', '-e', 'load_addr', '-n', 'Linux',
171 '-d', 'parts_dir/vmlinuz-*-sub_arch', 'boot_disk/uImage']
172 self.assertEqual(expected, fixture.mock.args)
173
174 def test_make_uInitrd(self):
175 self._mock_get_file_matching()
176 fixture = self._mock_do_run()
177 make_uInitrd('parts_dir', 'sub_arch', 'boot_disk')
178 expected = [
179 'sudo', 'mkimage', '-A', 'arm', '-O', 'linux', '-T', 'ramdisk',
180 '-C', 'none', '-a', '0', '-e', '0', '-n', 'initramfs',
181 '-d', 'parts_dir/initrd.img-*-sub_arch', 'boot_disk/uInitrd']
182 self.assertEqual(expected, fixture.mock.args)
183
184 def test_make_boot_script(self):
185 self._mock_get_file_matching()
186 fixture = self._mock_do_run()
187 make_boot_script('boot_script', 'tmp_dir')
188 expected = [
189 'sudo', 'mkimage', '-A', 'arm', '-O', 'linux', '-T', 'script',
190 '-C', 'none', '-a', '0', '-e', '0', '-n', 'boot script',
191 '-d', 'tmp_dir/boot.cmd', 'boot_script']
192 self.assertEqual(expected, fixture.mock.args)
193
194 def test_get_file_matching(self):
195 prefix = ''.join(
196 random.choice(string.ascii_lowercase) for x in range(5))
197 file1 = self._create_temp_file_as_fixture(prefix)
198 directory = os.path.dirname(file1)
199 self.assertEqual(
200 file1, _get_file_matching('%s/%s*' % (directory, prefix)))
201
202 def test_get_file_matching_too_many_files_found(self):
203 prefix = ''.join(
204 random.choice(string.ascii_lowercase) for x in range(5))
205 file1 = self._create_temp_file_as_fixture(prefix)
206 file2 = self._create_temp_file_as_fixture(prefix)
207 directory = os.path.dirname(file1)
208 self.assertRaises(
209 ValueError, _get_file_matching, '%s/%s*' % (directory, prefix))
210
211 def test_get_file_matching_no_files_found(self):
212 self.assertRaises(
213 ValueError, _get_file_matching, '/foo/bar/baz/*non-existent')
214
215 def test_run_mkimage(self):
216 # Create a fake boot script.
217 filename = self._create_temp_file_as_fixture()
218 f = open(filename, 'w')
219 f.write("setenv bootcmd 'fatload mmc 0:1 0x80000000 uImage;\nboot")
220 f.close()
221
222 img = self._create_temp_file_as_fixture()
223 # Use that fake boot script to create a boot loader using mkimage.
224 # Send stdout to file as mkimage will print to stdout and we don't
225 # want that.
226 retval = _run_mkimage(
227 'script', '0', '0', 'boot script', filename, img,
228 stdout=open(self._create_temp_file_as_fixture(), 'w'))
229
230 self.assertEqual(0, retval)
231
232 def _create_temp_file_as_fixture(self, prefix='tmp'):
233 _, filename = tempfile.mkstemp(prefix=prefix)
234 self.addCleanup(os.unlink, filename)
235 return filename

Subscribers

People subscribed via source and target branches