Repository :
http://git.fedorahosted.org/cgit/copr.git
On branch : master
---------------------------------------------------------------
commit c5a8d40d0f18433ba5d23be9384110f1a6e58bc1
Author: Valentin Gologuzov <vgologuz(a)redhat.com>
Date: Mon Oct 20 18:44:37 2014 +0200
[backend] refactored a bit Action.py
---------------------------------------------------------------
backend/backend/actions.py | 190 ++++++++++++++++++++++++-------------------
1 files changed, 106 insertions(+), 84 deletions(-)
diff --git a/backend/backend/actions.py b/backend/backend/actions.py
index edcbd78..d9b4f00 100644
--- a/backend/backend/actions.py
+++ b/backend/backend/actions.py
@@ -10,7 +10,16 @@ from mockremote import createrepo
class Action(object):
""" Object to send data back to fronted
- :param multiprocessing.Queue events: ? collects events for logging
+ :param multiprocessing.Queue events: collects events for logging
+ :param multiprocessing.Lock lock: Global lock for backend
+ :param backend.callback.FrontendCallback frontent_callback:
+ object to post data back to frontend
+
+ :param str destdir: filepath with build results
+
+ :param dict action: action job, fields:
+ - action_type: main field determining what action to apply
+ # TODO: describe actions
"""
@@ -25,100 +34,113 @@ class Action(object):
def event(self, what):
self.events.put({"when": time.time(), "who":
"action", "what": what})
+ def handle_legal_flag(self):
+ self.event("Action legal-flag: ignoring")
+
+ def handle_rename(self, result):
+ self.event("Action rename")
+ old_path = os.path.normpath(os.path.join(
+ self.destdir, self.data["old_value"]))
+ new_path = os.path.normpath(os.path.join(
+ self.destdir, self.data["new_value"]))
+
+ if os.path.exists(old_path):
+ if not os.path.exists(new_path):
+ shutil.move(old_path, new_path)
+ result.result = 1 # success
+ else:
+ result.message = "Destination directory already exist."
+ result.result = 2 # failure
+ else: # nothing to do, that is success too
+ result.result = 1 # success
+ result.job_ended_on = time.time()
+
+ def handle_delete_copr_project(self):
+ self.event("Action delete copr")
+ project = self.data["old_value"]
+ path = os.path.normpath(self.destdir + '/' + project)
+ if os.path.exists(path):
+ self.event("Removing copr {0}".format(path))
+ shutil.rmtree(path)
+
+ def handle_delete_build(self):
+ self.event("Action delete build")
+ project = self.data["old_value"]
+ packages = [os.path.basename(x).replace(".src.rpm", "") for x
in \
+ self.data["data"].split()]
+
+ path = os.path.join(self.destdir, project)
+
+ self.event("Packages to delete {0}".format('
'.join(packages)))
+ self.event("Copr path {0}".format(path))
+
+ try:
+ chroot_list = os.listdir(path)
+ except OSError:
+ # already deleted
+ chroot_list = []
+
+ for chroot in chroot_list:
+ self.event("In chroot {0}".format(chroot))
+ altered = False
+
+ # We need to delete the files only if they belong
+ # to the build. For example if my build fails and I send
+ # fixed pkg with the same version again, it succeeds and
+ # than I delete the failed, it would delete the succeeded
+ # files as well - that would be wrong.
+ for pkg in packages:
+ if self.data["object_type"] == "build-succeeded" or
\
+ (self.data["object_type"] == "build-failed"
and
+ os.path.exists(os.path.join(path, chroot, pkg,
"fail"))
+ ):
+ pkg_path = os.path.join(path, chroot, pkg)
+ if os.path.isdir(pkg_path):
+ self.event("Removing build {0}".format(pkg_path))
+ shutil.rmtree(pkg_path)
+ altered = True
+ else:
+ self.event(
+ "Package {0} dir not found in chroot {1}"
+ .format(pkg, chroot))
+
+ if altered:
+ self.event("Running createrepo")
+ _, _, err = createrepo(os.path.join(path, chroot), self.lock)
+ if err.strip():
+ self.event(
+ "Error making local repo: {0}".format(err))
+
+ log_path = os.path.join(
+ path, chroot,
+ 'build-{0}.log'.format(self.data['object_id']))
+
+ if os.path.isfile(log_path):
+ self.event("Removing log {0}".format(log_path))
+ os.unlink(log_path)
+
def run(self):
""" Handle action (other then builds) - like rename or delete of
project """
result = Bunch()
result.id = self.data["id"]
- if self.data["action_type"] == ActionType.DELETE:
+ action_type = self.data["action_type"]
+
+ if action_type == ActionType.DELETE:
if self.data["object_type"] == "copr":
- self.event("Action delete copr")
- project = self.data["old_value"]
- path = os.path.normpath(self.destdir + '/' + project)
- if os.path.exists(path):
- self.event("Removing copr {0}".format(path))
- shutil.rmtree(path)
-
- elif self.data["object_type"] in ["build-succeeded",
- "build-skipped",
- "build-failed"]:
- self.event("Action delete build")
- project = self.data["old_value"]
- packages = [os.path.basename(x).replace(".src.rpm",
"") for x in \
- self.data["data"].split()]
-
- path = os.path.join(self.destdir, project)
-
- self.event("Packages to delete {0}".format('
'.join(packages)))
- self.event("Copr path {0}".format(path))
-
- try:
- chroot_list = os.listdir(path)
- except OSError:
- # already deleted
- chroot_list = []
- for chroot in chroot_list:
- self.event("In chroot {0}".format(chroot))
- altered = False
-
- # We need to delete the files only if they belong
- # to the build. For example if my build fails and I send
- # fixed pkg with the same version again, it succeeds and
- # than I delete the failed, it would delete the succeeded
- # files as well - that would be wrong.
- for pkg in packages:
- if self.data["object_type"] ==
"build-succeeded" or \
- (self.data["object_type"] ==
"build-failed" and
- os.path.exists(os.path.join(path, chroot, pkg,
"fail"))
- ):
- pkg_path = os.path.join(path, chroot, pkg)
- if os.path.isdir(pkg_path):
- self.event("Removing build
{0}".format(pkg_path))
- shutil.rmtree(pkg_path)
- altered = True
- else:
- self.event(
- "Package {0} dir not found in chroot {1}"
- .format(pkg, chroot))
-
- if altered:
- self.event("Running createrepo")
- _, _, err = createrepo(os.path.join(path, chroot),
self.lock)
- if err.strip():
- self.event(
- "Error making local repo:
{0}".format(err))
-
- log_path = os.path.join(
- path, chroot,
- 'build-{0}.log'.format(self.data['object_id']))
-
- if os.path.isfile(log_path):
- self.event("Removing log {0}".format(log_path))
- os.unlink(log_path)
+ self.handle_delete_copr_project()
+ elif self.data["object_type"] in \
+ ["build-succeeded", "build-skipped",
"build-failed"]:
+ self.handle_delete_build()
result.job_ended_on = time.time()
result.result = 1 # success
- elif self.data["action_type"] == ActionType.RENAME:
- self.event("Action rename")
- old_path = os.path.normpath(os.path.join(
- self.destdir, self.data["old_value"]))
- new_path = os.path.normpath(os.path.join(
- self.destdir, self.data["new_value"]))
-
- if os.path.exists(old_path):
- if not os.path.exists(new_path):
- shutil.move(old_path, new_path)
- result.result = 1 # success
- else:
- result.message = "Destination directory already exist."
- result.result = 2 # failure
- else: # nothing to do, that is success too
- result.result = 1 # success
- result.job_ended_on = time.time()
+ elif action_type == ActionType.LEGAL_FLAG:
+ self.handle_legal_flag()
- elif self.data["action_type"] == ActionType.LEGAL_FLAG:
- self.event("Action legal-flag: ignoring")
+ elif action_type == ActionType.RENAME:
+ self.handle_rename(result)
if "result" in result:
self.frontend_callback.update({"actions": [result]})