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

Proposed by Guilherme Salgado
Status: Merged
Approved by: Martin Ohlsson
Approved revision: 171
Merged at revision: 172
Proposed branch: lp:~salgado/linaro-image-tools/port-ensure_command
Merge into: lp:linaro-image-tools/11.11
Prerequisite: lp:~salgado/linaro-image-tools/port-create_boot_cmd
Diff against target: 119 lines (+55/-17)
3 files modified
linaro-media-create (+12/-17)
media_create/ensure_command.py (+17/-0)
media_create/tests/test_media_create.py (+26/-0)
To merge this branch: bzr merge lp:~salgado/linaro-image-tools/port-ensure_command
Reviewer Review Type Date Requested Status
Martin Ohlsson (community) Approve
Review via email: mp+41579@code.launchpad.net

Description of the change

Port ensure_command to python.

I've used os.system() to call 'which' and 'apt-get', but I'm happy to change that to a subprocess.Popen() if anybody feels it's worth doing so.

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

Hi Guilherme,

I think code looks fine. I had to do some reading on context managers to be able to fully understand it ;)

/Martin

review: Approve

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-23 12:02:34 +0000
+++ linaro-media-create 2010-11-23 12:02:34 +0000
@@ -70,13 +70,8 @@
70# off of a l-m-c working tree.70# off of a l-m-c working tree.
71export PYTHONPATH="$script_dir"71export PYTHONPATH="$script_dir"
7272
73ensure_command() {73python -m media_create.ensure_command sfdisk util-linux
74 # ensure_command foo foo-package74python -m media_create.ensure_command fdisk util-linux
75 which "$1" 2>/dev/null 1>/dev/null || ( echo "Installing required command $1 from package $2" && sudo apt-get install "$2" )
76}
77
78ensure_command sfdisk util-linux
79ensure_command fdisk util-linux
8075
81usage() {76usage() {
82 echo "usage: $(basename $0) --mmc /dev/sdd"77 echo "usage: $(basename $0) --mmc /dev/sdd"
@@ -281,18 +276,18 @@
281276
282DEPLOY_STEPS="$PARTITION_STEP prepare_partitions $BOOTFS_STEP $ROOTFS_STEP"277DEPLOY_STEPS="$PARTITION_STEP prepare_partitions $BOOTFS_STEP $ROOTFS_STEP"
283278
284ensure_command mkimage uboot-mkimage279python -m media_create.ensure_command mkimage uboot-mkimage
285ensure_command uuidgen uuid-runtime280python -m media_create.ensure_command uuidgen uuid-runtime
286ensure_command parted parted281python -m media_create.ensure_command parted parted
287ensure_command wget wget282python -m media_create.ensure_command wget wget
288ensure_command md5sum coreutils283python -m media_create.ensure_command md5sum coreutils
289ensure_command realpath realpath284python -m media_create.ensure_command realpath realpath
290case "$RFS" in285case "$RFS" in
291 ext2|ext3|ext4)286 ext2|ext3|ext4)
292 ensure_command "mkfs.$RFS" e2fsprogs287 python -m media_create.ensure_command "mkfs.$RFS" e2fsprogs
293 ;;288 ;;
294 btrfs)289 btrfs)
295 ensure_command mkfs.btrfs btrfs-tools290 python -m media_create.ensure_command mkfs.btrfs btrfs-tools
296 ;;291 ;;
297esac292esac
298293
@@ -356,8 +351,8 @@
356 arch_is_arm=yes351 arch_is_arm=yes
357 ;;352 ;;
358 *)353 *)
359 ensure_command qemu-arm-static qemu-arm-static354 python -m media_create.ensure_command qemu-arm-static qemu-arm-static
360 ensure_command qemu-img qemu-kvm355 python -m media_create.ensure_command qemu-img qemu-kvm
361 sudo cp /usr/bin/qemu-arm-static ${chroot}/usr/bin356 sudo cp /usr/bin/qemu-arm-static ${chroot}/usr/bin
362 ;;357 ;;
363 esac358 esac
364359
=== added file 'media_create/ensure_command.py'
--- media_create/ensure_command.py 1970-01-01 00:00:00 +0000
+++ media_create/ensure_command.py 2010-11-23 12:02:34 +0000
@@ -0,0 +1,17 @@
1import os
2import sys
3
4
5def apt_get_install(command, package):
6 print ("Installing required command %s from package %s"
7 % (command, package))
8 os.system('sudo apt-get install %s' % package)
9
10
11def ensure_command(command, package):
12 if os.system('which %s 2>/dev/null 1>/dev/null' % command) != 0:
13 apt_get_install(command, package)
14
15
16if __name__ == '__main__':
17 ensure_command(sys.argv[1], sys.argv[2])
018
=== modified file 'media_create/tests/test_media_create.py'
--- media_create/tests/test_media_create.py 2010-11-23 12:02:34 +0000
+++ media_create/tests/test_media_create.py 2010-11-23 12:02:34 +0000
@@ -1,9 +1,35 @@
1from contextlib import contextmanager
1import subprocess2import subprocess
2import sys3import sys
34
4from testtools import TestCase5from testtools import TestCase
56
6from media_create.boot_cmd import create_boot_cmd7from media_create.boot_cmd import create_boot_cmd
8from media_create import ensure_command
9
10
11class TestEnsureCommand(TestCase):
12
13 apt_get_called = False
14
15 def test_command_already_present(self):
16 with self.mock_apt_get_install():
17 ensure_command.ensure_command('apt-get', 'apt')
18 self.assertFalse(self.apt_get_called)
19
20 def test_command_not_present(self):
21 with self.mock_apt_get_install():
22 ensure_command.ensure_command('apt-get-two-o', 'apt-2')
23 self.assertTrue(self.apt_get_called)
24
25 @contextmanager
26 def mock_apt_get_install(self):
27 def mock_apt_get_install(cmd, pkg):
28 self.apt_get_called = True
29 orig_func = ensure_command.apt_get_install
30 ensure_command.apt_get_install = mock_apt_get_install
31 yield
32 ensure_command.apt_get_install = orig_func
733
834
9class TestCreateBootCMD(TestCase):35class TestCreateBootCMD(TestCase):

Subscribers

People subscribed via source and target branches