#569 Use click for the command-line
Merged 6 years ago by abompard. Opened 6 years ago by abompard.
abompard/fedora-hubs feature/click  into  develop

@@ -1,6 +1,6 @@ 

  web: /usr/bin/flask-3 run --host 0.0.0.0 --port 5000

- triage: fedora-hubs-triage

- worker: fedora-hubs-worker

+ triage: fedora-hubs triage

+ worker: fedora-hubs worker

  sse: /usr/bin/twistd-3 -l - --pidfile= -n hubs-sse

  fedmsg_hub: /usr/bin/fedmsg-hub-3

  fedmsg_relay: /usr/bin/fedmsg-relay-3

@@ -4,7 +4,7 @@ 

  Documentation=https://pagure.io/fedora-hubs/

  

  [Service]

- ExecStart=/usr/bin/fedora-hubs-triage

+ ExecStart=/usr/bin/fedora-hubs triage

  EnvironmentFile=/etc/sysconfig/fedora-hubs

  Type=simple

  User=hubs

@@ -4,7 +4,7 @@ 

  Documentation=https://pagure.io/fedora-hubs/

  

  [Service]

- ExecStart=/usr/bin/fedora-hubs-worker

+ ExecStart=/usr/bin/fedora-hubs worker

  EnvironmentFile=/etc/sysconfig/fedora-hubs

  Type=simple

  User=hubs

file modified
+3 -1
@@ -25,6 +25,7 @@ 

  BuildRequires:  python3-beautifulsoup4

  BuildRequires:  python3-bleach

  BuildRequires:  python3-blinker

+ BuildRequires:  python3-click

  BuildRequires:  python3-dateutil

  BuildRequires:  python3-decorator

  BuildRequires:  python3-dogpile-cache
@@ -56,6 +57,7 @@ 

  Requires:       python3-beautifulsoup4

  Requires:       python3-bleach

  Requires:       python3-blinker

+ Requires:       python3-click

  Requires:       python3-dateutil

  Requires:       python3-decorator

  Requires:       python3-dogpile-cache
