mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-12 21:45:47 +00:00
* add models tests (#1) also replace single quote to double one to confort PEP docstring + move _check_output to class properties to make it available for mocking * alpm tests implementation * try to replace os with pathlib * update tests for pathlib * fix includes glob and trim version from dependencies * build_tools package tests * repository component tests * add sign tests * complete status tests * handle exceptions in actual_version calls * complete core tests * move configuration to root conftest * application tests * complete application tests * change copyright to more generic one * base web tests * complete web tests * complete testkit also add argument parsers test
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 2021 Evgenii Alekseev.
|
||||
# Copyright (c) 2021 ahriman team.
|
||||
#
|
||||
# This file is part of ahriman
|
||||
# (see https://github.com/arcan1s/ahriman).
|
||||
@ -21,74 +21,74 @@ import datetime
|
||||
import subprocess
|
||||
|
||||
from logging import Logger
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from ahriman.core.exceptions import InvalidOption
|
||||
|
||||
|
||||
def check_output(*args: str, exception: Optional[Exception],
|
||||
cwd: Optional[str] = None, stderr: int = subprocess.STDOUT,
|
||||
logger: Optional[Logger] = None) -> str:
|
||||
'''
|
||||
cwd: Optional[Path] = None, logger: Optional[Logger] = None) -> str:
|
||||
"""
|
||||
subprocess wrapper
|
||||
:param args: command line arguments
|
||||
:param exception: exception which has to be reraised instead of default subprocess exception
|
||||
:param cwd: current working directory
|
||||
:param stderr: standard error output mode
|
||||
:param logger: logger to log command result if required
|
||||
:return: command output
|
||||
'''
|
||||
"""
|
||||
try:
|
||||
result = subprocess.check_output(args, cwd=cwd, stderr=stderr).decode('utf8').strip()
|
||||
result = subprocess.check_output(args, cwd=cwd, stderr=subprocess.STDOUT).decode("utf8").strip()
|
||||
if logger is not None:
|
||||
for line in result.splitlines():
|
||||
logger.debug(line)
|
||||
except subprocess.CalledProcessError as e:
|
||||
if e.output is not None and logger is not None:
|
||||
for line in e.output.decode('utf8').splitlines():
|
||||
for line in e.output.decode("utf8").splitlines():
|
||||
logger.debug(line)
|
||||
raise exception or e
|
||||
return result
|
||||
|
||||
|
||||
def package_like(filename: str) -> bool:
|
||||
'''
|
||||
def package_like(filename: Path) -> bool:
|
||||
"""
|
||||
check if file looks like package
|
||||
:param filename: name of file to check
|
||||
:return: True in case if name contains `.pkg.` and not signature, False otherwise
|
||||
'''
|
||||
return '.pkg.' in filename and not filename.endswith('.sig')
|
||||
"""
|
||||
name = filename.name
|
||||
return ".pkg." in name and not name.endswith(".sig")
|
||||
|
||||
|
||||
def pretty_datetime(timestamp: Optional[int]) -> str:
|
||||
'''
|
||||
"""
|
||||
convert datetime object to string
|
||||
:param timestamp: datetime to convert
|
||||
:return: pretty printable datetime as string
|
||||
'''
|
||||
return '' if timestamp is None else datetime.datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
|
||||
"""
|
||||
return "" if timestamp is None else datetime.datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
|
||||
def pretty_size(size: Optional[float], level: int = 0) -> str:
|
||||
'''
|
||||
"""
|
||||
convert size to string
|
||||
:param size: size to convert
|
||||
:param level: represents current units, 0 is B, 1 is KiB etc
|
||||
:return: pretty printable size as string
|
||||
'''
|
||||
"""
|
||||
def str_level() -> str:
|
||||
if level == 0:
|
||||
return 'B'
|
||||
return "B"
|
||||
if level == 1:
|
||||
return 'KiB'
|
||||
return "KiB"
|
||||
if level == 2:
|
||||
return 'MiB'
|
||||
return "MiB"
|
||||
if level == 3:
|
||||
return 'GiB'
|
||||
raise InvalidOption(level) # I hope it will not be more than 1024 GiB
|
||||
return "GiB"
|
||||
raise InvalidOption(level) # must never happen actually
|
||||
|
||||
if size is None:
|
||||
return ''
|
||||
if size < 1024:
|
||||
return f'{round(size, 2)} {str_level()}'
|
||||
return ""
|
||||
if size < 1024 or level == 3:
|
||||
return f"{size:.1f} {str_level()}"
|
||||
return pretty_size(size / 1024, level + 1)
|
||||
|
Reference in New Issue
Block a user