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

View File

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

View File

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

View File

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