Skip to content

Commit

Permalink
Accept a list of bad idea file patterns to ignore
Browse files Browse the repository at this point in the history
- new command line option --ignore-bad-ideas wich takes a list of
  patterns like --ignore.
- new config section 'ignore-bad-ideas' similar to 'ignore'

Closes mgedmin#67
  • Loading branch information
brechtm committed Aug 11, 2016
1 parent c0f7fa7 commit eb1504d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
38 changes: 28 additions & 10 deletions check_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ def add_directories(names):
'.#*',
]

IGNORE_BAD_IDEAS = []

_sep = r'\\' if os.path.sep == '\\' else os.path.sep

SUGGESTIONS = [(re.compile(pattern.replace('/', _sep)), suggestion) for pattern, suggestion in [
Expand All @@ -533,21 +535,29 @@ def add_directories(names):
r'recursive-include \1 *.\2'),
]]

CFG_SECTION_CHECK_MANIFEST = 'check-manifest'
CFG_IGNORE_DEFAULT_RULES = (CFG_SECTION_CHECK_MANIFEST, 'ignore-default-rules')
CFG_IGNORE = (CFG_SECTION_CHECK_MANIFEST, 'ignore')
CFG_IGNORE_BAD_IDEAS = (CFG_SECTION_CHECK_MANIFEST, 'ignore-bad-ideas')


def read_config():
"""Read configuration from setup.cfg."""
# XXX modifies global state, which is kind of evil
ignore_bad_ideas = []
config = ConfigParser.ConfigParser()
config.read(['setup.cfg'])
if not config.has_section('check-manifest'):
if not config.has_section(CFG_SECTION_CHECK_MANIFEST):
return
if (config.has_option('check-manifest', 'ignore-default-rules')
and config.getboolean('check-manifest', 'ignore-default-rules')):
if (config.has_option(*CFG_IGNORE_DEFAULT_RULES)
and config.getboolean(*CFG_IGNORE_DEFAULT_RULES)):
del IGNORE[:]
if config.has_option('check-manifest', 'ignore'):
patterns = [p.strip() for p in config.get('check-manifest',
'ignore').splitlines()]
if config.has_option(*CFG_IGNORE):
patterns = [p.strip() for p in config.get(*CFG_IGNORE).splitlines()]
IGNORE.extend(p for p in patterns if p)
if config.has_option(*CFG_IGNORE_BAD_IDEAS):
lines = config.get(*CFG_IGNORE_BAD_IDEAS).splitlines()
IGNORE_BAD_IDEAS.extend(line.strip() for line in lines if line)


def read_manifest():
Expand Down Expand Up @@ -830,13 +840,15 @@ def check_manifest(source_tree='.', create=False, update=False,
" matching any of the files, sorry!")
all_ok = False
bad_ideas = find_bad_ideas(all_source_files)
if bad_ideas:
filtered_bad_ideas = [bad_idea for bad_idea in bad_ideas
if not file_matches(bad_idea, IGNORE_BAD_IDEAS)]
if filtered_bad_ideas:
warning("you have %s in source control!\nthat's a bad idea:"
" auto-generated files should not be versioned"
% bad_ideas[0])
if len(bad_ideas) > 1:
% filtered_bad_ideas[0])
if len(filtered_bad_ideas) > 1:
warning("this also applies to the following:\n%s"
% format_list(bad_ideas[1:]))
% format_list(filtered_bad_ideas[1:]))
all_ok = False
return all_ok

Expand Down Expand Up @@ -864,11 +876,17 @@ def main():
parser.add_argument('--ignore', metavar='patterns', default=None,
help='ignore files/directories matching these'
' comma-separated patterns')
parser.add_argument('--ignore-bad-ideas', metavar='patterns',
default=[], help='ignore bad idea files/directories '
'matching these comma-separated patterns')
args = parser.parse_args()

if args.ignore:
IGNORE.extend(args.ignore.split(','))

if args.ignore_bad_ideas:
IGNORE_BAD_IDEAS.extend(args.ignore_bad_ideas.split(','))

if args.verbose:
global VERBOSE
VERBOSE = True
Expand Down
39 changes: 39 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,16 @@ def test_read_config_override_ignores(self):
self.assertEqual(check_manifest.IGNORE,
['foo', 'bar'])

def test_read_config_ignore_bad_ideas(self):
import check_manifest
with open('setup.cfg', 'w') as f:
f.write('[check-manifest]\n'
'ignore-bad-ideas = \n'
' foo\n'
' bar\n')
check_manifest.read_config()
self.assertEqual(check_manifest.IGNORE_BAD_IDEAS, ['foo', 'bar'])

def test_read_manifest_no_manifest(self):
import check_manifest
check_manifest.read_manifest()
Expand Down Expand Up @@ -619,13 +629,16 @@ def setUp(self):
sys.argv = ['check-manifest']
self.OLD_IGNORE = check_manifest.IGNORE
self.OLD_IGNORE_REGEXPS = check_manifest.IGNORE_REGEXPS
self.OLD_IGNORE_BAD_IDEAS = check_manifest.IGNORE_BAD_IDEAS
check_manifest.IGNORE = ['default-ignore-rules']
check_manifest.IGNORE_REGEXPS = ['default-ignore-regexps']
check_manifest.IGNORE_BAD_IDEAS = []

def tearDown(self):
import check_manifest
check_manifest.IGNORE = self.OLD_IGNORE
check_manifest.IGNORE_REGEXPS = self.OLD_IGNORE_REGEXPS
check_manifest.IGNORE_BAD_IDEAS = self.OLD_IGNORE_BAD_IDEAS
sys.argv = self._orig_sys_argv
self._se_patcher.stop()
self._cm_patcher.stop()
Expand Down Expand Up @@ -656,6 +669,13 @@ def test_extra_ignore_args(self):
self.assertEqual(check_manifest.IGNORE,
['default-ignore-rules', 'x', 'y', 'z'])

def test_ignore_bad_ideas_args(self):
import check_manifest
sys.argv.append('--ignore-bad-ideas=x,y,z')
check_manifest.main()
self.assertEqual(check_manifest.IGNORE_BAD_IDEAS,
['x', 'y', 'z'])


class TestZestIntegration(unittest.TestCase):

Expand Down Expand Up @@ -1365,6 +1385,25 @@ def test_bad_ideas(self):
self.assertIn("this also applies to the following:\n moo.mo",
sys.stderr.getvalue())

def test_ignore_bad_ideas(self):
from check_manifest import check_manifest
with open('setup.cfg', 'w') as f:
f.write('[check-manifest]\n'
'ignore =\n'
' subdir/bar.egg-info\n'
'ignore-bad-ideas =\n'
' *.mo\n'
' subdir/bar.egg-info\n')
self._create_repo_with_code()
self._add_to_vcs('foo.egg-info')
self._add_to_vcs('moo.mo')
self._add_to_vcs('subdir/bar.egg-info')
self.assertFalse(check_manifest())
self.assertIn("you have foo.egg-info in source control!",
sys.stderr.getvalue())
self.assertNotIn("moo.mo", sys.stderr.getvalue())
self.assertNotIn("bar.egg-info", sys.stderr.getvalue())

def test_missing_source_files(self):
# https://github.com/mgedmin/check-manifest/issues/32
from check_manifest import check_manifest
Expand Down

0 comments on commit eb1504d

Please sign in to comment.