From cacf6b75e3188838e98f5f22e87ff8b6e65a59b2 Mon Sep 17 00:00:00 2001 From: Bulat Gaifullin Date: Tue, 26 Jan 2016 11:06:16 +0300 Subject: [PATCH] Added command-line interface for create_repository Change-Id: I22b39adc9a40be8e9d500a81dfef7cbf49f8b651 Implements: blueprint build-repository --- packetary/cli/commands/base.py | 28 ++++++++----- packetary/cli/commands/clone.py | 3 +- packetary/cli/commands/create.py | 61 ++++++++++++++++++++++++++++ packetary/cli/commands/packages.py | 4 +- packetary/cli/commands/unresolved.py | 3 +- packetary/tests/test_cli_commands.py | 32 +++++++++++++++ setup.cfg | 1 + 7 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 packetary/cli/commands/create.py diff --git a/packetary/cli/commands/base.py b/packetary/cli/commands/base.py index a466623..301c3b8 100644 --- a/packetary/cli/commands/base.py +++ b/packetary/cli/commands/base.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2015 Mirantis, Inc. +# Copyright 2016 Mirantis, Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -56,16 +56,6 @@ class BaseRepoCommand(command.Command): default="x86_64", help='The target architecture.') - parser.add_argument( - '-r', '--repositories', - dest='repositories', - type=read_from_file, - metavar='FILENAME', - required=True, - help="The path to file with list of repositories." - "See documentation about format." - ) - return parser def take_action(self, parsed_args): @@ -92,6 +82,22 @@ class BaseRepoCommand(command.Command): """ +class RepositoriesMixin(object): + def get_parser(self, prog_name): + """Specifies common options.""" + parser = super(RepositoriesMixin, self).get_parser(prog_name) + parser.add_argument( + '-r', '--repositories', + dest='repositories', + type=read_from_file, + metavar='FILENAME', + required=True, + help="The path to file with list of repositories." + "See documentation about format." + ) + return parser + + class PackagesMixin(object): """Added arguments to declare list of packages.""" diff --git a/packetary/cli/commands/clone.py b/packetary/cli/commands/clone.py index 1e0d22e..e114b77 100644 --- a/packetary/cli/commands/clone.py +++ b/packetary/cli/commands/clone.py @@ -18,9 +18,10 @@ from packetary.cli.commands.base import BaseRepoCommand from packetary.cli.commands.base import PackagesMixin +from packetary.cli.commands.base import RepositoriesMixin -class CloneCommand(PackagesMixin, BaseRepoCommand): +class CloneCommand(PackagesMixin, RepositoriesMixin, BaseRepoCommand): """Clones the specified repository to local folder.""" def get_parser(self, prog_name): diff --git a/packetary/cli/commands/create.py b/packetary/cli/commands/create.py new file mode 100644 index 0000000..28c1a92 --- /dev/null +++ b/packetary/cli/commands/create.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- + +# Copyright 2016 Mirantis, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +from packetary.cli.commands.base import BaseRepoCommand +from packetary.cli.commands.utils import read_from_file + + +class CreateCommand(BaseRepoCommand): + """Creates the new repository.""" + + def get_parser(self, prog_name): + parser = super(CreateCommand, self).get_parser(prog_name) + parser.add_argument( + '--repository', + type=read_from_file, + metavar='FILENAME', + required=True, + help="The path of file that contains description of repository." + ) + parser.add_argument( + '--package-files', + type=read_from_file, + metavar='FILENAME', + required=True, + help="The path to file that contains list of URLs \ + of package files." + ) + return parser + + def take_repo_action(self, api, parsed_args): + api.create_repository( + parsed_args.repository, + parsed_args.package_files + ) + self.stdout.write("Successfully completed.") + + +def debug(argv=None): + """Helper to debug the Create command.""" + from packetary.cli.app import debug + debug("create", CreateCommand, argv) + + +if __name__ == "__main__": + debug() diff --git a/packetary/cli/commands/packages.py b/packetary/cli/commands/packages.py index ce4780d..b3fc82d 100644 --- a/packetary/cli/commands/packages.py +++ b/packetary/cli/commands/packages.py @@ -18,9 +18,11 @@ from packetary.cli.commands.base import BaseProduceOutputCommand from packetary.cli.commands.base import PackagesMixin +from packetary.cli.commands.base import RepositoriesMixin -class ListOfPackages(PackagesMixin, BaseProduceOutputCommand): +class ListOfPackages( + PackagesMixin, RepositoriesMixin, BaseProduceOutputCommand): """Gets the list of packages from repository(es).""" columns = ( diff --git a/packetary/cli/commands/unresolved.py b/packetary/cli/commands/unresolved.py index a4b3669..f564024 100644 --- a/packetary/cli/commands/unresolved.py +++ b/packetary/cli/commands/unresolved.py @@ -17,9 +17,10 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from packetary.cli.commands.base import BaseProduceOutputCommand +from packetary.cli.commands.base import RepositoriesMixin -class ListOfUnresolved(BaseProduceOutputCommand): +class ListOfUnresolved(RepositoriesMixin, BaseProduceOutputCommand): """Gets the list of external dependencies for repository(es).""" columns = ( diff --git a/packetary/tests/test_cli_commands.py b/packetary/tests/test_cli_commands.py index d93dd0d..8d248ae 100644 --- a/packetary/tests/test_cli_commands.py +++ b/packetary/tests/test_cli_commands.py @@ -26,12 +26,14 @@ subprocess.mswindows = False from packetary.api import RepositoryApi from packetary.cli.commands import clone +from packetary.cli.commands import create from packetary.cli.commands import packages from packetary.cli.commands import unresolved from packetary.objects.statistics import CopyStatistics from packetary.tests import base from packetary.tests.stubs.generator import gen_package from packetary.tests.stubs.generator import gen_relation +from packetary.tests.stubs.generator import gen_repository @mock.patch("packetary.cli.commands.base.BaseRepoCommand.stdout") @@ -56,6 +58,11 @@ class TestCliCommands(base.TestCase): "--skip-mandatory" ] + create_argv = [ + "--repository", "repository.yaml", + "--package-files", "package-files.yaml", + ] + packages_argv = [ "-r", "repositories.yaml", "-t", "deb", @@ -145,3 +152,28 @@ class TestCliCommands(base.TestCase): "test; any; -", stdout_mock.write.call_args_list[3][0][0] ) + + @mock.patch("packetary.cli.commands.create.read_from_file") + def test_create_cmd(self, read_file_in_create_mock, api_mock, + read_file_mock, stdout_mock): + read_file_in_create_mock.side_effect = [ + [{"name": "repo"}], + ["/test1.deb", "/test2.deb", "/test3.deb"], + ] + api_instance = mock.MagicMock(spec=RepositoryApi) + api_mock.create.return_value = api_instance + api_instance.create_repository.return_value = gen_repository() + self.start_cmd(create, self.create_argv) + api_mock.create.assert_called_once_with( + mock.ANY, "deb", "x86_64" + ) + self.check_common_config(api_mock.create.call_args[0][0]) + read_file_in_create_mock.assert_any_call("repository.yaml") + read_file_in_create_mock.assert_any_call("package-files.yaml") + api_instance.create_repository.assert_called_once_with( + [{'name': 'repo'}], + ['/test1.deb', '/test2.deb', '/test3.deb'] + ) + stdout_mock.write.assert_called_once_with( + "Successfully completed." + ) diff --git a/setup.cfg b/setup.cfg index 8fa8e31..6baab3d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,6 +36,7 @@ packetary.drivers = packetary = clone=packetary.cli.commands.clone:CloneCommand + create=packetary.cli.commands.create:CreateCommand packages=packetary.cli.commands.packages:ListOfPackages unresolved=packetary.cli.commands.unresolved:ListOfUnresolved