skeleton class of the Invocation object
authorsalimfadhley <sal@stodge.org>
Sun, 23 Jun 2013 07:49:35 +0000 (08:49 +0100)
committerSalim Fadhley <sal@stodge.org>
Sun, 23 Jun 2013 20:21:53 +0000 (21:21 +0100)
jenkinsapi/invocation.py [moved from jenkinsapi/invokation.py with 97% similarity]
jenkinsapi/job.py
jenkinsapi_tests/systests/test_jenkins.py
jenkinsapi_tests/systests/test_nodes.py
jenkinsapi_tests/systests/test_parameterized_builds.py

similarity index 97%
rename from jenkinsapi/invokation.py
rename to jenkinsapi/invocation.py
index de95fef..6be65c7 100644 (file)
@@ -17,7 +17,7 @@ class Invocation(object):
         Start watching the job
         """
 
-    def __exit__(self):
+    def __exit__(self, type, value, traceback):
         """
         Finish watching the job - it will track which new queue items or builds have
         been created as a consequence of invoking the job.
index bf636ce..68af880 100644 (file)
@@ -5,6 +5,7 @@ import xml.etree.ElementTree as ET
 from collections import defaultdict
 from time import sleep
 from jenkinsapi.build import Build
+from jenkinsapi.invocation import Invocation
 from jenkinsapi.jenkinsbase import JenkinsBase
 from jenkinsapi import exceptions
 from jenkinsapi.mutable_jenkins_thing import MutableJenkinsThing
@@ -92,56 +93,62 @@ class Job(JenkinsBase, MutableJenkinsThing):
         assert isinstance(block, bool)
         assert isinstance(skip_if_running, bool)
 
+        # Create a new invocation instance
+        invocation = Invocation(self)
+
         # Either copy the params dict or make a new one.
         build_params = build_params and dict(
             build_params.items()) or {}  # Via POSTed JSON
         params = {}  # Via Get string
 
-        if self.is_queued():
-            raise WillNotBuild('%s is already queued' % repr(self))
 
-        elif self.is_running():
-            if skip_if_running:
-                log.warn(
-                    "Will not request new build because %s is already running", self.name)
-            else:
-                log.warn(
-                    "Will re-schedule %s even though it is already running", self.name)
+        with invocation:
+            if self.is_queued():
+                raise WillNotBuild('%s is already queued' % repr(self))
 
-        log.info("Attempting to start %s on %s", self.name, repr(
-            self.get_jenkins_obj()))
+            elif self.is_running():
+                if skip_if_running:
+                    log.warn(
+                        "Will not request new build because %s is already running", self.name)
+                else:
+                    log.warn(
+                        "Will re-schedule %s even though it is already running", self.name)
 
-        url = self.get_build_triggerurl()
+            log.info("Attempting to start %s on %s", self.name, repr(
+                self.get_jenkins_obj()))
 
-        if cause:
-            build_params['cause'] = cause
+            url = self.get_build_triggerurl()
 
-        if securitytoken:
-            params['token'] = securitytoken
+            if cause:
+                build_params['cause'] = cause
 
-        response = self.jenkins.requester.post_and_confirm_status(
-            url,
-            data={'json': self.mk_json_from_build_parameters(
-                build_params)},  # See above - build params have to be JSON encoded & posted.
-            params=params,
-            valid=[200, 201]
-        )
-        if invoke_pre_check_delay > 0:
-            log.info(
-                "Waiting for %is to allow Jenkins to catch up", invoke_pre_check_delay)
-            sleep(invoke_pre_check_delay)
-        if block:
-            total_wait = 0
+            if securitytoken:
+                params['token'] = securitytoken
 
-            while self.is_queued():
+            response = self.jenkins.requester.post_and_confirm_status(
+                url,
+                data={'json': self.mk_json_from_build_parameters(
+                    build_params)},  # See above - build params have to be JSON encoded & posted.
+                params=params,
+                valid=[200, 201]
+            )
+            if invoke_pre_check_delay > 0:
                 log.info(
-                    "Waited %is for %s to begin...", total_wait, self.name)
-                sleep(invoke_block_delay)
-                total_wait += invoke_block_delay
-            if self.is_running():
-                running_build = self.get_last_build()
-                running_build.block_until_complete(
-                    delay=invoke_pre_check_delay)
+                    "Waiting for %is to allow Jenkins to catch up", invoke_pre_check_delay)
+                sleep(invoke_pre_check_delay)
+            if block:
+                total_wait = 0
+
+                while self.is_queued():
+                    log.info(
+                        "Waited %is for %s to begin...", total_wait, self.name)
+                    sleep(invoke_block_delay)
+                    total_wait += invoke_block_delay
+                if self.is_running():
+                    running_build = self.get_last_build()
+                    running_build.block_until_complete(
+                        delay=invoke_pre_check_delay)
+        return invocation
 
     def _buildid_for_type(self, buildtype):
         self.poll()
index 4e936e6..3c32e77 100644 (file)
@@ -2,6 +2,7 @@
 System tests for `jenkinsapi.jenkins` module.
 '''
 import unittest
+from jenkinsapi.invocation import Invocation
 from jenkinsapi_tests.test_utils.random_strings import random_string
 from jenkinsapi_tests.systests.base import BaseSystemTest, EMPTY_JOB_CONFIG
 
@@ -39,6 +40,12 @@ class JobTests(BaseSystemTest):
         job = self.jenkins.create_job(job_name, EMPTY_JOB_CONFIG)
         job.invoke()
 
+    def test_invocation_object(self):
+        job_name = 'create_%s' % random_string()
+        job = self.jenkins.create_job(job_name, EMPTY_JOB_CONFIG)
+        ii = job.invoke()
+        self.assertIsInstance(ii, Invocation)
+
     def test_get_jobs_list(self):
         job1_name = 'first_%s' % random_string()
         job2_name = 'second_%s' % random_string()
@@ -75,4 +82,4 @@ class JobTests(BaseSystemTest):
         self.assertJobIsPresent(copied_job_name)
 
 if __name__ == '__main__':
-    unittest.main()
\ No newline at end of file
+    unittest.main()
index e485e9f..26e5edb 100644 (file)
@@ -3,7 +3,6 @@ System tests for `jenkinsapi.jenkins` module.
 '''
 import logging
 import unittest
-from jenkinsapi.jenkins import Jenkins
 from jenkinsapi_tests.systests.base import BaseSystemTest
 from jenkinsapi_tests.test_utils.random_strings import random_string
 
index 0974bf8..7eead9d 100644 (file)
@@ -3,8 +3,9 @@ System tests for `jenkinsapi.jenkins` module.
 '''
 import time
 import unittest
-from jenkinsapi_tests.test_utils.random_strings import random_string
 from jenkinsapi_tests.systests.base import BaseSystemTest
+from jenkinsapi_tests.test_utils.random_strings import random_string
+
 
 JOB_CONFIG = """
 <?xml version='1.0' encoding='UTF-8'?>