From 1a2fe807ad6507a7a29c5fa8d5c91595c92f19c6 Mon Sep 17 00:00:00 2001 From: Dominik Turecek Date: Dec 09 2019 13:23:47 +0000 Subject: cli, python, frontend: add command to list all available chroots Fixes: #1117 --- diff --git a/cli/copr_cli/main.py b/cli/copr_cli/main.py index 632dd9f..41826d0 100644 --- a/cli/copr_cli/main.py +++ b/cli/copr_cli/main.py @@ -11,6 +11,7 @@ import six import simplejson import requests from collections import defaultdict +from textwrap import indent import logging if six.PY2: @@ -534,6 +535,16 @@ class Commands(object): ) print(json_dumps(project_chroot)) + def action_list_chroots(self, args): + """List all currently available chroots. + """ + chroots = self.client.mock_chroot_proxy.get_list() + chroots = simplejson.loads(json_dumps(chroots)) + for chroot, comment in chroots.items(): + print(chroot) + if comment: + print(indent(comment, ' ')) + ######################################################### ### Package actions ### ######################################################### @@ -1050,6 +1061,9 @@ def setup_parser(): parser_get_chroot.add_argument("coprchroot", help="Path to a project chroot as owner/project/chroot or project/chroot") parser_get_chroot.set_defaults(func="action_get_chroot") + parser_list_chroots = subparsers.add_parser("list-chroots", help="List all currently available chroots.") + parser_list_chroots.set_defaults(func="action_list_chroots") + ######################################################### ### Package options ### ######################################################### diff --git a/cli/tests/test_cli.py b/cli/tests/test_cli.py index 802116f..4e6c23b 100644 --- a/cli/tests/test_cli.py +++ b/cli/tests/test_cli.py @@ -681,3 +681,15 @@ def test_edit_permissions_request(ocnfig, action): test_me( # we don't parse '=' here ['some/project', '--admin', 'bad_status=nothing'], {'your user': {'admin': 'bad_status=nothing'}} ) + + +@mock.patch('copr.v3.proxies.mock_chroot.MockChrootProxy.get_list') +def test_list_chroots(list_chroots): + list_chroots.return_value = Munch({ + "fedora-18-x86_64": "", + "fedora-17-x86_64": "A short chroot comment", + "fedora-17-i386": "Chroot comment containing [url with four\nwords](https://copr.fedorainfracloud.org/)", + "fedora-rawhide-i386": "", + }) + + main.main(argv=["list-chroots"]) diff --git a/frontend/copr-frontend.spec b/frontend/copr-frontend.spec index c0eca70..e6496a1 100644 --- a/frontend/copr-frontend.spec +++ b/frontend/copr-frontend.spec @@ -85,6 +85,7 @@ BuildRequires: python3-flask-sqlalchemy BuildRequires: python3-flask-whooshee BuildRequires: python3-flask-wtf BuildRequires: python3-gobject +BuildRequires: python3-html2text BuildRequires: python3-html5-parser BuildRequires: python3-humanize BuildRequires: python3-lxml @@ -138,6 +139,7 @@ Requires: python3-flask-whooshee Requires: python3-flask-wtf Requires: python3-flask-wtf Requires: python3-gobject +Requires: python3-html2text Requires: python3-html5-parser Requires: python3-humanize Requires: python3-lxml diff --git a/frontend/coprs_frontend/coprs/__init__.py b/frontend/coprs_frontend/coprs/__init__.py index 8535757..3050442 100644 --- a/frontend/coprs_frontend/coprs/__init__.py +++ b/frontend/coprs_frontend/coprs/__init__.py @@ -63,7 +63,7 @@ from coprs.views import api_ns from coprs.views.api_ns import api_general from coprs.views import apiv3_ns from coprs.views.apiv3_ns import (apiv3_general, apiv3_builds, apiv3_packages, apiv3_projects, apiv3_project_chroots, - apiv3_modules, apiv3_build_chroots, + apiv3_modules, apiv3_build_chroots, apiv3_mock_chroots, apiv3_permissions) from coprs.views import coprs_ns from coprs.views.coprs_ns import coprs_builds diff --git a/frontend/coprs_frontend/coprs/logic/coprs_logic.py b/frontend/coprs_frontend/coprs/logic/coprs_logic.py index 6abcc43..7d8a525 100644 --- a/frontend/coprs_frontend/coprs/logic/coprs_logic.py +++ b/frontend/coprs_frontend/coprs/logic/coprs_logic.py @@ -806,6 +806,10 @@ class MockChrootsLogic(object): return [ch.name for ch in cls.get_multiple(active_only=True).all()] @classmethod + def active_names_with_comments(cls): + return [(ch.name, ch.comment) for ch in cls.get_multiple(active_only=True).all()] + + @classmethod def new(cls, mock_chroot): db.session.add(mock_chroot) diff --git a/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_mock_chroots.py b/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_mock_chroots.py new file mode 100644 index 0000000..5cbd4c4 --- /dev/null +++ b/frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_mock_chroots.py @@ -0,0 +1,17 @@ +import flask +from html2text import html2text +from coprs.views.apiv3_ns import apiv3_ns +from coprs.logic.coprs_logic import MockChrootsLogic + + +@apiv3_ns.route("/mock-chroots/list") +def list_chroots(): + chroots = MockChrootsLogic.active_names_with_comments() + response = {} + for chroot, comment in chroots: + if comment: + response[chroot] = html2text(comment).strip("\n") + else: + response[chroot] = "" + + return flask.jsonify(response) diff --git a/frontend/coprs_frontend/tests/coprs_test_case.py b/frontend/coprs_frontend/tests/coprs_test_case.py index e7a60bb..eb882e9 100644 --- a/frontend/coprs_frontend/tests/coprs_test_case.py +++ b/frontend/coprs_frontend/tests/coprs_test_case.py @@ -151,11 +151,13 @@ class CoprsTestCase(object): self.mc1.distgit_branch = models.DistGitBranch(name='f18') self.mc2 = models.MockChroot( - os_release="fedora", os_version="17", arch="x86_64", is_active=True) + os_release="fedora", os_version="17", arch="x86_64", is_active=True, + comment="A short chroot comment") self.mc2.distgit_branch = models.DistGitBranch(name='fedora-17') self.mc3 = models.MockChroot( - os_release="fedora", os_version="17", arch="i386", is_active=True) + os_release="fedora", os_version="17", arch="i386", is_active=True, + comment="Chroot comment containing url with four words") self.mc3.distgit_branch = self.mc2.distgit_branch self.mc4 = models.MockChroot( diff --git a/frontend/coprs_frontend/tests/test_apiv3/test_mock_chroot.py b/frontend/coprs_frontend/tests/test_apiv3/test_mock_chroot.py new file mode 100644 index 0000000..b72326a --- /dev/null +++ b/frontend/coprs_frontend/tests/test_apiv3/test_mock_chroot.py @@ -0,0 +1,15 @@ +import json +from tests.coprs_test_case import CoprsTestCase + + +class TestMockChroot(CoprsTestCase): + + def test_list_available_chroots(self, f_mock_chroots): + r = self.tc.get("/api_3/mock-chroots/list") + assert r.status_code == 200 + assert json.loads(r.data) == { + "fedora-18-x86_64": "", + "fedora-17-x86_64": "A short chroot comment", + "fedora-17-i386": "Chroot comment containing [url with four\nwords](https://copr.fedorainfracloud.org/)", + "fedora-rawhide-i386": "", + } diff --git a/python/copr/v3/__init__.py b/python/copr/v3/__init__.py index ea1022d..6251ad6 100644 --- a/python/copr/v3/__init__.py +++ b/python/copr/v3/__init__.py @@ -6,6 +6,7 @@ from .proxies import BaseProxy from .proxies.project import ProjectProxy from .proxies.build import BuildProxy from .proxies.package import PackageProxy +from .proxies.mock_chroot import MockChrootProxy from .proxies.project_chroot import ProjectChrootProxy from .proxies.build_chroot import BuildChrootProxy from .proxies.module import ModuleProxy @@ -24,6 +25,7 @@ __all__ = [ BaseProxy, BuildProxy, PackageProxy, + MockChrootProxy, ProjectChrootProxy, BuildChrootProxy, ModuleProxy, diff --git a/python/copr/v3/client.py b/python/copr/v3/client.py index 47a0276..6bca684 100644 --- a/python/copr/v3/client.py +++ b/python/copr/v3/client.py @@ -4,6 +4,7 @@ from .proxies.project import ProjectProxy from .proxies.build import BuildProxy from .proxies.package import PackageProxy from .proxies.module import ModuleProxy +from .proxies.mock_chroot import MockChrootProxy from .proxies.project_chroot import ProjectChrootProxy from .proxies.build_chroot import BuildChrootProxy @@ -16,6 +17,7 @@ class Client(object): self.build_proxy = BuildProxy(config) self.package_proxy = PackageProxy(config) self.module_proxy = ModuleProxy(config) + self.mock_chroot_proxy = MockChrootProxy(config) self.project_chroot_proxy = ProjectChrootProxy(config) self.build_chroot_proxy = BuildChrootProxy(config) diff --git a/python/copr/v3/proxies/mock_chroot.py b/python/copr/v3/proxies/mock_chroot.py new file mode 100644 index 0000000..022220e --- /dev/null +++ b/python/copr/v3/proxies/mock_chroot.py @@ -0,0 +1,18 @@ +from __future__ import absolute_import + +import os +from . import BaseProxy +from ..requests import Request, munchify + + +class MockChrootProxy(BaseProxy): + + def get_list(self, pagination=None): + """List all currently available chroots. + + :return: Munch + """ + endpoint = "/mock-chroots/list" + request = Request(endpoint, api_base_url=self.api_base_url) + response = request.send() + return munchify(response)