~ubuntu-branches/ubuntu/trusty/python-jenkinsapi/trusty-proposed

« back to all changes in this revision

Viewing changes to jenkinsapi/build.py

  • Committer: Package Import Robot
  • Author(s): Al Stone
  • Date: 2013-07-22 20:55:13 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130722205513-z1obexz5cajsft30
Tags: 0.2.11-1
* Update to latest upstream. 
* Update watch file
* Closes: #712130 -- updated to newer upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import time
 
2
import pytz
 
3
import urllib2
 
4
import urlparse
 
5
import datetime
1
6
from jenkinsapi.artifact import Artifact
2
7
from jenkinsapi import config
3
8
from jenkinsapi.jenkinsbase import JenkinsBase
4
 
from jenkinsapi.exceptions import NoResults, FailedNoResults
5
 
from jenkinsapi.constants import STATUS_FAIL, STATUS_ABORTED, RESULTSTATUS_FAILURE
 
9
from jenkinsapi.exceptions import NoResults
 
10
from jenkinsapi.constants import STATUS_SUCCESS
6
11
from jenkinsapi.result_set import ResultSet
7
12
 
8
13
from time import sleep
27
32
    def __str__(self):
28
33
        return self._data['fullDisplayName']
29
34
 
30
 
    def id(self):
 
35
    @property
 
36
    def name(self):
 
37
        return str(self)
 
38
 
 
39
    def get_number(self):
31
40
        return self._data["number"]
32
41
 
33
42
    def get_status(self):
44
53
        return maxRevision
45
54
 
46
55
    def _get_git_rev(self):
47
 
        for item in self._data['actions']:
48
 
            branch = item.get('buildsByBranchName')
49
 
            head = branch and branch.get('origin/HEAD')
50
 
            if head:
51
 
                return head['revision']['SHA1']
 
56
        # Sometimes we have None as part of actions. Filter those actions
 
57
        # which have lastBuiltRevision in them
 
58
        _actions = [x for x in self._data['actions'] if x \
 
59
                        and "lastBuiltRevision" in x]
 
60
        for item in _actions:
 
61
            revision = item["lastBuiltRevision"]["SHA1"]
 
62
            return revision
52
63
 
53
64
    def _get_hg_rev(self):
54
 
        revs = [(item['date'], item['node'])
55
 
                for item in self._data['changeSet']['items']]
56
 
        revs = sorted(revs, key=lambda tup: float(tup[0].split('-')[0]))
57
 
        return revs[-1][1] # get last commit revision
 
65
        return [x['mercurialNodeName'] for x in self._data['actions'] if 'mercurialNodeName' in x][0]
58
66
 
59
67
    def get_duration(self):
60
68
        return self._data["duration"]
61
69
 
62
70
    def get_artifacts( self ):
63
71
        for afinfo in self._data["artifacts"]:
64
 
            url = "%sartifact/%s" % ( self.baseurl, afinfo["relativePath"] )
 
72
            url = "%s/artifact/%s" % ( self.baseurl, afinfo["relativePath"] )
65
73
            af = Artifact( afinfo["fileName"], url, self )
66
74
            yield af
67
 
            del af, url
68
75
 
69
76
    def get_artifact_dict(self):
70
 
        return dict( (a.filename, a) for a in self.get_artifacts() )
 
77
        return dict(
 
78
            (af.filename, af) for af in self.get_artifacts()
 
79
        )
71
80
 
72
81
    def get_upstream_job_name(self):
73
82
        """
209
218
        self.poll()
210
219
        return self._data["building"]
211
220
 
 
221
    def block(self):
 
222
        while self.is_running():
 
223
            time.sleep(1)
 
224
 
212
225
    def is_good( self ):
213
226
        """
214
227
        Return a bool, true if the build was good.
215
228
        If the build is still running, return False.
216
229
        """
217
 
        return ( not self.is_running() ) and self._data["result"] == 'SUCCESS'
 
230
        return ( not self.is_running() ) and self._data["result"] == STATUS_SUCCESS
218
231
 
219
232
    def block_until_complete(self, delay=15):
220
233
        assert isinstance( delay, int )
221
234
        count = 0
222
235
        while self.is_running():
223
236
            total_wait = delay * count
224
 
            log.info("Waited %is for %s #%s to complete" % ( total_wait, self.job.id(), self.id() ) )
 
237
            log.info("Waited %is for %s #%s to complete" % ( total_wait, self.job.name, self.name ) )
225
238
            sleep( delay )
226
239
            count += 1
227
240
 
243
256
        if self.STR_TOTALCOUNT not in self.get_actions():
244
257
            raise NoResults( "%s does not have any published results" % str(self) )
245
258
        buildstatus = self.get_status()
246
 
        if buildstatus in [ STATUS_FAIL, RESULTSTATUS_FAILURE, STATUS_ABORTED ]:
247
 
            raise FailedNoResults( self.STR_TPL_NOTESTS_ERR % ( str(self), buildstatus ) )
248
259
        if not self.get_actions()[self.STR_TOTALCOUNT]:
249
260
            raise NoResults( self.STR_TPL_NOTESTS_ERR % ( str(self), buildstatus ) )
250
261
        obj_results = ResultSet( result_url, build=self )
259
270
    def get_actions(self):
260
271
        all_actions = {}
261
272
        for dct_action in self._data["actions"]:
 
273
            if dct_action is None: continue
262
274
            all_actions.update( dct_action )
263
275
        return all_actions
264
276
 
265
277
    def get_timestamp(self):
266
 
        return self._data['timestamp']
 
278
        '''
 
279
        Returns build timestamp in UTC
 
280
        '''
 
281
        # Java timestamps are given in miliseconds since the epoch start!
 
282
        naive_timestamp = datetime.datetime(*time.gmtime(self._data['timestamp']/1000.0)[:6])
 
283
        return pytz.utc.localize(naive_timestamp)
 
284
 
 
285
    def get_console(self):
 
286
        """
 
287
        Return the current state of the text console.
 
288
        """
 
289
        url = "%s/consoleText" % self.baseurl
 
290
        return self.job.jenkins.requester.get_url(url).content
 
291
 
 
292
    def stop(self):
 
293
        """
 
294
        Stops the build execution if it's running
 
295
        :return boolean True if succeded False otherwise or the build is not running
 
296
        """
 
297
        if self.is_running():
 
298
            url = "%s/stop" % self.baseurl
 
299
            self.job.jenkins.requester.post_and_confirm_status(url, data='')
 
300
            return True
 
301
        return False