@@ -207,7 +209,7 @@ 

  %doc README.rst

  %{python3_sitelib}/*

  %{_unitdir}/*.service

- %{_bindir}/fedora-hubs-*

+ %{_bindir}/fedora-hubs

  %dir %{_sysconfdir}/%{name}

  %config(noreplace) %attr(640,root,%{username}) %{_sysconfdir}/%{name}/hubs.py

  %config(noreplace) %{_sysconfdir}/%{name}/logging.ini

file modified
+8 -13
@@ -20,13 +20,13 @@ 

  

  from __future__ import unicode_literals

  

- import argparse

  import json

  import logging

  import logging.config

  import random

  import sys

  

+ import click

  import retask.queue

  

  import hubs.app
@@ -34,6 +34,7 @@ 

  import hubs.feed

  import hubs.models

  import hubs.widgets.base

+ #from hubs.commands import cli

  

  

  log = logging.getLogger('hubs.backend.triage')
@@ -175,19 +176,13 @@ 

      return widgets

  

  

- def parse_args(args):

-     parser = argparse.ArgumentParser(

-         description='Triage messages from the bus.')

-     parser.add_argument("-d", "--debug", action="store_true",

-                         help="debugging output level.")

-     return parser.parse_args()

- 

- 

- def main(args=None):

-     args = args if args is not None else sys.argv

-     args = parse_args(args)

+ @click.command("triage")

+ @click.option("-d", "--debug/--no-debug", default=False,

+               help="Debugging output level.")

+ def main(debug):

+     """Triage messages from the bus."""

      logging.config.dictConfig(fedmsg_config['logging'])

-     log_level = logging.DEBUG if args.debug else logging.INFO

+     log_level = logging.DEBUG if debug else logging.INFO

      logging.basicConfig(level=log_level)

  

      # XXX - for flask.url_for to work

file modified
+7 -12
@@ -25,12 +25,12 @@ 

  

  from __future__ import unicode_literals

  

- import argparse

  import json

  import logging

  import logging.config

  import sys

  

+ import click

  import fedmsg.meta

  import retask.queue

  
@@ -74,18 +74,13 @@ 

      sse_queue.enqueue(retask.task.Task(json.dumps(sse_task)))

  

  

- def parse_args(args):

-     parser = argparse.ArgumentParser(description='Rebuild widget caches.')

-     parser.add_argument("-d", "--debug", action="store_true",

-                         help="debugging output level.")

-     return parser.parse_args()

- 

- 

- def main(args=None):

-     args = args if args is not None else sys.argv

-     args = parse_args(args)

+ @click.command("worker")

+ @click.option("-d", "--debug/--no-debug", default=False,

+               help="Debugging output level.")

+ def main(debug):

+     """Rebuild widget caches."""

      logging.config.dictConfig(fedmsg_config['logging'])

-     log_level = logging.DEBUG if args.debug else logging.INFO

+     log_level = logging.DEBUG if debug else logging.INFO

      logging.basicConfig(level=log_level)

  

      # XXX - for flask.url_for to work

file added
+15
@@ -0,0 +1,15 @@ 

+ import click

+ 

+ from hubs.backend.triage import main as triage

+ from hubs.backend.worker import main as worker

+ from hubs.fas.scripts import create_team_from_fas, sync_teams_from_fas

+ 

+ 

+ @click.group()

+ def cli():

+     pass

+ 

+ cli.add_command(triage)

+ cli.add_command(worker)

+ cli.add_command(create_team_from_fas)

+ cli.add_command(sync_teams_from_fas)

file modified
+13 -18
@@ -1,8 +1,7 @@ 

  from __future__ import unicode_literals, print_function

  

  

- from argparse import ArgumentParser

- 

+ import click

  from fedora.client import AppError

  

  import hubs.app
@@ -14,12 +13,10 @@ 

  from .api import sync_team_hub, sync_team_hub_roles

  

  

- def create_group_from_fas():

-     parser = ArgumentParser()

-     parser.add_argument(

-         "name", help="group to create the hub for")

-     args = parser.parse_args()

-     hub_name = args.name

+ @click.command("create-team-from-fas")

+ @click.argument("name")

+ def create_team_from_fas(hub_name):

+     """Create the team hub NAME from FAS."""

      fedmsg_config = get_fedmsg_config()

      hubs.database.init(fedmsg_config['hubs.sqlalchemy.uri'])

      hub = Hub.by_name(hub_name, "team")
@@ -42,20 +39,18 @@ 

      print("It will be synced from FAS in the background.")

  

  

- def sync_group_from_fas():

-     parser = ArgumentParser()

-     parser.add_argument(

-         "name", nargs="*", help="team hub to sync")

-     parser.add_argument(

-         "--no-roles", action="store_true", help="do not sync roles")

-     args = parser.parse_args()

+ @click.command("sync-teams-from-fas")

+ @click.option("--roles/--no-roles", default=False, help="Sync the roles.")

+ @click.argument("name", nargs=-1)

+ def sync_teams_from_fas(roles, names):

+     """Sync all the team hubs NAMEs from FAS."""

      fedmsg_config = get_fedmsg_config()

      hubs.database.init(fedmsg_config['hubs.sqlalchemy.uri'])

-     if not args.name:

+     if not names:

          hubs_to_sync = Hub.query.filter_by(hub_type="team").all()

      else:

          hubs_to_sync = []

-         for hub_name in args.name:

+         for hub_name in names:

              hub = hubs.models.Hub.by_name(hub_name, "team")

              if hub is None:

                  print("The team hub {} does not exist.".format(hub_name))
@@ -67,7 +62,7 @@ 

              affected_users = []

              try:

                  sync_team_hub(hub.id)

-                 if not args.no_roles:

+                 if roles:

                      affected_users = sync_team_hub_roles(hub.id)

              except AppError as e:

                  print("Failed syncing hub {}: {}".format(hub.name, e.message))

file modified
+1
@@ -3,6 +3,7 @@ 

  beautifulsoup4

  bleach

  blinker

+ click

  python-dateutil

  decorator

  dogpile.cache

file modified
+1 -4
@@ -47,10 +47,7 @@ 

              "cache_invalidator = hubs.backend.consumer:CacheInvalidatorExtraordinaire",  # noqa: E501

          ],

          'console_scripts': [

-             "fedora-hubs-triage = hubs.backend.triage:main",

-             "fedora-hubs-worker = hubs.backend.worker:main",

-             "fedora-hubs-create-group-from-fas = hubs.fas.scripts:create_group_from_fas",  # noqa: E501

-             "fedora-hubs-sync-group-from-fas = hubs.fas.scripts:sync_group_from_fas",  # noqa: E501

+             "fedora-hubs = hubs.commands:cli",

          ],

      },

  )

file modified
+1 -1
@@ -4,7 +4,7 @@ 

  Documentation=https://pagure.io/fedora-hubs/

  

  [Service]

- ExecStart=/usr/bin/fedora-hubs-triage

+ ExecStart=/usr/bin/fedora-hubs triage

  WorkingDirectory=/srv/hubs/fedora-hubs/

  Environment=HUBS_CONFIG=/srv/hubs/fedora-hubs/config

  Type=simple

file modified
+1 -1
@@ -4,7 +4,7 @@ 

  Documentation=https://pagure.io/fedora-hubs/

  

  [Service]

- ExecStart=/usr/bin/fedora-hubs-worker

+ ExecStart=/usr/bin/fedora-hubs worker

  Environment=HUBS_CONFIG=/srv/git/fedora-hubs/config

  Type=simple

  User=root