With this version of Copr CLI one can list someone's coprs and create a new one
for himself.
This relies on cliff and requests to perform its task.
---
Ok, so here is the first draft/lines of code for the copr CLI.
Comments welcome :)
cli_requirements.txt | 4 ++
copr_cli/__init__.py | 2 +
copr_cli/main.log | 5 +++
copr_cli/main.py | 69 ++++++++++++++++++++++++++++++
copr_cli/subcommands.py | 109 ++++++++++++++++++++++++++++++++++++++++++++++++
coprcli-setup.py | 69 ++++++++++++++++++++++++++++++
6 files changed, 258 insertions(+)
create mode 100644 cli_requirements.txt
create mode 100644 copr_cli/__init__.py
create mode 100644 copr_cli/main.log
create mode 100644 copr_cli/main.py
create mode 100644 copr_cli/subcommands.py
create mode 100644 coprcli-setup.py
diff --git a/cli_requirements.txt b/cli_requirements.txt
new file mode 100644
index 0000000..7d3bfe6
--- /dev/null
+++ b/cli_requirements.txt
@@ -0,0 +1,4 @@
+# Used for when working from a virtualenv.
+# Use this file by running "$ pip install -r cli_requirements.txt"
+cliff
+requests
diff --git a/copr_cli/__init__.py b/copr_cli/__init__.py
new file mode 100644
index 0000000..17d0be3
--- /dev/null
+++ b/copr_cli/__init__.py
@@ -0,0 +1,2 @@
+#!/usr/bin/python -tt
+#-*- coding: UTF-8 -*-
diff --git a/copr_cli/main.log b/copr_cli/main.log
new file mode 100644
index 0000000..e98f62a
--- /dev/null
+++ b/copr_cli/main.log
@@ -0,0 +1,5 @@
+[2013-01-27 14:15:46,649] DEBUG __main__ initialize_app
+[2013-01-27 14:16:02,242] DEBUG __main__ initialize_app
+[2013-01-27 14:16:33,725] DEBUG __main__ initialize_app
+[2013-01-27 14:16:57,768] DEBUG __main__ initialize_app
+[2013-01-27 14:30:07,034] DEBUG __main__ initialize_app
diff --git a/copr_cli/main.py b/copr_cli/main.py
new file mode 100644
index 0000000..557d144
--- /dev/null
+++ b/copr_cli/main.py
@@ -0,0 +1,69 @@
+#!/usr/bin/python -tt
+#-*- coding: UTF-8 -*-
+
+import logging
+import sys
+
+
+import cliff.app
+import cliff.commandmanager
+from cliff.commandmanager import CommandManager
+
+__version__ = '0.1.0'
+__description__ = "CLI tool to run copr"
+
+copr_url = 'http://localhost:5000'
+copr_api_url = '{0}/api'.format(copr_url)
+
+
+class CoprCli(cliff.app.App):
+
+ log = logging.getLogger(__name__)
+
+ def __init__(self):
+ manager = cliff.commandmanager.CommandManager('copr_cli.subcommands')
+ super(CoprCli, self).__init__(
+ description=__description__,
+ version=__version__,
+ command_manager=manager,
+ )
+ requests_log = logging.getLogger("requests")
+ requests_log.setLevel(logging.WARN)
+
+ def initialize_app(self, argv):
+ self.log.debug('initialize_app')
+
+ def prepare_to_run_command(self, cmd):
+ self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__)
+
+ def clean_up(self, cmd, result, err):
+ self.log.debug('clean_up %s', cmd.__class__.__name__)
+ if err:
+ self.log.debug('got an error: %s', err)
+
+ # Overload run_subcommand to gracefully handle unknown commands.
+ def run_subcommand(self, argv):
+ try:
+ self.command_manager.find_command(argv)
+ except ValueError as e:
+ if "Unknown command" in str(e):
+ print "%r is an unknown command" % ' '.join(argv)
+ print "Try \"copr -h\""
+ sys.exit(1)
+ else:
+ raise
+
+ return super(CoprCli, self).run_subcommand(argv)
+
+
+def main(argv=sys.argv[1:]):
+ """ Main function """
+ myapp = CoprCli()
+ return myapp.run(argv)
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv[1:]))
+ #add_copr('test2', 'fedora-rawhide-x86_64',
+ #description='Test repos #2')
+ #list_copr()
diff --git a/copr_cli/subcommands.py b/copr_cli/subcommands.py
new file mode 100644
index 0000000..1231208
--- /dev/null
+++ b/copr_cli/subcommands.py
@@ -0,0 +1,109 @@
+#-*- coding: UTF-8 -*-
+
+import ConfigParser
+import json
+import logging
+import requests
+import os
+import sys
+
+import cliff.lister
+import cliff.show
+
+from cliff.command import Command
+
+from main import copr_api_url
+
+
+def set_user():
+ """ Retrieve the user information from the config file.
"""
+ config = ConfigParser.ConfigParser()
+ config.read(os.path.join(os.path.expanduser('~'), '.config',
+ 'copr'))
+ username = config.get('copr-cli', 'username', None)
+ token = config.get('copr-cli', 'token', None)
+ return {'username': username, 'token': token}
+
+
+class List(cliff.lister.Lister):
+ """ List all the copr of a user. """
+
+ log = logging.getLogger(__name__)
+
+ def get_parser(self, prog_name):
+ parser = super(type(self), self).get_parser(prog_name)
+ parser.add_argument("username", nargs='?')
+ return parser
+
+ def take_action(self, args):
+ user = set_user()
+
+ if args.username:
+ user['username'] = args.username
+ URL = '{0}/owned/'.format(copr_api_url)
+ req = requests.get(URL, params=user)
+ output = json.loads(req.text)
+ if 'repos' in output:
+ if output['repos']:
+ columns = ['name', 'description', 'repos',
'instructions']
+ values = []
+ for entry in output['repos']:
+ values.append([entry[key] for key in columns])
+ return (columns, values)
+ else:
+ columns = ['output']
+ values = ['No copr retrieved for user: "{0}"'.format(
+ user['username'])]
+ return (columns, [values])
+ else:
+ columns = ['output']
+ values = ['Wrong output format returned by the server']
+ return (columns, [values])
+
+
+class AddCopr(Command):
+ """ Create a new copr. """
+
+ log = logging.getLogger(__name__)
+
+ def get_parser(self, prog_name):
+ parser = super(type(self), self).get_parser(prog_name)
+ parser.add_argument("name")
+ parser.add_argument("--chroot", dest="chroots",
action='append',
+ help="")
+ parser.add_argument('--repo', dest='repos',
action='append',
+ help="")
+ parser.add_argument('--initial-pkgs', dest='initial_pkgs',
+ action='append',
+ help="")
+ parser.add_argument('--description',
+ help="")
+ parser.add_argument('--instructions',
+ help="")
+ return parser
+
+ def take_action(self, args):
+ user = set_user()
+ URL = '{0}/copr/new/'.format(copr_api_url)
+
+ repos = None
+ if args.repos:
+ repos = ",".join(args.repos)
+ initial_pkgs = None
+ if args.initial_pkgs:
+ initial_pkgs = ",".join(args.initial_pkgs)
+ data = {'name': args.name,
+ 'repos': repos,
+ 'initial_pkgs': initial_pkgs,
+ 'description': args.description,
+ 'instructions': args.instructions
+ }
+ for chroot in args.chroots:
+ data[chroot] = 'y'
+
+ req = requests.post(URL, params=user, data=data)
+ output = json.loads(req.text)
+ if output['output'] == 'ok':
+ print output['message']
+ else:
+ print 'Something went wrong:\n {0}'.format(output['error'])
diff --git a/coprcli-setup.py b/coprcli-setup.py
new file mode 100644
index 0000000..a7a40d3
--- /dev/null
+++ b/coprcli-setup.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+
+try:
+ from setuptools import setup
+except ImportError:
+ from ez_setup import use_setuptools
+ use_setuptools()
+ from setuptools import setup
+
+import sys
+
+f = open('README')
+long_description = f.read().strip()
+f.close()
+
+# Ridiculous as it may seem, we need to import multiprocessing and
+# logging here in order to get tests to pass smoothly on python 2.7.
+try:
+ import multiprocessing
+ import logging
+except ImportError:
+ pass
+
+from copr_cli.main import __description__, __version__
+
+requires = [
+ 'cliff',
+]
+
+subcommands = [
+ 'list = copr_cli.subcommands:List',
+ 'add-copr = copr_cli.subcommands:AddCopr',
+]
+
+__name__ = 'copr-cli'
+__version__ = __version__
+__description__ = __description__
+__author__ = "Pierre-Yves Chibon"
+__author_email__ = "pingou(a)pingoured.fr"
+__url__ = "http://fedorahosted.org/copr/"
+
+setup(
+ name=__name__,
+ version=__version__,
+ description=__description__,
+ long_description=long_description,
+ author=__author__,
+ author_email=__author_email__,
+ url=__url__,
+ license='GPLv2+',
+ classifiers=[
+ "License :: OSI Approved :: GNU General Public License v2 or later
(GPLv2+)",
+ "Programming Language :: Python :: 2",
+ "Programming Language :: Python :: 2.7",
+ "Topic :: System :: Archiving :: Packaging",
+ "Development Status :: 1 - Alpha",
+ ],
+ install_requires=requires,
+ packages=['copr_cli'],
+ namespace_packages=['copr_cli'],
+ include_package_data=True,
+ zip_safe=False,
+ entry_points={
+ 'console_scripts': [
+ 'copr-cli = copr_cli.main:main'
+ ],
+ 'copr_cli.subcommands': subcommands,
+ },
+)
--
1.8.1