generated as a by-product of executing a Jenkins build.
"""
- def __init__(self, filename, url, build=None):
+ def __init__(self, filename, url, build):
self.filename = filename
self.url = url
self.build = build
log.info("This file did not originate from Jenkins, so cannot check.")
else:
log.info("Local file is missing, downloading new.")
- filename = self._do_download(fspath)
+ filepath = self._do_download(fspath)
try:
- self._verify_download(filename)
+ self._verify_download(filepath)
except ArtifactBroken:
log.warning("fingerprint of the downloaded artifact could not be verified")
- return filename
+ return fspath
+
+ def get_jenkins_obj(self):
+ return self.build.get_jenkins_obj()
+
+ def getData(self):
+ """
+ Grab the text of the artifact
+ """
+ response = self.get_jenkins_obj().requester.get_and_confirm_status(self.url)
+ return response.text
def _do_download(self, fspath):
"""
Download the the artifact to a path.
"""
- if self.build:
- opener = self.build.get_jenkins_obj().get_opener()
- f = opener(self.url)
- with open(fspath, "wb") as out:
- out.write(f.read())
-
- return fspath
- else:
- filename, _ = urllib.urlretrieve(self.url, filename=fspath)
- return filename
+ with open(fspath, "wb") as out:
+ out.write(self.getData())
+ return fspath
def _verify_download(self, fspath):
"""
raise
return md5.hexdigest()
- def savetodir(self, dirpath):
+ def save_to_dir(self, dirpath):
"""
Save the artifact to a folder. The containing directory must be exist, but use the artifact's
default filename.
def get_artifacts( self ):
for afinfo in self._data["artifacts"]:
- url = "%sartifact/%s" % ( self.baseurl, afinfo["relativePath"] )
+ url = "%s/artifact/%s" % ( self.baseurl, afinfo["relativePath"] )
af = Artifact( afinfo["fileName"], url, self )
yield af
- del af, url
def get_artifact_dict(self):
- return dict( (a.url[len(a.build.baseurl + "artifact/"):], a) for a in self.get_artifacts() )
+ return dict(
+ (af.filename, af) for af in self.get_artifacts()
+ )
def get_upstream_job_name(self):
"""
'''
System tests for `jenkinsapi.jenkins` module.
'''
+import os
import time
+import shutil
+import tempfile
import unittest
from jenkinsapi_tests.test_utils.random_strings import random_string
from jenkinsapi_tests.systests.base import BaseSystemTest
<artifacts>*.txt</artifacts>
<latestOnly>false</latestOnly>
</hudson.tasks.ArtifactArchiver>
+ <hudson.tasks.Fingerprinter>
+ <targets></targets>
+ <recordBuildArtifacts>true</recordBuildArtifacts>
+ </hudson.tasks.Fingerprinter>
</publishers>
<buildWrappers/>
</project>""".strip()
artifacts = b.get_artifact_dict()
self.assertIsInstance(artifacts, dict)
- outfile = artifacts['out.txt']
+ artifact = artifacts['out.txt']
+
+ tempDir = tempfile.mkdtemp()
+
+ try:
+ tf = tempfile.NamedTemporaryFile(mode='wb')
+ artifact.save_to_dir(tempDir)
+ readBackText = open(os.path.join(tempDir, artifact.filename), 'rb').read().strip()
+ self.assertTrue(readBackText.startswith('PING localhost'))
+ self.assertTrue(readBackText.endswith('ms'))
+ finally:
+ shutil.rmtree(tempDir)
+
# TODO: Actually verify the download