Skip to content

Commit

Permalink
Restructure pdc client command structure
Browse files Browse the repository at this point in the history
JIRA: PDC-1129
  • Loading branch information
lao605 authored and zkl94 committed Oct 30, 2015
1 parent 55ddbe0 commit faa6411
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 196 deletions.
22 changes: 8 additions & 14 deletions pdc_client/plugin_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#

import logging
import sys


DATA_PREFIX = 'data__'
Expand All @@ -47,7 +46,6 @@ class PDCClientPlugin(object):
def __init__(self, runner):
self.logger = logging.getLogger(self.__class__.__name__)
self.runner = runner
self.help_all = '--help-all' in sys.argv

@property
def client(self):
Expand All @@ -62,22 +60,18 @@ def _before_register(self, parser):
def register(self):
raise NotImplementedError('Plugin must implement `register` method.')

def add_command(self, *args, **kwargs):
"""Define new subcommand.
def set_command(self, *args, **kwargs):
"""Define new command.
For accepted arguments, see `argparse.ArgumentParser.add_argument`.
"""
return self.parser.add_parser(*args, **kwargs)
if 'help' not in kwargs:
kwargs['help'] = ''
cmd = self.parser.add_parser(*args, **kwargs)
self.subparsers = cmd.add_subparsers(metavar='ACTION')

def add_admin_command(self, *args, **kwargs):
"""Define new admin subcommand.
Help of this subcommand will be hidden for regular --help option, but
will show when --help-all is used. Otherwise identical to add_command.
"""
if not self.help_all:
kwargs.pop('help', None)
return self.parser.add_parser(*args, **kwargs)
def add_action(self, *args, **kwargs):
return self.subparsers.add_parser(*args, **kwargs)


def add_parser_arguments(parser, args, group=None, prefix=DATA_PREFIX):
Expand Down
39 changes: 20 additions & 19 deletions pdc_client/plugins/build_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@

class BuildImagePlugin(PDCClientPlugin):
def register(self):
subcmd = self.add_command('build-image-list', help='list all build images')
subcmd.add_argument('--show-md5', action='store_true',
help='whether to display md5 checksums')
add_parser_arguments(subcmd, {'component_name': {},
'rpm_version': {},
'rpm_release': {},
'image_id': {},
'image_format': {},
'md5': {},
'archive_build_nvr': {},
'archive_name': {},
'archive_size': {},
'archive_md5': {},
'release_id': {}},
group='Filtering')
self.set_command('build-image')

subcmd.set_defaults(func=self.list_build_image)
list_parser = self.add_action('list', help='list all build images')
list_parser.add_argument('--show-md5', action='store_true',
help='whether to display md5 checksums')
add_parser_arguments(list_parser, {'component_name': {},
'rpm_version': {},
'rpm_release': {},
'image_id': {},
'image_format': {},
'md5': {},
'archive_build_nvr': {},
'archive_name': {},
'archive_size': {},
'archive_md5': {},
'release_id': {}},
group='Filtering')
list_parser.set_defaults(func=self.list_build_image)

subcmd = self.add_command('build-image-info', help='display details of a build image')
subcmd.add_argument('image_id', metavar='IMAGE_ID')
subcmd.set_defaults(func=self.build_image_info)
info_parser = self.add_action('info', help='display details of a build image')
info_parser.add_argument('image_id', metavar='IMAGE_ID')
info_parser.set_defaults(func=self.build_image_info)

def _print_build_image_list(self, build_images, with_md5=False):
fmt = '{image_id}'
Expand Down
74 changes: 37 additions & 37 deletions pdc_client/plugins/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@

class GlobalComponentPlugin(PDCClientPlugin):
def register(self):
subcmd = self.add_command('global-component-list', help='list all global components')
self.set_command('global-component')

list_parser = self.add_action('list', help='list all global components')
filters = ('dist_git_path contact_role email label name upstream_homepage upstream_scm_type '
'upstream_scm_url'.split())
for arg in filters:
subcmd.add_argument('--' + arg.replace('_', '-'), dest='filter_' + arg)
subcmd.set_defaults(func=self.list_global_components)
list_parser.add_argument('--' + arg.replace('_', '-'), dest='filter_' + arg)
list_parser.set_defaults(func=self.list_global_components)

subcmd = self.add_command('global-component-info', help='display details of a global component')
subcmd.add_argument('global_component_id', metavar='GLOBAL_COMPONENT_ID')
subcmd.set_defaults(func=self.global_component_info)
info_parser = self.add_action('info', help='display details of a global component')
info_parser.add_argument('global_component_id', metavar='GLOBAL_COMPONENT_ID')
info_parser.set_defaults(func=self.global_component_info)

subcmd = self.add_admin_command('global-component-update',
help='update an existing global component')
subcmd.add_argument('global_component_id', metavar='GLOBAL_COMPONENT_ID')
self.add_global_component_arguments(subcmd)
subcmd.set_defaults(func=self.global_component_update)
update_parser = self.add_action('update', help='update an existing global component')
update_parser.add_argument('global_component_id', metavar='GLOBAL_COMPONENT_ID')
self.add_global_component_arguments(update_parser)
update_parser.set_defaults(func=self.global_component_update)

subcmd = self.add_admin_command('global-component-create',
help='create new global component')
self.add_global_component_arguments(subcmd, required=True)
subcmd.set_defaults(func=self.global_component_create)
create_parser = self.add_action('create', help='create new global component')
self.add_global_component_arguments(create_parser, required=True)
create_parser.set_defaults(func=self.global_component_create)

def add_global_component_arguments(self, parser, required=False):
add_create_update_args(parser,
Expand Down Expand Up @@ -117,31 +117,31 @@ def global_component_update(self, args):

class ReleaseComponentPlugin(PDCClientPlugin):
def register(self):
subcmd = self.add_command('release-component-list', help='list all release components')
self.add_include_inactive_release_argument(subcmd)
self.set_command('release-component')

list_parser = self.add_action('list', help='list all release components')
self.add_include_inactive_release_argument(list_parser)
filters = ('active brew_package bugzilla_component contact_role email global_component name release srpm_name '
'type'.split())
for arg in filters:
subcmd.add_argument('--' + arg.replace('_', '-'), dest='filter_' + arg)
subcmd.set_defaults(func=self.list_release_components)

subcmd = self.add_command('release-component-info', help='display details of a release component')
self.add_include_inactive_release_argument(subcmd)
subcmd.add_argument('release_component_id', metavar='RELEASE_COMPONENT_ID')
subcmd.set_defaults(func=self.release_component_info)

subcmd = self.add_admin_command('release-component-update',
help='update an existing release component')
subcmd.add_argument('release_component_id', metavar='RELEASE_COMPONENT_ID')
self.add_release_component_arguments(subcmd)
subcmd.set_defaults(func=self.release_component_update)

subcmd = self.add_admin_command('release-component-create',
help='create new release')
self.add_release_component_arguments(subcmd, required=True)
subcmd.add_argument('--release', dest='release', required=True)
subcmd.add_argument('--global-component', dest='global_component', required=True)
subcmd.set_defaults(func=self.release_component_create)
list_parser.add_argument('--' + arg.replace('_', '-'), dest='filter_' + arg)
list_parser.set_defaults(func=self.list_release_components)

info_parser = self.add_action('info', help='display details of a release component')
self.add_include_inactive_release_argument(info_parser)
info_parser.add_argument('release_component_id', metavar='RELEASE_COMPONENT_ID')
info_parser.set_defaults(func=self.release_component_info)

update_parser = self.add_action('update', help='update an existing release component')
update_parser.add_argument('release_component_id', metavar='RELEASE_COMPONENT_ID')
self.add_release_component_arguments(update_parser)
update_parser.set_defaults(func=self.release_component_update)

create_parser = self.add_action('create', help='create new release component')
self.add_release_component_arguments(create_parser, required=True)
create_parser.add_argument('--release', dest='release', required=True)
create_parser.add_argument('--global-component', dest='global_component', required=True)
create_parser.set_defaults(func=self.release_component_create)

def add_include_inactive_release_argument(self, parser):
parser.add_argument('--include-inactive-release', action='store_true',
Expand Down
37 changes: 21 additions & 16 deletions pdc_client/plugins/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,32 @@

class ComposePlugin(PDCClientPlugin):
def register(self):
subcmd = self.add_command('compose-list', help='list all composes')
subcmd.add_argument('--deleted', action='store_true',
help='show deleted composes')
subcmd.set_defaults(func=self.list_composes)
self.set_command('compose')

subcmd = self.add_command('compose-info', help='display details of a compose')
subcmd.add_argument('compose_id', metavar='COMPOSE_ID')
subcmd.set_defaults(func=self.compose_info)
list_parser = self.add_action('list', help='list all composes')
list_parser.add_argument('--deleted', action='store_true',
help='show deleted composes')
list_parser.set_defaults(func=self.list_composes)

subcmd = self.add_admin_command('compose-update',
info_parser = self.add_action('info', help='display details of a compose')
info_parser.add_argument('compose_id', metavar='COMPOSE_ID')
info_parser.set_defaults(func=self.compose_info)

update_parser = self.add_action('update',
help='partial update an existing compose.',
description='only some compose fields can be modified by this call.\
these are acceptance_testing, linked_releases and rtt_tested_architectures.')
subcmd.add_argument('compose_id', metavar='COMPOSE_ID')
self.add_compose_arguments(subcmd)
subcmd.set_defaults(func=self.compose_update)
self.compose_update = subcmd
these are acceptance_testing, linked_releases and rtt_tested_architectures.')
update_parser.add_argument('compose_id', metavar='COMPOSE_ID')
self.add_compose_arguments(update_parser)
update_parser.set_defaults(func=self.compose_update)
self.compose_update = update_parser

def add_compose_arguments(self, parser):
add_parser_arguments(parser, {
'acceptance_testing': {'choices': ['passed', 'failed', 'untested']},
'linked_releases': {'nargs': '*', 'metavar': 'RELEASE_ID'},
'rtt_tested_architectures': {'nargs': '*', 'default': [], 'help': 'input format VARIANT:ARCHES:TESTING_STATUS'}})
'rtt_tested_architectures': {'nargs': '*', 'default': [],
'help': 'input format VARIANT:ARCHES:TESTING_STATUS'}})

def list_composes(self, args):
filters = {}
Expand Down Expand Up @@ -64,7 +67,7 @@ def compose_info(self, args, compose_id=None):
print fmt.format('Compose Date', compose['compose_date'])
print fmt.format('Compose Respin', compose['compose_respin'])
print fmt.format('Compose Type', compose['compose_type'])
print fmt. format('Acceptance Testing', compose['acceptance_testing'])
print fmt.format('Acceptance Testing', compose['acceptance_testing'])
print fmt.format('Deleted', compose['deleted'])
print fmt.format('Release', compose['release'])
print fmt.format('Rpm Mapping Template', compose['rpm_mapping_template'])
Expand Down Expand Up @@ -106,7 +109,8 @@ def get_compose_data(self, args):
for rtt in rtts:
parts = rtt.split(':')
if not len(parts) == 3:
self.compose_update.error('Please input rtt-tested-architectures in format VARIANT:ARCHES:TESTING_STATUS.\n')
self.compose_update.error(
'Please input rtt-tested-architectures in format VARIANT:ARCHES:TESTING_STATUS.\n')

variant = parts[0]
arches = parts[1]
Expand All @@ -117,4 +121,5 @@ def get_compose_data(self, args):

return data


PLUGIN_CLASSES = [ComposePlugin]
41 changes: 21 additions & 20 deletions pdc_client/plugins/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pdc_client import get_paged
from pdc_client.plugin_helpers import PDCClientPlugin, add_parser_arguments, extract_arguments


info_desc = """Generally there may be duplicate file names. If the file name
you provide matches more that image, you will get a list of all those images
together with their SHA256 checksums. You desambiguate by providing the
Expand All @@ -31,27 +30,28 @@ def size_format(num):

