Merge "use python-cliff for CLI"

This commit is contained in:
Jenkins 2015-12-31 10:25:46 +00:00 committed by Gerrit Code Review
commit d9f6acb85a
6 changed files with 85 additions and 1 deletions

View File

@ -1,2 +1,3 @@
Fabric>=1.7.0
six>=1.9.0
cliff>=1.7.0

View File

@ -33,4 +33,9 @@ setuptools.setup(
'Fabric >= 1.10.0'],
entry_points={
'console_scripts': [
'shotgun = shotgun.cli:main']})
'shotgun = shotgun.cli:main',
'shotgun2 = shotgun.cli2:main'],
'shotgun': [
'snapshot = shotgun.cli2:SnapshotCommand',
]
})

View File

@ -11,3 +11,10 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import pkg_resources
try:
__version__ = pkg_resources.require(__name__)[0].version
except pkg_resources.DistributionNotFound as e:
__version__ = "Unknown"

View File

@ -17,6 +17,7 @@
import argparse
import json
import logging
import warnings
from shotgun.logger import configure_logger
configure_logger()
@ -68,4 +69,6 @@ def make_snapshot(args):
def main():
"""Entry point"""
warnings.warn("This command is deprecated. "
"Please use 'shotgun2' instead", DeprecationWarning)
make_snapshot(parse_args())

67
shotgun/cli2.py Normal file
View File

@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import yaml
from cliff.app import App
from cliff.command import Command
from cliff.commandmanager import CommandManager
import shotgun
from shotgun.logger import configure_logger
from shotgun.config import Config
from shotgun.manager import Manager
logger = logging.getLogger(__name__)
class Base(object):
def initialize_cmd(self, parsed_args):
with open(parsed_args.config, "r") as f:
self.config = Config(yaml.safe_load(f))
self.manager = Manager(self.config)
class SnapshotCommand(Command, Base):
def get_parser(self, prog_name):
parser = super(SnapshotCommand, self).get_parser(prog_name)
parser.add_argument(
'--config',
required=True,
help='Path to snapshot config file')
return parser
def take_action(self, parsed_args):
"""Generates snapshot
:param parsed_args: argparse object
"""
self.initialize_cmd(parsed_args)
snapshot_path = self.manager.snapshot()
logger.info(u'Snapshot path: {0}'.format(snapshot_path))
def main(argv=None):
configure_logger()
return App(
description="Shotgun CLI",
version=shotgun.__version__,
command_manager=CommandManager('shotgun')
).run(argv)

View File

@ -14,6 +14,7 @@ BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
Prefix: %{_prefix}
BuildArch: noarch
Requires: postgresql
Requires: python-cliff >= 1.7.0
Requires: python-fabric >= 1.10.0
Requires: python-argparse
Requires: python-six >= 1.9.0