[copr] master: Introduce MockChrootLogic - relax the mock chroot name requirements (c89f919)

bkabrda at fedoraproject.org bkabrda at fedoraproject.org
Mon Jan 28 13:38:22 UTC 2013


Repository : http://git.fedorahosted.org/cgit/copr.git

On branch  : master

>---------------------------------------------------------------

commit c89f91968423be3765afaf73c59db0ceef5a8239
Author: Bohuslav Kabrda <bkabrda at redhat.com>
Date:   Mon Jan 28 14:23:32 2013 +0100

    Introduce MockChrootLogic
    - relax the mock chroot name requirements


>---------------------------------------------------------------

 coprs_frontend/coprs/exceptions.py        |    3 +
 coprs_frontend/coprs/logic/coprs_logic.py |   71 +++++++++++++++++++++++++++++
 coprs_frontend/coprs/models.py            |    6 ---
 coprs_frontend/manage.py                  |   56 +++++++++--------------
 4 files changed, 96 insertions(+), 40 deletions(-)

diff --git a/coprs_frontend/coprs/exceptions.py b/coprs_frontend/coprs/exceptions.py
index 8635520..09888e2 100644
--- a/coprs_frontend/coprs/exceptions.py
+++ b/coprs_frontend/coprs/exceptions.py
@@ -4,6 +4,9 @@ class ArgumentMissingException(BaseException):
 class MalformedArgumentException(ValueError):
     pass
 
+class NotFoundException(BaseException):
+    pass
+
 class DuplicateException(BaseException):
     pass
 
diff --git a/coprs_frontend/coprs/logic/coprs_logic.py b/coprs_frontend/coprs/logic/coprs_logic.py
index fc0293d..3403c04 100644
--- a/coprs_frontend/coprs/logic/coprs_logic.py
+++ b/coprs_frontend/coprs/logic/coprs_logic.py
@@ -160,3 +160,74 @@ class CoprChrootsLogic(object):
         for mock_chroot in current_chroots:
             if mock_chroot not in new_chroots:
                 copr.mock_chroots.remove(mock_chroot)
+
+class MockChrootsLogic(object):
+    @classmethod
+    def get(cls, user, os_release, os_version, arch, active_only = False):
+        return models.MockChroot.query.filter(models.MockChroot.os_release==os_release,
+                                              models.MockChroot.os_version==os_version,
+                                              models.MockChroot.arch==arch)
+
+    @classmethod
+    def get_multiple(cls, user, active_only=False):
+        query = models.MockChroot.query
+        if active_only:
+            query = query.filter(models.MockChroot.is_active==True)
+
+    @classmethod
+    def add(cls, user, name):
+        name_tuple = cls.tuple_from_name(user, name)
+        if cls.get(user, *name_tuple).first():
+            raise exceptions.DuplicateException('Mock chroot with this name already exists.')
+        new_chroot = models.MockChroot(os_release=name_tuple[0],
+                                       os_version=name_tuple[1],
+                                       arch=name_tuple[2])
+        cls.new(user, new_chroot)
+        return new_chroot
+
+    @classmethod
+    def new(cls, user, mock_chroot):
+        db.session.add(mock_chroot)
+
+    @classmethod
+    def edit_by_name(cls, user, name, is_active):
+        name_tuple = cls.tuple_from_name(user, name)
+        mock_chroot = cls.get(user, *name_tuple).first()
+        if not mock_chroot:
+            raise exceptions.NotFoundException('Mock chroot with this name doesn\'t exist.')
+
+        mock_chroot.is_active = is_active
+        cls.update(user, mock_chroot)
+        return mock_chroot
+
+    @classmethod
+    def update(cls, user, mock_chroot):
+        db.session.add(mock_chroot)
+
+
+    @classmethod
+    def delete_by_name(cls, user, name):
+        name_tuple = cls.tuple_from_name(user, name)
+        mock_chroot = cls.get(user, *name_tuple).first()
+        if not mock_chroot:
+            raise exceptions.NotFoundException('Mock chroot with this name doesn\'t exist.')
+
+        cls.delete(user, mock_chroot)
+
+    @classmethod
+    def delete(cls, user, mock_chroot):
+        db.session.delete(mock_chroot)
+
+    @classmethod
+    def tuple_from_name(cls, user, name):
+        split_name = name.rsplit('-')
+        if len(split_name) < 2:
+            raise exceptions.MalformedArgumentException(
+                    'Chroot Name doesn\'t contain dash, can\'t determine chroot architecure.')
+        if '-' in split_name[0]:
+            os_release, os_version = split_name[0].rsplit('-')
+        else:
+            os_release, os_version = split_name[0], ''
+
+        arch = split_name[1]
+        return (os_release, os_version, arch)
diff --git a/coprs_frontend/coprs/models.py b/coprs_frontend/coprs/models.py
index f75586f..b59e7fa 100644
--- a/coprs_frontend/coprs/models.py
+++ b/coprs_frontend/coprs/models.py
@@ -193,12 +193,6 @@ class MockChroot(db.Model, Serializer):
     def chroot_name(self):
         return '{0}-{1}-{2}'.format(self.os_release, self.os_version, self.arch)
 
