From c95de2f89e799807627787a94b8d8c693d173d9d Mon Sep 17 00:00:00 2001 From: salimfadhley Date: Wed, 12 Jun 2013 00:22:09 +0100 Subject: [PATCH] some refactoring of builds --- jenkinsapi/artifact.py | 33 ++++++++++++---------- jenkinsapi/build.py | 7 +++-- .../systests/test_jenkins_artifacts.py | 21 +++++++++++++- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/jenkinsapi/artifact.py b/jenkinsapi/artifact.py index 7edcd2b..d5e90b0 100644 --- a/jenkinsapi/artifact.py +++ b/jenkinsapi/artifact.py @@ -24,7 +24,7 @@ class Artifact(object): 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 @@ -52,27 +52,30 @@ class Artifact(object): 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): """ @@ -96,7 +99,7 @@ class Artifact(object): 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. diff --git a/jenkinsapi/build.py b/jenkinsapi/build.py index e676cac..f9caca4 100644 --- a/jenkinsapi/build.py +++ b/jenkinsapi/build.py @@ -66,13 +66,14 @@ class Build(JenkinsBase): 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): """ diff --git a/jenkinsapi_tests/systests/test_jenkins_artifacts.py b/jenkinsapi_tests/systests/test_jenkins_artifacts.py index 282c175..e1ea886 100644 --- a/jenkinsapi_tests/systests/test_jenkins_artifacts.py +++ b/jenkinsapi_tests/systests/test_jenkins_artifacts.py @@ -1,7 +1,10 @@ ''' 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 @@ -30,6 +33,10 @@ PINGER_JOB_CONFIG = """ *.txt false + + + true + """.strip() @@ -49,7 +56,19 @@ class TestPingerJob(BaseSystemTest): 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 -- 2.7.4