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
1=== modified file 'linaro-media-create'
2--- linaro-media-create 2010-11-23 12:02:34 +0000
3+++ linaro-media-create 2010-11-23 12:02:34 +0000
4@@ -70,13 +70,8 @@
5 # off of a l-m-c working tree.
6 export PYTHONPATH="$script_dir"
7
8-ensure_command() {
9- # ensure_command foo foo-package
10- which "$1" 2>/dev/null 1>/dev/null || ( echo "Installing required command $1 from package $2" && sudo apt-get install "$2" )
11-}
12-
13-ensure_command sfdisk util-linux
14-ensure_command fdisk util-linux
15+python -m media_create.ensure_command sfdisk util-linux
16+python -m media_create.ensure_command fdisk util-linux
17
18 usage() {
19 echo "usage: $(basename $0) --mmc /dev/sdd"
20@@ -281,18 +276,18 @@
21
22 DEPLOY_STEPS="$PARTITION_STEP prepare_partitions $BOOTFS_STEP $ROOTFS_STEP"
23
24-ensure_command mkimage uboot-mkimage
25-ensure_command uuidgen uuid-runtime
26-ensure_command parted parted
27-ensure_command wget wget
28-ensure_command md5sum coreutils
29-ensure_command realpath realpath
30+python -m media_create.ensure_command mkimage uboot-mkimage
31+python -m media_create.ensure_command uuidgen uuid-runtime
32+python -m media_create.ensure_command parted parted
33+python -m media_create.ensure_command wget wget
34+python -m media_create.ensure_command md5sum coreutils
35+python -m media_create.ensure_command realpath realpath
36 case "$RFS" in
37 ext2|ext3|ext4)
38- ensure_command "mkfs.$RFS" e2fsprogs
39+ python -m media_create.ensure_command "mkfs.$RFS" e2fsprogs
40 ;;
41 btrfs)
42- ensure_command mkfs.btrfs btrfs-tools
43+ python -m media_create.ensure_command mkfs.btrfs btrfs-tools
44 ;;
45 esac
46
47@@ -356,8 +351,8 @@
48 arch_is_arm=yes
49 ;;
50 *)
51- ensure_command qemu-arm-static qemu-arm-static
52- ensure_command qemu-img qemu-kvm
53+ python -m media_create.ensure_command qemu-arm-static qemu-arm-static
54+ python -m media_create.ensure_command qemu-img qemu-kvm
55 sudo cp /usr/bin/qemu-arm-static ${chroot}/usr/bin
56 ;;
57 esac
58
59=== added file 'media_create/ensure_command.py'
60--- media_create/ensure_command.py 1970-01-01 00:00:00 +0000
61+++ media_create/ensure_command.py 2010-11-23 12:02:34 +0000
62@@ -0,0 +1,17 @@
63+import os
64+import sys
65+
66+
67+def apt_get_install(command, package):
68+ print ("Installing required command %s from package %s"
69+ % (command, package))
70+ os.system('sudo apt-get install %s' % package)
71+
72+
73+def ensure_command(command, package):
74+ if os.system('which %s 2>/dev/null 1>/dev/null' % command) != 0:
75+ apt_get_install(command, package)
76+
77+
78+if __name__ == '__main__':
79+ ensure_command(sys.argv[1], sys.argv[2])
80
81=== modified file 'media_create/tests/test_media_create.py'
82--- media_create/tests/test_media_create.py 2010-11-23 12:02:34 +0000
83+++ media_create/tests/test_media_create.py 2010-11-23 12:02:34 +0000
84@@ -1,9 +1,35 @@
85+from contextlib import contextmanager
86 import subprocess
87 import sys
88
89 from testtools import TestCase
90
91 from media_create.boot_cmd import create_boot_cmd
92+from media_create import ensure_command
93+
94+
95+class TestEnsureCommand(TestCase):
96+
97+ apt_get_called = False
98+
99+ def test_command_already_present(self):
100+ with self.mock_apt_get_install():
101+ ensure_command.ensure_command('apt-get', 'apt')
102+ self.assertFalse(self.apt_get_called)
103+
104+ def test_command_not_present(self):
105+ with self.mock_apt_get_install():
106+ ensure_command.ensure_command('apt-get-two-o', 'apt-2')
107+ self.assertTrue(self.apt_get_called)
108+
109+ @contextmanager
110+ def mock_apt_get_install(self):
111+ def mock_apt_get_install(cmd, pkg):
112+ self.apt_get_called = True
113+ orig_func = ensure_command.apt_get_install
114+ ensure_command.apt_get_install = mock_apt_get_install
115+ yield
116+ ensure_command.apt_get_install = orig_func
117
118
119 class TestCreateBootCMD(TestCase):

Subscribers

People subscribed via source and target branches