Package coprs :: Package logic :: Module coprs_logic
[hide private]
[frames] | no frames]

Source Code for Module coprs.logic.coprs_logic

  1  import os 
  2  import time 
  3  import datetime 
  4   
  5  from sqlalchemy import and_ 
  6  from sqlalchemy.sql import func 
  7  from sqlalchemy import asc 
  8  from sqlalchemy.event import listen 
  9  from sqlalchemy.orm.attributes import NEVER_SET 
 10  from sqlalchemy.orm.exc import NoResultFound 
 11  from sqlalchemy.orm.attributes import get_history 
 12   
 13  from copr_common.enums import ActionTypeEnum, BackendResultEnum 
 14  from coprs import db 
 15  from coprs import exceptions 
 16  from coprs import helpers 
 17  from coprs import models 
 18  from coprs.exceptions import MalformedArgumentException 
 19  from coprs.logic import users_logic 
 20  from coprs.whoosheers import CoprWhoosheer 
 21  from coprs.helpers import fix_protocol_for_backend 
 22   
 23  from coprs.logic.actions_logic import ActionsLogic 
 24  from coprs.logic.users_logic import UsersLogic 
25 26 27 -class CoprsLogic(object):
28 """ 29 Used for manipulating Coprs. 30 31 All methods accept user object as a first argument, 32 as this may be needed in future. 33 """ 34 35 @classmethod
36 - def get_all(cls):
37 """ Return all coprs without those which are deleted. """ 38 query = (db.session.query(models.Copr) 39 .join(models.Copr.user) 40 .options(db.contains_eager(models.Copr.user)) 41 .filter(models.Copr.deleted == False)) 42 return query
43 44 @classmethod
45 - def get_by_id(cls, copr_id):
46 return cls.get_all().filter(models.Copr.id == copr_id)
47 48 @classmethod
49 - def attach_build(cls, query):
50 query = (query.outerjoin(models.Copr.builds) 51 .options(db.contains_eager(models.Copr.builds)) 52 .order_by(models.Build.submitted_on.desc())) 53 return query
54 55 @classmethod
56 - def attach_mock_chroots(cls, query):
57 query = (query.outerjoin(*models.Copr.mock_chroots.attr) 58 .options(db.contains_eager(*models.Copr.mock_chroots.attr)) 59 .order_by(models.MockChroot.os_release.asc()) 60 .order_by(models.MockChroot.os_version.asc()) 61 .order_by(models.MockChroot.arch.asc())) 62 return query
63 64 @classmethod
65 - def get_multiple_by_username(cls, username, **kwargs):
66 with_builds = kwargs.get("with_builds", False) 67 with_mock_chroots = kwargs.get("with_mock_chroots", False) 68 69 query = ( 70 cls.get_all() 71 .filter(models.User.username == username) 72 ) 73 74 if with_builds: 75 query = cls.attach_build(query) 76 77 if with_mock_chroots: 78 query = cls.attach_mock_chroots(query) 79 80 return query
81 82 @classmethod
83 - def get_multiple_by_group_id(cls, group_id, **kwargs):
84 with_builds = kwargs.get("with_builds", False) 85 with_mock_chroots = kwargs.get("with_mock_chroots", False) 86 87 query = ( 88 cls.get_all() 89 .filter(models.Copr.group_id == group_id) 90 ) 91 92 if with_builds: 93 query = cls.attach_build(query) 94 95 if with_mock_chroots: 96 query = cls.attach_mock_chroots(query) 97 98 return query
99 100 @classmethod
101 - def get(cls, username, coprname, **kwargs):
102 query = cls.get_multiple_by_username(username, **kwargs) 103 query = query.filter(models.Copr.name == coprname) 104 return query
105 106 @classmethod
107 - def get_by_group_id(cls, group_id, coprname, **kwargs):
108 query = cls.get_multiple_by_group_id(group_id, **kwargs) 109 query = query.filter(models.Copr.name == coprname) 110 return query
111 112 @classmethod
113 - def get_multiple(cls, include_deleted=False, include_unlisted_on_hp=True):
114 query = ( 115 db.session.query(models.Copr) 116 .join(models.Copr.user) 117 .outerjoin(models.Group) 118 .options(db.contains_eager(models.Copr.user)) 119 ) 120 121 if not include_deleted: 122 query = query.filter(models.Copr.deleted.is_(False)) 123 124 if not include_unlisted_on_hp: 125 query = query.filter(models.Copr.unlisted_on_hp.is_(False)) 126 127 return query
128 129 @classmethod
130 - def set_query_order(cls, query, desc=False):
131 if desc: 132 query = query.order_by(models.Copr.id.desc()) 133 else: 134 query = query.order_by(models.Copr.id.asc()) 135 return query
136 137 # user_relation="owned", username=username, with_mock_chroots=False 138 @classmethod
139 - def get_multiple_owned_by_username(cls, username):
140 query = cls.get_multiple() 141 return query.filter(models.User.username == username)
142 143 @classmethod
144 - def filter_by_name(cls, query, name):
145 return query.filter(models.Copr.name == name)
146 147 @classmethod
148 - def filter_by_user_name(cls, query, username):
149 # should be already joined with the User table 150 return query.filter(models.User.username == username)
151 152 @classmethod
153 - def filter_by_group_name(cls, query, group_name):
154 # should be already joined with the Group table 155 return query.filter(models.Group.name == group_name)
156 157 @classmethod
158 - def filter_without_group_projects(cls, query):
159 return query.filter(models.Copr.group_id.is_(None))
160 161 @classmethod
162 - def join_builds(cls, query):
163 return (query.outerjoin(models.Copr.builds) 164 .options(db.contains_eager(models.Copr.builds)) 165 .order_by(models.Build.submitted_on.desc()))
166 167 @classmethod
168 - def join_mock_chroots(cls, query):
169 return (query.outerjoin(*models.Copr.mock_chroots.attr) 170 .options(db.contains_eager(*models.Copr.mock_chroots.attr)) 171 .order_by(models.MockChroot.os_release.asc()) 172 .order_by(models.MockChroot.os_version.asc()) 173 .order_by(models.MockChroot.arch.asc()))
174 175 @classmethod
176 - def get_playground(cls):
177 return cls.get_all().filter(models.Copr.playground == True)
178 179 @classmethod
180 - def set_playground(cls, user, copr):
181 if user.admin: 182 db.session.add(copr) 183 pass 184 else: 185 raise exceptions.InsufficientRightsException( 186 "User is not a system admin")
187 188 @classmethod
189 - def get_multiple_fulltext(cls, search_string):
190 query = (models.Copr.query.join(models.User) 191 .filter(models.Copr.deleted == False)) 192 if "/" in search_string: # copr search by its full name 193 if search_string[0] == '@': # searching for @group/project 194 group_name = "%{}%".format(search_string.split("/")[0][1:]) 195 project = "%{}%".format(search_string.split("/")[1]) 196 query = query.filter(and_(models.Group.name.ilike(group_name), 197 models.Copr.name.ilike(project), 198 models.Group.id == models.Copr.group_id)) 199 query = query.order_by(asc(func.length(models.Group.name)+func.length(models.Copr.name))) 200 else: # searching for user/project 201 user_name = "%{}%".format(search_string.split("/")[0]) 202 project = "%{}%".format(search_string.split("/")[1]) 203 query = query.filter(and_(models.User.username.ilike(user_name), 204 models.Copr.name.ilike(project), 205 models.User.id == models.Copr.user_id)) 206 query = query.order_by(asc(func.length(models.User.username)+func.length(models.Copr.name))) 207 else: # fulltext search 208 query = query.whooshee_search(search_string, whoosheer=CoprWhoosheer, order_by_relevance=100) 209 return query
210 211 @classmethod
212 - def add(cls, user, name, selected_chroots, repos=None, description=None, 213 instructions=None, check_for_duplicates=False, group=None, persistent=False, 214 auto_prune=True, use_bootstrap_container=False, follow_fedora_branching=False, **kwargs):
215 216 if not user.admin and persistent: 217 raise exceptions.NonAdminCannotCreatePersistentProject() 218 219 if not user.admin and not auto_prune: 220 raise exceptions.NonAdminCannotDisableAutoPrunning() 221 222 # form validation checks for duplicates 223 cls.new(user, name, group, check_for_duplicates=check_for_duplicates) 224 225 copr = models.Copr(name=name, 226 repos=repos or u"", 227 user=user, 228 description=description or u"", 229 instructions=instructions or u"", 230 created_on=int(time.time()), 231 persistent=persistent, 232 auto_prune=auto_prune, 233 use_bootstrap_container=use_bootstrap_container, 234 follow_fedora_branching=follow_fedora_branching, 235 **kwargs) 236 237 238 if group is not None: 239 UsersLogic.raise_if_not_in_group(user, group) 240 copr.group = group 241 242 copr_dir = models.CoprDir( 243 main=True, 244 name=name, 245 copr=copr) 246 247 db.session.add(copr_dir) 248 db.session.add(copr) 249 250 CoprChrootsLogic.new_from_names( 251 copr, selected_chroots) 252 253 db.session.flush() 254 ActionsLogic.send_create_gpg_key(copr) 255 256 return copr
257 258 @classmethod
259 - def new(cls, user, copr_name, group=None, check_for_duplicates=True):
260 if check_for_duplicates: 261 if group is None and cls.exists_for_user(user, copr_name).all(): 262 raise exceptions.DuplicateException( 263 "Copr: '{0}/{1}' already exists".format(user.name, copr_name)) 264 elif group: 265 if cls.exists_for_group(group, copr_name).all(): 266 db.session.rollback() 267 raise exceptions.DuplicateException( 268 "Copr: '@{0}/{1}' already exists".format(group.name, copr_name))
269 270 @classmethod
271 - def update(cls, user, copr):
272 # we should call get_history before other requests, otherwise 273 # the changes would be forgotten 274 if get_history(copr, "name").has_changes(): 275 raise MalformedArgumentException("Change name of the project is forbidden") 276 277 users_logic.UsersLogic.raise_if_cant_update_copr( 278 user, copr, "Only owners and admins may update their projects.") 279 280 if not user.admin and not copr.auto_prune: 281 raise exceptions.NonAdminCannotDisableAutoPrunning() 282 283 db.session.add(copr)
284 285 @classmethod
286 - def delete_unsafe(cls, user, copr):
287 """ 288 Deletes copr without termination of ongoing builds. 289 """ 290 cls.raise_if_cant_delete(user, copr) 291 # TODO: do we want to dump the information somewhere, so that we can 292 # search it in future? 293 cls.raise_if_unfinished_blocking_action( 294 copr, "Can't delete this project," 295 " another operation is in progress: {action}") 296 297 ActionsLogic.send_delete_copr(copr) 298 CoprDirsLogic.delete_all_by_copr(copr) 299 300 copr.deleted = True 301 return copr
302 303 @classmethod
304 - def exists_for_user(cls, user, coprname, incl_deleted=False):
305 existing = (models.Copr.query 306 .filter(models.Copr.name == coprname) 307 .filter(models.Copr.user_id == user.id)) 308 309 if not incl_deleted: 310 existing = existing.filter(models.Copr.deleted == False) 311 312 return cls.filter_without_group_projects(existing)
313 314 @classmethod
315 - def exists_for_group(cls, group, coprname, incl_deleted=False):
316 existing = (models.Copr.query 317 .filter(models.Copr.name == coprname) 318 .filter(models.Copr.group_id == group.id)) 319 320 if not incl_deleted: 321 existing = existing.filter(models.Copr.deleted == False) 322 323 return existing
324 325 @classmethod
326 - def unfinished_blocking_actions_for(cls, copr):
327 blocking_actions = [ActionTypeEnum("delete")] 328 329 actions = (models.Action.query 330 .filter(models.Action.object_type == "copr") 331 .filter(models.Action.object_id == copr.id) 332 .filter(models.Action.result == 333 BackendResultEnum("waiting")) 334 .filter(models.Action.action_type.in_(blocking_actions))) 335 336 return actions
337 338 @classmethod
339 - def get_yum_repos(cls, copr, empty=False):
340 repos = {} 341 release_tmpl = "{chroot.os_release}-{chroot.os_version}-{chroot.arch}" 342 build = models.Build.query.filter(models.Build.copr_id == copr.id).first() 343 if build or empty: 344 for chroot in copr.active_chroots: 345 release = release_tmpl.format(chroot=chroot) 346 repos[release] = fix_protocol_for_backend( 347 os.path.join(copr.repo_url, release + '/')) 348 return repos
349 350 @classmethod
351 - def raise_if_unfinished_blocking_action(cls, copr, message):
352 """ 353 Raise ActionInProgressException if given copr has an unfinished 354 action. Return None otherwise. 355 """ 356 357 unfinished_actions = cls.unfinished_blocking_actions_for(copr).all() 358 if unfinished_actions: 359 raise exceptions.ActionInProgressException( 360 message, unfinished_actions[0])
361 362 @classmethod
363 - def raise_if_cant_delete(cls, user, copr):
364 """ 365 Raise InsufficientRightsException if given copr cant be deleted 366 by given user. Return None otherwise. 367 """ 368 369 if not user.admin and user != copr.user: 370 raise exceptions.InsufficientRightsException( 371 "Only owners may delete their projects.")
372
373 374 -class CoprPermissionsLogic(object):
375 @classmethod
376 - def get(cls, copr, searched_user):
377 query = (models.CoprPermission.query 378 .filter(models.CoprPermission.copr == copr) 379 .filter(models.CoprPermission.user == searched_user)) 380 381 return query
382 383 @classmethod
384 - def get_for_copr(cls, copr):
385 query = models.CoprPermission.query.filter( 386 models.CoprPermission.copr == copr) 387 388 return query
389 390 @classmethod
391 - def get_admins_for_copr(cls, copr):
392 permissions = cls.get_for_copr(copr) 393 return [copr.user] + [p.user for p in permissions if p.copr_admin == helpers.PermissionEnum("approved")]
394 395 @classmethod
396 - def new(cls, copr_permission):
397 db.session.add(copr_permission)
398 399 @classmethod
400 - def update_permissions(cls, user, copr, copr_permission, 401 new_builder, new_admin):
402 403 users_logic.UsersLogic.raise_if_cant_update_copr( 404 user, copr, "Only owners and admins may update" 405 " their projects permissions.") 406 407 (models.CoprPermission.query 408 .filter(models.CoprPermission.copr_id == copr.id) 409 .filter(models.CoprPermission.user_id == copr_permission.user_id) 410 .update({"copr_builder": new_builder, 411 "copr_admin": new_admin}))
412 413 @classmethod
414 - def update_permissions_by_applier(cls, user, copr, copr_permission, new_builder, new_admin):
415 if copr_permission: 416 # preserve approved permissions if set 417 if (not new_builder or 418 copr_permission.copr_builder != helpers.PermissionEnum("approved")): 419 420 copr_permission.copr_builder = new_builder 421 422 if (not new_admin or 423 copr_permission.copr_admin != helpers.PermissionEnum("approved")): 424 425 copr_permission.copr_admin = new_admin 426 else: 427 perm = models.CoprPermission( 428 user=user, 429 copr=copr, 430 copr_builder=new_builder, 431 copr_admin=new_admin) 432 433 cls.new(perm)
434 435 @classmethod
436 - def delete(cls, copr_permission):
437 db.session.delete(copr_permission)
438
439 440 -class CoprDirsLogic(object):
441 @classmethod
442 - def get_or_create(cls, copr, dirname, main=False):
443 copr_dir = cls.get_by_copr(copr, dirname).first() 444 445 if copr_dir: 446 return copr_dir 447 448 copr_dir = models.CoprDir( 449 name=dirname, copr=copr, main=main) 450 451 db.session.add(copr_dir) 452 return copr_dir
453 454 @classmethod
455 - def get_by_copr(cls, copr, dirname):
456 return (db.session.query(models.CoprDir) 457 .join(models.Copr) 458 .filter(models.Copr.id==copr.id) 459 .filter(models.CoprDir.name==dirname))
460 461 @classmethod
462 - def get_by_ownername(cls, ownername, dirname):
463 return (db.session.query(models.CoprDir) 464 .filter(models.CoprDir.name==dirname) 465 .filter(models.CoprDir.ownername==ownername))
466 467 @classmethod
468 - def delete(cls, copr_dir):
469 db.session.delete(copr_dir)
470 471 @classmethod
472 - def delete_all_by_copr(cls, copr):
473 for copr_dir in copr.dirs: 474 db.session.delete(copr_dir)
475
476 477 -def on_auto_createrepo_change(target_copr, value_acr, old_value_acr, initiator):
478 """ Emit createrepo action when auto_createrepo re-enabled""" 479 if old_value_acr == NEVER_SET: 480 # created new copr, not interesting 481 return 482 if not old_value_acr and value_acr: 483 # re-enabled 484 ActionsLogic.send_createrepo(target_copr)
485 486 487 listen(models.Copr.auto_createrepo, 'set', on_auto_createrepo_change, 488 active_history=True, retval=False)
489 490 491 -class BranchesLogic(object):
492 @classmethod
493 - def get_or_create(cls, name, session=db.session):
494 item = session.query(models.DistGitBranch).filter_by(name=name).first() 495 if item: 496 return item 497 498 branch = models.DistGitBranch() 499 branch.name = name 500 session.add(branch) 501 return branch
502
503 504 -class CoprChrootsLogic(object):
505 @classmethod
506 - def get_multiple(cls):
507 return models.CoprChroot.query
508 509 @classmethod
510 - def mock_chroots_from_names(cls, names):
511 512 db_chroots = models.MockChroot.query.all() 513 mock_chroots = [] 514 for ch in db_chroots: 515 if ch.name in names: 516 mock_chroots.append(ch) 517 518 return mock_chroots
519 520 @classmethod
521 - def get_by_name(cls, copr, chroot_name, active_only=True):
522 mc = MockChrootsLogic.get_from_name(chroot_name, active_only=active_only).one() 523 query = ( 524 models.CoprChroot.query.join(models.MockChroot) 525 .filter(models.CoprChroot.copr_id == copr.id) 526 .filter(models.MockChroot.id == mc.id) 527 ) 528 return query
529 530 @classmethod
531 - def get_by_name_safe(cls, copr, chroot_name):
532 """ 533 :rtype: models.CoprChroot 534 """ 535 try: 536 return cls.get_by_name(copr, chroot_name).one() 537 except NoResultFound: 538 return None
539 540 @classmethod
541 - def new(cls, mock_chroot):
542 db.session.add(mock_chroot)
543 544 @classmethod
545 - def new_from_names(cls, copr, names):
549 550 @classmethod
551 - def create_chroot(cls, user, copr, mock_chroot, buildroot_pkgs=None, repos=None, comps=None, comps_name=None, 552 module_md=None, module_md_name=None, with_opts="", without_opts="", 553 delete_after=None, delete_notify=None):
554 """ 555 :type user: models.User 556 :type mock_chroot: models.MockChroot 557 """ 558 if buildroot_pkgs is None: 559 buildroot_pkgs = "" 560 if repos is None: 561 repos = "" 562 UsersLogic.raise_if_cant_update_copr( 563 user, copr, 564 "Only owners and admins may update their projects.") 565 566 chroot = models.CoprChroot(copr=copr, mock_chroot=mock_chroot) 567 cls._update_chroot(buildroot_pkgs, repos, comps, comps_name, module_md, module_md_name, chroot, 568 with_opts, without_opts, delete_after, delete_notify) 569 return chroot
570 571 @classmethod
572 - def update_chroot(cls, user, copr_chroot, buildroot_pkgs=None, repos=None, comps=None, comps_name=None, 573 module_md=None, module_md_name=None, with_opts="", without_opts="", 574 delete_after=None, delete_notify=None):
575 """ 576 :type user: models.User 577 :type copr_chroot: models.CoprChroot 578 """ 579 UsersLogic.raise_if_cant_update_copr( 580 user, copr_chroot.copr, 581 "Only owners and admins may update their projects.") 582 583 cls._update_chroot(buildroot_pkgs, repos, comps, comps_name, module_md, module_md_name, 584 copr_chroot, with_opts, without_opts, delete_after, delete_notify) 585 return copr_chroot
586 587 @classmethod
588 - def _update_chroot(cls, buildroot_pkgs, repos, comps, comps_name, module_md, module_md_name, 589 copr_chroot, with_opts, without_opts, delete_after, delete_notify):
590 if buildroot_pkgs is not None: 591 copr_chroot.buildroot_pkgs = buildroot_pkgs 592 593 if repos is not None: 594 copr_chroot.repos = repos.replace("\n", " ") 595 596 if with_opts is not None: 597 copr_chroot.with_opts = with_opts 598 599 if without_opts is not None: 600 copr_chroot.without_opts = without_opts 601 602 if comps_name is not None: 603 copr_chroot.update_comps(comps) 604 copr_chroot.comps_name = comps_name 605 ActionsLogic.send_update_comps(copr_chroot) 606 607 if module_md_name is not None: 608 copr_chroot.update_module_md(module_md) 609 copr_chroot.module_md_name = module_md_name 610 ActionsLogic.send_update_module_md(copr_chroot) 611 612 if delete_after is not None: 613 copr_chroot.delete_after = delete_after 614 615 if delete_notify is not None: 616 copr_chroot.delete_notify = delete_notify 617 618 db.session.add(copr_chroot)
619 620 @classmethod
621 - def update_from_names(cls, user, copr, names):
622 UsersLogic.raise_if_cant_update_copr( 623 user, copr, 624 "Only owners and admins may update their projects.") 625 current_chroots = copr.mock_chroots 626 new_chroots = cls.mock_chroots_from_names(names) 627 # add non-existing 628 for mock_chroot in new_chroots: 629 if mock_chroot not in current_chroots: 630 db.session.add( 631 models.CoprChroot(copr=copr, mock_chroot=mock_chroot)) 632 633 # delete no more present 634 to_remove = [] 635 for mock_chroot in current_chroots: 636 if mock_chroot not in new_chroots: 637 # can't delete here, it would change current_chroots and break 638 # iteration 639 to_remove.append(mock_chroot) 640 641 for mc in to_remove: 642 copr.mock_chroots.remove(mc)
643 644 @classmethod
645 - def remove_comps(cls, user, copr_chroot):
646 UsersLogic.raise_if_cant_update_copr( 647 user, copr_chroot.copr, 648 "Only owners and admins may update their projects.") 649 650 copr_chroot.comps_name = None 651 copr_chroot.comps_zlib = None 652 ActionsLogic.send_update_comps(copr_chroot) 653 db.session.add(copr_chroot)
654 655 @classmethod
656 - def remove_module_md(cls, user, copr_chroot):
657 UsersLogic.raise_if_cant_update_copr( 658 user, copr_chroot.copr, 659 "Only owners and admins may update their projects.") 660 661 copr_chroot.module_md_name = None 662 copr_chroot.module_md_zlib = None 663 ActionsLogic.send_update_module_md(copr_chroot) 664 db.session.add(copr_chroot)
665 666 @classmethod
667 - def remove_copr_chroot(cls, user, copr_chroot):
668 """ 669 :param models.CoprChroot chroot: 670 """ 671 UsersLogic.raise_if_cant_update_copr( 672 user, copr_chroot.copr, 673 "Only owners and admins may update their projects.") 674 675 db.session.delete(copr_chroot)
676 677 @classmethod
678 - def filter_outdated(cls, query):
679 return query.filter(models.CoprChroot.delete_after >= datetime.datetime.now())
680 681 @classmethod
682 - def filter_outdated_to_be_deleted(cls, query):
683 return query.filter(models.CoprChroot.delete_after < datetime.datetime.now())
684
685 686 -class MockChrootsLogic(object):
687 @classmethod
688 - def get(cls, os_release, os_version, arch, active_only=False, noarch=False):
698 699 @classmethod
700 - def get_from_name(cls, chroot_name, active_only=False, noarch=False):
701 """ 702 chroot_name should be os-version-architecture, e.g. fedora-rawhide-x86_64 703 the architecture could be optional with noarch=True 704 705 Return MockChroot object for textual representation of chroot 706 """ 707 708 name_tuple = cls.tuple_from_name(chroot_name, noarch=noarch) 709 return cls.get(name_tuple[0], name_tuple[1], name_tuple[2], 710 active_only=active_only, noarch=noarch)
711 712 @classmethod
713 - def get_multiple(cls, active_only=False):
714 query = models.MockChroot.query 715 if active_only: 716 query = query.filter(models.MockChroot.is_active == True) 717 return query
718 719 @classmethod
720 - def add(cls, name):
721 name_tuple = cls.tuple_from_name(name) 722 if cls.get(*name_tuple).first(): 723 raise exceptions.DuplicateException( 724 "Mock chroot with this name already exists.") 725 new_chroot = models.MockChroot(os_release=name_tuple[0], 726 os_version=name_tuple[1], 727 arch=name_tuple[2]) 728 cls.new(new_chroot) 729 return new_chroot
730 731 @classmethod
732 - def active_names(cls):
733 return [ch.name for ch in cls.get_multiple(active_only=True).all()]
734 735 @classmethod
736 - def new(cls, mock_chroot):
737 db.session.add(mock_chroot)
738 739 @classmethod
740 - def edit_by_name(cls, name, is_active):
741 name_tuple = cls.tuple_from_name(name) 742 mock_chroot = cls.get(*name_tuple).first() 743 if not mock_chroot: 744 raise exceptions.NotFoundException( 745 "Mock chroot with this name doesn't exist.") 746 747 mock_chroot.is_active = is_active 748 cls.update(mock_chroot) 749 return mock_chroot
750 751 @classmethod
752 - def update(cls, mock_chroot):
753 db.session.add(mock_chroot)
754 755 @classmethod
756 - def delete_by_name(cls, name):
757 name_tuple = cls.tuple_from_name(name) 758 mock_chroot = cls.get(*name_tuple).first() 759 if not mock_chroot: 760 raise exceptions.NotFoundException( 761 "Mock chroot with this name doesn't exist.") 762 763 cls.delete(mock_chroot)
764 765 @classmethod
766 - def delete(cls, mock_chroot):
767 db.session.delete(mock_chroot)
768 769 @classmethod
770 - def tuple_from_name(cls, name, noarch=False):
771 """ 772 input should be os-version-architecture, e.g. fedora-rawhide-x86_64 773 774 the architecture could be optional with noarch=True 775 776 returns ("os", "version", "arch") or ("os", "version", None) 777 """ 778 split_name = name.rsplit("-", 1) if noarch else name.rsplit("-", 2) 779 780 valid = False 781 if noarch and len(split_name) in [2, 3]: 782 valid = True 783 if not noarch and len(split_name) == 3: 784 valid = True 785 786 if not valid: 787 raise MalformedArgumentException( 788 "Chroot name is not valid") 789 790 if noarch and len(split_name) == 2: 791 split_name.append(None) 792 793 return tuple(split_name)
794