-    @classmethod
-    def get(cls, os_release, os_version, arch, active_only = False):
-        return cls.query.filter(cls.os_release==os_release,
-                                cls.os_version==os_version,
-                                cls.arch==arch).first()
-
 class CoprChroot(db.Model, Serializer):
     mock_chroot_id = db.Column(db.Integer, db.ForeignKey('mock_chroot.id'), primary_key = True)
     mock_chroot = db.relationship('MockChroot', backref = db.backref('copr_chroots'))
diff --git a/coprs_frontend/manage.py b/coprs_frontend/manage.py
index 766ca46..5c505be 100755
--- a/coprs_frontend/manage.py
+++ b/coprs_frontend/manage.py
@@ -5,7 +5,11 @@ import os
 import flask
 from flask.ext.script import Manager, Command, Option
 
-from coprs import app, db, models
+from coprs import app
+from coprs import db
+from coprs import exceptions
+from coprs import models
+from coprs.logic import coprs_logic
 
 class CreateSqliteFileCommand(Command):
     'Create the sqlite DB file (not the tables). Used for alembic, "create_db" does this automatically.'
@@ -63,37 +67,26 @@ class CreateChrootCommand(ChrootCommand):
     'Creates a mock chroot in DB'
     def run(self, chroot_names):
         for chroot_name in chroot_names:
-            split_chroot = chroot_name.split('-')
-            if len(split_chroot) < 3:
+            try:
+                coprs_logic.MockChrootsLogic.add(None, chroot_name)
+                db.session.commit()
+            except exceptions.MalformedArgumentException:
                 self.print_invalid_format(chroot_name)
-            elif models.MockChroot.get(*split_chroot):
+            except exceptions.DuplicateException:
                 self.print_already_exists(chroot_name)
-            else:
-                new_chroot = models.MockChroot(os_release=split_chroot[0],
-                                               os_version=split_chroot[1],
-                                               arch=split_chroot[2],
-                                               is_active=True)
-                db.session.add(new_chroot)
-                db.session.commit()
 
 class AlterChrootCommand(ChrootCommand):
     'Activates or deactivates a chroot'
     def run(self, chroot_names, action):
+        activate = (action == 'activate')
         for chroot_name in chroot_names:
-            split_chroot = chroot_name.split('-')
-            if len(split_chroot) < 3:
+            try:
+                coprs_logic.MockChrootsLogic.edit_by_name(None, chroot_name, activate)
+                db.session.commit()
+            except exceptions.MalformedArgumentException:
                 self.print_invalid_format(chroot_name)
-            chroot = models.MockChroot.get(*split_chroot)
-            if not chroot:
+            except exceptions.NotFoundException:
                 self.print_doesnt_exist(chroot_name)
-            else:
-                if action == 'activate':
-                    chroot.is_active = True
-                else:
-                    chroot.is_active = False
-
-                db.session.add(chroot)
-                db.session.commit()
 
     option_list = ChrootCommand.option_list + (
             Option('--action',
@@ -108,23 +101,18 @@ class DropChrootCommand(ChrootCommand):
     'Activates or deactivates a chroot'
     def run(self, chroot_names):
         for chroot_name in chroot_names:
-            split_chroot = chroot_name.split('-')
-            if len(split_chroot) < 3:
+            try:
+                coprs_logic.MockChrootsLogic.delete_by_name(None, chroot_name)
+                db.session.commit()
+            except exceptions.MalformedArgumentException:
                 self.print_invalid_format(chroot_name)
-            chroot = models.MockChroot.get(*split_chroot)
-            if not chroot:
+            except exceptions.NotFoundException:
                 self.print_doesnt_exist(chroot_name)
-            else:
-                db.session.delete(chroot)
-                db.session.commit()
 
 class DisplayChrootsCommand(Command):
     'Displays current mock chroots'
     def run(self, active_only):
-        chroots = models.MockChroot.query
-        if active_only:
-            chroots = chroots.filter(models.MockChroot.is_active==True)
-        for ch in chroots:
+        for ch in coprs_logic.MockChrootsLogic.get_multiple(active_only=active_only):
             print ch.chroot_name
 
     option_list = (



More information about the copr-devel mailing list