diff options
author | Daniil Baturin <daniil@baturin.org> | 2016-03-04 15:05:03 -0500 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2016-03-04 15:05:03 -0500 |
commit | 08a6856b3c8f3146343647f996fca9c48fb4e960 (patch) | |
tree | 1166dee4b9cee8884b3bdf3fb02d5297cac3877a /scripts | |
parent | c6bca34587458c1abee07ff7af7d618ea2f40b9c (diff) | |
download | vyos-build-08a6856b3c8f3146343647f996fca9c48fb4e960.tar.gz vyos-build-08a6856b3c8f3146343647f996fca9c48fb4e960.zip |
Factor out dependency checking functions to its own class (ref T9).
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/check-build-env | 49 | ||||
-rwxr-xr-x | scripts/check-vm-build-env | 48 | ||||
-rw-r--r-- | scripts/util.py | 36 |
3 files changed, 78 insertions, 55 deletions
diff --git a/scripts/check-build-env b/scripts/check-build-env index 6f08847f..6dcf885b 100755 --- a/scripts/check-build-env +++ b/scripts/check-build-env @@ -23,34 +23,31 @@ import os import sys -required_packages = [ - 'sudo', - 'make', - 'live-build', - 'pbuilder', - 'devscripts', - 'python-pystache' -] - - -def is_installed(name): - result = os.system("dpkg-query -W --showformat='${{Status}}\n' {name} 2>&1 | grep 'install ok installed' >/dev/null".format(name=name)) - return True if result == 0 else False - - -missing_packages = [] +import util + +deps = { + 'packages': [ + 'sudo', + 'make', + 'live-build', + 'pbuilder', + 'devscripts', + 'python-pystache' + ], + 'binaries': [] +} print("Checking if packages required for VyOS image build are installed") -for p in required_packages: - if not is_installed(p): - missing_packages.append(p) +checker = util.DependencyChecker(deps) -if missing_packages: - print("Your system does not have some of the required packages installed.") - print("Please install the following packages:") - print(" ".join(missing_packages)) - sys.exit(1) -else: - print("All required packages are installed") +missing = checker.get_missing_dependencies() +if not missing: + print("All dependencies are installed") sys.exit(0) +else: + checker.print_missing_deps() + sys.exit(1) + +sys.exit(0) + diff --git a/scripts/check-vm-build-env b/scripts/check-vm-build-env index 290b28c8..47ec5922 100755 --- a/scripts/check-vm-build-env +++ b/scripts/check-vm-build-env @@ -21,41 +21,31 @@ import os import sys -from distutils.spawn import find_executable -required_packages = [ - 'make', - 'qemu-system-x86', - 'qemu-utils' -] +import util +deps = { + 'packages': [ + 'make', + 'qemu-system-x86', + 'qemu-utils' + ], + 'binaries': ['packer'] +} -def is_installed(name): - result = os.system("dpkg-query -W --showformat='${{Status}}\n' {name} 2>&1 | grep 'install ok installed' >/dev/null".format(name=name)) - return True if result == 0 else False +print("Checking if packages required for VyOS image build are installed") +checker = util.DependencyChecker(deps) -missing_packages = [] - -print("Checking if packages required for VyOS VM image build are installed") - -for p in required_packages: - if not is_installed(p): - missing_packages.append(p) - -if missing_packages: - print("Your system does not have some of the required packages installed.") - print("Please install the following packages:") - print(" ".join(missing_packages)) - sys.exit(1) -else: - print("All required packages are installed") - -if find_executable("packer"): - print("Your system has Packer.") +missing = checker.get_missing_dependencies() +if not missing: + print("All dependencies are installed") + sys.exit(0) else: - print("Your system does not have Packer.") - print("Please install Packer from https://www.packer.io/downloads.html.") + checker.print_missing_deps() + if 'packer' in missing['binaries']: + print("Your system does not have Packer.") + print("Please install Packer from https://www.packer.io/downloads.html.") sys.exit(1) sys.exit(0) diff --git a/scripts/util.py b/scripts/util.py index 00377672..7cc33364 100644 --- a/scripts/util.py +++ b/scripts/util.py @@ -19,6 +19,7 @@ import sys import os +from distutils.spawn import find_executable import defaults @@ -27,3 +28,38 @@ def check_build_config(): print("Build config file ({file}) does not exist".format(file=defaults.BUILD_CONFIG)) print("If you are running this script by hand, you should better not. Run 'make iso' instead.") sys.exit(1) + + +class DependencyChecker(object): + def __init__(self, spec): + missing_packages = self._get_missing_packages(spec['packages']) + missing_binaries = self._get_missing_binaries(spec['binaries']) + self.__missing = {'packages': missing_packages, 'binaries': missing_binaries} + + + def _package_installed(self, name): + result = os.system("dpkg-query -W --showformat='${{Status}}\n' {name} 2>&1 | grep 'install ok installed' >/dev/null".format(name=name)) + return True if result == 0 else False + + def _get_missing_packages(self, packages): + missing_packages = [] + for p in packages: + if not self._package_installed(p): + missing_packages.append(p) + return missing_packages + + def _get_missing_binaries(self, binaries): + missing_binaries = [] + for b in binaries: + if not find_executable(b): + missing_binaries.append(b) + return missing_binaries + + def get_missing_dependencies(self): + if self.__missing['packages'] or self.__missing['binaries']: + return self.__missing + return None + + def print_missing_deps(self): + print("Missing packages: " + " ".join(self.__missing['packages'])) + print("Missing binaries: " + " ".join(self.__missing['binaries'])) |