Use Error class to represent CB errors

This commit is contained in:
Mikhail Gordeev 2020-04-20 14:29:27 +03:00
parent 2b5676a89a
commit fcec9c3b9b
3 changed files with 20 additions and 11 deletions

View File

@ -1,8 +1,9 @@
#!/usr/bin/python3 #!/usr/bin/python3
import argparse import argparse
import sys
from cloud_build import CB import cloud_build
PROG = 'cloud-build' PROG = 'cloud-build'
@ -29,12 +30,16 @@ def parse_args():
def main(): def main():
args = parse_args() args = parse_args()
cloud_build = CB(args) cb = cloud_build.CB(args)
cloud_build.create_images() cb.create_images()
cloud_build.copy_external_files() cb.copy_external_files()
cloud_build.sign() cb.sign()
cloud_build.sync() cb.sync()
if __name__ == '__main__': if __name__ == '__main__':
try:
main() main()
except cloud_build.Error as e:
print(e, file=sys.stdout)
exit(1)

View File

@ -1 +1,2 @@
from .cloud_build import CB # noqa: F401 from .cloud_build import CB # noqa: F401
from .cloud_build import Error # noqa: F401

View File

@ -22,6 +22,10 @@ PROG = 'cloud-build'
PathLike = Union[Path, str] PathLike = Union[Path, str]
class Error(Exception):
pass
class CB: class CB:
"""class for building cloud images""" """class for building cloud images"""
@ -48,13 +52,13 @@ class CB:
self.created_scripts: List[Path] = [] self.created_scripts: List[Path] = []
self.ensure_dirs() self.ensure_dirs()
self.ensure_run_once()
logging.basicConfig( logging.basicConfig(
filename=f'{data_dir}/{PROG}.log', filename=f'{data_dir}/{PROG}.log',
format='%(levelname)s:%(asctime)s - %(message)s', format='%(levelname)s:%(asctime)s - %(message)s',
) )
self.log = logging.getLogger(PROG) self.log = logging.getLogger(PROG)
self.log.setLevel(self.log_level) self.log.setLevel(self.log_level)
self.ensure_run_once()
self.info(f'Start {PROG}') self.info(f'Start {PROG}')
self.initialized = True self.initialized = True
@ -91,8 +95,7 @@ class CB:
try: try:
fcntl.flock(self.lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) fcntl.flock(self.lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError: # already locked except OSError: # already locked
print(f'{PROG} already running', file=sys.stderr) self.error(f'{PROG} already running')
exit(3)
@contextlib.contextmanager @contextlib.contextmanager
def pushd(self, new_dir): def pushd(self, new_dir):
@ -154,7 +157,7 @@ class CB:
def error(self, msg: str) -> None: def error(self, msg: str) -> None:
self.log.error(msg) self.log.error(msg)
raise Exception(msg) raise Error(msg)
def remote(self, branch: str) -> str: def remote(self, branch: str) -> str:
return self._remote.format(branch=branch) return self._remote.format(branch=branch)