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

Source Code for Module coprs.logic.packages_logic

  1  import json 
  2  import re 
  3   
  4  from sqlalchemy import bindparam, Integer 
  5  from sqlalchemy.sql import true, text 
  6   
  7  from coprs import app 
  8  from coprs import db 
  9  from coprs import exceptions 
 10  from coprs import models 
 11  from coprs import helpers 
 12   
 13  from coprs.logic import users_logic 
 14  from coprs.logic import builds_logic 
 15   
 16  log = app.logger 
17 18 19 -class PackagesLogic(object):
20 21 @classmethod
22 - def get_by_id(cls, package_id):
24 25 @classmethod
26 - def get_all(cls, copr_dir_id):
29 30 @classmethod
31 - def get_all_in_copr(cls, copr_id):
32 return (models.Package.query 33 .filter(models.Package.copr_id == copr_id))
34 35 @classmethod
36 - def get_copr_packages_list(cls, copr_dir):
37 query_select = """ 38 SELECT package.name, build.pkg_version, build.submitted_on, package.webhook_rebuild, order_to_status(subquery2.min_order_for_a_build) AS status, build.source_status 39 FROM package 40 LEFT OUTER JOIN (select MAX(build.id) as max_build_id_for_a_package, package_id 41 FROM build 42 WHERE build.copr_dir_id = :copr_dir_id 43 GROUP BY package_id) as subquery1 ON subquery1.package_id = package.id 44 LEFT OUTER JOIN build ON build.id = subquery1.max_build_id_for_a_package 45 LEFT OUTER JOIN (select build_id, min(status_to_order(status)) as min_order_for_a_build 46 FROM build_chroot 47 GROUP BY build_id) as subquery2 ON subquery2.build_id = subquery1.max_build_id_for_a_package 48 WHERE package.copr_dir_id = :copr_dir_id; 49 """ 50 51 if db.engine.url.drivername == "sqlite": 52 def sqlite_status_to_order(x): 53 if x == 3: 54 return 1 55 elif x == 6: 56 return 2 57 elif x == 7: 58 return 3 59 elif x == 4: 60 return 4 61 elif x == 0: 62 return 5 63 elif x == 1: 64 return 6 65 elif x == 5: 66 return 7 67 elif x == 2: 68 return 8 69 elif x == 8: 70 return 9 71 elif x == 9: 72 return 10 73 return 1000
74 75 def sqlite_order_to_status(x): 76 if x == 1: 77 return 3 78 elif x == 2: 79 return 6 80 elif x == 3: 81 return 7 82 elif x == 4: 83 return 4 84 elif x == 5: 85 return 0 86 elif x == 6: 87 return 1 88 elif x == 7: 89 return 5 90 elif x == 8: 91 return 2 92 elif x == 9: 93 return 8 94 elif x == 10: 95 return 9 96 return 1000
97 98 conn = db.engine.connect() 99 conn.connection.create_function("status_to_order", 1, sqlite_status_to_order) 100 conn.connection.create_function("order_to_status", 1, sqlite_order_to_status) 101 statement = text(query_select) 102 statement.bindparams(bindparam("copr_dir_id", Integer)) 103 result = conn.execute(statement, {"copr_dir_id": copr_dir.id}) 104 else: 105 statement = text(query_select) 106 statement.bindparams(bindparam("copr_dir_id", Integer)) 107 result = db.engine.execute(statement, {"copr_dir_id": copr_dir.id}) 108 109 return result 110 111 @classmethod
112 - def get_list_by_copr(cls, copr_id, package_name):
113 return models.Package.query.filter(models.Package.copr_id == copr_id, 114 models.Package.name == package_name)
115 116 @classmethod
117 - def get(cls, copr_dir_id, package_name):
118 return models.Package.query.filter(models.Package.copr_dir_id == copr_dir_id, 119 models.Package.name == package_name)
120 121 @classmethod
122 - def get_by_dir_name(cls, copr_dir_name, package_name):
123 return models.Package.query.join(models.CoprDir).filter( 124 models.CoprDir.name == copr_dir_name, models.Package.name == package_name)
125 126 @classmethod
127 - def get_or_create(cls, copr_dir, package_name, src_pkg):
128 package = cls.get_by_dir_name(copr_dir.name, package_name).first() 129 130 if package: 131 return package 132 133 package = models.Package( 134 name=src_pkg.name, 135 copr=src_pkg.copr, 136 source_type=src_pkg.source_type, 137 source_json=src_pkg.source_json, 138 copr_dir=copr_dir) 139 140 db.session.add(package) 141 return package
142 143 @classmethod
144 - def get_for_webhook_rebuild(cls, copr_id, webhook_secret, clone_url, commits, ref_type, ref):
145 clone_url_stripped = re.sub(r'(\.git)?/*$', '', clone_url) 146 147 packages = (models.Package.query.join(models.Copr) 148 .filter(models.Copr.webhook_secret == webhook_secret) 149 .filter(models.Package.source_type == helpers.BuildSourceEnum("scm")) 150 .filter(models.Package.copr_id == copr_id) 151 .filter(models.Package.webhook_rebuild == true()) 152 .filter(models.Package.source_json.contains(clone_url_stripped))) 153 154 result = [] 155 for package in packages: 156 package_clone_url = package.source_json_dict.get('clone_url', '') 157 package_clone_url_stripped = re.sub(r'(\.git)?/*$', '', package_clone_url) 158 159 if package_clone_url_stripped != clone_url_stripped: 160 continue 161 162 if cls.commits_belong_to_package(package, commits, ref_type, ref): 163 result += [package] 164 165 return result
166 167 @classmethod
168 - def commits_belong_to_package(cls, package, commits, ref_type, ref):
169 if ref_type == "tag": 170 matches = re.search(r'(.*)-[^-]+-[^-]+$', ref) 171 if matches and package.name != matches.group(1): 172 return False 173 else: 174 return True 175 176 committish = package.source_json_dict.get("committish") or '' 177 if committish and not ref.endswith(committish): 178 return False 179 180 path_match = True 181 for commit in commits: 182 for file_path in commit['added'] + commit['removed'] + commit['modified']: 183 path_match = False 184 if cls.path_belong_to_package(package, file_path): 185 path_match = True 186 break 187 if not path_match: 188 return False 189 190 return True
191 192 @classmethod
193 - def path_belong_to_package(cls, package, file_path):
194 data = package.source_json_dict 195 norm_file_path = file_path.strip('./') 196 package_subdir = data.get('subdirectory') or '' 197 return norm_file_path.startswith(package_subdir.strip('./'))
198 199 @classmethod
200 - def add(cls, user, copr_dir, package_name, source_type=helpers.BuildSourceEnum("unset"), source_json=json.dumps({})):
201 users_logic.UsersLogic.raise_if_cant_build_in_copr( 202 user, copr_dir.copr, 203 "You don't have permissions to build in this copr.") 204 205 if cls.exists(copr_dir.id, package_name).all(): 206 raise exceptions.DuplicateException( 207 "Project dir {} already has a package '{}'" 208 .format(copr_dir.full_name, package_name)) 209 210 package = models.Package( 211 name=package_name, 212 copr=copr_dir.copr, 213 copr_dir=copr_dir, 214 source_type=source_type, 215 source_json=source_json, 216 ) 217 218 db.session.add(package) 219 return package
220 221 @classmethod
222 - def exists(cls, copr_dir_id, package_name):
223 return (models.Package.query 224 .filter(models.Package.copr_dir_id == copr_dir_id) 225 .filter(models.Package.name == package_name))
226 227 228 @classmethod
229 - def delete_package(cls, user, package):
230 if not user.can_edit(package.copr): 231 raise exceptions.InsufficientRightsException( 232 "You are not allowed to delete package `{}`.".format(package.id)) 233 234 for build in package.builds: 235 builds_logic.BuildsLogic.delete_build(user, build) 236 237 db.session.delete(package)
238 239 240 @classmethod
241 - def reset_package(cls, user, package):
242 if not user.can_edit(package.copr): 243 raise exceptions.InsufficientRightsException( 244 "You are not allowed to reset package `{}`.".format(package.id)) 245 246 package.source_json = json.dumps({}) 247 package.source_type = helpers.BuildSourceEnum("unset") 248 249 db.session.add(package)
250 251 252 @classmethod
253 - def build_package(cls, user, copr, package, chroot_names=None, copr_dirname=None, **build_options):
254 if not package.has_source_type_set or not package.source_json: 255 raise exceptions.NoPackageSourceException('Unset default source for package {0}'.format(package.name)) 256 return builds_logic.BuildsLogic.create_new(user, copr, package.source_type, package.source_json, 257 chroot_names, copr_dirname=copr_dirname, **build_options)
258 259 260 @classmethod
261 - def batch_build(cls, user, copr, packages, chroot_names=None, **build_options):
262 new_builds = [] 263 264 batch = models.Batch() 265 db.session.add(batch) 266 267 for package in packages: 268 git_hashes = {} 269 skip_import = False 270 source_build = None 271 272 if (package.source_type == helpers.BuildSourceEnum('upload') or 273 package.source_type == helpers.BuildSourceEnum('link')): 274 source_build = package.last_build() 275 276 if not source_build or not source_build.build_chroots[0].git_hash: 277 raise exceptions.NoPackageSourceException( 278 "Could not get latest git hash for {}".format(package.name)) 279 280 for chroot_name in chroot_names: 281 git_hashes[chroot_name] = source_build.build_chroots[0].git_hash 282 skip_import = True 283 284 new_build = builds_logic.BuildsLogic.create_new( 285 user, 286 copr, 287 package.source_type, 288 package.source_json, 289 chroot_names, 290 git_hashes=git_hashes, 291 skip_import=skip_import, 292 batch=batch, 293 **build_options) 294 295 if source_build: 296 new_build.package_id = source_build.package_id 297 new_build.pkg_version = source_build.pkg_version 298 299 new_builds.append(new_build) 300 301 return new_builds
302