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

Source Code for Module coprs.logic.actions_logic

  1  import json 
  2  import time 
  3  import base64 
  4  from coprs import db 
  5  from coprs import models 
  6  from coprs import helpers 
  7  from flask import url_for 
8 9 10 -class ActionsLogic(object):
11 12 @classmethod
13 - def get(cls, action_id):
14 """ 15 Return single action identified by `action_id` 16 """ 17 18 query = models.Action.query.filter(models.Action.id == action_id) 19 return query
20 21 @classmethod
22 - def get_many(cls, action_type=None, result=None):
23 query = models.Action.query 24 if action_type is not None: 25 query = query.filter(models.Action.action_type == 26 int(action_type)) 27 if result is not None: 28 query = query.filter(models.Action.result == 29 int(result)) 30 31 return query
32 33 @classmethod
34 - def get_waiting(cls):
35 """ 36 Return actions that aren't finished 37 """ 38 39 query = (models.Action.query 40 .filter(models.Action.result == 41 helpers.BackendResultEnum("waiting")) 42 .filter(models.Action.action_type != 43 helpers.ActionTypeEnum("legal-flag")) 44 .order_by(models.Action.created_on.asc())) 45 46 return query
47 48 @classmethod
49 - def get_by_ids(cls, ids):
50 """ 51 Return actions matching passed `ids` 52 """ 53 54 return models.Action.query.filter(models.Action.id.in_(ids))
55 56 @classmethod
57 - def update_state_from_dict(cls, action, upd_dict):
58 """ 59 Update `action` object with `upd_dict` data 60 61 Updates result, message and ended_on parameters. 62 """ 63 64 for attr in ["result", "message", "ended_on"]: 65 value = upd_dict.get(attr, None) 66 if value: 67 setattr(action, attr, value) 68 69 db.session.add(action)
70 71 @classmethod
72 - def send_createrepo(cls, username, coprname, chroots):
73 data_dict = { 74 "username": username, 75 "projectname": coprname, 76 "chroots": chroots 77 } 78 action = models.Action( 79 action_type=helpers.ActionTypeEnum("createrepo"), 80 object_type="None", 81 object_id=0, 82 old_value="", 83 data=json.dumps(data_dict), 84 created_on=int(time.time()), 85 ) 86 db.session.add(action)
87 88 @classmethod
89 - def send_delete_build(cls, build):
90 """ Schedules build delete action 91 :type build: models.Build 92 """ 93 # don't delete skipped chroots 94 chroots_to_delete = [ 95 chroot.name for chroot in build.build_chroots 96 if chroot.state not in ["skipped"] 97 ] 98 if len(chroots_to_delete) == 0: 99 return 100 101 data_dict = { 102 "username": build.copr.owner_name, 103 "projectname": build.copr.name, 104 "chroots": chroots_to_delete 105 } 106 107 if build.is_older_results_naming_used: 108 if build.src_pkg_name is None or build.src_pkg_name == "": 109 return 110 data_dict["src_pkg_name"] = build.src_pkg_name 111 else: 112 data_dict["result_dir_name"] = build.result_dir_name 113 114 action = models.Action( 115 action_type=helpers.ActionTypeEnum("delete"), 116 object_type="build", 117 object_id=build.id, 118 old_value=build.copr.full_name, 119 data=json.dumps(data_dict), 120 created_on=int(time.time()) 121 ) 122 db.session.add(action)
123 124 @classmethod
125 - def send_update_comps(cls, chroot):
126 """ Schedules update comps.xml action 127 128 :type copr_chroot: models.CoprChroot 129 """ 130 131 if chroot.copr.is_a_group_project: 132 url_path = url_for('coprs_ns.group_chroot_view_comps', 133 group_name=chroot.copr.group.name, 134 coprname=chroot.copr.name, 135 chrootname=chroot.name) 136 else: 137 url_path = url_for('coprs_ns.chroot_view_comps', 138 username=chroot.copr.user.username, 139 coprname=chroot.copr.name, 140 chrootname=chroot.name) 141 142 data_dict = { 143 "ownername": chroot.copr.owner_name, 144 "projectname": chroot.copr.name, 145 "chroot": chroot.name, 146 "comps_present": chroot.comps_zlib is not None, 147 "url_path": url_path, 148 } 149 150 action = models.Action( 151 action_type=helpers.ActionTypeEnum("update_comps"), 152 object_type="copr_chroot", 153 data=json.dumps(data_dict), 154 created_on=int(time.time()) 155 ) 156 db.session.add(action)
157 158 @classmethod
159 - def send_update_module_md(cls, chroot):
160 """ Schedules update module_md.yaml action 161 162 :type copr_chroot: models.CoprChroot 163 """ 164 if chroot.copr.is_a_group_project: 165 url_path = url_for('coprs_ns.group_chroot_view_module_md', 166 group_name=chroot.copr.group.name, 167 coprname=chroot.copr.name, 168 chrootname=chroot.name) 169 else: 170 url_path = url_for('coprs_ns.chroot_view_module_md', 171 username=chroot.copr.user.username, 172 coprname=chroot.copr.name, 173 chrootname=chroot.name) 174 175 data_dict = { 176 "ownername": chroot.copr.owner_name, 177 "projectname": chroot.copr.name, 178 "chroot": chroot.name, 179 "module_md_present": chroot.module_md_zlib is not None, 180 "url_path": url_path, 181 } 182 183 action = models.Action( 184 action_type=helpers.ActionTypeEnum("update_module_md"), 185 object_type="copr_chroot", 186 data=json.dumps(data_dict), 187 created_on=int(time.time()) 188 ) 189 db.session.add(action)
190 191 @classmethod
192 - def send_create_gpg_key(cls, copr):
193 """ 194 :type copr: models.Copr 195 """ 196 197 data_dict = { 198 "username": copr.owner_name, 199 "projectname": copr.name, 200 } 201 202 action = models.Action( 203 action_type=helpers.ActionTypeEnum("gen_gpg_key"), 204 object_type="copr", 205 data=json.dumps(data_dict), 206 created_on=int(time.time()), 207 ) 208 db.session.add(action)
209 210 @classmethod
211 - def send_rawhide_to_release(cls, data):
212 action = models.Action( 213 action_type=helpers.ActionTypeEnum("rawhide_to_release"), 214 object_type="None", 215 data=json.dumps(data), 216 created_on=int(time.time()), 217 ) 218 db.session.add(action)
219 220 @classmethod
221 - def send_fork_copr(cls, src, dst, builds_map):
222 """ 223 :type src: models.Copr 224 :type dst: models.Copr 225 :type builds_map: dict where keys are forked builds IDs and values are IDs from the original builds. 226 """ 227 228 action = models.Action( 229 action_type=helpers.ActionTypeEnum("fork"), 230 object_type="copr", 231 old_value="{0}".format(src.full_name), 232 new_value="{0}".format(dst.full_name), 233 data=json.dumps({"user": dst.owner_name, "copr": dst.name, "builds_map": builds_map}), 234 created_on=int(time.time()), 235 ) 236 db.session.add(action)
237 238 @classmethod
239 - def send_build_module(cls, copr, modulemd):
240 """ 241 :type copr: models.Copr 242 :type modulemd: str content of module yaml file 243 """ 244 245 modulemd_b64 = base64.b64encode(modulemd) 246 data = { 247 "ownername": copr.owner_name, 248 "projectname": copr.name, 249 "chroots": [c.name for c in copr.active_chroots], 250 "modulemd_b64": modulemd_b64, 251 } 252 253 action = models.Action( 254 action_type=helpers.ActionTypeEnum("build_module"), 255 object_type="copr", 256 old_value="", 257 new_value="", 258 data=json.dumps(data), 259 created_on=int(time.time()), 260 ) 261 db.session.add(action)
262