From 0cde6b3864a32ee15ba30cebc0982ddf9c822150 Mon Sep 17 00:00:00 2001 From: salimfadhley Date: Wed, 12 Jun 2013 23:15:20 +0100 Subject: [PATCH] build timestamps are now timezone aware. Added the pytz class as a dependancy. --- jenkinsapi/build.py | 10 +++- jenkinsapi_tests/unittests/test_build.py | 83 +++++++++++++++++--------------- setup.py | 4 +- 3 files changed, 55 insertions(+), 42 deletions(-) diff --git a/jenkinsapi/build.py b/jenkinsapi/build.py index a787ba2..2122636 100644 --- a/jenkinsapi/build.py +++ b/jenkinsapi/build.py @@ -1,6 +1,7 @@ import time -import urlparse +import pytz import urllib2 +import urlparse import datetime from jenkinsapi.artifact import Artifact from jenkinsapi import config @@ -215,6 +216,10 @@ class Build(JenkinsBase): self.poll() return self._data["building"] + def block(self): + while self.is_running(): + time.sleep(1) + def is_good( self ): """ Return a bool, true if the build was good. @@ -272,7 +277,8 @@ class Build(JenkinsBase): Returns build timestamp in UTC ''' # Java timestamps are given in miliseconds since the epoch start! - return datetime.datetime(*time.gmtime(self._data['timestamp']/1000.0)[:6]) + naive_timestamp = datetime.datetime(*time.gmtime(self._data['timestamp']/1000.0)[:6]) + return pytz.utc.localize(naive_timestamp) def stop(self): """ diff --git a/jenkinsapi_tests/unittests/test_build.py b/jenkinsapi_tests/unittests/test_build.py index f219ead..4351173 100644 --- a/jenkinsapi_tests/unittests/test_build.py +++ b/jenkinsapi_tests/unittests/test_build.py @@ -1,44 +1,51 @@ +import pytz import mock import unittest import datetime from jenkinsapi.build import Build -class TestTimestamps(unittest.TestCase): - - DATA = { 'actions': [{'causes': [{'shortDescription': 'Started by user anonymous', - 'userId': None, - 'userName': 'anonymous'}]}], - 'artifacts': [], - 'building': False, - 'builtOn': '', - 'changeSet': {'items': [], 'kind': None}, - 'culprits': [], - 'description': None, - 'duration': 106, - 'estimatedDuration': 106, - 'executor': None, - 'fullDisplayName': 'foo #1', - 'id': '2013-05-31_23-15-40', - 'keepLog': False, - 'number': 1, - 'result': 'SUCCESS', - 'timestamp': 1370042140000, - 'url': 'http://localhost:8080/job/foo/1/'} - - @mock.patch.object(Build, '_poll') - def setUp(self, _poll): - _poll.return_value = self.DATA - self.j = mock.MagicMock() # Job - self.j.name = 'FooJob' - - self.b = Build('http://', 97, self.j) - - def test_timestamp(self): - self.assertIsInstance(self.b.get_timestamp(), datetime.datetime) - self.assertEqual(self.b.get_timestamp(), datetime.datetime(2013, 5, 31, 23, 15, 40)) - - def testName(self): - with self.assertRaises(AttributeError): - _ = self.b.id() - self.assertEquals(self.b.name, 'foo #1') + +class test_build(unittest.TestCase): + + DATA = { + 'actions': [{'causes': [{'shortDescription': 'Started by user anonymous', + 'userId': None, + 'userName': 'anonymous'}]}], + 'artifacts': [], + 'building': False, + 'builtOn': '', + 'changeSet': {'items': [], 'kind': None}, + 'culprits': [], + 'description': None, + 'duration': 106, + 'estimatedDuration': 106, + 'executor': None, + 'fullDisplayName': 'foo #1', + 'id': '2013-05-31_23-15-40', + 'keepLog': False, + 'number': 1, + 'result': 'SUCCESS', + 'timestamp': 1370042140000, + 'url': 'http://localhost:8080/job/foo/1/'} + + @mock.patch.object(Build, '_poll') + def setUp(self, _poll): + _poll.return_value = self.DATA + self.j = mock.MagicMock() # Job + self.j.name = 'FooJob' + + self.b = Build('http://', 97, self.j) + + def test_timestamp(self): + self.assertIsInstance(self.b.get_timestamp(), datetime.datetime) + + expected = pytz.utc.localize( + datetime.datetime(2013, 5, 31, 23, 15, 40)) + + self.assertEqual(self.b.get_timestamp(), expected) + + def testName(self): + with self.assertRaises(AttributeError): + _ = self.b.id() + self.assertEquals(self.b.name, 'foo #1') diff --git a/setup.py b/setup.py index e173e8b..83eda98 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ try: DESCRIPTION = open(os.path.join(PROJECT_ROOT, "README.rst")).read() except IOError, _: DESCRIPTION = SHORT_DESCRIPTION - + GLOBAL_ENTRY_POINTS = { "console_scripts": ["jenkins_invoke=jenkinsapi.command_line.jenkins_invoke:main"] } @@ -25,7 +25,7 @@ setup(name=PROJECT_NAME.lower(), packages=['jenkinsapi', 'jenkinsapi.utils', 'jenkinsapi.command_line', 'jenkinsapi_tests'], zip_safe=True, include_package_data=False, - install_requires=['requests==1.2.3'], + install_requires=['requests==1.2.3', 'pytz'], test_suite='jenkinsapi_tests', tests_require=['mock', 'nose'], extras_require={ -- 2.7.4