diff options
Diffstat (limited to 'distutils2/database.py')
| -rw-r--r-- | distutils2/database.py | 38 |
1 files changed, 10 insertions, 28 deletions
diff --git a/distutils2/database.py b/distutils2/database.py index 55fe71f..63918b5 100644 --- a/distutils2/database.py +++ b/distutils2/database.py @@ -5,11 +5,8 @@ import re import csv import sys import zipimport -from StringIO import StringIO -try: - from hashlib import md5 -except ImportError: - from distutils2._backport.hashlib import md5 +from io import StringIO +from hashlib import md5 from distutils2 import logger from distutils2.errors import PackagingError @@ -122,7 +119,7 @@ def _generate_cache(use_egg_info, paths): _cache_generated_egg = True -class Distribution(object): +class Distribution: """Created with the *path* of the ``.dist-info`` directory provided to the constructor. It reads the metadata contained in ``METADATA`` when it is instantiated.""" @@ -162,8 +159,7 @@ class Distribution(object): def _get_records(self, local=False): results = [] - record = self.get_distinfo_file('RECORD') - try: + with self.get_distinfo_file('RECORD') as record: record_reader = csv.reader(record, delimiter=',', lineterminator='\n') for row in record_reader: @@ -173,20 +169,15 @@ class Distribution(object): path = path.replace('/', os.sep) path = os.path.join(sys.prefix, path) results.append((path, checksum, size)) - finally: - record.close() return results def get_resource_path(self, relative_path): - resources_file = self.get_distinfo_file('RESOURCES') - try: + with self.get_distinfo_file('RESOURCES') as resources_file: resources_reader = csv.reader(resources_file, delimiter=',', lineterminator='\n') for relative, destination in resources_reader: if relative == relative_path: return destination - finally: - resources_file.close() raise KeyError( 'no resource file with relative path %r is installed' % relative_path) @@ -284,7 +275,7 @@ class Distribution(object): __hash__ = object.__hash__ -class EggInfoDistribution(object): +class EggInfoDistribution: """Created with the *path* of the ``.egg-info`` directory or file provided to the constructor. It reads the metadata contained in the file itself, or if the given path happens to be a directory, the metadata is read from the @@ -318,7 +309,7 @@ class EggInfoDistribution(object): def yield_lines(strs): """Yield non-empty/non-comment lines of a ``basestring`` or sequence""" - if isinstance(strs, basestring): + if isinstance(strs, str): for s in strs.splitlines(): s = s.strip() # skip blank lines/comments @@ -337,11 +328,8 @@ class EggInfoDistribution(object): self.metadata = Metadata(path=meta_path) try: req_path = os.path.join(path, 'EGG-INFO', 'requires.txt') - fp = open(req_path, 'r') - try: + with open(req_path, 'r') as fp: requires = fp.read() - finally: - fp.close() except IOError: requires = None else: @@ -361,11 +349,8 @@ class EggInfoDistribution(object): if os.path.isdir(path): path = os.path.join(path, 'PKG-INFO') try: - fp = open(os.path.join(path, 'requires.txt'), 'r') - try: + with open(os.path.join(path, 'requires.txt'), 'r') as fp: requires = fp.read() - finally: - fp.close() except IOError: requires = None self.metadata = Metadata(path=path) @@ -427,11 +412,8 @@ class EggInfoDistribution(object): def list_installed_files(self, local=False): def _md5(path): - f = open(path, 'rb') - try: + with open(path, 'rb') as f: content = f.read() - finally: - f.close() return md5(content).hexdigest() def _size(path): |
