some refactoring of builds
authorsalimfadhley <sal@stodge.org>
Tue, 11 Jun 2013 23:22:09 +0000 (00:22 +0100)
committersalimfadhley <sal@stodge.org>
Tue, 11 Jun 2013 23:22:09 +0000 (00:22 +0100)
jenkinsapi/artifact.py
jenkinsapi/build.py
jenkinsapi_tests/systests/test_jenkins_artifacts.py

index 7edcd2b..d5e90b0 100644 (file)
@@ -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.
index e676cac..f9caca4 100644 (file)
@@ -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):
         """
index 282c175..e1ea886 100644 (file)
@@ -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 = """
       <artifacts>*.txt</artifacts>
       <latestOnly>false</latestOnly>
     </hudson.tasks.ArtifactArchiver>
+    <hudson.tasks.Fingerprinter>
+      <targets></targets>
+      <recordBuildArtifacts>true</recordBuildArtifacts>
+    </hudson.tasks.Fingerprinter>
   </publishers>
   <buildWrappers/>
 </project>""".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