Add a way to delete an individual set of job results.

People have been asking for a way to delete bad jobs.

Signed-off-by: Martin Bligh <mbligh@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@968 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/tko/db.py b/tko/db.py
index 7b1f265..d76312e 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -114,6 +114,20 @@
 			self.con.commit()
 
 
+	def delete(self, table, where, commit = None):
+		cmd = ['delete from', table]
+		if commit == None:
+			commit = self.autocommit
+		if where and isinstance(where, types.DictionaryType):
+			keys = [field + '=%s' for field in where.keys()]
+			values = [where[field] for field in where.keys()]
+			cmd += ['where', ' and '.join(keys)]
+		self.dprint('%s %s' % (' '.join(cmd),values))
+		self.cur.execute(' '.join(cmd), values)
+		if commit:
+			self.con.commit()
+		
+
 	def update(self, table, data, where, commit = None):
 		"""\
 			'update table set data values (%s ... %s) where ...'
@@ -139,6 +153,17 @@
 			self.con.commit()
 
 
+	def delete_job(self, tag, commit = None):
+		job_idx = self.find_job(tag)
+		for test_idx in self.find_tests(job_idx):
+			where = {'test_idx' : test_idx}
+			self.delete('iteration_result', where)
+			self.delete('test_attributes', where)
+		where = {'job_idx' : job_idx}
+		self.delete('tests', where)
+		self.delete('jobs', where)
+
+
 	def insert_job(self, tag, job, commit = None):
 		job.machine_idx = self.lookup_machine(job.machine)
 		if not job.machine_idx:
@@ -153,6 +178,7 @@
 		for test in job.tests:
 			self.insert_test(job, test, commit=commit)
 
+
 	def insert_test(self, job, test, commit = None):
 		kver = self.insert_kernel(test.kernel, commit=commit)
 		data = {'job_idx':job.index, 'test':test.testname,
@@ -250,6 +276,15 @@
 			return None
 
 
+	def find_tests(self, job_idx):
+		where = { 'job_idx':job_idx }
+		rows = self.select('test_idx', 'tests', where)
+		if rows:
+			return [row[0] for row in rows]
+		else:
+			return None
+
+
 	def find_job(self, tag):
 		rows = self.select('job_idx', 'jobs', {'tag': tag})
 		if rows:
diff --git a/tko/delete_job_results b/tko/delete_job_results
new file mode 100644
index 0000000..ec4c106
--- /dev/null
+++ b/tko/delete_job_results
@@ -0,0 +1,23 @@
+#!/usr/bin/python
+import os, sys, shutil
+thisdir = os.path.dirname(os.path.abspath(sys.argv[0]))
+sys.path.insert(0, os.path.abspath(os.path.join(thisdir, '../tko')))
+import db
+
+usage = "usage: delete_job_results <job tag>"
+
+if len(sys.argv) < 2:
+	print usage
+	sys.exit(2)
+tag = sys.argv[1]
+resultsdir = os.path.abspath(os.path.join(thisdir, '../results', tag))
+
+if not os.path.isdir(resultsdir):
+	raise "Results directory %s does not exist" % resultsdir
+db = db.db()
+if not db.find_job(tag):
+	raise "Job tag %s does not exist in database" % tag
+
+shutil.rmtree(resultsdir)
+db.delete_job(tag)
+