diff options
author | Andy Lutomirski <luto@kernel.org> | 2019-11-13 13:06:48 -0800 |
---|---|---|
committer | Andy Lutomirski <luto@kernel.org> | 2019-11-13 13:06:48 -0800 |
commit | 73bb57a4e1b08e61bc290c282c787d8e0719135e (patch) | |
tree | 4c38f6c15cb02aff7df43423c3b8cda92a7dd116 | |
parent | 0c733ddb1ef4952bda0614cc68ce80f1c8433ffc (diff) | |
download | virtme-73bb57a4e1b08e61bc290c282c787d8e0719135e.tar.gz |
Add util.find_binary() to find binaries
This factors the logic out from find_busybox()
Signed-off-by: Andy Lutomirski <luto@kernel.org>
-rw-r--r-- | virtme/mkinitramfs.py | 22 | ||||
-rw-r--r-- | virtme/util.py | 33 |
2 files changed, 36 insertions, 19 deletions
diff --git a/virtme/mkinitramfs.py b/virtme/mkinitramfs.py index 98c7528..1a21c9b 100644 --- a/virtme/mkinitramfs.py +++ b/virtme/mkinitramfs.py @@ -13,6 +13,7 @@ import os.path import shlex import itertools from . import cpiowriter +from . import util def make_base_layout(cw): for dir in (b'lib', b'bin', b'var', b'etc', b'newroot', b'dev', b'proc', @@ -167,22 +168,5 @@ def mkinitramfs(out, config) -> None: cw.write_trailer() def find_busybox(root, is_native) -> Optional[str]: - names = ['busybox-static', 'busybox.static', 'busybox'] - dirs = [os.path.join(*i) for i in itertools.product( - ['usr/local', 'usr', ''], - ['bin', 'sbin'])] - - for n in names: - if is_native: - # Search PATH first - path = shutil.which(n) - if path is not None: - return path - - for d in dirs: - path = os.path.join(root, d, n) - if os.path.isfile(path): - return path - - # We give up. - return None + return util.find_binary(['busybox-static', 'busybox.static', 'busybox'], + root=root, use_path=is_native) diff --git a/virtme/util.py b/virtme/util.py new file mode 100644 index 0000000..16b7ef7 --- /dev/null +++ b/virtme/util.py @@ -0,0 +1,33 @@ +# -*- mode: python -*- +# util.py: Misc helpers +# Copyright © 2014-2019 Andy Lutomirski +# Licensed under the GPLv2, which is available in the virtme distribution +# as a file called LICENSE with SHA-256 hash: +# 8177f97513213526df2cf6184d8ff986c675afb514d4e68a404010521b880643 + +from typing import Optional, Sequence + +import os +import shutil +import itertools + +def find_binary(names: Sequence[str], root: str = '/', + use_path: bool = True) -> Optional[str]: + dirs = [os.path.join(*i) for i in itertools.product( + ['usr/local', 'usr', ''], + ['bin', 'sbin'])] + + for n in names: + if use_path: + # Search PATH first + path = shutil.which(n) + if path is not None: + return path + + for d in dirs: + path = os.path.join(root, d, n) + if os.path.isfile(path): + return path + + # We give up. + return None |