Add job id transformation/check to Luxi Python client
authorHrvoje Ribicic <riba@google.com>
Wed, 15 Jan 2014 13:48:51 +0000 (13:48 +0000)
committerHrvoje Ribicic <riba@google.com>
Mon, 20 Jan 2014 12:11:45 +0000 (13:11 +0100)
This patch adds checks to the Luxi client, making sure that job ids
are converted from strings to ints before being passed on, or that an
error is reported.

Signed-off-by: Hrvoje Ribicic <riba@google.com>
Reviewed-by: Klaus Aehlig <aehlig@google.com>

lib/luxi.py

index 0de9185..7f66761 100644 (file)
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2006, 2007, 2011, 2012 Google Inc.
+# Copyright (C) 2006, 2007, 2011, 2012, 2014 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -487,13 +487,25 @@ class Client(object):
       jobs_state.append([op.__getstate__() for op in ops])
     return self.CallMethod(REQ_SUBMIT_MANY_JOBS, (jobs_state, ))
 
+  @staticmethod
+  def _PrepareJobId(request_name, job_id):
+    try:
+      return int(job_id)
+    except ValueError:
+      raise RequestError("Invalid parameter passed to %s as job id: "
+                         " expected integer, got value %s" %
+                         (request_name, job_id))
+
   def CancelJob(self, job_id):
+    job_id = Client._PrepareJobId(REQ_CANCEL_JOB, job_id)
     return self.CallMethod(REQ_CANCEL_JOB, (job_id, ))
 
   def ArchiveJob(self, job_id):
+    job_id = Client._PrepareJobId(REQ_ARCHIVE_JOB, job_id)
     return self.CallMethod(REQ_ARCHIVE_JOB, (job_id, ))
 
   def ChangeJobPriority(self, job_id, priority):
+    job_id = Client._PrepareJobId(REQ_CHANGE_JOB_PRIORITY, job_id)
     return self.CallMethod(REQ_CHANGE_JOB_PRIORITY, (job_id, priority))
 
   def AutoArchiveJobs(self, age):
@@ -524,6 +536,7 @@ class Client(object):
                             min(WFJC_TIMEOUT, timeout)))
 
   def WaitForJobChange(self, job_id, fields, prev_job_info, prev_log_serial):
+    job_id = Client._PrepareJobId(REQ_WAIT_FOR_JOB_CHANGE, job_id)
     while True:
       result = self.WaitForJobChangeOnce(job_id, fields,
                                          prev_job_info, prev_log_serial)