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
11 """ Object to send data back to fronted """
12
13 - def __init__(self, opts, events, action):
19
21 self.events.put({'when':time.time(), 'who':'action', 'what':what})
22
24 """ Handle action (other then builds) - like rename or delete of project """
25 result = Bunch()
26 result.id = self.data['id']
27 if self.data['action_type'] == 0:
28 if self.data['object_type'] == 'copr':
29 self.event("Action delete copr")
30 project = self.data['old_value']
31 path = os.path.normpath(self.destdir + '/' + project)
32 if os.path.exists(path):
33 self.event('Removing copr %s' % path)
34 shutil.rmtree(path)
35
36 elif self.data['object_type'] == 'build':
37 self.event("Action delete build")
38 project = self.data['old_value']
39 packages = map(lambda x:
40 os.path.basename(x).replace('.src.rpm', ''),
41 self.data['data'].split())
42
43 path = os.path.join(self.destdir, project)
44
45 self.event("Packages to delete {0}".format(' '.join(packages)))
46 self.event("Copr path {0}".format(path))
47
48 for chroot in os.listdir(path):
49 self.event("In chroot {0}".format(chroot))
50 altered = False
51
52 for pkg in packages:
53 pkg_path = os.path.join(path, chroot, pkg)
54 if os.path.isdir(pkg_path):
55 self.event('Removing build {0}'.format(pkg_path))
56 shutil.rmtree(pkg_path)
57 altered = True
58 else:
59 self.event('Package {0} dir not found in chroot {1}'
60 .format(pkg, chroot))
61
62 if altered:
63 self.event("Running createrepo")
64 rc, out, err = createrepo(os.path.join(path, chroot))
65 if err.strip():
66 self.event("Error making local repo: {0}".format(err))
67
68 log_path = os.path.join(
69 path, chroot,
70 'build-{0}.log'.format(self.data['object_id']))
71
72 if os.path.isfile(log_path):
73 self.event("Removing log {0}".format(log_path))
74 os.unlink(log_path)
75
76 result.job_ended_on = time.time()
77 result.result = 1
78 elif self.data['action_type'] == 1:
79 self.event("Action rename")
80 old_path = os.path.normpath(self.destdir + '/', self.data['old_value'])
81 new_path = os.path.normpath(self.destdir + '/', self.data['new_value'])
82 if os.path.exists(old_path):
83 if not os.path.exists(new_path):
84 shutil.move(old_path, new_path)
85 result.result = 1
86 else:
87 result.message = 'Destination directory already exist.'
88 result.result = 2
89 else:
90 result.result = 1
91 result.job_ended_on = time.time()
92 elif self.data['action_type'] == 2:
93 self.event("Action legal-flag: ignoring")
94
95 if 'result' in result:
96 self.frontend_callback.post_to_frontend( {'actions': [result]} )
97