Commit 37ccd6f7 authored by Éric Araujo's avatar Éric Araujo

Fix packaging.database.Distribution.list_distinfo_files (#12785).

This method was supposed to return only the file under the dist-info
directory, but it actually returned all installed files.

The tests didn’t catch this because they were flawed; I updated them.
Thanks to Nadeem Vawda and Jeremy Kloth for testing.

As a bonus, the removal of os.path.relpath use should also fix the
Windows buildbots.
parent 86ca04cc
...@@ -263,6 +263,8 @@ class Distribution: ...@@ -263,6 +263,8 @@ class Distribution:
:returns: iterator of paths :returns: iterator of paths
""" """
for path, checksum, size in self._get_records(local): for path, checksum, size in self._get_records(local):
# XXX add separator or use real relpath algo
if path.startswith(self.path):
yield path yield path
def __eq__(self, other): def __eq__(self, other):
......
...@@ -4,7 +4,6 @@ import csv ...@@ -4,7 +4,6 @@ import csv
import sys import sys
import shutil import shutil
import tempfile import tempfile
from os.path import relpath # separate import for backport concerns
from hashlib import md5 from hashlib import md5
from textwrap import dedent from textwrap import dedent
...@@ -32,11 +31,11 @@ def get_hexdigest(filename): ...@@ -32,11 +31,11 @@ def get_hexdigest(filename):
return checksum.hexdigest() return checksum.hexdigest()
def record_pieces(file): def record_pieces(path):
path = relpath(file, sys.prefix) path = os.path.join(*path)
digest = get_hexdigest(file) digest = get_hexdigest(path)
size = os.path.getsize(file) size = os.path.getsize(path)
return [path, digest, size] return path, digest, size
class FakeDistsMixin: class FakeDistsMixin:
...@@ -141,12 +140,10 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase): ...@@ -141,12 +140,10 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase):
for path, dirs, files in os.walk(dist_location): for path, dirs, files in os.walk(dist_location):
for f in files: for f in files:
record_writer.writerow(record_pieces( record_writer.writerow(record_pieces((path, f)))
os.path.join(path, f)))
for file in ('INSTALLER', 'METADATA', 'REQUESTED'): for file in ('INSTALLER', 'METADATA', 'REQUESTED'):
record_writer.writerow(record_pieces( record_writer.writerow(record_pieces((distinfo_dir, file)))
os.path.join(distinfo_dir, file))) record_writer.writerow([record_file])
record_writer.writerow([relpath(record_file, sys.prefix)])
with open(record_file) as file: with open(record_file) as file:
record_reader = csv.reader(file, lineterminator='\n') record_reader = csv.reader(file, lineterminator='\n')
...@@ -171,15 +168,17 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase): ...@@ -171,15 +168,17 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase):
distinfo_name + '.dist-info') distinfo_name + '.dist-info')
true_path = [self.fake_dists_path, distinfo_name, true_path = [self.fake_dists_path, distinfo_name,
'grammar', 'utils.py'] 'grammar', 'utils.py']
true_path = relpath(os.path.join(*true_path), sys.prefix) true_path = os.path.join(*true_path)
false_path = [self.fake_dists_path, 'towel_stuff-0.1', 'towel_stuff', false_path = [self.fake_dists_path, 'towel_stuff-0.1', 'towel_stuff',
'__init__.py'] '__init__.py']
false_path = relpath(os.path.join(*false_path), sys.prefix) false_path = os.path.join(*false_path)
# Test if the distribution uses the file in question # Test if the distribution uses the file in question
dist = Distribution(distinfo_dir) dist = Distribution(distinfo_dir)
self.assertTrue(dist.uses(true_path)) self.assertTrue(dist.uses(true_path), 'dist %r is supposed to use %r' %
self.assertFalse(dist.uses(false_path)) (dist, true_path))
self.assertFalse(dist.uses(false_path), 'dist %r is not supposed to '
'use %r' % (dist, true_path))
def test_get_distinfo_file(self): def test_get_distinfo_file(self):
# Test the retrieval of dist-info file objects. # Test the retrieval of dist-info file objects.
...@@ -215,20 +214,23 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase): ...@@ -215,20 +214,23 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase):
'MAGICFILE') 'MAGICFILE')
def test_list_distinfo_files(self): def test_list_distinfo_files(self):
# Test for the iteration of RECORD path entries.
distinfo_name = 'towel_stuff-0.1' distinfo_name = 'towel_stuff-0.1'
distinfo_dir = os.path.join(self.fake_dists_path, distinfo_dir = os.path.join(self.fake_dists_path,
distinfo_name + '.dist-info') distinfo_name + '.dist-info')
dist = Distribution(distinfo_dir) dist = Distribution(distinfo_dir)
# Test for the iteration of the raw path # Test for the iteration of the raw path
distinfo_record_paths = self.records[distinfo_dir].keys() distinfo_files = [os.path.join(distinfo_dir, filename) for filename in
os.listdir(distinfo_dir)]
found = dist.list_distinfo_files() found = dist.list_distinfo_files()
self.assertEqual(sorted(found), sorted(distinfo_record_paths)) self.assertEqual(sorted(found), sorted(distinfo_files))
# Test for the iteration of local absolute paths # Test for the iteration of local absolute paths
distinfo_record_paths = [os.path.join(sys.prefix, path) distinfo_files = [os.path.join(sys.prefix, distinfo_dir, path) for
for path in self.records[distinfo_dir]] path in distinfo_files]
found = dist.list_distinfo_files(local=True) found = sorted(dist.list_distinfo_files(local=True))
self.assertEqual(sorted(found), sorted(distinfo_record_paths)) if os.sep != '/':
self.assertNotIn('/', found[0])
self.assertIn(os.sep, found[0])
self.assertEqual(found, sorted(distinfo_files))
def test_get_resources_path(self): def test_get_resources_path(self):
distinfo_name = 'babar-0.1' distinfo_name = 'babar-0.1'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment