cpython: 73aa4c9305b3 (original) (raw)
--- a/Lib/distutils/filelist.py +++ b/Lib/distutils/filelist.py @@ -201,6 +201,7 @@ class FileList: Return True if files are found, False otherwise. """
# XXX docstring lying about what the special chars are?[](#l1.7) files_found = False[](#l1.8) pattern_re = translate_pattern(pattern, anchor, prefix, is_regex)[](#l1.9) self.debug_print("include_pattern: applying regex r'%s'" %[](#l1.10)
@@ -284,11 +285,14 @@ def glob_to_re(pattern): # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix, # and by extension they shouldn't match such "special characters" under # any OS. So change all non-escaped dots in the RE to match any
character except the special characters.
XXX currently the "special characters" are just slash -- i.e. this is
Unix-only.
- pattern_re = re.sub(r'((?<!\)(\\)*).', r'\1[^/]', pattern_re)
character except the special characters (currently: just os.sep).
- sep = os.sep
- if os.sep == '\':
# we're using a regex to manipulate a regex, so we need[](#l1.23)
# to escape the backslash twice[](#l1.24)
sep = r'\\\\'[](#l1.25)
- escaped = r'\1[^%s]' % sep
- pattern_re = re.sub(r'((?<!\)(\\)*).', escaped, pattern_re) return pattern_re @@ -312,9 +316,11 @@ def translate_pattern(pattern, anchor=1, if prefix is not None: # ditch end of pattern character empty_pattern = glob_to_re('')
prefix_re = (glob_to_re(prefix))[:-len(empty_pattern)][](#l1.35)
# paths should always use / in manifest templates[](#l1.36)
pattern_re = "^%s/.*%s" % (prefix_re, pattern_re)[](#l1.37)
prefix_re = glob_to_re(prefix)[:-len(empty_pattern)][](#l1.38)
sep = os.sep[](#l1.39)
if os.sep == '\\':[](#l1.40)
sep = r'\\'[](#l1.41)
else: # no prefix -- respect anchor flag if anchor: pattern_re = "^" + pattern_repattern_re = "^" + sep.join((prefix_re, ".*" + pattern_re))[](#l1.42)
--- a/Lib/distutils/tests/test_filelist.py +++ b/Lib/distutils/tests/test_filelist.py @@ -1,4 +1,5 @@ """Tests for distutils.filelist.""" +import os import re import unittest from distutils import debug @@ -9,6 +10,26 @@ from distutils.filelist import glob_to_r from test.support import captured_stdout, run_unittest from distutils.tests import support +MANIFEST_IN = """[](#l2.13) +include ok +include xo +exclude xo +include foo.tmp +include buildout.cfg +global-include *.x +global-include *.txt +global-exclude *.tmp +recursive-include f *.oo +recursive-exclude global *.x +graft dir +prune dir3 +""" + + +def make_local_path(s):
+ class FileListTestCase(support.LoggingSilencer, unittest.TestCase): @@ -22,16 +43,62 @@ class FileListTestCase(support.LoggingSi self.clear_logs() def test_glob_to_re(self):
# simple cases[](#l2.40)
self.assertEqual(glob_to_re('foo*'), 'foo[^/]*\\Z(?ms)')[](#l2.41)
self.assertEqual(glob_to_re('foo?'), 'foo[^/]\\Z(?ms)')[](#l2.42)
self.assertEqual(glob_to_re('foo??'), 'foo[^/][^/]\\Z(?ms)')[](#l2.43)
sep = os.sep[](#l2.44)
if os.sep == '\\':[](#l2.45)
sep = re.escape(os.sep)[](#l2.46)
for glob, regex in ([](#l2.48)
# simple cases[](#l2.49)
('foo*', r'foo[^%(sep)s]*\Z(?ms)'),[](#l2.50)
('foo?', r'foo[^%(sep)s]\Z(?ms)'),[](#l2.51)
('foo??', r'foo[^%(sep)s][^%(sep)s]\Z(?ms)'),[](#l2.52)
# special cases[](#l2.53)
(r'foo\\*', r'foo\\\\[^%(sep)s]*\Z(?ms)'),[](#l2.54)
(r'foo\\\*', r'foo\\\\\\[^%(sep)s]*\Z(?ms)'),[](#l2.55)
('foo????', r'foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s]\Z(?ms)'),[](#l2.56)
(r'foo\\??', r'foo\\\\[^%(sep)s][^%(sep)s]\Z(?ms)')):[](#l2.57)
regex = regex % {'sep': sep}[](#l2.58)
self.assertEqual(glob_to_re(glob), regex)[](#l2.59)
- def test_process_template_line(self):
# testing all MANIFEST.in template patterns[](#l2.62)
file_list = FileList()[](#l2.63)
l = make_local_path[](#l2.64)
# special cases[](#l2.66)
self.assertEqual(glob_to_re(r'foo\\*'), r'foo\\\\[^/]*\Z(?ms)')[](#l2.67)
self.assertEqual(glob_to_re(r'foo\\\*'), r'foo\\\\\\[^/]*\Z(?ms)')[](#l2.68)
self.assertEqual(glob_to_re('foo????'), r'foo[^/][^/][^/][^/]\Z(?ms)')[](#l2.69)
self.assertEqual(glob_to_re(r'foo\\??'), r'foo\\\\[^/][^/]\Z(?ms)')[](#l2.70)
# simulated file list[](#l2.71)
file_list.allfiles = ['foo.tmp', 'ok', 'xo', 'four.txt',[](#l2.72)
'buildout.cfg',[](#l2.73)
# filelist does not filter out VCS directories,[](#l2.74)
# it's sdist that does[](#l2.75)
l('.hg/last-message.txt'),[](#l2.76)
l('global/one.txt'),[](#l2.77)
l('global/two.txt'),[](#l2.78)
l('global/files.x'),[](#l2.79)
l('global/here.tmp'),[](#l2.80)
l('f/o/f.oo'),[](#l2.81)
l('dir/graft-one'),[](#l2.82)
l('dir/dir2/graft2'),[](#l2.83)
l('dir3/ok'),[](#l2.84)
l('dir3/sub/ok.txt'),[](#l2.85)
][](#l2.86)
for line in MANIFEST_IN.split('\n'):[](#l2.88)
if line.strip() == '':[](#l2.89)
continue[](#l2.90)
file_list.process_template_line(line)[](#l2.91)
wanted = ['ok',[](#l2.93)
'buildout.cfg',[](#l2.94)
'four.txt',[](#l2.95)
l('.hg/last-message.txt'),[](#l2.96)
l('global/one.txt'),[](#l2.97)
l('global/two.txt'),[](#l2.98)
l('f/o/f.oo'),[](#l2.99)
l('dir/graft-one'),[](#l2.100)
l('dir/dir2/graft2'),[](#l2.101)
][](#l2.102)
self.assertEqual(file_list.files, wanted)[](#l2.104)
def test_debug_print(self): file_list = FileList() @@ -117,6 +184,7 @@ class FileListTestCase(support.LoggingSi self.assertEqual(file_list.allfiles, ['a.py', 'b.txt']) def test_process_template(self):
l = make_local_path[](#l2.112) # invalid lines[](#l2.113) file_list = FileList()[](#l2.114) for action in ('include', 'exclude', 'global-include',[](#l2.115)
@@ -127,7 +195,7 @@ class FileListTestCase(support.LoggingSi # include file_list = FileList()
file_list.set_allfiles(['a.py', 'b.txt', 'd/c.py'])[](#l2.120)
file_list.set_allfiles(['a.py', 'b.txt', l('d/c.py')])[](#l2.121)
file_list.process_template_line('include *.py') self.assertEqual(file_list.files, ['a.py']) @@ -139,31 +207,31 @@ class FileListTestCase(support.LoggingSi # exclude file_list = FileList()
file_list.files = ['a.py', 'b.txt', 'd/c.py'][](#l2.129)
file_list.files = ['a.py', 'b.txt', l('d/c.py')][](#l2.130)
file_list.process_template_line('exclude *.py')
self.assertEqual(file_list.files, ['b.txt', 'd/c.py'])[](#l2.133)
self.assertEqual(file_list.files, ['b.txt', l('d/c.py')])[](#l2.134) self.assertNoWarnings()[](#l2.135)
file_list.process_template_line('exclude *.rb')
self.assertEqual(file_list.files, ['b.txt', 'd/c.py'])[](#l2.138)
self.assertEqual(file_list.files, ['b.txt', l('d/c.py')])[](#l2.139) self.assertWarnings()[](#l2.140)
# global-include file_list = FileList()
file_list.set_allfiles(['a.py', 'b.txt', 'd/c.py'])[](#l2.144)
file_list.set_allfiles(['a.py', 'b.txt', l('d/c.py')])[](#l2.145)
file_list.process_template_line('global-include *.py')
self.assertEqual(file_list.files, ['a.py', 'd/c.py'])[](#l2.148)
self.assertEqual(file_list.files, ['a.py', l('d/c.py')])[](#l2.149) self.assertNoWarnings()[](#l2.150)
file_list.process_template_line('global-include *.rb')
self.assertEqual(file_list.files, ['a.py', 'd/c.py'])[](#l2.153)
self.assertEqual(file_list.files, ['a.py', l('d/c.py')])[](#l2.154) self.assertWarnings()[](#l2.155)
# global-exclude file_list = FileList()
file_list.files = ['a.py', 'b.txt', 'd/c.py'][](#l2.159)
file_list.files = ['a.py', 'b.txt', l('d/c.py')][](#l2.160)
file_list.process_template_line('global-exclude *.py') self.assertEqual(file_list.files, ['b.txt']) @@ -175,50 +243,52 @@ class FileListTestCase(support.LoggingSi # recursive-include file_list = FileList()
file_list.set_allfiles(['a.py', 'd/b.py', 'd/c.txt', 'd/d/e.py'])[](#l2.168)
file_list.set_allfiles(['a.py', l('d/b.py'), l('d/c.txt'),[](#l2.169)
l('d/d/e.py')])[](#l2.170)
file_list.process_template_line('recursive-include d *.py')
self.assertEqual(file_list.files, ['d/b.py', 'd/d/e.py'])[](#l2.173)
self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')])[](#l2.174) self.assertNoWarnings()[](#l2.175)
file_list.process_template_line('recursive-include e *.py')
self.assertEqual(file_list.files, ['d/b.py', 'd/d/e.py'])[](#l2.178)
self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')])[](#l2.179) self.assertWarnings()[](#l2.180)
# recursive-exclude file_list = FileList()
file_list.files = ['a.py', 'd/b.py', 'd/c.txt', 'd/d/e.py'][](#l2.184)
file_list.files = ['a.py', l('d/b.py'), l('d/c.txt'), l('d/d/e.py')][](#l2.185)
file_list.process_template_line('recursive-exclude d *.py')
self.assertEqual(file_list.files, ['a.py', 'd/c.txt'])[](#l2.188)
self.assertEqual(file_list.files, ['a.py', l('d/c.txt')])[](#l2.189) self.assertNoWarnings()[](#l2.190)
file_list.process_template_line('recursive-exclude e *.py')
self.assertEqual(file_list.files, ['a.py', 'd/c.txt'])[](#l2.193)
self.assertEqual(file_list.files, ['a.py', l('d/c.txt')])[](#l2.194) self.assertWarnings()[](#l2.195)
# graft file_list = FileList()
file_list.set_allfiles(['a.py', 'd/b.py', 'd/d/e.py', 'f/f.py'])[](#l2.199)
file_list.set_allfiles(['a.py', l('d/b.py'), l('d/d/e.py'),[](#l2.200)
l('f/f.py')])[](#l2.201)
file_list.process_template_line('graft d')
self.assertEqual(file_list.files, ['d/b.py', 'd/d/e.py'])[](#l2.204)
self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')])[](#l2.205) self.assertNoWarnings()[](#l2.206)
file_list.process_template_line('graft e')
self.assertEqual(file_list.files, ['d/b.py', 'd/d/e.py'])[](#l2.209)
self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')])[](#l2.210) self.assertWarnings()[](#l2.211)
# prune file_list = FileList()
file_list.files = ['a.py', 'd/b.py', 'd/d/e.py', 'f/f.py'][](#l2.215)
file_list.files = ['a.py', l('d/b.py'), l('d/d/e.py'), l('f/f.py')][](#l2.216)
file_list.process_template_line('prune d')
self.assertEqual(file_list.files, ['a.py', 'f/f.py'])[](#l2.219)
self.assertEqual(file_list.files, ['a.py', l('f/f.py')])[](#l2.220) self.assertNoWarnings()[](#l2.221)
file_list.process_template_line('prune e')
self.assertEqual(file_list.files, ['a.py', 'f/f.py'])[](#l2.224)
self.assertEqual(file_list.files, ['a.py', l('f/f.py')])[](#l2.225) self.assertWarnings()[](#l2.226)
--- a/Lib/distutils/tests/test_sdist.py +++ b/Lib/distutils/tests/test_sdist.py @@ -7,6 +7,12 @@ import zipfile from os.path import join from textwrap import dedent +try:
+ from test.support import captured_stdout, check_warnings, run_unittest from distutils.command.sdist import sdist, show_formats @@ -28,6 +34,7 @@ setup(name='fake') MANIFEST = """[](#l3.17)
file GENERATED by distutils, do NOT edit
README +buildout.cfg inroot.txt setup.py data%(sep)sdata.dt @@ -39,13 +46,6 @@ somecode%(sep)sdoc.dat somecode%(sep)sdoc.txt """ -try:
- - class SDistTestCase(PyPIRCCommandTestCase): def setUp(self): @@ -143,7 +143,7 @@ class SDistTestCase(PyPIRCCommandTestCas dist_folder = join(self.tmp_dir, 'dist') result = os.listdir(dist_folder) result.sort()
self.assertEqual(result, ['fake-1.0.tar', 'fake-1.0.tar.gz'] )[](#l3.42)
self.assertEqual(result, ['fake-1.0.tar', 'fake-1.0.tar.gz'])[](#l3.43)
os.remove(join(dist_folder, 'fake-1.0.tar')) os.remove(join(dist_folder, 'fake-1.0.tar.gz')) @@ -180,11 +180,18 @@ class SDistTestCase(PyPIRCCommandTestCas self.write_file((data_dir, 'data.dt'), '#') some_dir = join(self.tmp_dir, 'some') os.mkdir(some_dir)
# make sure VCS directories are pruned (#14004)[](#l3.51)
hg_dir = join(self.tmp_dir, '.hg')[](#l3.52)
os.mkdir(hg_dir)[](#l3.53)
self.write_file((hg_dir, 'last-message.txt'), '#')[](#l3.54)
# a buggy regex used to prevent this from working on windows (#6884)[](#l3.55)
self.write_file((self.tmp_dir, 'buildout.cfg'), '#')[](#l3.56) self.write_file((self.tmp_dir, 'inroot.txt'), '#')[](#l3.57) self.write_file((some_dir, 'file.txt'), '#')[](#l3.58) self.write_file((some_dir, 'other_file.txt'), '#')[](#l3.59)
dist.data_files = [('data', ['data/data.dt',
'buildout.cfg',[](#l3.62) 'inroot.txt',[](#l3.63) 'notexisting']),[](#l3.64) 'some/file.txt',[](#l3.65)
@@ -214,15 +221,15 @@ class SDistTestCase(PyPIRCCommandTestCas zip_file.close() # making sure everything was added
self.assertEqual(len(content), 11)[](#l3.70)
self.assertEqual(len(content), 12)[](#l3.71)
# checking the MANIFEST f = open(join(self.tmp_dir, 'MANIFEST')) try: manifest = f.read()
self.assertEqual(manifest, MANIFEST % {'sep': os.sep})[](#l3.77) finally:[](#l3.78) f.close()[](#l3.79)
self.assertEqual(manifest, MANIFEST % {'sep': os.sep})[](#l3.80)
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') def test_metadata_check_option(self):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -124,6 +124,9 @@ Core and Builtins Library ------- +- Issue #6884: Fix long-standing bugs with MANIFEST.in parsing in distutils