New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add build system support for un-ignore rules #12965
Changes from 4 commits
6cf5fc6
ca33fc4
c3838d2
b3c4151
d3a8f4a
a393e1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,11 +31,46 @@ class MbedIgnoreSet(object): | |
|
||
def __init__(self): | ||
self._ignore_patterns = [] | ||
self._ignore_regex = re.compile("$^") | ||
self._ignore_regexes = [] | ||
self._unignore_patterns = [] | ||
self._unignore_regexes = [] | ||
|
||
def is_ignored(self, file_path): | ||
"""Check if file path is ignored by any .mbedignore thus far""" | ||
return self._ignore_regex.match(normcase(file_path)) | ||
|
||
# find longest ignore and unignore pattern that matches the path | ||
ignore_match_pattern = None | ||
unignore_match_pattern = None | ||
|
||
filepath_normcase = normcase(file_path) | ||
|
||
for regex_index in range(0, len(self._ignore_regexes)): | ||
this_regex_match = self._ignore_regexes[regex_index].match(filepath_normcase) | ||
if this_regex_match: | ||
if ignore_match_pattern is None: | ||
# no previous match | ||
ignore_match_pattern = self._ignore_patterns[regex_index] | ||
elif len(self._ignore_patterns[regex_index]) > len(ignore_match_pattern): | ||
# found a longer match | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to see a comment explaining why a longer match is important, rather than just repeating what the code says. |
||
ignore_match_pattern = self._ignore_patterns[regex_index] | ||
|
||
for regex_index in range(0, len(self._unignore_regexes)): | ||
this_regex_match = self._unignore_regexes[regex_index].match(filepath_normcase) | ||
if this_regex_match: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to factor out this duplicate logic into a helper function? |
||
if unignore_match_pattern is None: | ||
# no previous match | ||
unignore_match_pattern = self._unignore_patterns[regex_index] | ||
elif len(self._unignore_patterns[regex_index]) > len(unignore_match_pattern): | ||
# found a longer match | ||
unignore_match_pattern = self._unignore_patterns[regex_index] | ||
|
||
if ignore_match_pattern is None: | ||
return False | ||
|
||
if unignore_match_pattern is not None and len(unignore_match_pattern) >= len(ignore_match_pattern): | ||
return False | ||
|
||
return True | ||
|
||
def add_ignore_patterns(self, in_name, patterns): | ||
"""Ignore all files and directories matching the paterns in | ||
|
@@ -45,14 +80,42 @@ def add_ignore_patterns(self, in_name, patterns): | |
in_name - the filename prefix that this ignore will apply to | ||
patterns - the list of patterns we will ignore in the future | ||
""" | ||
|
||
if len(patterns) == 0: | ||
return | ||
|
||
if in_name == ".": | ||
self._ignore_patterns.extend(normcase(p) for p in patterns) | ||
patterns_normpath = list(normcase(p) for p in patterns) | ||
else: | ||
self._ignore_patterns.extend( | ||
normcase(join(in_name, pat)) for pat in patterns) | ||
if self._ignore_patterns: | ||
self._ignore_regex = re.compile("|".join( | ||
fnmatch.translate(p) for p in self._ignore_patterns)) | ||
patterns_normpath = list(normcase(join(in_name, pat)) for pat in patterns) | ||
|
||
self._ignore_patterns.extend(patterns_normpath) | ||
self._ignore_regexes.extend(re.compile(fnmatch.translate(p)) for p in patterns_normpath) | ||
|
||
|
||
def add_unignore_patterns(self, in_name, patterns): | ||
"""Un-ignore all files and directories matching the patterns in | ||
directories named by in_name. | ||
|
||
Un-ignore rules take precedence based on depth. So ignoring | ||
a/b/* then unignoring a/b/c.cpp would allow c.cpp to build. | ||
But unignoring a/* then ignoring a/b/* would prevent c.cpp from building. | ||
|
||
Positional arguments: | ||
in_name - the filename prefix that this unignore will apply to | ||
patterns - the list of patterns we will unignore in the future | ||
""" | ||
|
||
if len(patterns) == 0: | ||
return | ||
|
||
if in_name == ".": | ||
patterns_normpath = list(normcase(p) for p in patterns) | ||
else: | ||
patterns_normpath = list(normcase(join(in_name, pat)) for pat in patterns) | ||
|
||
self._unignore_patterns.extend(patterns_normpath) | ||
self._unignore_regexes.extend(re.compile(fnmatch.translate(p)) for p in patterns_normpath) | ||
|
||
def add_mbedignore(self, in_name, filepath): | ||
"""Add a series of patterns to the ignored paths | ||
|
@@ -62,8 +125,25 @@ def add_mbedignore(self, in_name, filepath): | |
patterns - the list of patterns we will ignore in the future | ||
""" | ||
with open (filepath) as f: | ||
patterns = [l.strip() for l in f | ||
if l.strip() != "" and not l.startswith("#")] | ||
self.add_ignore_patterns(in_name, patterns) | ||
|
||
ignore_patterns = [] | ||
unignore_patterns = [] | ||
|
||
for line in f: | ||
line_text = line.strip() | ||
|
||
if line_text == "" or line_text.startswith("#"): | ||
# no content on this line | ||
continue | ||
|
||
if line_text.startswith("!"): | ||
# unignore rule | ||
unignore_patterns.append(line_text[1:]) | ||
else: | ||
# ignore rule | ||
ignore_patterns.append(line_text) | ||
|
||
self.add_ignore_patterns(in_name, ignore_patterns) | ||
self.add_unignore_patterns(in_name, unignore_patterns) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use
zip
here to tidy this loop up? Then we can say something like:This way we have both of the elements there without having to introduce the extra
regex_index
variable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heh you can tell I'm mostly a C++ programmer... I had no idea this existed