Trees | Indices | Help |
---|
|
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") 2831 """ 32 Used for manipulation which affects multiply models 33 """ 34 35 @classmethod25937 """ 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 @classmethod66 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 @classmethod83 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, created121 122 @staticmethod124 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 @staticmethod135 """ 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 @staticmethod148 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 @staticmethod154 copr_repo = helpers.copr_repo_fullname(repo_url) 155 if not copr_repo: 156 return None 157 owner, copr = copr_repo.split("/") 158 return ComplexLogic.get_copr_by_owner_safe(owner, copr)159 160 @staticmethod162 try: 163 return CoprDirsLogic.get_by_ownername(ownername, copr_dirname).one() 164 except sqlalchemy.orm.exc.NoResultFound: 165 raise ObjectNotFound(message="copr dir {}/{} does not exist." 166 .format(ownername, copr_dirname))167 168 @staticmethod170 try: 171 return CoprsLogic.get_by_id(copr_id).one() 172 except sqlalchemy.orm.exc.NoResultFound: 173 raise ObjectNotFound( 174 message="Project with id {} does not exist." 175 .format(copr_id))176 177 @staticmethod179 try: 180 return BuildsLogic.get_by_id(build_id).one() 181 except sqlalchemy.orm.exc.NoResultFound: 182 raise ObjectNotFound( 183 message="Build {} does not exist.".format(build_id))184 185 @staticmethod187 try: 188 return PackagesLogic.get_by_id(package_id).one() 189 except sqlalchemy.orm.exc.NoResultFound: 190 raise ObjectNotFound( 191 message="Package {} does not exist.".format(package_id))192 193 @staticmethod195 try: 196 return PackagesLogic.get(copr_dir.id, package_name).one() 197 except sqlalchemy.orm.exc.NoResultFound: 198 raise ObjectNotFound( 199 message="Package {} in the copr_dir {} does not exist." 200 .format(package_name, copr_dir))201 202 @staticmethod204 try: 205 group = UsersLogic.get_group_by_alias(group_name).one() 206 except sqlalchemy.orm.exc.NoResultFound: 207 raise ObjectNotFound( 208 message="Group {} does not exist.".format(group_name)) 209 return group210 211 @staticmethod213 try: 214 chroot = CoprChrootsLogic.get_by_name_safe(copr, chroot_name) 215 except (ValueError, KeyError, RuntimeError) as e: 216 raise ObjectNotFound(message=str(e)) 217 218 if not chroot: 219 raise ObjectNotFound( 220 message="Chroot name {} does not exist.".format(chroot_name)) 221 222 return chroot223 224 @staticmethod226 names = flask.g.user.user_groups 227 if names: 228 query = UsersLogic.get_groups_by_names_list(names) 229 return query.filter(User.name == user_name) 230 else: 231 return []232 233 @staticmethod235 importing = BuildsLogic.get_build_importing_queue(background=False).count() 236 pending = BuildsLogic.get_pending_build_tasks(background=False).count() 237 running = BuildsLogic.get_build_tasks(StatusEnum("running")).count() 238 239 return dict( 240 importing=importing, 241 pending=pending, 242 running=running, 243 )244 245 @classmethod247 coprs = CoprsLogic.filter_without_group_projects( 248 CoprsLogic.get_multiple_owned_by_username( 249 flask.g.user.username, include_unlisted_on_hp=False)).all() 250 251 for group in user.user_groups: 252 coprs.extend(CoprsLogic.get_multiple_by_group_id(group.id).all()) 253 254 coprs += [perm.copr for perm in user.copr_permissions if 255 perm.get_permission("admin") == helpers.PermissionEnum("approved") or 256 perm.get_permission("builder") == helpers.PermissionEnum("approved")] 257 258 return set(coprs)334263 self.user = user 264 self.group = group 265 266 if group and not user.can_build_in_group(group): 267 raise exceptions.InsufficientRightsException( 268 "Only members may create projects in the particular groups.")269271 return CoprsLogic.get_by_group_id(self.group.id, name).first() if self.group \ 272 else CoprsLogic.filter_without_group_projects(CoprsLogic.get(flask.g.user.name, name)).first()273275 fcopr = self.get(copr, name) 276 if not fcopr: 277 fcopr = self.create_object(models.Copr, copr, 278 exclude=["id", "group_id", "created_on", 279 "scm_repo_url", "scm_api_type", "scm_api_auth_json", 280 "persistent", "auto_prune", "contact", "webhook_secret"]) 281 282 fcopr.forked_from_id = copr.id 283 fcopr.user = self.user 284 fcopr.user_id = self.user.id 285 fcopr.created_on = int(time.time()) 286 if name: 287 fcopr.name = name 288 if self.group: 289 fcopr.group = self.group 290 fcopr.group_id = self.group.id 291 292 fcopr_dir = models.CoprDir(name=fcopr.name, copr=fcopr, main=True) 293 294 for chroot in list(copr.copr_chroots): 295 CoprChrootsLogic.create_chroot(self.user, fcopr, chroot.mock_chroot, chroot.buildroot_pkgs, 296 chroot.repos, comps=chroot.comps, comps_name=chroot.comps_name, 297 with_opts=chroot.with_opts, without_opts=chroot.without_opts) 298 db.session.add(fcopr) 299 db.session.add(fcopr_dir) 300 301 return fcopr302304 fpackage = PackagesLogic.get(fcopr.main_dir.id, package.name).first() 305 if not fpackage: 306 fpackage = self.create_object(models.Package, package, exclude=["id", "copr_id", "copr_dir_id", "webhook_rebuild"]) 307 fpackage.copr = fcopr 308 fpackage.copr_dir = fcopr.main_dir 309 db.session.add(fpackage) 310 return fpackage311313 fbuild = self.create_object(models.Build, build, exclude=["id", "copr_id", "copr_dir_id", "package_id", "result_dir"]) 314 fbuild.copr = fcopr 315 fbuild.package = fpackage 316 fbuild.copr_dir = fcopr.main_dir 317 db.session.add(fbuild) 318 db.session.flush() 319 320 fbuild.result_dir = '{:08}'.format(fbuild.id) 321 fbuild.build_chroots = [self.create_object(models.BuildChroot, c, exclude=["id", "build_id", "result_dir"]) for c in build_chroots] 322 for chroot in fbuild.build_chroots: 323 chroot.result_dir = '{:08}-{}'.format(fbuild.id, fpackage.name) 324 chroot.status = StatusEnum("forked") 325 db.session.add(fbuild) 326 return fbuild327337 338 @classmethod406340 """ Return dict with proper build config contents """ 341 chroot = None 342 for i in copr.copr_chroots: 343 if i.mock_chroot.name == chroot_id: 344 chroot = i 345 if not chroot: 346 return {} 347 348 packages = "" if not chroot.buildroot_pkgs else chroot.buildroot_pkgs 349 350 repos = [{ 351 "id": "copr_base", 352 "baseurl": copr.repo_url + "/{}/".format(chroot_id), 353 "name": "Copr repository", 354 }] 355 356 if copr.module_hotfixes: 357 repos[0]["module_hotfixes"] = True 358 359 if not copr.auto_createrepo: 360 repos.append({ 361 "id": "copr_base_devel", 362 "baseurl": copr.repo_url + "/{}/devel/".format(chroot_id), 363 "name": "Copr buildroot", 364 }) 365 366 367 repos.extend(cls.get_additional_repo_views(copr.repos_list, chroot_id)) 368 repos.extend(cls.get_additional_repo_views(chroot.repos_list, chroot_id)) 369 370 return { 371 'project_id': copr.repo_id, 372 'additional_packages': packages.split(), 373 'repos': repos, 374 'chroot': chroot_id, 375 'use_bootstrap_container': copr.use_bootstrap_container, 376 'with_opts': chroot.with_opts.split(), 377 'without_opts': chroot.without_opts.split(), 378 }379 380 @classmethod382 repos = [] 383 for repo in repos_list: 384 params = helpers.parse_repo_params(repo) 385 repo_view = { 386 "id": helpers.generate_repo_name(repo), 387 "baseurl": helpers.pre_process_repo_url(chroot_id, repo), 388 "name": "Additional repo " + helpers.generate_repo_name(repo), 389 } 390 391 copr = ComplexLogic.get_copr_by_repo_safe(repo) 392 if copr and copr.module_hotfixes: 393 params["module_hotfixes"] = True 394 395 repo_view.update(params) 396 repos.append(repo_view) 397 return repos398 399 @classmethod401 base_repo = "copr://{}".format(copr_chroot.copr.full_name) 402 repos = [base_repo] + copr_chroot.repos_list + copr_chroot.copr.repos_list 403 if not copr_chroot.copr.auto_createrepo: 404 repos.append("copr://{}/devel".format(copr_chroot.copr.full_name)) 405 return repos
Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 | http://epydoc.sourceforge.net |