Repository: fedora-infra/bodhi

Issue:
Ship the development bshell as bodhi-shell
We have a super nice utility in the dev box called ```bshell``` that gets you a Python shell initialized with lots of handy Bodhi, like aliases for the models. We should make a new server CLI called ```bodhi-shell``` that does this for user convenience.
Ship the development bshell as bodhi-shell
We have a super nice utility in the dev box called ```bshell``` that gets you a Python shell initialized with lots of handy Bodhi, like aliases for the models. We should make a new server CLI called ```bodhi-shell``` that does this for user convenience.


Discussion:

Resolving patch:
diff --git a/bodhi/server/scripts/bshell.py b/bodhi/server/scripts/bshell.py
new file mode 100644
--- /dev/null
+++ b/bodhi/server/scripts/bshell.py
@@ -0,0 +1,42 @@
+# Copyright (c) 2018 Sebastian Wojciechowski
+#
+# This file is part of Bodhi.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+"""This script will run a handy python shell initialized with Bodhi models."""
+
+import sys
+from subprocess import call
+import logging
+
+import click
+
+from bodhi.server.config import get_configfile
+
+
+logger = logging.getLogger('bodhi-shell')
+logging.basicConfig(level=logging.INFO)
+
+
+@click.command()
+@click.version_option(message='%(version)s')
+def get_bodhi_shell():
+    """Run python shell initialized with Bodhi models."""
+    configfile = get_configfile()
+    if configfile is not None:
+        call(['pshell-3', configfile])
+    else:
+        click.echo("Config file not found!")
+        sys.exit(1)
diff --git a/docs/conf.py b/docs/conf.py
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -264,6 +264,8 @@
      'Look for overrides that are past their expiration dates and mark them expired',
      ['Randy Barlow'], 1),
     ('user/man_pages/bodhi-sar', 'bodhi-sar', 'display user data', ['Randy Barlow'], 1),
+    ('user/man_pages/bodhi-shell', 'bodhi-shell', 'run python shell initialized with Bodhi models',
+     ['Randy Barlow', 'Sebastian Wojciechowski'], 1),
     ('user/man_pages/bodhi-skopeo-lite', 'bodhi-skopeo-lite',
      'Copy containers between registries',
      ['Owen Taylor'], 1),
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -163,6 +163,7 @@ def get_requirements(requirements_file='requirements.txt'):
     bodhi-check-policies = bodhi.server.scripts.check_policies:check
     bodhi-skopeo-lite = bodhi.server.scripts.skopeo_lite:main
     bodhi-sar = bodhi.server.scripts.sar:get_user_data
+    bodhi-shell = bodhi.server.scripts.bshell:get_bodhi_shell
     [moksha.consumer]
     masher = bodhi.server.consumers.masher:Masher
     updates = bodhi.server.consumers.updates:UpdatesHandler


Test patch:
diff --git a/bodhi/tests/server/scripts/test_bshell.py b/bodhi/tests/server/scripts/test_bshell.py
new file mode 100644
--- /dev/null
+++ b/bodhi/tests/server/scripts/test_bshell.py
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+# Copyright © 2018 Sebastian Wojciechowski
+#
+# This file is part of Bodhi.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+"""
+This module contains tests for the bodhi.server.scripts.bshell module.
+"""
+
+import unittest
+
+from click import testing
+from mock import patch
+
+from bodhi.server.scripts import bshell
+
+
+class TestMain(unittest.TestCase):
+    """
+    This class contains tests for the get_bodhi_shell() function.
+    """
+    @patch('bodhi.server.scripts.bshell.get_configfile')
+    @patch('bodhi.server.scripts.bshell.call')
+    def test_config_file_found(self, call, get_configfile):
+        """Assert correct behavior when the config file exists"""
+        get_configfile.return_value = "/path/to/config.ini"
+        runner = testing.CliRunner()
+        r = runner.invoke(bshell.get_bodhi_shell)
+
+        self.assertEqual(r.exit_code, 0)
+        self.assertEqual(r.output, "")
+        call.assert_called_once_with(['pshell-3', '/path/to/config.ini'])
+
+    @patch('bodhi.server.scripts.bshell.get_configfile')
+    @patch('bodhi.server.scripts.bshell.call')
+    def test_config_file_not_found(self, call, get_configfile):
+        """Assert correct behavior when the config file is not found"""
+        get_configfile.return_value = None
+        runner = testing.CliRunner()
+        r = runner.invoke(bshell.get_bodhi_shell)
+
+        self.assertEqual(r.exit_code, 1)
+        self.assertEqual(r.output, "Config file not found!\n")
+        call.assert_not_called()