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

Source Code for Module coprs.logic.modules_logic

  1  import os 
  2  import time 
  3  import base64 
  4  import requests 
  5  from collections import defaultdict 
  6  from sqlalchemy import and_ 
  7  from datetime import datetime 
  8  from coprs import models 
  9  from coprs import db 
 10  from coprs import exceptions 
 11  from coprs.logic import builds_logic 
 12  from coprs.logic.dist_git_logic import DistGitLogic 
 13  from wtforms import ValidationError 
 14   
 15  import gi 
 16  gi.require_version('Modulemd', '1.0') 
 17  from gi.repository import Modulemd 
18 19 20 -class ModulesLogic(object):
21 @classmethod
22 - def get(cls, module_id):
23 """ 24 Return single module identified by `module_id` 25 """ 26 return models.Module.query.filter(models.Module.id == module_id)
27 28 @classmethod
29 - def get_by_nsv(cls, copr, name, stream, version):
35 36 @classmethod
37 - def get_by_nsv_str(cls, copr, nsv):
38 name, stream, version = nsv.split("-") 39 return cls.get_by_nsv(copr, name, stream, version)
40 41 @classmethod
42 - def get_multiple(cls):
43 return models.Module.query.order_by(models.Module.id.desc())
44 45 @classmethod
46 - def get_multiple_by_copr(cls, copr):
48 49 @classmethod
50 - def yaml2modulemd(cls, yaml):
51 mmd = Modulemd.ModuleStream() 52 mmd.import_from_string(yaml) 53 return mmd
54 55 @classmethod
56 - def from_modulemd(cls, mmd):
57 yaml_b64 = base64.b64encode(mmd.dumps().encode("utf-8")).decode("utf-8") 58 return models.Module(name=mmd.get_name(), stream=mmd.get_stream(), version=mmd.get_version(), 59 summary=mmd.get_summary(), description=mmd.get_description(), yaml_b64=yaml_b64)
60 61 @classmethod
62 - def validate(cls, mmd):
63 if not all([mmd.get_name(), mmd.get_stream(), mmd.get_version()]): 64 raise ValidationError("Module should contain name, stream and version")
65 66 @classmethod
67 - def add(cls, user, copr, module):
68 if not user.can_build_in(copr): 69 raise exceptions.InsufficientRightsException("You don't have permissions to build in this copr.") 70 71 module.copr_id = copr.id 72 module.copr = copr 73 module.created_on = time.time() 74 75 db.session.add(module) 76 return module
77 78 @classmethod
79 - def set_defaults_for_optional_params(cls, mmd, filename=None):
80 mmd.set_name(mmd.get_name() or str(os.path.splitext(filename)[0])) 81 mmd.set_stream(mmd.get_stream() or "master") 82 mmd.set_version(mmd.get_version() or int(datetime.now().strftime("%Y%m%d%H%M%S")))
83
84 85 -class ModuleBuildFacade(object):
86 - def __init__(self, user, copr, yaml, filename=None, distgit_name=None):
87 self.user = user 88 self.copr = copr 89 self.yaml = yaml 90 self.filename = filename 91 self.distgit = DistGitLogic.get_with_default(distgit_name) 92 93 self.modulemd = ModulesLogic.yaml2modulemd(yaml) 94 ModulesLogic.set_defaults_for_optional_params(self.modulemd, filename=filename) 95 ModulesLogic.validate(self.modulemd)
96
97 - def submit_build(self):
98 module = ModulesLogic.add(self.user, self.copr, ModulesLogic.from_modulemd(self.modulemd)) 99 if not self.platform_chroots: 100 raise ValidationError("Module platform is {} which doesn't match to any chroots enabled in {} project" 101 .format(self.platform, self.copr.full_name)) 102 self.add_builds(self.modulemd.get_rpm_components(), module) 103 return module
104 105 @classmethod
106 - def get_build_batches(cls, rpms):
107 """ 108 Determines Which component should be built in which batch. Returns an ordered list of grouped components, 109 first group of components should be built as a first batch, second as second and so on. 110 Particular components groups are represented by dicts and can by built in a random order within the batch. 111 :return: list of lists 112 """ 113 batches = defaultdict(dict) 114 for pkgname, rpm in rpms.items(): 115 batches[rpm.get_buildorder()][pkgname] = rpm 116 return [batches[number] for number in sorted(batches.keys())]
117 118 @property
119 - def platform(self):
120 platform = self.modulemd.get_buildrequires().get("platform", []) 121 return platform if isinstance(platform, list) else [platform]
122 123 @property
124 - def platform_chroots(self):
125 """ 126 Return a list of chroot names based on buildrequired platform and enabled chroots for the project. 127 Example: Copr chroots are ["fedora-22-x86-64", "fedora-23-x86_64"] and modulemd specifies "f23" as a platform, 128 then `platform_chroots` are ["fedora-23-x86_64"] 129 Alternatively, the result will be same for "-f22" platform 130 :return: list of strings 131 """ 132 133 # Just to be sure, that all chroot abbreviations from platform are in expected format, e.g. f28 or -f30 134 for abbrev in self.platform: 135 if not (abbrev.startswith(("f", "-f")) and abbrev.lstrip("-f").isnumeric()): 136 raise ValidationError("Unexpected platform '{}', it should be e.g. f28 or -f30".format(abbrev)) 137 138 chroot_archs = {} 139 for chroot in self.copr.active_chroots: 140 chroot_archs.setdefault(chroot.name_release, []).append(chroot.arch) 141 142 def abbrev2chroots(abbrev): 143 name_release = abbrev.replace("-", "").replace("f", "fedora-") 144 return ["{}-{}".format(name_release, arch) for arch in chroot_archs.get(name_release, [])]
145 146 exclude_chroots = set() 147 select_chroots = set() 148 for abbrev in self.platform: 149 abbrev_chroots = abbrev2chroots(abbrev) 150 if not abbrev_chroots: 151 raise ValidationError("Module platform stream {} doesn't match to any enabled chroots in the {} project" 152 .format(abbrev, self.copr.full_name)) 153 (exclude_chroots if abbrev.startswith("-") else select_chroots).update(abbrev_chroots) 154 155 chroots = {chroot.name for chroot in self.copr.active_chroots} 156 chroots -= exclude_chroots 157 if select_chroots: 158 chroots &= select_chroots 159 return chroots
160 161
162 - def add_builds(self, rpms, module):
163 blocked_by_id = None 164 for group in self.get_build_batches(rpms): 165 batch = models.Batch() 166 batch.blocked_by_id = blocked_by_id 167 db.session.add(batch) 168 for pkgname, rpm in group.items(): 169 clone_url = self.get_clone_url(pkgname, rpm) 170 build = builds_logic.BuildsLogic.create_new_from_scm(self.user, self.copr, scm_type="git", 171 clone_url=clone_url, committish=rpm.peek_ref(), 172 chroot_names=self.platform_chroots) 173 build.batch = batch 174 build.batch_id = batch.id 175 build.module_id = module.id 176 db.session.add(build) 177 178 # Every batch needs to by blocked by the previous one 179 blocked_by_id = batch.id
180
181 - def get_clone_url(self, pkgname, rpm):
182 if rpm.peek_repository(): 183 return rpm.peek_repository() 184 185 return self.distgit.package_clone_url(pkgname)
186
187 188 -class ModulemdGenerator(object):
189 - def __init__(self, name="", stream="", version=0, summary="", config=None):
190 self.config = config 191 licenses = Modulemd.SimpleSet() 192 licenses.add("unknown") 193 self.mmd = Modulemd.ModuleStream(mdversion=1, name=name, stream=stream, version=version, summary=summary, 194 description="", content_licenses=licenses, module_licenses=licenses)
195 196 @property
197 - def nsv(self):
198 return "{}-{}-{}".format(self.mmd.get_name(), self.mmd.get_stream(), self.mmd.get_version())
199
200 - def add_api(self, packages):
201 mmd_set = Modulemd.SimpleSet() 202 for package in packages: 203 mmd_set.add(str(package)) 204 self.mmd.set_rpm_api(mmd_set)
205
206 - def add_filter(self, packages):
207 mmd_set = Modulemd.SimpleSet() 208 for package in packages: 209 mmd_set.add(str(package)) 210 self.mmd.set_rpm_filter(mmd_set)
211
212 - def add_profiles(self, profiles):
213 for i, values in profiles: 214 name, packages = values 215 profile = Modulemd.Profile(name=name) 216 for package in packages: 217 profile.add_rpm(str(package)) 218 self.mmd.add_profile(profile)
219
220 - def add_components(self, packages, filter_packages, builds):
221 build_ids = sorted(list(set([int(id) for p, id in zip(packages, builds) 222 if p in filter_packages]))) 223 for package in filter_packages: 224 build_id = builds[packages.index(package)] 225 build = builds_logic.BuildsLogic.get_by_id(build_id).first() 226 build_chroot = self._build_chroot(build) 227 buildorder = build_ids.index(int(build.id)) 228 rationale = "User selected the package as a part of the module" 229 self.add_component(package, build, build_chroot, rationale, buildorder)
230
231 - def _build_chroot(self, build):
232 chroot = None 233 for chroot in build.build_chroots: 234 if chroot.name == "custom-1-x86_64": 235 break 236 return chroot
237
238 - def add_component(self, package_name, build, chroot, rationale, buildorder=1):
239 ref = str(chroot.git_hash) if chroot else "" 240 distgit_url = self.config["DIST_GIT_URL"].replace("/cgit", "/git") 241 url = os.path.join(distgit_url, build.copr.full_name, "{}.git".format(build.package.name)) 242 component = Modulemd.ComponentRpm(name=str(package_name), rationale=rationale, 243 repository=url, ref=ref, buildorder=1) 244 self.mmd.add_rpm_component(component)
245
246 - def generate(self):
247 return self.mmd.dumps()
248
249 250 -class ModuleProvider(object):
251 - def __init__(self, filename, yaml):
252 self.filename = filename 253 self.yaml = yaml
254 255 @classmethod
256 - def from_input(cls, obj):
257 if hasattr(obj, "read"): 258 return cls.from_file(obj) 259 return cls.from_url(obj)
260 261 @classmethod
262 - def from_file(cls, ref):
263 return cls(ref.filename, ref.read().decode("utf-8"))
264 265 @classmethod
266 - def from_url(cls, url):
267 if not url.endswith(".yaml"): 268 raise ValidationError("This URL doesn't point to a .yaml file") 269 270 request = requests.get(url) 271 if request.status_code != 200: 272 raise requests.RequestException("This URL seems to be wrong") 273 return cls(os.path.basename(url), request.text)
274