Skip to content

Commit

Permalink
Add top-level executable for rosdistro-reviewer
Browse files Browse the repository at this point in the history
  • Loading branch information
cottsay committed Jul 26, 2024
1 parent 028acc9 commit 455ab9c
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 1 deletion.
9 changes: 9 additions & 0 deletions rosdistro_reviewer/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

import sys

from rosdistro_reviewer.command import main


sys.exit(main())
34 changes: 34 additions & 0 deletions rosdistro_reviewer/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

from typing import Any

from colcon_core.command \
import LOG_LEVEL_ENVIRONMENT_VARIABLE \
as COLCON_LOG_LEVEL_ENVIRONMENT_VARIABLE
from colcon_core.command import main as colcon_main
from colcon_core.environment_variable import EnvironmentVariable
from rosdistro_reviewer.verb.review import ReviewVerb

"""Environment variable to set the log level"""
LOG_LEVEL_ENVIRONMENT_VARIABLE = EnvironmentVariable(
'ROSDISTRO_REVIEWER_LOG_LEVEL',
COLCON_LOG_LEVEL_ENVIRONMENT_VARIABLE.description)

"""Environment variable to set the configuration directory"""
HOME_ENVIRONMENT_VARIABLE = EnvironmentVariable(
'ROSDISTRO_REVIEWER_HOME',
'Set the configuration directory (default: ~/.rosdistro_reviewer)')


def main(*args: str, **kwargs: str) -> Any:
"""Execute the main logic of the command."""
colcon_kwargs = {
'command_name': 'rosdistro-reviewer',
'verb_group_name': 'rosdistro_reviewer.verb',
'environment_variable_group_name':
'rosdistro_reviewer.environment_variable',
'default_verb': ReviewVerb,
**kwargs,
}
return colcon_main(*args, **colcon_kwargs)
Empty file.
47 changes: 47 additions & 0 deletions rosdistro_reviewer/verb/review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

import logging
from pathlib import Path

from colcon_core.logging import colcon_logger
from colcon_core.logging import get_effective_console_level
from colcon_core.plugin_system import satisfies_version
from colcon_core.verb import VerbExtensionPoint
from rosdistro_reviewer.review.element_analyzer \
import analyze
from rosdistro_reviewer.review.element_analyzer \
import get_element_analyzer_extensions


class ReviewVerb(VerbExtensionPoint):
"""Generate a review for rosdistro changes."""

def __init__(self): # noqa: D107
super().__init__()
satisfies_version(VerbExtensionPoint.EXTENSION_POINT_VERSION, '^1.0')
log_level = get_effective_console_level(colcon_logger)
logging.getLogger('git').setLevel(log_level)

def add_arguments(self, *, parser): # noqa: D102
parser.add_argument(
'--target-ref', default=None,
help='Git commit-ish to use as the base for determining what '
'changes should be reviewed (default: commit prior to '
'--head-ref)')
parser.add_argument(
'--head-ref', default=None,
help='Git commit-ish which contains changes that should be '
'reviewed (default: uncommitted changes)')

def main(self, *, context): # noqa: D102
analyzers = get_element_analyzer_extensions()
review = analyze(
analyzers.values(),
Path.cwd(),
target_ref=context.args.target_ref,
head_ref=context.args.head_ref)
if review:
root = Path.cwd() if context.args.head_ref is None else None
print('\n' + review.to_text(root=root) + '\n')
return 0
8 changes: 7 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ long_description_content_type = text/markdown
[options]
python_requires = >=3.6
install_requires =
colcon-core
colcon-core>=0.17.1
GitPython
unidiff
PyYAML
Expand All @@ -47,6 +47,12 @@ markers =
linter

[options.entry_points]
console_scripts =
rosdistro-reviewer = rosdistro_reviewer.command:main
rosdistro_reviewer.environment_variable =
extension_blocklist = colcon_core.extension_point:EXTENSION_BLOCKLIST_ENVIRONMENT_VARIABLE
home = rosdistro_reviewer.command:HOME_ENVIRONMENT_VARIABLE
log_level = rosdistro_reviewer.command:LOG_LEVEL_ENVIRONMENT_VARIABLE

[flake8]
extend_ignore =
Expand Down
2 changes: 2 additions & 0 deletions test/spell_check.words
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ returncode
rosdistro
rstrip
rtype
runpy
scspell
setuptools
thomas
unidiff
unittest
waldo
yaml
28 changes: 28 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

from runpy import run_module
from unittest.mock import patch

import pytest


@patch(
'colcon_core.argument_parser.get_argument_parser_extensions',
return_value={},
)
def test_main(get_argument_parser_extensions):
with patch('sys.argv', ['__main__', '--help']):
with pytest.raises(SystemExit) as e:
run_module('rosdistro_reviewer')
assert e.value.code == 0

with patch(
'rosdistro_reviewer.verb.review.ReviewVerb.main',
return_value=42,
) as verb_main:
with patch('sys.argv', ['__main__', '--log-base', '/dev/null']):
with pytest.raises(SystemExit) as e:
run_module('rosdistro_reviewer')
assert e.value.code == 42
verb_main.assert_called_once()

0 comments on commit 455ab9c

Please sign in to comment.