Skip to content

Commit

Permalink
Simplify logic and compile regex in advance (oppia#7339)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuichenli authored and kevinlee12 committed Aug 9, 2019
1 parent 42c43e2 commit acb8f58
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions scripts/pre_commit_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@

MANDATORY_PATTERNS_REGEXP = [
{
'regexp': r'Copyright \d{4} The Oppia Authors\. All Rights Reserved\.',
'regexp': re.compile(
r'Copyright \d{4} The Oppia Authors\. All Rights Reserved\.'),
'message': 'Please ensure this file should contain a proper '
'copyright notice.',
'included_types': ('.py', '.js', '.sh', '.ts'),
Expand All @@ -246,7 +247,7 @@

MANDATORY_PATTERNS_JS_REGEXP = [
{
'regexp': r'^\s\*\s@fileoverview\s[a-zA-Z0-9_]+',
'regexp': re.compile(r'^\s\*\s@fileoverview\s[a-zA-Z0-9_]+'),
'message': 'Please ensure this file should contain a file '
'overview i.e. a short description of the file.',
'included_types': ('.js', '.ts'),
Expand Down Expand Up @@ -1544,19 +1545,18 @@ def _check_for_mandatory_pattern_in_file(
'excluded_files'] +
regexp_to_check[
'excluded_dirs'])]))):
pattern_found_list.append(False)
pattern_found_list.append(index)
for line in file_content:
if re.search(regexp_to_check['regexp'], line):
pattern_found_list[index] = True
if regexp_to_check['regexp'].search(line):
pattern_found_list.pop()
break
if not all(pattern_found_list):
if pattern_found_list:
failed = True
for index, pattern_found in enumerate(
pattern_found_list):
if not pattern_found:
print '%s --> %s' % (
filepath,
pattern_list[index]['message'])
for pattern_found in pattern_found_list:
print '%s --> %s' % (
filepath,
pattern_list[pattern_found]['message'])

return failed

def _check_mandatory_patterns(self):
Expand Down

0 comments on commit acb8f58

Please sign in to comment.