From 065e8eb834d65b4e0572611c16f7f2950db4512b Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 11 May 2013 10:59:30 +0100 Subject: Verified that not isinstance(IOError(), os.error), so there's no value in this statement being in the try block. --HG-- branch : distribute extra : rebase_source : 79dc86f2251503336feaa3d3f99c744d0ea45887 --- pkg_resources.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'pkg_resources.py') diff --git a/pkg_resources.py b/pkg_resources.py index 69601480..9d406e38 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1409,10 +1409,10 @@ class ZipProvider(EggProvider): ) timestamp = time.mktime(date_time) + if not WRITE_SUPPORT: + raise IOError('"os.rename" and "os.unlink" are not supported ' + 'on this platform') try: - if not WRITE_SUPPORT: - raise IOError('"os.rename" and "os.unlink" are not supported ' - 'on this platform') real_path = manager.get_cache_path( self.egg_name, self._parts(zip_path) -- cgit v1.2.3 From 9027ce5419a346e068927df72dbfcd650cdee33f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 11 May 2013 12:43:23 +0100 Subject: Extract static method for calculating size and timestamp on a zip resource --HG-- branch : distribute extra : rebase_source : 3acd099bb90d9abf4d3b265946e0c59b8edcae6e --- pkg_resources.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'pkg_resources.py') diff --git a/pkg_resources.py b/pkg_resources.py index 9d406e38..579e3b46 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1392,6 +1392,16 @@ class ZipProvider(EggProvider): self._extract_resource(manager, self._eager_to_zip(name)) return self._extract_resource(manager, zip_path) + @staticmethod + def _get_date_and_size(zip_stat): + t,d,size = zip_stat[5], zip_stat[6], zip_stat[3] + date_time = ( + (d>>9)+1980, (d>>5)&0xF, d&0x1F, # ymd + (t&0xFFFF)>>11, (t>>5)&0x3F, (t&0x1F) * 2, 0, 0, -1 # hms, etc. + ) + timestamp = time.mktime(date_time) + return timestamp, size + def _extract_resource(self, manager, zip_path): if zip_path in self._index(): @@ -1401,13 +1411,7 @@ class ZipProvider(EggProvider): ) return os.path.dirname(last) # return the extracted directory name - zip_stat = self.zipinfo[zip_path] - t,d,size = zip_stat[5], zip_stat[6], zip_stat[3] - date_time = ( - (d>>9)+1980, (d>>5)&0xF, d&0x1F, # ymd - (t&0xFFFF)>>11, (t>>5)&0x3F, (t&0x1F) * 2, 0, 0, -1 # hms, etc. - ) - timestamp = time.mktime(date_time) + timestamp, size = self._get_date_and_size(self.zipinfo[zip_path]) if not WRITE_SUPPORT: raise IOError('"os.rename" and "os.unlink" are not supported ' -- cgit v1.2.3 From 7adfcead12a21b2ed6b13f5a75477914b524cb12 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 11 May 2013 12:54:26 +0100 Subject: Extract method to determine if a temporary file is current. --HG-- branch : distribute extra : rebase_source : ec2c1860f0ce9abbc3ea999aa54304ea9cc6ecd7 --- pkg_resources.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'pkg_resources.py') diff --git a/pkg_resources.py b/pkg_resources.py index 579e3b46..b59f1703 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1422,11 +1422,8 @@ class ZipProvider(EggProvider): self.egg_name, self._parts(zip_path) ) - if os.path.isfile(real_path): - stat = os.stat(real_path) - if stat.st_size==size and stat.st_mtime==timestamp: - # size and stamp match, don't bother extracting - return real_path + if self.is_current(real_path, zip_path): + return real_path outf, tmpnam = _mkstemp(".$extract", dir=os.path.dirname(real_path)) os.write(outf, self.loader.get_data(zip_path)) @@ -1439,11 +1436,9 @@ class ZipProvider(EggProvider): except os.error: if os.path.isfile(real_path): - stat = os.stat(real_path) - - if stat.st_size==size and stat.st_mtime==timestamp: - # size and stamp match, somebody did it just ahead of - # us, so we're done + if self._is_current(real_path, zip_path): + # the file became current since it was checked above, + # so proceed. return real_path elif os.name=='nt': # Windows, del old file and retry unlink(real_path) @@ -1456,6 +1451,16 @@ class ZipProvider(EggProvider): return real_path + def _is_current(self, file_path, zip_path): + """ + Return True if the file_path is current for this zip_path + """ + timestamp, size = self._get_date_and_size(self.zipinfo[zip_path]) + if not os.path.isfile(file_path): + return False + stat = os.stat(file_path) + return stat.st_size==size and stat.st_mtime==timestamp + def _get_eager_resources(self): if self.eagers is None: eagers = [] -- cgit v1.2.3 From f0a3905357e596c9a2a85358fda0f41384ff3d39 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 11 May 2013 13:05:32 +0100 Subject: Fix for yet unpublished issue to ensure that get_resource_filename always re-extracts the content of a temporary filename if it does not match that of the source content. --HG-- branch : distribute extra : rebase_source : 5605ee258010cde1237db058b770c62264c215e2 --- pkg_resources.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'pkg_resources.py') diff --git a/pkg_resources.py b/pkg_resources.py index b59f1703..2ec645d3 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1459,7 +1459,14 @@ class ZipProvider(EggProvider): if not os.path.isfile(file_path): return False stat = os.stat(file_path) - return stat.st_size==size and stat.st_mtime==timestamp + if stat.st_size!=size or stat.st_mtime!=timestamp: + return False + # check that the contents match + zip_contents = self.loader.get_data(zip_path) + f = open(file_path, 'rb') + file_contents = f.read() + f.close() + return zip_contents == file_contents def _get_eager_resources(self): if self.eagers is None: -- cgit v1.2.3 From b9661afd506c7c5f43d01c092ce313f33e730892 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 12 May 2013 13:34:15 -0400 Subject: Fix AttributeError on underscore-prefixed method name. --HG-- branch : distribute extra : rebase_source : 8ceb2c2f067a60225bb83817a8584d93f489a3d0 --- pkg_resources.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pkg_resources.py') diff --git a/pkg_resources.py b/pkg_resources.py index 2ec645d3..f8de449e 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1422,7 +1422,7 @@ class ZipProvider(EggProvider): self.egg_name, self._parts(zip_path) ) - if self.is_current(real_path, zip_path): + if self._is_current(real_path, zip_path): return real_path outf, tmpnam = _mkstemp(".$extract", dir=os.path.dirname(real_path)) -- cgit v1.2.3