From 19ee776a996479088fd519c9e9909777cec6b510 Mon Sep 17 00:00:00 2001 From: Seblu <sebastien.luttringer@smartjog.com> Date: Wed, 8 Jun 2011 12:24:34 +0200 Subject: [PATCH] Donwloading image from repository verify md5 --- installsystems/repository.py | 5 ++++- installsystems/tools.py | 31 ++++++++++--------------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/installsystems/repository.py b/installsystems/repository.py index 6113d2b..95f1940 100644 --- a/installsystems/repository.py +++ b/installsystems/repository.py @@ -148,7 +148,10 @@ class Repository(object): def get(self, name, version): '''return a package from a name and version of pakage''' desc = self.db.get(name, version) - return PackageImage(os.path.join(self.config.path, desc["md5"]), verbose=self.verbose) + p = PackageImage(os.path.join(self.config.path, desc["md5"]), verbose=self.verbose) + if p.md5 != desc["md5"]: + raise Exception("Invalid package MD5") + return p class RepositoryConfig(object): '''Repository configuration container''' diff --git a/installsystems/tools.py b/installsystems/tools.py index 504f048..efe4b45 100644 --- a/installsystems/tools.py +++ b/installsystems/tools.py @@ -12,10 +12,18 @@ import shutil import urllib2 from installsystems.tarball import Tarball -def md5sum(path): +def md5sum(path=None, fileobj=None): '''Compute md5 of a file''' + if path is None and fileobj is None: + raise ValueError("No path or fileobj specified") + if fileobj is None: + fileobj = uopen(path) m = hashlib.md5() - m.update(open(path, "r").read()) + while True: + buf = fileobj.read(1024 * m.block_size) + if len(buf) == 0: + break + m.update(buf) return m.hexdigest() def copy(source, destination, uid=None, gid=None, mode=None, timeout=None): @@ -92,22 +100,3 @@ def uopen(path): return urllib2.urlopen(path) else: raise NotImplementedError - -def extractdata(image, name, target, filelist=None): - '''Extract a databall name into target - This will be done accross a forking to allow higher performance and - on the fly checksumming - ''' - filename = "%s-%s%s" % (image.id, name, image.extension_data) - if filename not in image.datas.keys(): - raise Exception("No such data tarball in %s" % image.name) - datainfo = image.datas[filename] - fileobject = ropen(filename) - tarball = Tarball.open(fileobj=fileobject, mode="r|gz") - if filelist is None: - tarball.extractall(target) - else: - for f in filelist: - tarball.extract(f, target) - tarball.close() - fileobject.close() -- GitLab