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
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)

View File

@ -1 +1,2 @@
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]
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)