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

« back to all changes in this revision

Viewing changes to jenkinsapi/artifact.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:
7
7
This module provides a class called Artifact which allows you to download objects from the server
8
8
and also access them as a stream.
9
9
"""
10
 
from __future__ import with_statement
11
 
import urllib
12
10
import os
13
11
import logging
14
12
import hashlib
15
13
 
 
14
from jenkinsapi.fingerprint import Fingerprint
16
15
from jenkinsapi.exceptions import ArtifactBroken
17
 
from jenkinsapi.fingerprint import Fingerprint
18
16
 
19
17
log = logging.getLogger(__name__)
20
18
 
21
19
class Artifact(object):
22
20
    """
23
 
    Represents a single Jenkins artifact, usually some kind of file 
 
21
    Represents a single Jenkins artifact, usually some kind of file
24
22
    generated as a by-product of executing a Jenkins build.
25
23
    """
26
24
 
27
 
    def __init__(self, filename, url, build=None):
 
25
    def __init__(self, filename, url, build):
28
26
        self.filename = filename
29
27
        self.url = url
30
28
        self.build = build
52
50
                log.info("This file did not originate from Jenkins, so cannot check.")
53
51
        else:
54
52
            log.info("Local file is missing, downloading new.")
55
 
        filename = self._do_download(fspath)
 
53
        filepath = self._do_download(fspath)
56
54
        try:
57
 
            self._verify_download(filename)
 
55
            self._verify_download(filepath)
58
56
        except ArtifactBroken:
59
57
            log.warning("fingerprint of the downloaded artifact could not be verified")
60
 
        return filename
 
58
        return fspath
 
59
 
 
60
    def get_jenkins_obj(self):
 
61
        return self.build.get_jenkins_obj()
 
62
 
 
63
    def get_data(self):
 
64
        """
 
65
        Grab the text of the artifact
 
66
        """
 
67
        response = self.get_jenkins_obj().requester.get_and_confirm_status(self.url)
 
68
        return response.content
61
69
 
62
70
    def _do_download(self, fspath):
63
71
        """
64
72
        Download the the artifact to a path.
65
73
        """
66
 
        filename, _ = urllib.urlretrieve(self.url, filename=fspath)
67
 
        return filename
 
74
        with open(fspath, "wb") as out:
 
75
            out.write(self.get_data())
 
76
        return fspath
68
77
 
69
78
    def _verify_download(self, fspath):
70
79
        """
88
97
            raise
89
98
        return md5.hexdigest()
90
99
 
91
 
    def savetodir(self, dirpath):
 
100
    def save_to_dir(self, dirpath):
92
101
        """
93
102
        Save the artifact to a folder. The containing directory must be exist, but use the artifact's
94
103
        default filename.