Improve exception handling

This commit is contained in:
jpic 2021-04-24 20:07:09 +02:00
parent 2c5b9ab442
commit cfc026987d
4 changed files with 19 additions and 9 deletions

View File

@ -3,6 +3,8 @@ import binascii
import glob
import os
from ..exceptions import ShlaxException
class Copy:
def __init__(self, *args):
@ -15,12 +17,16 @@ class Copy:
else:
self.src.append(src)
def listfiles(self):
async def listfiles(self, target):
if getattr(self, '_listfiles', None):
return self._listfiles
result = []
for src in self.src:
if not await target.parent.exists(src):
target.output.fail(self)
raise ShlaxException(f'File not found {src}')
if os.path.isfile(src):
result.append(src)
continue
@ -39,7 +45,7 @@ class Copy:
async def __call__(self, target):
await target.mkdir(self.dst)
for path in self.listfiles():
for path in await self.listfiles(target):
if os.path.isdir(path):
await target.mkdir(os.path.join(self.dst, path))
elif '/' in path:
@ -55,9 +61,11 @@ class Copy:
def __str__(self):
return f'Copy({", ".join(self.src)}, {self.dst})'
async def cachekey(self):
async def cachekey(self, target):
async def chksum(path):
with open(path, 'rb') as f:
return (path, str(binascii.crc32(f.read())))
results = await asyncio.gather(*[chksum(f) for f in self.listfiles()])
results = await asyncio.gather(
*[chksum(f) for f in await self.listfiles(target)]
)
return {path: chks for path, chks in results}

View File

@ -13,7 +13,7 @@ import importlib
import os
import sys
from .proc import ProcFailure
from .exceptions import ShlaxException
class Group(cli2.Group):
@ -57,10 +57,11 @@ class Command(cli2.Command):
try:
result = super().__call__(*argv)
except ProcFailure:
except ShlaxException as exc:
# just output the failure without TB, as command was already
# printed anyway
pass
self.exit_code = 1
self['target'].value.output.fail(exc)
if self['target'].value.results:
if self['target'].value.results[-1].status == 'failure':

View File

@ -7,10 +7,11 @@ import os
import shlex
import sys
from .exceptions import ShlaxException
from .output import Output
class ProcFailure(Exception):
class ProcFailure(ShlaxException):
def __init__(self, proc):
self.proc = proc

View File

@ -88,7 +88,7 @@ class Buildah(Target):
prefix = tag
break
if hasattr(action, 'cachekey'):
action_key = action.cachekey()
action_key = action.cachekey(self)
if asyncio.iscoroutine(action_key):
action_key = str(await action_key)
else: