From e7b02b3be142c07ccb054692b636f0fd0b93b8ff Mon Sep 17 00:00:00 2001 From: Aleksey Maksimov Date: Wed, 23 Apr 2014 21:07:07 +0800 Subject: [PATCH] Fixed md5 calculation --- jenkinsapi/api.py | 4 +-- jenkinsapi/artifact.py | 5 +++- jenkinsapi/build.py | 2 +- jenkinsapi_tests/systests/job_configs.py | 4 +-- .../systests/test_downstream_upstream.py | 2 +- .../systests/test_jenkins_artifacts.py | 13 +++++++--- .../systests/test_parameterized_builds.py | 29 ++++++++++++---------- 7 files changed, 35 insertions(+), 24 deletions(-) diff --git a/jenkinsapi/api.py b/jenkinsapi/api.py index 283f7ad..c191907 100644 --- a/jenkinsapi/api.py +++ b/jenkinsapi/api.py @@ -132,7 +132,7 @@ def block_until_complete(jenkinsurl, jobs, maxwait=12000, interval=30, obj_jenkins = Jenkins(jenkinsurl, username=username, password=password) obj_jobs = [obj_jenkins[jid] for jid in jobs] - for time_left in xrange(maxwait, 0, -interval): + for time_left in range(maxwait, 0, -interval): still_running = [j for j in obj_jobs if j.is_queued_or_running()] if not still_running: return @@ -191,7 +191,7 @@ def install_artifacts(artifacts, dirstruct, installdir, basestaticurl): # It's probably a static file, # we can get it from the static collection staticurl = urlparse.urljoin(basestaticurl, artifactname) - theartifact = Artifact(artifactname, staticurl) + theartifact = Artifact(artifactname, staticurl, None) theartifact.save(destpath) installed.append(destpath) return installed diff --git a/jenkinsapi/artifact.py b/jenkinsapi/artifact.py index b84dac9..0490bf1 100644 --- a/jenkinsapi/artifact.py +++ b/jenkinsapi/artifact.py @@ -93,7 +93,10 @@ class Artifact(object): try: with open(fspath, 'rb') as f: for chunk in iter(lambda: f.read(chunksize), ''): - md5.update(chunk) + if chunk: + md5.update(chunk) + else: + break except: raise return md5.hexdigest() diff --git a/jenkinsapi/build.py b/jenkinsapi/build.py index d150cc5..a235531 100644 --- a/jenkinsapi/build.py +++ b/jenkinsapi/build.py @@ -364,7 +364,7 @@ class Build(JenkinsBase): Return the current state of the text console. """ url = "%s/consoleText" % self.baseurl - return self.job.jenkins.requester.get_url(url).content + return self.job.jenkins.requester.get_url(url).content.decode('utf-8') def stop(self): """ diff --git a/jenkinsapi_tests/systests/job_configs.py b/jenkinsapi_tests/systests/job_configs.py index 15448e7..1cf58a6 100644 --- a/jenkinsapi_tests/systests/job_configs.py +++ b/jenkinsapi_tests/systests/job_configs.py @@ -38,7 +38,7 @@ LONG_RUNNING_JOB = """ false - ping -c 200 localhost + ping -c 50 localhost @@ -140,7 +140,7 @@ JOB_WITH_ARTIFACTS = """ false - ping -c 5 localhost | tee out.txt + ping -c 10 localhost | tee out.txt gzip < out.txt > out.gz diff --git a/jenkinsapi_tests/systests/test_downstream_upstream.py b/jenkinsapi_tests/systests/test_downstream_upstream.py index df9c97f..1d25205 100644 --- a/jenkinsapi_tests/systests/test_downstream_upstream.py +++ b/jenkinsapi_tests/systests/test_downstream_upstream.py @@ -101,7 +101,7 @@ class TestDownstreamUpstream(BaseSystemTest): self.jenkins['A'].invoke() - for _ in xrange(10): + for _ in range(10): try: self.jenkins['C'].get_last_completed_buildnumber() > 0 except NoBuildData: diff --git a/jenkinsapi_tests/systests/test_jenkins_artifacts.py b/jenkinsapi_tests/systests/test_jenkins_artifacts.py index 7775545..4de95a6 100644 --- a/jenkinsapi_tests/systests/test_jenkins_artifacts.py +++ b/jenkinsapi_tests/systests/test_jenkins_artifacts.py @@ -20,7 +20,7 @@ from jenkinsapi_tests.test_utils.random_strings import random_string class TestPingerJob(BaseSystemTest): - def test_invoke_job(self): + def test_artefacts(self): job_name = 'create_%s' % random_string() job = self.jenkins.create_job(job_name, JOB_WITH_ARTIFACTS) job.invoke(block=True) @@ -41,14 +41,19 @@ class TestPingerJob(BaseSystemTest): try: # Verify that we can handle text artifacts text_artifact.save_to_dir(tempDir) - readBackText = open(os.path.join( - tempDir, text_artifact.filename), 'rb').read().strip() + readBackText = open(os.path.join(tempDir, + text_artifact.filename), + 'rb').read().strip() + readBackText = readBackText.decode('ascii') self.assertTrue(re.match(r'^PING \S+ \(127.0.0.1\)', readBackText)) self.assertTrue(readBackText.endswith('ms')) # Verify that we can hande binary artifacts binary_artifact.save_to_dir(tempDir) - readBackText = gzip.open(os.path.join(tempDir, binary_artifact.filename,), 'rb').read().strip() + readBackText = gzip.open(os.path.join(tempDir, + binary_artifact.filename,), + 'rb').read().strip() + readBackText = readBackText.decode('ascii') self.assertTrue(re.match(r'^PING \S+ \(127.0.0.1\)', readBackText)) self.assertTrue(readBackText.endswith('ms')) finally: diff --git a/jenkinsapi_tests/systests/test_parameterized_builds.py b/jenkinsapi_tests/systests/test_parameterized_builds.py index d9bcc5d..2c69cfd 100644 --- a/jenkinsapi_tests/systests/test_parameterized_builds.py +++ b/jenkinsapi_tests/systests/test_parameterized_builds.py @@ -7,7 +7,10 @@ try: import unittest2 as unittest except ImportError: import unittest -from StringIO import StringIO +try: + from StringIO import StringIO +except ImportError: + from io import StringIO from jenkinsapi_tests.systests.base import BaseSystemTest from jenkinsapi_tests.test_utils.random_strings import random_string from jenkinsapi_tests.systests.job_configs import JOB_WITH_FILE @@ -25,11 +28,11 @@ class TestParameterizedBuilds(BaseSystemTest): job = self.jenkins.create_job(job_name, JOB_WITH_FILE) job.invoke(block=True, files={'file.txt': param_file}) - b = job.get_last_build() - while b.is_running(): + build = job.get_last_build() + while build.is_running(): time.sleep(0.25) - artifacts = b.get_artifact_dict() + artifacts = build.get_artifact_dict() self.assertIsInstance(artifacts, dict) art_file = artifacts['file.txt'] self.assertTrue(art_file.get_data().strip(), file_data) @@ -41,16 +44,16 @@ class TestParameterizedBuilds(BaseSystemTest): job = self.jenkins.create_job(job_name, JOB_WITH_PARAMETERS) job.invoke(block=True, build_params={'B': param_B}) - b = job.get_last_build() - while b.is_running(): + build = job.get_last_build() + while build.is_running(): time.sleep(0.25) - artifacts = b.get_artifact_dict() + artifacts = build.get_artifact_dict() self.assertIsInstance(artifacts, dict) artB = artifacts['b.txt'] self.assertTrue(artB.get_data().strip(), param_B) - self.assertIn(param_B, b.get_console()) + self.assertIn(param_B, build.get_console()) def test_parameterized_job_build_queuing(self): """Accept multiple builds of parameterized jobs with unique @@ -66,19 +69,19 @@ class TestParameterizedBuilds(BaseSystemTest): self.assertTrue(job.has_queued_build(params)) - while(job.has_queued_build(params)): + while job.has_queued_build(params): time.sleep(0.25) - b = job.get_last_build() - while b.is_running(): + build = job.get_last_build() + while build.is_running(): time.sleep(0.25) - artifacts = b.get_artifact_dict() + artifacts = build.get_artifact_dict() self.assertIsInstance(artifacts, dict) artB = artifacts['b.txt'] self.assertTrue(artB.get_data().strip(), param_B) - self.assertIn(param_B, b.get_console()) + self.assertIn(param_B, build.get_console()) def test_parameterized_job_build_rejection(self): """Reject build of paramterized job when existing build with same -- 2.7.4