class ImagePlugin(PDCClientPlugin):
def register(self):
subcmd = self.add_command('image-list', help='list all images')
subcmd.add_argument('--show-sha256', action='store_true',
help='whether to display SHA256 checksums along with the file names')
add_parser_arguments(subcmd, {'arch': {},
'compose': {},
'file_name': {},
'image_format': {},
'image_type': {},
'implant_md5': {},
'md5': {},
'sha1': {},
'sha256': {},
'volume_id': {}},
self.set_command('image')

list_parser = self.add_action('list', help='list all images')
list_parser.add_argument('--show-sha256', action='store_true',
help='whether to display SHA256 checksums along with the file names')
add_parser_arguments(list_parser, {'arch': {},
'compose': {},
'file_name': {},
'image_format': {},
'image_type': {},
'implant_md5': {},
'md5': {},
'sha1': {},
'sha256': {},
'volume_id': {}},
group='Filtering')
subcmd.set_defaults(func=self.image_list)
list_parser.set_defaults(func=self.image_list)

subcmd = self.add_command('image-info', help='display details of an image',
description=info_desc)
subcmd.add_argument('filename', metavar='FILENAME')
subcmd.add_argument('--sha256', nargs='?')
subcmd.set_defaults(func=self.image_info)
info_parser = self.add_action('info', help='display details of an image', description=info_desc)
info_parser.add_argument('filename', metavar='FILENAME')
info_parser.add_argument('--sha256', nargs='?')
info_parser.set_defaults(func=self.image_info)

def _print_image_list(self, images, with_sha=False):
fmt = '{file_name}'
Expand Down Expand Up @@ -111,4 +111,5 @@ def image_info(self, args):
for compose in image['composes']:
print ' * {}'.format(compose)


PLUGIN_CLASSES = [ImagePlugin]
6 changes: 4 additions & 2 deletions pdc_client/plugins/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

class PermissionPlugin(PDCClientPlugin):
def register(self):
subcmd = self.add_command('list-my-permissions', help='list my permissions')
subcmd.set_defaults(func=self.permission_list)
self.set_command('permission')

list_parser = self.add_action('list', help='list my permissions')
list_parser.set_defaults(func=self.permission_list)

def permission_list(self, args):
permissions = self.__get_permissions(self.client.auth['current-user'])
Expand Down
42 changes: 21 additions & 21 deletions pdc_client/plugins/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@

class ReleasePlugin(PDCClientPlugin):
def register(self):
subcmd = self.add_command('release-list', help='list all releases')
subcmd.add_argument('--inactive', action='store_true',
help='show only inactive releases')
subcmd.add_argument('--all', action='store_true',
help='show both active and inactive releases')
subcmd.set_defaults(func=self.list_releases)

subcmd = self.add_command('release-info', help='display details of a release')
subcmd.add_argument('release_id', metavar='RELEASE_ID')
subcmd.set_defaults(func=self.release_info)

subcmd = self.add_admin_command('release-update',
help='update an existing release')
subcmd.add_argument('release_id', metavar='RELEASE_ID')
self.add_release_arguments(subcmd)
subcmd.set_defaults(func=self.release_update)

subcmd = self.add_admin_command('release-create',
help='create new release')
self.add_release_arguments(subcmd, required=True)
subcmd.set_defaults(func=self.release_create)
self.set_command('release')

list_parser = self.add_action('list', help='list all releases')
list_parser.add_argument('--inactive', action='store_true',
help='show only inactive releases')
list_parser.add_argument('--all', action='store_true',
help='show both active and inactive releases')
list_parser.set_defaults(func=self.list_releases)

info_parser = self.add_action('info', help='display details of a release')
info_parser.add_argument('release_id', metavar='RELEASE_ID')
info_parser.set_defaults(func=self.release_info)

update_parser = self.add_action('update', help='update an existing release')
update_parser.add_argument('release_id', metavar='RELEASE_ID')
self.add_release_arguments(update_parser)
update_parser.set_defaults(func=self.release_update)

create_parser = self.add_action('create', help='create new release')
self.add_release_arguments(create_parser, required=True)
create_parser.set_defaults(func=self.release_create)

def add_release_arguments(self, parser, required=False):
group = parser.add_mutually_exclusive_group()
Expand Down
Loading

0 comments on commit faa6411

Please sign in to comment.