skeleton class of the Invocation object
authorsalimfadhley <sal@stodge.org>
Sun, 23 Jun 2013 07:49:35 +0000 (08:49 +0100)
committersalimfadhley <sal@stodge.org>
Sun, 23 Jun 2013 07:49:35 +0000 (08:49 +0100)
jenkinsapi/invocation.py [new file with mode: 0644]
jenkinsapi/invokation.py [deleted file]
jenkinsapi/job.py
jenkinsapi_tests/systests/test_jenkins.py
jenkinsapi_tests/systests/test_nodes.py
jenkinsapi_tests/systests/test_parameterized_builds.py

diff --git a/jenkinsapi/invocation.py b/jenkinsapi/invocation.py
new file mode 100644 (file)
index 0000000..6be65c7
--- /dev/null
@@ -0,0 +1,60 @@
+class Invocation(object):
+    """
+    Represents the state and consequences of a single attempt to start a job.
+    This class provides a context manager which is intended to watch the state of the job
+    before and after the invoke. It will detect whether a process got queued, launched
+    or whether nothing at all happened.
+
+    An instance of this object will be returned by job.invoke()
+    """
+
+    def __init__(self, job):
+        self.job = job
+
+
+    def __enter__(self):
+        """
+        Start watching the job
+        """
+
+    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.
+        """
+
+    def block(self, until='completed'):
+        """
+        Block this item until a condition is met.
+        Setting until to 'running' blocks the item until it is running (i.e. it's no longer queued)
+        """
+
+    def stop(self):
+        """
+        Stop this item, whether it is on the queue or blocked.
+        """
+
+    def is_queued(self):
+        """
+        Returns True if this item is on the queue
+        """
+
+    def is_running(self):
+        """
+        Returns True if this item is executing now
+        """
+
+    def is_queued_or_running(self):
+        return self.is_queued() or self.is_running()
+
+    def get_queue_item(self):
+        """
+        If the item is queued it will return that QueueItem, otherwise it will
+        raise an exception.
+        """
+
+    def get_build(self):
+        """
+        If the item is building it will return a Build object, otherwise it will
+        raise an exception.
+        """
diff --git a/jenkinsapi/invokation.py b/jenkinsapi/invokation.py
deleted file mode 100644 (file)
index de95fef..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-class Invocation(object):
-    """
-    Represents the state and consequences of a single attempt to start a job.
-    This class provides a context manager which is intended to watch the state of the job
-    before and after the invoke. It will detect whether a process got queued, launched
-    or whether nothing at all happened.
-
-    An instance of this object will be returned by job.invoke()
-    """
-
-    def __init__(self, job):
-        self.job = job
-
-
-    def __enter__(self):
-        """
-        Start watching the job
-        """
-
-    def __exit__(self):
-        """
-        Finish watching the job - it will track which new queue items or builds have
-        been created as a consequence of invoking the job.
-        """
-
-    def block(self, until='completed'):
-        """
-        Block this item until a condition is met.
-        Setting until to 'running' blocks the item until it is running (i.e. it's no longer queued)
-        """
-
-    def stop(self):
-        """
-        Stop this item, whether it is on the queue or blocked.
-        """
-
-    def is_queued(self):
-        """
-        Returns True if this item is on the queue
-        """
-
-    def is_running(self):
-        """
-        Returns True if this item is executing now
-        """
-
-    def is_queued_or_running(self):
-        return self.is_queued() or self.is_running()
-
-    def get_queue_item(self):
-        """
-        If the item is queued it will return that QueueItem, otherwise it will
-        raise an exception.
-        """
-
-    def get_build(self):
-        """
-        If the item is building it will return a Build object, otherwise it will
-        raise an exception.
-        """
index bf636ce3bcbbfc11d3d03d9e54180fbdc0229446..68af88036f58b817f783ac954ea239767ae58459 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 4e936e6534eeca26eb3096bc00991ae754fd0655..3c32e77b76e0eded375b2a046f8515aaf37fa9cb 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 e485e9f6e10ab1307b1aa5a4417f0a5b6312f526..26e5edb5f869aafc840644e5ca59387ad0afbf62 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 0974bf8df159d6ae5b35fac462d02f211ee16fe5..7eead9d0f03a36f39c4bbc798fb39682b4a3bfe3 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'?>