Skip to content

Commit

Permalink
* Add failed command to error message (hitachienergy#3001)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbbroot committed Mar 9, 2022
1 parent da5062a commit 691d1d9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __get_package_candidate_version(self, package: str, version: str = '') -> st
if 'Candidate' in line:
return line.split(': ')[-1]

raise CriticalError(f'Candidate for {package} not found.')
raise CriticalError(f'Candidate for {package} not found, command: `{self.command()}`')

def get_package_info(self, package: str, version: str = '') -> Dict[str, str]:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ def __init__(self, process_name: str, retries: int, pipe_args: List[str] = None)
self.__proc_name: str = process_name
self.__retries: int = retries
self.__pipe_args: List[str] = pipe_args # used for __or__
self.__command: str = ''

def name(self) -> str:
return self.__proc_name

def pipe_args(self) -> List[str]:
return self.__pipe_args or []

def command(self) -> str:
return self.__command

def run(self, args: List[str],
capture_output: bool = True,
accept_nonzero_returncode: bool = False) -> subprocess.CompletedProcess:
Expand All @@ -35,13 +39,15 @@ def run(self, args: List[str],
process_args = [self.__proc_name]
process_args.extend(args)

self.__command = f'{self.__proc_name} {" ".join(args)}'

additional_args = {'encoding': 'utf-8'}
if capture_output:
additional_args['stdout'] = subprocess.PIPE
additional_args['stderr'] = subprocess.PIPE

for count in range(self.__retries):
logging.debug(f'[{count + 1}/{self.__retries}] Running: {self.__proc_name} {" ".join(args)} ')
logging.debug(f'[{count + 1}/{self.__retries}] Running: `{self.__command}` ')

process = subprocess.run(process_args, **additional_args)

Expand All @@ -53,7 +59,7 @@ def run(self, args: List[str],

logging.warn(process.stderr)

raise CriticalError('Retries count reached maximum!')
raise CriticalError(f'Retries count reached maximum, command: `{self.__command}`')

def __or__(self, command) -> str:
"""
Expand All @@ -65,10 +71,10 @@ def __or__(self, command) -> str:
"""
lproc_name = f'{self.__proc_name} {" ".join(self.__pipe_args)}'
rproc_name = f'{command.name()} {" ".join(command.pipe_args())}'
whole_process_name = f'{lproc_name} | {rproc_name}'
self.__command = f'{lproc_name} | {rproc_name}'

for count in range(self.__retries):
logging.debug(f'[{count + 1}/{self.__retries}] Running: {whole_process_name}')
logging.debug(f'[{count + 1}/{self.__retries}] Running: `{self.__command}`')

lproc = subprocess.Popen([self.__proc_name] + self.__pipe_args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Expand All @@ -82,7 +88,7 @@ def __or__(self, command) -> str:

logging.warn(lproc.stderr if not lproc.returncode == 0 else rproc.stderr)

raise CriticalError('Retries count reached maximum!')
raise CriticalError(f'Retries count reached maximum, command: `{self.__command}`')

def _run_and_filter(self, args: List[str]) -> List[str]:
"""
Expand All @@ -92,6 +98,4 @@ def _run_and_filter(self, args: List[str]) -> List[str]:
:returns: filtered output lines from the subprocess stdout
"""
raw_output = self.run(args).stdout

elems: List[str] = [elem for elem in raw_output.split('\n')]
return list(filter(lambda elem: elem != '', elems)) # filter empty lines
return list(filter(lambda elem: elem != '', raw_output.split('\n'))) # filter empty lines
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ def query(self, package: str, queryformat: str, archlist: List[str]) -> List[str
def output_handler(output: str):
""" In addition to errors, handle missing packages """
if not output:
raise PackageNotfound(f'repoquery failed for package `{package}`, reason: package not found')
raise PackageNotfound(f'repoquery failed for package `{package}`, reason: package not found, command: `{self.command()}`')
elif 'error' in output:
raise CriticalError(f'repoquery failed for package `{package}`, reason: `{output}`')
raise CriticalError(f'repoquery failed for package `{package}`, reason: `{output}`, command: `{self.command()}`')

return self.__query(package, queryformat, archlist, False, False, output_handler)

Expand All @@ -97,6 +97,6 @@ def get_dependencies(self, package: str, queryformat: str, archlist: List[str])
def output_handler(output: str):
""" Handle errors """
if 'error' in output:
raise CriticalError(f'repoquery failed for package `{package}`, reason: `{output}`')
raise CriticalError(f'repoquery failed for package `{package}`, reason: `{output}`, command: `{self.command()}`')

return self.__query(package, queryformat, archlist, True, True, output_handler)

0 comments on commit 691d1d9

Please sign in to comment.