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
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
23 self.events.put({"when": time.time(), "who": "action", "what": what})
24
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:
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
57 chroot_list = []
58 for chroot in chroot_list:
59 self.event("In chroot {0}".format(chroot))
60 altered = False
61
62
63
64
65
66
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
98
99 elif self.data["action_type"] == 1:
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
110 else:
111 result.message = "Destination directory already exist."
112 result.result = 2
113 else:
114 result.result = 1
115 result.job_ended_on = time.time()
116
117 elif self.data["action_type"] == 2:
118 self.event("Action legal-flag: ignoring")
119
120 if "result" in result:
121 self.frontend_callback.post_to_frontend({"actions": [result]})
122