From fcec9c3b9be9caeef55656e71841d209868827c2 Mon Sep 17 00:00:00 2001 From: Mikhail Gordeev Date: Mon, 20 Apr 2020 14:29:27 +0300 Subject: [PATCH] Use Error class to represent CB errors --- cloud-build.py | 19 ++++++++++++------- cloud_build/__init__.py | 1 + cloud_build/cloud_build.py | 11 +++++++---- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/cloud-build.py b/cloud-build.py index b2f5322..29aefbc 100755 --- a/cloud-build.py +++ b/cloud-build.py @@ -1,8 +1,9 @@ #!/usr/bin/python3 import argparse +import sys -from cloud_build import CB +import cloud_build PROG = 'cloud-build' @@ -29,12 +30,16 @@ def parse_args(): def main(): args = parse_args() - cloud_build = CB(args) - cloud_build.create_images() - cloud_build.copy_external_files() - cloud_build.sign() - cloud_build.sync() + cb = cloud_build.CB(args) + cb.create_images() + cb.copy_external_files() + cb.sign() + cb.sync() if __name__ == '__main__': - main() + try: + main() + except cloud_build.Error as e: + print(e, file=sys.stdout) + exit(1) diff --git a/cloud_build/__init__.py b/cloud_build/__init__.py index 40f642e..9bd25e9 100644 --- a/cloud_build/__init__.py +++ b/cloud_build/__init__.py @@ -1 +1,2 @@ from .cloud_build import CB # noqa: F401 +from .cloud_build import Error # noqa: F401 diff --git a/cloud_build/cloud_build.py b/cloud_build/cloud_build.py index 1931093..1760d5c 100755 --- a/cloud_build/cloud_build.py +++ b/cloud_build/cloud_build.py @@ -22,6 +22,10 @@ PROG = 'cloud-build' PathLike = Union[Path, str] +class Error(Exception): + pass + + class CB: """class for building cloud images""" @@ -48,13 +52,13 @@ class CB: self.created_scripts: List[Path] = [] self.ensure_dirs() - self.ensure_run_once() logging.basicConfig( filename=f'{data_dir}/{PROG}.log', format='%(levelname)s:%(asctime)s - %(message)s', ) self.log = logging.getLogger(PROG) self.log.setLevel(self.log_level) + self.ensure_run_once() self.info(f'Start {PROG}') self.initialized = True @@ -91,8 +95,7 @@ class CB: try: fcntl.flock(self.lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) except OSError: # already locked - print(f'{PROG} already running', file=sys.stderr) - exit(3) + self.error(f'{PROG} already running') @contextlib.contextmanager def pushd(self, new_dir): @@ -154,7 +157,7 @@ class CB: def error(self, msg: str) -> None: self.log.error(msg) - raise Exception(msg) + raise Error(msg) def remote(self, branch: str) -> str: return self._remote.format(branch=branch)