do not use set_defaults for architecture arguments

according to the source code defaults always updates the values
dictionary. This in this specific case it is impossible to override the
value it will be always empty list.

In order to handle it we are adding another property to the Handler
class which allows to run with None architecture list.

This particular set_defaults behaviour is still useful for other cases
when we have to run command without any specific architecture
This commit is contained in:
Evgenii Alekseev 2021-10-03 00:59:24 +03:00
parent 444da87fdc
commit 1192f12f91
23 changed files with 190 additions and 54 deletions

View File

@ -95,7 +95,7 @@ def _set_add_parser(root: SubParserAction) -> argparse.ArgumentParser:
parser.add_argument("--source", help="package source", choices=PackageSource, type=PackageSource,
default=PackageSource.Auto)
parser.add_argument("--without-dependencies", help="do not add dependencies", action="store_true")
parser.set_defaults(handler=handlers.Add, architecture=[])
parser.set_defaults(handler=handlers.Add)
return parser
@ -110,7 +110,7 @@ def _set_check_parser(root: SubParserAction) -> argparse.ArgumentParser:
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("package", help="filter check by package base", nargs="*")
parser.add_argument("--no-vcs", help="do not check VCS packages", action="store_true")
parser.set_defaults(handler=handlers.Update, architecture=[], no_aur=False, no_manual=True, dry_run=True)
parser.set_defaults(handler=handlers.Update, no_aur=False, no_manual=True, dry_run=True)
return parser
@ -127,7 +127,7 @@ def _set_clean_parser(root: SubParserAction) -> argparse.ArgumentParser:
parser.add_argument("--no-chroot", help="do not clear build chroot", action="store_true")
parser.add_argument("--no-manual", help="do not clear directory with manually added packages", action="store_true")
parser.add_argument("--no-packages", help="do not clear directory with built packages", action="store_true")
parser.set_defaults(handler=handlers.Clean, architecture=[], no_log=True, unsafe=True)
parser.set_defaults(handler=handlers.Clean, no_log=True, unsafe=True)
return parser
@ -181,7 +181,7 @@ def _set_rebuild_parser(root: SubParserAction) -> argparse.ArgumentParser:
parser = root.add_parser("rebuild", help="rebuild repository", description="rebuild whole repository",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--depends-on", help="only rebuild packages that depend on specified package", action="append")
parser.set_defaults(handler=handlers.Rebuild, architecture=[])
parser.set_defaults(handler=handlers.Rebuild)
return parser
@ -194,7 +194,7 @@ def _set_remove_parser(root: SubParserAction) -> argparse.ArgumentParser:
parser = root.add_parser("remove", help="remove package", description="remove package",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("package", help="package name or base", nargs="+")
parser.set_defaults(handler=handlers.Remove, architecture=[])
parser.set_defaults(handler=handlers.Remove)
return parser
@ -208,7 +208,7 @@ def _set_remove_unknown_parser(root: SubParserAction) -> argparse.ArgumentParser
description="remove packages which are missing in AUR",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--dry-run", help="just perform check for packages without removal", action="store_true")
parser.set_defaults(handler=handlers.RemoveUnknown, architecture=[])
parser.set_defaults(handler=handlers.RemoveUnknown)
return parser
@ -221,7 +221,7 @@ def _set_report_parser(root: SubParserAction) -> argparse.ArgumentParser:
parser = root.add_parser("report", help="generate report", description="generate report",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("target", help="target to generate report", nargs="*")
parser.set_defaults(handler=handlers.Report, architecture=[])
parser.set_defaults(handler=handlers.Report)
return parser
@ -269,7 +269,7 @@ def _set_sign_parser(root: SubParserAction) -> argparse.ArgumentParser:
parser = root.add_parser("sign", help="sign packages", description="(re-)sign packages and repository database",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("package", help="sign only specified packages", nargs="*")
parser.set_defaults(handler=handlers.Sign, architecture=[])
parser.set_defaults(handler=handlers.Sign)
return parser
@ -316,7 +316,7 @@ def _set_sync_parser(root: SubParserAction) -> argparse.ArgumentParser:
parser = root.add_parser("sync", help="sync repository", description="sync packages to remote server",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("target", help="target to sync", nargs="*")
parser.set_defaults(handler=handlers.Sync, architecture=[])
parser.set_defaults(handler=handlers.Sync)
return parser
@ -333,7 +333,7 @@ def _set_update_parser(root: SubParserAction) -> argparse.ArgumentParser:
parser.add_argument("--no-aur", help="do not check for AUR updates. Implies --no-vcs", action="store_true")
parser.add_argument("--no-manual", help="do not include manual updates", action="store_true")
parser.add_argument("--no-vcs", help="do not check VCS packages", action="store_true")
parser.set_defaults(handler=handlers.Update, architecture=[])
parser.set_defaults(handler=handlers.Update)
return parser

View File

@ -30,6 +30,8 @@ class Dump(Handler):
dump configuration handler
"""
ALLOW_AUTO_ARCHITECTURE_RUN = False
_print = print
@classmethod

View File

@ -34,9 +34,11 @@ from ahriman.models.repository_paths import RepositoryPaths
class Handler:
"""
base handler class for command callbacks
:cvar ALLOW_AUTO_ARCHITECTURE_RUN: allow to define architecture from existing repositories
:cvar ALLOW_MULTI_ARCHITECTURE_RUN: allow to run with multiple architectures
"""
ALLOW_AUTO_ARCHITECTURE_RUN = True
ALLOW_MULTI_ARCHITECTURE_RUN = True
@classmethod
@ -85,9 +87,11 @@ class Handler:
:param args: command line args
:return: list of architectures for which tree is created
"""
if args.architecture is None:
if not cls.ALLOW_AUTO_ARCHITECTURE_RUN and args.architecture is None:
# for some parsers (e.g. config) we need to run with specific architecture
# for those cases architecture must be set explicitly
raise MissingArchitecture(args.command)
if args.architecture:
if args.architecture: # architecture is specified explicitly
return set(args.architecture)
config = Configuration()
@ -96,7 +100,7 @@ class Handler:
root = config.getpath("repository", "root") # pylint: disable=assignment-from-no-return
architectures = RepositoryPaths.known_architectures(root)
if not architectures:
if not architectures: # well we did not find anything
raise MissingArchitecture(args.command)
return architectures

View File

@ -31,6 +31,8 @@ class Init(Handler):
repository init handler
"""
ALLOW_AUTO_ARCHITECTURE_RUN = False
@classmethod
def run(cls: Type[Handler], args: argparse.Namespace, architecture: str,
configuration: Configuration, no_report: bool) -> None:

View File

@ -31,6 +31,8 @@ class KeyImport(Handler):
key import packages handler
"""
ALLOW_AUTO_ARCHITECTURE_RUN = False # it should be called only as "no-architecture"
@classmethod
def run(cls: Type[Handler], args: argparse.Namespace, architecture: str,
configuration: Configuration, no_report: bool) -> None:

View File

@ -31,6 +31,8 @@ class Search(Handler):
packages search handler
"""
ALLOW_AUTO_ARCHITECTURE_RUN = False # it should be called only as "no-architecture"
@classmethod
def run(cls: Type[Handler], args: argparse.Namespace, architecture: str,
configuration: Configuration, no_report: bool) -> None:

View File

@ -37,6 +37,8 @@ class Setup(Handler):
:cvar SUDOERS_PATH: path to sudoers.d include configuration
"""
ALLOW_AUTO_ARCHITECTURE_RUN = False
ARCHBUILD_COMMAND_PATH = Path("/usr/bin/archbuild")
BIN_DIR_PATH = Path("/usr/local/bin")
MIRRORLIST_PATH = Path("/etc/pacman.d/mirrorlist")

View File

@ -33,6 +33,8 @@ class Status(Handler):
package status handler
"""
ALLOW_AUTO_ARCHITECTURE_RUN = False
@classmethod
def run(cls: Type[Handler], args: argparse.Namespace, architecture: str,
configuration: Configuration, no_report: bool) -> None:

View File

@ -32,6 +32,8 @@ class StatusUpdate(Handler):
status update handler
"""
ALLOW_AUTO_ARCHITECTURE_RUN = False
@classmethod
def run(cls: Type[Handler], args: argparse.Namespace, architecture: str,
configuration: Configuration, no_report: bool) -> None:

View File

@ -35,6 +35,8 @@ class User(Handler):
user management handler
"""
ALLOW_AUTO_ARCHITECTURE_RUN = False # it should be called only as "no-architecture"
@classmethod
def run(cls: Type[Handler], args: argparse.Namespace, architecture: str,
configuration: Configuration, no_report: bool) -> None:

View File

@ -31,6 +31,7 @@ class Web(Handler):
web server handler
"""
ALLOW_AUTO_ARCHITECTURE_RUN = False
ALLOW_MULTI_ARCHITECTURE_RUN = False # required to be able to spawn external processes
@classmethod

View File

@ -27,7 +27,7 @@ def args() -> argparse.Namespace:
fixture for command line arguments
:return: command line arguments test instance
"""
return argparse.Namespace(lock=None, force=False, unsafe=False, no_report=True)
return argparse.Namespace(architecture=None, lock=None, force=False, unsafe=False, no_report=True)
@pytest.fixture

View File

@ -71,7 +71,6 @@ def test_extract_architectures(args: argparse.Namespace, configuration: Configur
"""
must generate list of available architectures
"""
args.architecture = []
args.configuration = configuration.path
known_architectures_mock = mocker.patch("ahriman.models.repository_paths.RepositoryPaths.known_architectures")
@ -84,7 +83,6 @@ def test_extract_architectures_empty(args: argparse.Namespace, configuration: Co
"""
must raise exception if no available architectures found
"""
args.architecture = []
args.command = "config"
args.configuration = configuration.path
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.known_architectures", return_value=set())
@ -93,12 +91,12 @@ def test_extract_architectures_empty(args: argparse.Namespace, configuration: Co
Handler.extract_architectures(args)
def test_extract_architectures_exception(args: argparse.Namespace) -> None:
def test_extract_architectures_exception(args: argparse.Namespace, mocker: MockerFixture) -> None:
"""
must raise exception on missing architectures
"""
args.command = "config"
args.architecture = None
mocker.patch.object(Handler, "ALLOW_AUTO_ARCHITECTURE_RUN", False)
with pytest.raises(MissingArchitecture):
Handler.extract_architectures(args)

View File

@ -18,3 +18,10 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc
Dump.run(args, "x86_64", configuration, True)
application_mock.assert_called_once()
print_mock.assert_called()
def test_disallow_auto_architecture_run() -> None:
"""
must not allow multi architecture run
"""
assert not Dump.ALLOW_AUTO_ARCHITECTURE_RUN

View File

@ -16,3 +16,10 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc
Init.run(args, "x86_64", configuration, True)
create_tree_mock.assert_called_once()
init_mock.assert_called_once()
def test_disallow_auto_architecture_run() -> None:
"""
must not allow multi architecture run
"""
assert not Init.ALLOW_AUTO_ARCHITECTURE_RUN

View File

@ -27,3 +27,10 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc
KeyImport.run(args, "x86_64", configuration, True)
application_mock.assert_called_once()
def test_disallow_auto_architecture_run() -> None:
"""
must not allow multi architecture run
"""
assert not KeyImport.ALLOW_AUTO_ARCHITECTURE_RUN

View File

@ -53,3 +53,10 @@ def test_log_fn(args: argparse.Namespace, configuration: Configuration, aur_pack
Search.run(args, "x86_64", configuration, True)
print_mock.assert_called() # we don't really care about call details tbh
def test_disallow_auto_architecture_run() -> None:
"""
must not allow multi architecture run
"""
assert not Search.ALLOW_AUTO_ARCHITECTURE_RUN

View File

@ -149,3 +149,10 @@ def test_create_executable(args: argparse.Namespace, mocker: MockerFixture) -> N
Setup.create_executable(args.build_command, "x86_64")
symlink_text_mock.assert_called_once()
unlink_text_mock.assert_called_once()
def test_disallow_auto_architecture_run() -> None:
"""
must not allow multi architecture run
"""
assert not Setup.ALLOW_AUTO_ARCHITECTURE_RUN

View File

@ -81,3 +81,10 @@ def test_imply_with_report(args: argparse.Namespace, configuration: Configuratio
Status.run(args, "x86_64", configuration, True)
load_mock.assert_called_once()
def test_disallow_auto_architecture_run() -> None:
"""
must not allow multi architecture run
"""
assert not Status.ALLOW_AUTO_ARCHITECTURE_RUN

View File

@ -86,3 +86,10 @@ def test_imply_with_report(args: argparse.Namespace, configuration: Configuratio
StatusUpdate.run(args, "x86_64", configuration, True)
load_mock.assert_called_once()
def test_disallow_auto_architecture_run() -> None:
"""
must not allow multi architecture run
"""
assert not StatusUpdate.ALLOW_AUTO_ARCHITECTURE_RUN

View File

@ -257,3 +257,10 @@ def test_write_configuration_not_loaded(configuration: Configuration, mocker: Mo
User.write_configuration(configuration, secure=True)
write_mock.assert_not_called()
chmod_mock.assert_not_called()
def test_disallow_auto_architecture_run() -> None:
"""
must not allow multi architecture run
"""
assert not User.ALLOW_AUTO_ARCHITECTURE_RUN

View File

@ -31,6 +31,13 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc
run_mock.assert_called_once()
def test_disallow_auto_architecture_run() -> None:
"""
must not allow multi architecture run
"""
assert not Web.ALLOW_AUTO_ARCHITECTURE_RUN
def test_disallow_multi_architecture_run() -> None:
"""
must not allow multi architecture run

View File

@ -41,43 +41,64 @@ def test_multiple_architectures(parser: argparse.ArgumentParser) -> None:
must accept multiple architectures
"""
args = parser.parse_args(["-a", "x86_64", "-a", "i686", "config"])
assert len(args.architecture) == 2
assert args.architecture == ["x86_64", "i686"]
def test_subparsers_add(parser: argparse.ArgumentParser) -> None:
def test_subparsers_add_architecture(parser: argparse.ArgumentParser) -> None:
"""
add command must imply empty architectures list
add command must correctly parse architecture list
"""
args = parser.parse_args(["add", "ahriman"])
assert args.architecture == []
assert args.architecture is None
args = parser.parse_args(["-a", "x86_64", "add", "ahriman"])
assert args.architecture == ["x86_64"]
def test_subparsers_check(parser: argparse.ArgumentParser) -> None:
"""
check command must imply empty architecture list, no-aur, no-manual and dry-run
check command must imply no-aur, no-manual and dry-run
"""
args = parser.parse_args(["check"])
assert args.architecture == []
assert not args.no_aur
assert args.no_manual
assert args.dry_run
def test_subparsers_check_architecture(parser: argparse.ArgumentParser) -> None:
"""
check command must correctly parse architecture list
"""
args = parser.parse_args(["check"])
assert args.architecture is None
args = parser.parse_args(["-a", "x86_64", "check"])
assert args.architecture == ["x86_64"]
def test_subparsers_clean(parser: argparse.ArgumentParser) -> None:
"""
clean command must imply empty architectures list, unsafe and no-log
clean command must imply unsafe and no-log
"""
args = parser.parse_args(["clean"])
assert args.architecture == []
assert args.no_log
assert args.unsafe
def test_subparsers_clean_architecture(parser: argparse.ArgumentParser) -> None:
"""
clean command must correctly parse architecture list
"""
args = parser.parse_args(["clean"])
assert args.architecture is None
args = parser.parse_args(["-a", "x86_64", "clean"])
assert args.architecture == ["x86_64"]
def test_subparsers_config(parser: argparse.ArgumentParser) -> None:
"""
config command must imply lock, no-log, no-report and unsafe
"""
args = parser.parse_args(["config"])
args = parser.parse_args(["-a", "x86_64", "config"])
assert args.architecture == ["x86_64"]
assert args.lock is None
assert args.no_log
assert args.no_report
@ -88,7 +109,8 @@ def test_subparsers_init(parser: argparse.ArgumentParser) -> None:
"""
init command must imply no_report
"""
args = parser.parse_args(["init"])
args = parser.parse_args(["-a", "x86_64", "init"])
assert args.architecture == ["x86_64"]
assert args.no_report
@ -102,36 +124,42 @@ def test_subparsers_key_import(parser: argparse.ArgumentParser) -> None:
assert args.no_report
def test_subparsers_rebuild(parser: argparse.ArgumentParser) -> None:
def test_subparsers_key_import_architecture(parser: argparse.ArgumentParser) -> None:
"""
rebuild command must imply empty architectures list
check command must correctly parse architecture list
"""
args = parser.parse_args(["-a", "x86_64", "key-import", "key"])
assert args.architecture == [""]
def test_subparsers_rebuild_architecture(parser: argparse.ArgumentParser) -> None:
"""
rebuild command must correctly parse architecture list
"""
args = parser.parse_args(["rebuild"])
assert args.architecture == []
assert args.architecture is None
args = parser.parse_args(["-a", "x86_64", "rebuild"])
assert args.architecture == ["x86_64"]
def test_subparsers_remove(parser: argparse.ArgumentParser) -> None:
def test_subparsers_remove_architecture(parser: argparse.ArgumentParser) -> None:
"""
remove command must imply empty architectures list
remove command must correctly parse architecture list
"""
args = parser.parse_args(["remove", "ahriman"])
assert args.architecture == []
assert args.architecture is None
args = parser.parse_args(["-a", "x86_64", "remove", "ahriman"])
assert args.architecture == ["x86_64"]
def test_subparsers_remove_unknown(parser: argparse.ArgumentParser) -> None:
def test_subparsers_report_architecture(parser: argparse.ArgumentParser) -> None:
"""
remove-unknown command must imply empty architectures list
"""
args = parser.parse_args(["remove-unknown"])
assert args.architecture == []
def test_subparsers_report(parser: argparse.ArgumentParser) -> None:
"""
report command must imply empty architectures list
report command must correctly parse architecture list
"""
args = parser.parse_args(["report"])
assert args.architecture == []
assert args.architecture is None
args = parser.parse_args(["-a", "x86_64", "report"])
assert args.architecture == ["x86_64"]
def test_subparsers_search(parser: argparse.ArgumentParser) -> None:
@ -146,12 +174,21 @@ def test_subparsers_search(parser: argparse.ArgumentParser) -> None:
assert args.unsafe
def test_subparsers_search_architecture(parser: argparse.ArgumentParser) -> None:
"""
search command must correctly parse architecture list
"""
args = parser.parse_args(["-a", "x86_64", "search", "ahriman"])
assert args.architecture == [""]
def test_subparsers_setup(parser: argparse.ArgumentParser) -> None:
"""
setup command must imply lock, no-log, no-report and unsafe
"""
args = parser.parse_args(["-a", "x86_64", "setup", "--packager", "John Doe <john@doe.com>",
"--repository", "aur-clone"])
assert args.architecture == ["x86_64"]
assert args.lock is None
assert args.no_log
assert args.no_report
@ -180,12 +217,14 @@ def test_subparsers_setup_option_sign_target(parser: argparse.ArgumentParser) ->
assert all(isinstance(target, SignSettings) for target in args.sign_target)
def test_subparsers_sign(parser: argparse.ArgumentParser) -> None:
def test_subparsers_sign_architecture(parser: argparse.ArgumentParser) -> None:
"""
sign command must imply empty architectures list
sign command must correctly parse architecture list
"""
args = parser.parse_args(["sign"])
assert args.architecture == []
assert args.architecture is None
args = parser.parse_args(["-a", "x86_64", "sign"])
assert args.architecture == ["x86_64"]
def test_subparsers_status(parser: argparse.ArgumentParser) -> None:
@ -193,6 +232,7 @@ def test_subparsers_status(parser: argparse.ArgumentParser) -> None:
status command must imply lock, no-log, no-report and unsafe
"""
args = parser.parse_args(["-a", "x86_64", "status"])
assert args.architecture == ["x86_64"]
assert args.lock is None
assert args.no_log
assert args.no_report
@ -204,6 +244,7 @@ def test_subparsers_status_update(parser: argparse.ArgumentParser) -> None:
status-update command must imply lock, no-log, no-report and unsafe
"""
args = parser.parse_args(["-a", "x86_64", "status-update"])
assert args.architecture == ["x86_64"]
assert args.lock is None
assert args.no_log
assert args.no_report
@ -220,20 +261,24 @@ def test_subparsers_status_update_option_status(parser: argparse.ArgumentParser)
assert isinstance(args.status, BuildStatusEnum)
def test_subparsers_sync(parser: argparse.ArgumentParser) -> None:
def test_subparsers_sync_architecture(parser: argparse.ArgumentParser) -> None:
"""
sync command must imply empty architectures list
sync command must correctly parse architecture list
"""
args = parser.parse_args(["sync"])
assert args.architecture == []
assert args.architecture is None
args = parser.parse_args(["-a", "x86_64", "sync"])
assert args.architecture == ["x86_64"]
def test_subparsers_update(parser: argparse.ArgumentParser) -> None:
def test_subparsers_update_architecture(parser: argparse.ArgumentParser) -> None:
"""
update command must imply empty architectures list
update command must correctly parse architecture list
"""
args = parser.parse_args(["update"])
assert args.architecture == []
assert args.architecture is None
args = parser.parse_args(["-a", "x86_64", "update"])
assert args.architecture == ["x86_64"]
def test_subparsers_user(parser: argparse.ArgumentParser) -> None:
@ -248,6 +293,14 @@ def test_subparsers_user(parser: argparse.ArgumentParser) -> None:
assert args.unsafe
def test_subparsers_user_architecture(parser: argparse.ArgumentParser) -> None:
"""
user command must correctly parse architecture list
"""
args = parser.parse_args(["-a", "x86_64", "user", "username"])
assert args.architecture == [""]
def test_subparsers_user_option_role(parser: argparse.ArgumentParser) -> None:
"""
user command must convert role option to useraccess instance
@ -263,6 +316,7 @@ def test_subparsers_web(parser: argparse.ArgumentParser) -> None:
web command must imply lock, no_report and parser
"""
args = parser.parse_args(["-a", "x86_64", "web"])
assert args.architecture == ["x86_64"]
assert args.lock is None
assert args.no_report
assert args.parser is not None and args.parser()