Convert module to work with python 2.4 and fix a bug (#2251)

- Avoiding the use of 'with ... as ...' and 'except ... as ...' constructs.

- Make use of posixpath.join() rather than concatenating slashes ourselves
  (To avoid having consecutive slashes which broke something on our side)
This commit is contained in:
Dag Wieers 2016-05-23 17:18:05 +02:00 committed by Matt Clay
parent 275210b06e
commit 0e3713971a

View file

@ -25,6 +25,7 @@ from lxml import etree
import os import os
import hashlib import hashlib
import sys import sys
import posixpath
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
@ -133,9 +134,9 @@ class Artifact(object):
return self.version and self.version.endswith("SNAPSHOT") return self.version and self.version.endswith("SNAPSHOT")
def path(self, with_version=True): def path(self, with_version=True):
base = self.group_id.replace(".", "/") + "/" + self.artifact_id base = posixpath.join(self.group_id.replace(".", "/"), self.artifact_id)
if with_version and self.version: if with_version and self.version:
return base + "/" + self.version return posixpath.join(base, self.version)
else: else:
return base return base
@ -182,8 +183,7 @@ class Artifact(object):
class MavenDownloader: class MavenDownloader:
def __init__(self, module, base="http://repo1.maven.org/maven2"): def __init__(self, module, base="http://repo1.maven.org/maven2"):
self.module = module self.module = module
if base.endswith("/"): base = base.rstrip("/")
base = base.rstrip("/")
self.base = base self.base = base
self.user_agent = "Maven Artifact Downloader/1.0" self.user_agent = "Maven Artifact Downloader/1.0"
@ -213,9 +213,9 @@ class MavenDownloader:
elif not artifact.is_snapshot(): elif not artifact.is_snapshot():
version = artifact.version version = artifact.version
if artifact.classifier: if artifact.classifier:
return self.base + "/" + artifact.path() + "/" + artifact.artifact_id + "-" + version + "-" + artifact.classifier + "." + artifact.extension return posixpath.join(self.base, artifact.path(), artifact.artifact_id + "-" + version + "-" + artifact.classifier + "." + artifact.extension)
return self.base + "/" + artifact.path() + "/" + artifact.artifact_id + "-" + version + "." + artifact.extension return posixpath.join(self.base, artifact.path(), artifact.artifact_id + "-" + version + "." + artifact.extension)
def _request(self, url, failmsg, f): def _request(self, url, failmsg, f):
# Hack to add parameters in the way that fetch_url expects # Hack to add parameters in the way that fetch_url expects
@ -240,9 +240,10 @@ class MavenDownloader:
if not self.verify_md5(filename, url + ".md5"): if not self.verify_md5(filename, url + ".md5"):
response = self._request(url, "Failed to download artifact " + str(artifact), lambda r: r) response = self._request(url, "Failed to download artifact " + str(artifact), lambda r: r)
if response: if response:
with open(filename, 'w') as f: f = open(filename, 'w')
# f.write(response.read()) # f.write(response.read())
self._write_chunks(response, f, report_hook=self.chunk_report) self._write_chunks(response, f, report_hook=self.chunk_report)
f.close()
return True return True
else: else:
return False return False
@ -286,9 +287,10 @@ class MavenDownloader:
def _local_md5(self, file): def _local_md5(self, file):
md5 = hashlib.md5() md5 = hashlib.md5()
with open(file, 'rb') as f: f = open(file, 'rb')
for chunk in iter(lambda: f.read(8192), ''): for chunk in iter(lambda: f.read(8192), ''):
md5.update(chunk) md5.update(chunk)
f.close()
return md5.hexdigest() return md5.hexdigest()
@ -328,12 +330,12 @@ def main():
try: try:
artifact = Artifact(group_id, artifact_id, version, classifier, extension) artifact = Artifact(group_id, artifact_id, version, classifier, extension)
except ValueError as e: except ValueError, e:
module.fail_json(msg=e.args[0]) module.fail_json(msg=e.args[0])
prev_state = "absent" prev_state = "absent"
if os.path.isdir(dest): if os.path.isdir(dest):
dest = dest + "/" + artifact_id + "-" + version + "." + extension dest = posixpath.join(dest, artifact_id + "-" + version + "." + extension)
if os.path.lexists(dest) and downloader.verify_md5(dest, downloader.find_uri_for_artifact(artifact) + '.md5'): if os.path.lexists(dest) and downloader.verify_md5(dest, downloader.find_uri_for_artifact(artifact) + '.md5'):
prev_state = "present" prev_state = "present"
else: else:
@ -349,7 +351,7 @@ def main():
module.exit_json(state=state, dest=dest, group_id=group_id, artifact_id=artifact_id, version=version, classifier=classifier, extension=extension, repository_url=repository_url, changed=True) module.exit_json(state=state, dest=dest, group_id=group_id, artifact_id=artifact_id, version=version, classifier=classifier, extension=extension, repository_url=repository_url, changed=True)
else: else:
module.fail_json(msg="Unable to download the artifact") module.fail_json(msg="Unable to download the artifact")
except ValueError as e: except ValueError, e:
module.fail_json(msg=e.args[0]) module.fail_json(msg=e.args[0])