nvchecker/nvchecker_source/cmd.py

41 lines
1001 B
Python
Raw Normal View History

2017-02-28 07:24:53 +00:00
# MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
2017-02-28 07:24:53 +00:00
2017-07-04 09:04:29 +00:00
import asyncio
2013-11-03 10:21:50 +00:00
2018-04-11 08:18:41 +00:00
import structlog
2020-08-14 12:04:05 +00:00
from nvchecker.api import GetVersionError
2020-08-13 12:01:02 +00:00
2018-04-11 08:18:41 +00:00
logger = structlog.get_logger(logger_name=__name__)
2013-11-03 10:21:50 +00:00
2020-08-13 12:42:24 +00:00
async def run_cmd(cmd: str) -> str:
logger.debug('running cmd', cmd=cmd)
p = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
2013-11-03 10:21:50 +00:00
output, error = await p.communicate()
2020-08-13 12:42:24 +00:00
output_s = output.strip().decode('latin1')
error_s = error.strip().decode(errors='replace')
2017-07-04 09:04:29 +00:00
if p.returncode != 0:
2020-08-13 12:01:02 +00:00
raise GetVersionError(
'command exited with error',
2020-08-13 12:42:24 +00:00
cmd=cmd, error=error_s,
returncode=p.returncode)
elif not output_s:
2020-08-13 12:01:02 +00:00
raise GetVersionError(
'command exited without output',
2020-08-13 12:42:24 +00:00
cmd=cmd, error=error_s,
returncode=p.returncode)
else:
2020-08-13 12:42:24 +00:00
return output_s
async def get_version(
name, conf, *, cache, keymanager=None
):
cmd = conf['cmd']
return await cache.get(cmd, run_cmd)