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

Source Code for Module coprs.logic.complex_logic

  1  # coding: utf-8 
  2   
  3  import datetime 
  4  import time 
  5  import flask 
  6  import sqlalchemy 
  7   
  8  from .. import db 
  9  from .builds_logic import BuildsLogic 
 10  from copr_common.enums import StatusEnum 
 11  from coprs import helpers 
 12  from coprs import models 
 13  from coprs import exceptions 
 14  from coprs.exceptions import ObjectNotFound, ActionInProgressException 
 15  from coprs.logic.packages_logic import PackagesLogic 
 16  from coprs.logic.actions_logic import ActionsLogic 
 17   
 18  from coprs.logic.users_logic import UsersLogic 
 19  from coprs.models import User, Copr 
 20  from .coprs_logic import CoprsLogic, CoprDirsLogic, CoprChrootsLogic, PinnedCoprsLogic 
 21   
 22   
 23  @sqlalchemy.event.listens_for(models.Copr.deleted, "set") 
24 -def unpin_projects_on_delete(copr, deleted, oldvalue, event):
25 if not deleted: 26 return 27 PinnedCoprsLogic.delete_by_copr(copr)
28
29 30 -class ComplexLogic(object):
31 """ 32 Used for manipulation which affects multiply models 33 """ 34 35 @classmethod
36 - def delete_copr(cls, copr, admin_action=False):
37 """ 38 Delete copr and all its builds. 39 40 :param copr: 41 :param admin_action: set to True to bypass permission check 42 :raises ActionInProgressException: 43 :raises InsufficientRightsException: 44 """ 45 46 if admin_action: 47 user = copr.user 48 else: 49 user = flask.g.user 50 51 builds_query = BuildsLogic.get_multiple_by_copr(copr=copr) 52 53 if copr.persistent: 54 raise exceptions.InsufficientRightsException("This project is protected against deletion.") 55 56 for build in builds_query: 57 # Don't send delete action for each build, rather send an action to delete 58 # a whole project as a part of CoprsLogic.delete_unsafe() method. 59 BuildsLogic.delete_build(user, build, send_delete_action=False) 60 61 CoprsLogic.delete_unsafe(user, copr)
62 63 64 @classmethod
65 - def delete_expired_projects(cls):
66 query = ( 67 models.Copr.query 68 .filter(models.Copr.delete_after.isnot(None)) 69 .filter(models.Copr.delete_after < datetime.datetime.now()) 70 .filter(models.Copr.deleted.isnot(True)) 71 ) 72 for copr in query.all(): 73 print("deleting project '{}'".format(copr.full_name)) 74 try: 75 cls.delete_copr(copr, admin_action=True) 76 except ActionInProgressException as e: 77 print(e) 78 print("project {} postponed".format(copr.full_name))
79 80 81 @classmethod
82 - def fork_copr(cls, copr, user, dstname, dstgroup=None):
83 forking = ProjectForking(user, dstgroup) 84 created = (not bool(forking.get(copr, dstname))) 85 fcopr = forking.fork_copr(copr, dstname) 86 87 if fcopr.full_name == copr.full_name: 88 raise exceptions.DuplicateException("Source project should not be same as destination") 89 90 builds_map = {} 91 srpm_builds_src = [] 92 srpm_builds_dst = [] 93 94 for package in copr.main_dir.packages: 95 fpackage = forking.fork_package(package, fcopr) 96 97 builds = PackagesLogic.last_successful_build_chroots(package) 98 if not builds: 99 continue 100 101 for build, build_chroots in builds.items(): 102 fbuild = forking.fork_build(build, fcopr, fpackage, build_chroots) 103 104 if build.result_dir: 105 srpm_builds_src.append(build.result_dir) 106 srpm_builds_dst.append(fbuild.result_dir) 107 108 for chroot, fchroot in zip(build_chroots, fbuild.build_chroots): 109 if not chroot.result_dir: 110 continue 111 if chroot.name not in builds_map: 112 builds_map[chroot.name] = {chroot.result_dir: fchroot.result_dir} 113 else: 114 builds_map[chroot.name][chroot.result_dir] = fchroot.result_dir 115 116 builds_map['srpm-builds'] = dict(zip(srpm_builds_src, srpm_builds_dst)) 117 118 db.session.commit() 119 ActionsLogic.send_fork_copr(copr, fcopr, builds_map) 120 return fcopr, created
121 122 @staticmethod
123 - def get_group_copr_safe(group_name, copr_name, **kwargs):
124 group = ComplexLogic.get_group_by_name_safe(group_name) 125 try: 126 return CoprsLogic.get_by_group_id( 127 group.id, copr_name, **kwargs).one() 128 except sqlalchemy.orm.exc.NoResultFound: 129 raise ObjectNotFound( 130 message="Project @{}/{} does not exist." 131 .format(group_name, copr_name))
132 133 @staticmethod
134 - def get_copr_safe(user_name, copr_name, **kwargs):
135 """ Get one project. 136 137 This always return personal project. For group projects see get_group_copr_safe(). 138 """ 139 try: 140 return CoprsLogic.get(user_name, copr_name, **kwargs).filter(Copr.group_id.is_(None)).one() 141 except sqlalchemy.orm.exc.NoResultFound: 142 raise ObjectNotFound( 143 message="Project {}/{} does not exist." 144 .format(user_name, copr_name))
145 146 @staticmethod
147 - def get_copr_by_owner_safe(owner_name, copr_name, **kwargs):
148 if owner_name[0] == "@": 149 return ComplexLogic.get_group_copr_safe(owner_name[1:], copr_name, **kwargs) 150 return ComplexLogic.get_copr_safe(owner_name, copr_name, **kwargs)
151 152 @staticmethod
153 - def get_copr_by_repo_safe(repo_url):
154 copr_repo = helpers.copr_repo_fullname(repo_url) 155 if not copr_repo: 156 return None 157 try: 158 owner, copr = copr_repo.split("/") 159 except: 160 # invalid format, e.g. multiple slashes in copr_repo 161 return None 162 return ComplexLogic.get_copr_by_owner_safe(owner, copr)
163 164 @staticmethod
165 - def get_copr_dir_safe(ownername, copr_dirname, **kwargs):
166 try: 167 return CoprDirsLogic.get_by_ownername(ownername, copr_dirname).one() 168 except sqlalchemy.orm.exc.NoResultFound: 169 raise ObjectNotFound(message="copr dir {}/{} does not exist." 170 .format(ownername, copr_dirname))
171 172 @staticmethod
173 - def get_copr_by_id_safe(copr_id):
174 try: 175 return CoprsLogic.get_by_id(copr_id).one() 176 except sqlalchemy.orm.exc.NoResultFound: 177 raise ObjectNotFound( 178 message="Project with id {} does not exist." 179 .format(copr_id))
180 181 @staticmethod
182 - def get_build_safe(build_id):
183 try: 184 return BuildsLogic.get_by_id(build_id).one() 185 except sqlalchemy.orm.exc.NoResultFound: 186 raise ObjectNotFound( 187 message="Build {} does not exist.".format(build_id))
188 189 @staticmethod
190 - def get_package_by_id_safe(package_id):
191 try: 192 return PackagesLogic.get_by_id(package_id).one() 193 except sqlalchemy.orm.exc.NoResultFound: 194 raise ObjectNotFound( 195 message="Package {} does not exist.".format(package_id))
196 197 @staticmethod
198 - def get_package_safe(copr_dir, package_name):
199 try: 200 return PackagesLogic.get(copr_dir.id, package_name).one() 201 except sqlalchemy.orm.exc.NoResultFound: 202 raise ObjectNotFound( 203 message="Package {} in the copr_dir {} does not exist." 204 .format(package_name, copr_dir))
205 206 @staticmethod
207 - def get_group_by_name_safe(group_name):
208 try: 209 group = UsersLogic.get_group_by_alias(group_name).one() 210 except sqlalchemy.orm.exc.NoResultFound: 211 raise ObjectNotFound( 212 message="Group {} does not exist.".format(group_name)) 213 return group
214 215 @staticmethod
216 - def get_copr_chroot_safe(copr, chroot_name):
217 try: 218 chroot = CoprChrootsLogic.get_by_name_safe(copr, chroot_name) 219 except (ValueError, KeyError, RuntimeError) as e: 220 raise ObjectNotFound(message=str(e)) 221 222 if not chroot: 223 raise ObjectNotFound( 224 message="Chroot name {} does not exist.".format(chroot_name)) 225 226 return chroot
227 228 @staticmethod
229 - def get_active_groups_by_user(user_name):
230 names = flask.g.user.user_groups 231 if names: 232 query = UsersLogic.get_groups_by_names_list(names) 233 return query.filter(User.name == user_name) 234 else: 235 return []
236 237 @staticmethod
238 - def get_queue_sizes():
239 importing = BuildsLogic.get_build_importing_queue(background=False).count() 240 pending = BuildsLogic.get_pending_build_tasks(background=False).count() 241 running = BuildsLogic.get_build_tasks(StatusEnum("running")).count() 242 243 return dict( 244 importing=importing, 245 pending=pending, 246 running=running, 247 )
248 249 @classmethod
250 - def get_coprs_permissible_by_user(cls, user):
251 coprs = CoprsLogic.filter_without_group_projects( 252 CoprsLogic.get_multiple_owned_by_username( 253 flask.g.user.username, include_unlisted_on_hp=False)).all() 254 255 for group in user.user_groups: 256 coprs.extend(CoprsLogic.get_multiple_by_group_id(group.id).all()) 257 258 coprs += [perm.copr for perm in user.copr_permissions if 259 perm.get_permission("admin") == helpers.PermissionEnum("approved") or 260 perm.get_permission("builder") == helpers.PermissionEnum("approved")] 261 262 return set(coprs)
263
264 265 -class ProjectForking(object):
266 - def __init__(self, user, group=None):
267 self.user = user 268 self.group = group 269 270 if group and not user.can_build_in_group(group): 271 raise exceptions.InsufficientRightsException( 272 "Only members may create projects in the particular groups.")
273
274 - def get(self, copr, name):
275 return CoprsLogic.get_by_group_id(self.group.id, name).first() if self.group \ 276 else CoprsLogic.filter_without_group_projects(CoprsLogic.get(flask.g.user.name, name)).first()
277
278 - def fork_copr(self, copr, name):
279 fcopr = self.get(copr, name) 280 if not fcopr: 281 fcopr = self.create_object(models.Copr, copr, 282 exclude=["id", "group_id", "created_on", 283 "scm_repo_url", "scm_api_type", "scm_api_auth_json", 284 "persistent", "auto_prune", "contact", "webhook_secret"]) 285 286 fcopr.forked_from_id = copr.id 287 fcopr.user = self.user 288 fcopr.user_id = self.user.id 289 fcopr.created_on = int(time.time()) 290 if name: 291 fcopr.name = name 292 if self.group: 293 fcopr.group = self.group 294 fcopr.group_id = self.group.id 295 296 fcopr_dir = models.CoprDir(name=fcopr.name, copr=fcopr, main=True) 297 298 for chroot in list(copr.copr_chroots): 299 CoprChrootsLogic.create_chroot(self.user, fcopr, chroot.mock_chroot, chroot.buildroot_pkgs, 300 chroot.repos, comps=chroot.comps, comps_name=chroot.comps_name, 301 with_opts=chroot.with_opts, without_opts=chroot.without_opts) 302 db.session.add(fcopr) 303 db.session.add(fcopr_dir) 304 305 return fcopr
306
307 - def fork_package(self, package, fcopr):
308 fpackage = PackagesLogic.get(fcopr.main_dir.id, package.name).first() 309 if not fpackage: 310 fpackage = self.create_object(models.Package, package, exclude=["id", "copr_id", "copr_dir_id", "webhook_rebuild"]) 311 fpackage.copr = fcopr 312 fpackage.copr_dir = fcopr.main_dir 313 db.session.add(fpackage) 314 return fpackage
315
316 - def fork_build(self, build, fcopr, fpackage, build_chroots):
317 fbuild = self.create_object(models.Build, build, exclude=["id", "copr_id", "copr_dir_id", "package_id", "result_dir"]) 318 fbuild.copr = fcopr 319 fbuild.package = fpackage 320 fbuild.copr_dir = fcopr.main_dir 321 db.session.add(fbuild) 322 db.session.flush() 323 324 fbuild.result_dir = '{:08}'.format(fbuild.id) 325 fbuild.build_chroots = [self.create_object(models.BuildChroot, c, exclude=["id", "build_id", "result_dir"]) for c in build_chroots] 326 for chroot in fbuild.build_chroots: 327 chroot.result_dir = '{:08}-{}'.format(fbuild.id, fpackage.name) 328 chroot.status = StatusEnum("forked") 329 db.session.add(fbuild) 330 return fbuild
331
332 - def create_object(self, clazz, from_object, exclude=list()):
333 arguments = {} 334 for name, column in from_object.__mapper__.columns.items(): 335 if not name in exclude: 336 arguments[name] = getattr(from_object, name) 337 return clazz(**arguments)
338
339 340 -class BuildConfigLogic(object):
341 342 @classmethod
343 - def generate_build_config(cls, copr, chroot_id):
344 """ Return dict with proper build config contents """ 345 chroot = None 346 for i in copr.copr_chroots: 347 if i.mock_chroot.name == chroot_id: 348 chroot = i 349 if not chroot: 350 return {} 351 352 packages = "" if not chroot.buildroot_pkgs else chroot.buildroot_pkgs 353 354 repos = [{ 355 "id": "copr_base", 356 "baseurl": copr.repo_url + "/{}/".format(chroot_id), 357 "name": "Copr repository", 358 }] 359 360 if copr.module_hotfixes: 361 repos[0]["module_hotfixes"] = True 362 363 if not copr.auto_createrepo: 364 repos.append({ 365 "id": "copr_base_devel", 366 "baseurl": copr.repo_url + "/{}/devel/".format(chroot_id), 367 "name": "Copr buildroot", 368 }) 369 370 371 repos.extend(cls.get_additional_repo_views(copr.repos_list, chroot_id)) 372 repos.extend(cls.get_additional_repo_views(chroot.repos_list, chroot_id)) 373 374 return { 375 'project_id': copr.repo_id, 376 'additional_packages': packages.split(), 377 'repos': repos, 378 'chroot': chroot_id, 379 'use_bootstrap_container': copr.use_bootstrap_container, 380 'with_opts': chroot.with_opts.split(), 381 'without_opts': chroot.without_opts.split(), 382 }
383 384 @classmethod
385 - def get_additional_repo_views(cls, repos_list, chroot_id):
386 repos = [] 387 for repo in repos_list: 388 params = helpers.parse_repo_params(repo) 389 repo_view = { 390 "id": helpers.generate_repo_name(repo), 391 "baseurl": helpers.pre_process_repo_url(chroot_id, repo), 392 "name": "Additional repo " + helpers.generate_repo_name(repo), 393 } 394 395 copr = ComplexLogic.get_copr_by_repo_safe(repo) 396 if copr and copr.module_hotfixes: 397 params["module_hotfixes"] = True 398 399 repo_view.update(params) 400 repos.append(repo_view) 401 return repos
402 403 @classmethod
404 - def generate_additional_repos(cls, copr_chroot):
405 base_repo = "copr://{}".format(copr_chroot.copr.full_name) 406 repos = [base_repo] + copr_chroot.repos_list + copr_chroot.copr.repos_list 407 if not copr_chroot.copr.auto_createrepo: 408 repos.append("copr://{}/devel".format(copr_chroot.copr.full_name)) 409 return repos
410