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"] == "build": 40 self.event("Action delete build") 41 project = self.data["old_value"] 42 packages = map(lambda x: 43 os.path.basename(x).replace(".src.rpm", ""), 44 self.data["data"].split()) 45 46 path = os.path.join(self.destdir, project) 47 48 self.event("Packages to delete {0}".format(' '.join(packages))) 49 self.event("Copr path {0}".format(path)) 50 51 for chroot in os.listdir(path): 52 self.event("In chroot {0}".format(chroot)) 53 altered = False 54 55 for pkg in packages: 56 pkg_path = os.path.join(path, chroot, pkg) 57 if os.path.isdir(pkg_path): 58 self.event("Removing build {0}".format(pkg_path)) 59 shutil.rmtree(pkg_path) 60 altered = True 61 else: 62 self.event("Package {0} dir not found in chroot {1}" 63 .format(pkg, chroot)) 64 65 if altered: 66 self.event("Running createrepo") 67 rc, out, err = createrepo(os.path.join(path, chroot), self.lock) 68 if err.strip(): 69 self.event( 70 "Error making local repo: {0}".format(err)) 71 72 log_path = os.path.join( 73 path, chroot, 74 'build-{0}.log'.format(self.data['object_id'])) 75 76 if os.path.isfile(log_path): 77 self.event("Removing log {0}".format(log_path)) 78 os.unlink(log_path) 79 80 result.job_ended_on = time.time() 81 result.result = 1 # success 82 83 elif self.data["action_type"] == 1: # rename 84 self.event("Action rename") 85 old_path = os.path.normpath( 86 self.destdir + '/', self.data["old_value"]) 87 new_path = os.path.normpath( 88 self.destdir + '/', self.data["new_value"]) 89 90 if os.path.exists(old_path): 91 if not os.path.exists(new_path): 92 shutil.move(old_path, new_path) 93 result.result = 1 # success 94 else: 95 result.message = "Destination directory already exist." 96 result.result = 2 # failure 97 else: # nothing to do, that is success too 98 result.result = 1 # success 99 result.job_ended_on = time.time() 100 101 elif self.data["action_type"] == 2: # legal-flag 102 self.event("Action legal-flag: ignoring") 103 104 if "result" in result: 105 self.frontend_callback.post_to_frontend({"actions": [result]})
106