Package backend :: Module actions
[hide private]
[frames] | no frames]

Source Code for Module backend.actions

  1  from bunch import Bunch 
  2  from callback import FrontendCallback 
  3  import os.path 
  4  import shutil 
  5  import time 
  6   
  7  from mockremote import createrepo 
  8   
  9   
10 -class Action(object):
11 12 """ Object to send data back to fronted """ 13
14 - def __init__(self, opts, events, action, lock):
15 super(Action, self).__init__() 16 self.frontend_callback = FrontendCallback(opts) 17 self.destdir = opts.destdir 18 self.data = action 19 self.events = events 20 self.lock = lock
21
22 - def event(self, what):
23 self.events.put({"when": time.time(), "who": "action", "what": what})
24
25 - def run(self):
26 """ Handle action (other then builds) - like rename or delete of project """ 27 result = Bunch() 28 result.id = self.data["id"] 29 30 if self.data["action_type"] == 0: # delete 31 if self.data["object_type"] == "copr": 32 self.event("Action delete copr") 33 project = self.data["old_value"] 34 path = os.path.normpath(self.destdir + '/' + project) 35 if os.path.exists(path): 36 self.event("Removing copr {0}".format(path)) 37 shutil.rmtree(path) 38 39 elif self.data["object_type"] in ["build-succeeded", 40 "build-skipped", 41 "build-failed"]: 42 self.event("Action delete build") 43 project = self.data["old_value"] 44 packages = map(lambda x: 45 os.path.basename(x).replace(".src.rpm", ""), 46 self.data["data"].split()) 47 48 path = os.path.join(self.destdir, project) 49 50 self.event("Packages to delete {0}".format(' '.join(packages))) 51 self.event("Copr path {0}".format(path)) 52 53 try: 54 chroot_list = os.listdir(path) 55 except OSError: 56 # already deleted 57 chroot_list = [] 58 for chroot in chroot_list: 59 self.event("In chroot {0}".format(chroot)) 60 altered = False 61 62 # We need to delete the files only if they belong 63 # to the build. For example if my build fails and I send 64 # fixed pkg with the same version again, it succeeds and 65 # than I delete the failed, it would delete the succeeded 66 # files as well - that would be wrong. 67 for pkg in packages: 68 if self.data["object_type"] == "build-succeeded" or \ 69 (self.data["object_type"] == "build-failed" and 70 os.path.exists(os.path.join(path, chroot, pkg, "fail"))): 71 pkg_path = os.path.join(path, chroot, pkg) 72 if os.path.isdir(pkg_path): 73 self.event("Removing build {0}".format(pkg_path)) 74 shutil.rmtree(pkg_path) 75 altered = True 76 else: 77 self.event( 78 "Package {0} dir not found in chroot {1}" 79 .format(pkg, chroot)) 80 81 if altered: 82 self.event("Running createrepo") 83 _, _, err = createrepo(os.path.join(path, chroot), self.lock) 84 if err.strip(): 85 self.event( 86 "Error making local repo: {0}".format(err)) 87 88 log_path = os.path.join( 89 path, chroot, 90 'build-{0}.log'.format(self.data['object_id'])) 91 92 if os.path.isfile(log_path): 93 self.event("Removing log {0}".format(log_path)) 94 os.unlink(log_path) 95 96 result.job_ended_on = time.time() 97 result.result = 1 # success 98 99 elif self.data["action_type"] == 1: # rename 100 self.event("Action rename") 101 old_path = os.path.normpath( 102 self.destdir + '/', self.data["old_value"]) 103 new_path = os.path.normpath( 104 self.destdir + '/', self.data["new_value"]) 105 106 if os.path.exists(old_path): 107 if not os.path.exists(new_path): 108 shutil.move(old_path, new_path) 109 result.result = 1 # success 110 else: 111 result.message = "Destination directory already exist." 112 result.result = 2 # failure 113 else: # nothing to do, that is success too 114 result.result = 1 # success 115 result.job_ended_on = time.time() 116 117 elif self.data["action_type"] == 2: # legal-flag 118 self.event("Action legal-flag: ignoring") 119 120 if "result" in result: 121 self.frontend_callback.post_to_frontend({"actions": [result]})
122