-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Introduce new
--format
argument (and remove --xunit-file
)
Any output (text or xUnit) is now directed to the standard output. It will be easier to add new formats (such as CSV or JSON, which I plan).
- Loading branch information
Showing
10 changed files
with
241 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
from . import commands | ||
from . import githost | ||
from . import output | ||
|
||
|
||
TODAY = datetime.date.today() | ||
|
@@ -40,8 +41,8 @@ class Config: | |
path: str = "." | ||
max_age: int = 90 | ||
|
||
output_format: output.OutputFormat = output.OutputFormat.TEXT | ||
colorize_errors: bool = True | ||
xunit_file: str = None | ||
|
||
calm_branches: typing.Sequence = ("gh-pages", "master", "main", "prod", "maint(enance)?/.*") | ||
ignore_branches_without_pull_request: bool = False | ||
|
@@ -96,6 +97,11 @@ def name_and_details(self): | |
details += f", linked to {pr.state} PR/MR #{pr.number} ({pr.url})" | ||
return details | ||
|
||
def to_text(self): | ||
return ( | ||
f"{self.author[:30]: <30} - {self.age: >4} days - {self.name_and_details}" | ||
) | ||
|
||
|
||
def get_repository_info(path): | ||
"""Extract the repository owner and name from the origin remote.""" | ||
|
@@ -131,12 +137,12 @@ def get_branches(config: Config): | |
branch = branch.strip()[len("origin/") :] | ||
if config.ignore_branch(branch) or "->" in branch: | ||
continue | ||
output = commands.get_output( | ||
out = commands.get_output( | ||
("git", "log", f"origin/{branch}", "-1", "--format=%ae %ci"), | ||
cwd=config.path, | ||
)[0] | ||
# line looks like "[email protected] 2018-12-19 14:18:52 +0100" | ||
email, date, *_rest = output.split(" ") | ||
email, date, *_rest = out.split(" ") | ||
date = datetime.date(*[int(s) for s in date.split("-")]) | ||
age = (TODAY - date).days | ||
branches.append( | ||
|
@@ -151,7 +157,7 @@ def get_branches(config: Config): | |
) | ||
|
||
if not branches: | ||
return () | ||
return [] | ||
|
||
if config.host_api_access: | ||
pr_getter = githost.PullRequestGetter(config.platform, config.host_owner, config.host_api_access) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import enum | ||
|
||
|
||
__all__ = ["StrEnum"] | ||
|
||
try: | ||
from enum import StrEnum | ||
except ImportError: # Python < 3.11 | ||
# fmt: off | ||
# pylint: disable=consider-using-f-string | ||
# Lifted from Python standard library, with docstrings removed for brevity. | ||
# https://github.com/python/cpython/blob/c1712ef066321c01bf09cba3f22fc474b5b8dfa7/Lib/enum.py | ||
class StrEnum(str, enum.Enum): | ||
def __new__(cls, *values): | ||
"values must already be of type `str`" | ||
if len(values) > 3: | ||
raise TypeError('too many arguments for str(): %r' % (values, )) | ||
if len(values) == 1: | ||
# it must be a string | ||
if not isinstance(values[0], str): | ||
raise TypeError('%r is not a string' % (values[0], )) | ||
if len(values) >= 2: | ||
# check that encoding argument is a string | ||
if not isinstance(values[1], str): | ||
raise TypeError('encoding must be a string, not %r' % (values[1], )) | ||
if len(values) == 3: | ||
# check that errors argument is a string | ||
if not isinstance(values[2], str): | ||
raise TypeError('errors must be a string, not %r' % (values[2])) | ||
value = str(*values) | ||
member = str.__new__(cls, value) | ||
member._value_ = value | ||
return member | ||
|
||
@staticmethod | ||
def _generate_next_value_(name, start, count, last_values): | ||
return name.lower() | ||
# fmt: on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import enum | ||
import os | ||
import typing | ||
import xml.etree.ElementTree | ||
|
||
from . import compat | ||
|
||
|
||
class OutputFormat(compat.StrEnum): | ||
TEXT = enum.auto() | ||
XUNIT = enum.auto() | ||
|
||
|
||
def text_formatter( | ||
objects: list, | ||
ok_message: str, | ||
error_message: str, | ||
colorize_errors=True, | ||
**unsupported_options, | ||
) -> str: | ||
if colorize_errors: | ||
warn = "\033[91m{}\033[0m".format | ||
else: | ||
warn = lambda text: text # pylint: disable=unnecessary-lambda-assignment | ||
|
||
lines = [] | ||
if ok_message: | ||
lines.append(ok_message) | ||
if error_message: | ||
lines.append(warn(error_message)) | ||
|
||
for obj in objects: | ||
lines.append(warn(obj.to_text()) if obj.is_old else obj.to_text()) | ||
return os.linesep.join(lines) | ||
|
||
|
||
def xunit_formatter( | ||
objects: list, | ||
error_message: str, | ||
xunit_suite_name: str, | ||
xunit_case_name: str, | ||
xunit_class_name: str, | ||
**unsupported_options, | ||
) -> str: | ||
stdout = os.linesep.join(obj.to_text() for obj in objects) | ||
suite = xml.etree.ElementTree.Element( | ||
"testsuite", | ||
{ | ||
"name": xunit_suite_name, | ||
"tests": "1", | ||
"errors": "0", | ||
"failures": "1" if error_message else "0", | ||
}, | ||
) | ||
case = xml.etree.ElementTree.SubElement( | ||
suite, | ||
"testcase", | ||
{"classname": xunit_class_name, "name": xunit_case_name}, | ||
) | ||
if error_message: | ||
failure = xml.etree.ElementTree.SubElement( | ||
case, | ||
"failure", | ||
{"message": error_message}, | ||
) | ||
failure.text = stdout | ||
|
||
system_out = xml.etree.ElementTree.SubElement(case, "system-out") | ||
system_out.text = stdout | ||
|
||
# FIXME (dbaty, 2024-03-28): this element is always empty and | ||
# could be removed (because the schema says it's optional). | ||
system_err = xml.etree.ElementTree.SubElement(case, "system-err") | ||
system_err.text = "" | ||
|
||
return xml.etree.ElementTree.tostring(suite, encoding="utf-8").decode("utf-8") | ||
|
||
|
||
def get_formatter(format_) -> typing.Callable: | ||
if format_ == OutputFormat.TEXT: | ||
return text_formatter | ||
if format_ == OutputFormat.XUNIT: | ||
return xunit_formatter | ||
raise ValueError(f"Unknown output format: '{format_}'") | ||
|
||
|
||
def printer(objects: list, output_format, **options): | ||
formatter = get_formatter(output_format) | ||
formatted = formatter(objects, **options) | ||
print(formatted) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.