Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,10 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
@@ -0,0 +1,59 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import os
import re
from typing import Type, List
import commit_validation.pal_allowedlist as pal_allowedlist
from commit_validation.commit_validation import Commit, CommitValidator, SOURCE_FILE_EXTENSIONS, VERBOSE
az_platform_regex = re.compile(r'^\+\s*#.*\bAZ_PLATFORM_')
class AzPlatformValidator(CommitValidator):
"""A file-level validator that makes sure a file does not contain AZ_PLATFORM macros"""
def __init__(self) -> None:
self.pal_allowedlist = pal_allowedlist.load()
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
if os.path.splitext(file_name)[1].lower() not in SOURCE_FILE_EXTENSIONS:
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on extension.')
continue
if self.pal_allowedlist.is_match(file_name):
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on PAL allowedlist.')
continue
file_diff = commit.get_file_diff(file_name)
previous_line_context = ""
line_number = 1
for line in file_diff.splitlines():
# we only care about lines that start with +
if line.startswith('+'):
if az_platform_regex.search(line):
error_message = str(f'{file_name}:{line_number}::{self.__class__.__name__} FAILED - Source file contains an AZ_PLATFORM '
f'macro in code:\n'
f' {previous_line_context}\n'
f'---> {line}\n')
if VERBOSE: print(error_message)
errors.append(error_message)
previous_line_context = line
line_number += 1
return (not errors)
def get_validator() -> Type[AzPlatformValidator]:
"""Returns the validator class for this module"""
return AzPlatformValidator
@@ -0,0 +1,60 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import os
import re
from typing import Type, List
import commit_validation.pal_allowedlist as pal_allowedlist
from commit_validation.commit_validation import Commit, CommitValidator, SOURCE_FILE_EXTENSIONS, VERBOSE
ifdef_regex = re.compile(r'^\+\s*#\s*ifn?def\s+AZ_TRAIT_')
defined_regex = re.compile(r'\sdefined\s*\(\s*AZ_TRAIT_')
class AzTraitValidator(CommitValidator):
"""A file-level validator that makes sure a file does not contain existence checks for AZ_TRAIT macros"""
def __init__(self) -> None:
self.pal_allowedlist = pal_allowedlist.load()
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
if os.path.splitext(file_name)[1].lower() not in SOURCE_FILE_EXTENSIONS:
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on extension.')
continue
if self.pal_allowedlist.is_match(file_name):
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on PAL allowedlist.')
continue
file_diff = commit.get_file_diff(file_name)
previous_line_context = ""
for line in file_diff.splitlines():
# we only care about added lines.
if line.startswith('+'):
if ifdef_regex.search(line) or defined_regex.search(line):
error_message = str(
f'{file_name}::{self.__class__.__name__} FAILED - Source file contains an existence '
f'check for an AZ_TRAIT macro in this code: \n'
f' {previous_line_context}\n'
f' ----> {line}\n'
f'Traits should be tested for true/false, since they are guaranteed to exist on all platforms.')
if VERBOSE: print(error_message)
errors.append(error_message)
previous_line_context = line
return (not errors)
def get_validator() -> Type[AzTraitValidator]:
"""Returns the validator class for this module"""
return AzTraitValidator
@@ -0,0 +1,52 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import fnmatch
import os
import re
from typing import Type, List
from commit_validation.commit_validation import Commit, CommitValidator, IsFileSkipped, SOURCE_AND_SCRIPT_FILE_EXTENSIONS, EXCLUDED_VALIDATION_PATTERNS, VERBOSE
class CopyrightHeaderValidator(CommitValidator):
"""A file-level validator that makes sure a file contains the standard copyright header"""
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
for pattern in EXCLUDED_VALIDATION_PATTERNS:
if fnmatch.fnmatch(file_name, pattern):
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - Validation pattern excluded on path.')
break
else:
if IsFileSkipped(file_name):
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on extension.')
continue
copyright_regex = re.compile(r'copyright[\s]*(?:\(c\))?[\s]*amazon\.com', re.IGNORECASE)
# copyright header validator does not use the diff, as it needs to check the front
# of the file for the header.
with open(file_name, 'rt', encoding='utf8', errors='replace') as fh:
for line in fh:
if copyright_regex.search(line):
break
else:
error_message = str(f'{file_name}::{self.__class__.__name__} FAILED - Source file missing copyright headers.')
errors.append(error_message)
if VERBOSE: print(error_message)
return (not errors)
def get_validator() -> Type[CopyrightHeaderValidator]:
"""Returns the validator class for this module"""
return CopyrightHeaderValidator
@@ -0,0 +1,58 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import fnmatch
import os
import re
from typing import Type, List
from commit_validation.commit_validation import Commit, CommitValidator, IsFileSkipped, SOURCE_AND_SCRIPT_FILE_EXTENSIONS, EXCLUDED_VALIDATION_PATTERNS, VERBOSE
__STARTS_WITH_UNCHANGED_LINE = r'(\A|^(?=[^+-]).*\n)'
__NONZERO_CHANGED_WHITESPACE_LINES = r'(^[+-][\r\t\f\v ]*\n)+'
__ENDS_WITH_UNCHANGED_LINE = r'([^+-]|\Z)'
__NONADJACENT_WHITESPACE =\
__STARTS_WITH_UNCHANGED_LINE + __NONZERO_CHANGED_WHITESPACE_LINES + __ENDS_WITH_UNCHANGED_LINE
_NONADJACENT_WHITESPACE_DIFF_REGEX = re.compile(__NONADJACENT_WHITESPACE, re.MULTILINE)
_NONWHITESPACE_DIFF_REGEX = re.compile(r'^[+-][\t ]*\S', re.MULTILINE)
class WhitespaceValidator(CommitValidator):
"""A diff-level validator that makes sure a change does not mix whitespace-only changes with code changes"""
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
file_identifier = f"{file_name}::{self.__class__.__name__}"
for pattern in EXCLUDED_VALIDATION_PATTERNS:
if fnmatch.fnmatch(file_name, pattern):
if VERBOSE: print(f'{file_identifier} SKIPPED - Validation pattern excluded on path.')
break
else:
if IsFileSkipped(file_name):
if VERBOSE: print(f'{file_identifier} SKIPPED - File excluded based on extension.')
continue
diff = commit.get_file_diff(file_name)
if _NONADJACENT_WHITESPACE_DIFF_REGEX.search(diff) and _NONWHITESPACE_DIFF_REGEX.search(diff):
error_message = str(f'{file_identifier} FAILED - Source file contains whitespace-only changes which are '
f'non-contiguous with other non-whitespace changes. Make whitespace-only changes as a '
f'separate change.')
errors.append(error_message)
if VERBOSE: print(error_message)
return (not errors)
def get_validator() -> Type[WhitespaceValidator]:
"""Returns the validator class for this module"""
return WhitespaceValidator
@@ -0,0 +1,139 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import fnmatch
import os
import pathlib
from typing import Type, List
from commit_validation.commit_validation import Commit, CommitValidator, EXCLUDED_VALIDATION_PATTERNS, VERBOSE
# Disallowed File Name Patterns
# these must be LOWER CASE since they will be compared after a 'lower'
AZ_CODEGEN_FILENAME_PATTERN = "*.generated*"
QT_COMPILER_PATTERNS = ["moc_*", "qrc_*", "ui_*.h"]
CMAKE_CACHE_FILENAME_PATTERNS = ["cmakecache.txt", "cmake_install.cmake", "ctesttestfile.cmake", "ctestcostdata.txt"]
OTHER_GENERATED_FILES = ["appxmanifest.xml"]
# Disallowed File Extensions
CMAKE_CACHE_EXTENSIONS = [".rule", ".stamp", ".depend"]
TEMPFILE_EXTENSIONS = [".backup", ".bak", ".bkup", ".temp", ".tempfile", ".temporary", ".tmp"]
EXECUTION_ARTIFACT_EXTENSIONS = [".cache", ".dmp", ".log", ".pyc"]
CLANG_FILE_EXTENSIONS = [".o", ".s"]
MSVS_FILE_EXTENSIONS = [
".aps",
".csproj",
".exp",
".filters",
".idb",
".ilk",
".lastbuildstate",
".meta",
".ncb",
".obj",
".opensdf",
".pch",
".pdb",
".pgc",
".pgd",
".psess",
".rsp",
".sbr",
".sdf",
".sln",
".suo",
".tlb",
".tlh",
".tli",
".tlog",
".vap",
".vbg",
".vbproj",
".vcxitems",
".vcxproj",
".vdproj",
".vmx",
".vsp",
".vspscc",
".vspx",
".vssscc",
".vup",
]
class GeneratedFilesValidator(CommitValidator):
"""A file-level validator to check if a file is a true source file, and not generated during build or execution"""
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
file_identifier = f"{file_name}::{self.__class__.__name__}"
for pattern in EXCLUDED_VALIDATION_PATTERNS:
if fnmatch.fnmatch(file_name, pattern):
if VERBOSE: print(f'{file_identifier} SKIPPED - Validation pattern excluded on path.')
break
else:
file_path_lower = pathlib.Path(file_name.lower())
extension = file_path_lower.suffix
if extension in CMAKE_CACHE_EXTENSIONS:
error_message = str(f'{file_identifier} FAILED - Autogenerated CMake file detected by extension.')
if VERBOSE: print(error_message)
errors.append(error_message)
continue
if extension in MSVS_FILE_EXTENSIONS:
error_message = str(f'{file_identifier} FAILED - Autogenerated Visual Studio file detected by extension.')
if VERBOSE: print(error_message)
errors.append(error_message)
continue
if extension in CLANG_FILE_EXTENSIONS:
error_message = str(f'{file_identifier} FAILED - Autogenerated Clang file detected by extension.')
if VERBOSE: print(error_message)
errors.append(error_message)
continue
if extension in TEMPFILE_EXTENSIONS:
error_message = str(f'{file_identifier} FAILED - Autogenerated temporary file detected by extension.')
if VERBOSE: print(error_message)
errors.append(error_message)
continue
if extension in EXECUTION_ARTIFACT_EXTENSIONS:
error_message = str(f'{file_identifier} FAILED - Execution artifact detected by extension.')
if VERBOSE: print(error_message)
errors.append(error_message)
continue
for cmake_pattern in QT_COMPILER_PATTERNS:
if fnmatch.fnmatch(file_path_lower.name, cmake_pattern):
error_message = str(f'{file_identifier} FAILED - Autogenerated QT file detected by pattern.')
if VERBOSE: print(error_message)
errors.append(error_message)
break
for cmake_pattern in CMAKE_CACHE_FILENAME_PATTERNS:
if fnmatch.fnmatch(file_path_lower.name, cmake_pattern):
error_message = str(f'{file_identifier} FAILED - Autogenerated CMake file detected by name.')
if VERBOSE: print(error_message)
errors.append(error_message)
break
if fnmatch.fnmatch(file_path_lower.name, AZ_CODEGEN_FILENAME_PATTERN):
error_message = str(f'{file_identifier} FAILED - Autogenerated AzCodeGen file detected by name.')
if VERBOSE: print(error_message)
errors.append(error_message)
continue
if file_path_lower.name in OTHER_GENERATED_FILES:
error_message = str(f'{file_identifier} FAILED - Generated file (others).')
if VERBOSE: print(error_message)
errors.append(error_message)
continue
return (not errors)
def get_validator() -> Type[GeneratedFilesValidator]:
"""Returns the validator class for this module"""
return GeneratedFilesValidator
@@ -0,0 +1,80 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import os
import re
from typing import Type, List
import commit_validation.pal_allowedlist as pal_allowedlist
from commit_validation.commit_validation import Commit, CommitValidator, SOURCE_FILE_EXTENSIONS, VERBOSE
MERGE_TO_MARKER_REGEX = re.compile(r'^<<<<<<<')
MERGE_BASE_REGEX = re.compile(r'^\|\|\|\|\|\|\|')
MERGE_DIFF_REGEX = re.compile(r'^=======\n') # include newline as equals sign is a common visual separator
MERGE_FROM_MARKER_REGEX = re.compile(r'^>>>>>>>')
class GitConflictValidator(CommitValidator):
"""A file-level validator that for conflict markers"""
def __init__(self) -> None:
self.pal_allowedlist = pal_allowedlist.load()
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
if os.path.splitext(file_name)[1].lower() not in SOURCE_FILE_EXTENSIONS:
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on extension.')
continue
if self.pal_allowedlist.is_match(file_name):
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on PAL allowedlist.')
continue
# we never want conflict markers to be added to our repository
# so we don't look at the file diffs, but the file contents.
with open(file_name, 'rt', encoding='utf8', errors='replace') as fh:
previous_line_context = ""
for line_number, line in enumerate(fh):
if MERGE_TO_MARKER_REGEX.search(line):
error_message = str(f'{file_name}::{self.__class__.__name__} FAILED - Source file contains git merge '
f'conflict start-marker on line {line_number + 1}:\n'
f' {previous_line_context}'
f'----> {line}')
if VERBOSE: print(error_message)
errors.append(error_message)
if MERGE_BASE_REGEX.search(line):
error_message = str(f'{file_name}::{self.__class__.__name__} FAILED - Source file contains git merge '
f'conflict diff3-marker on line {line_number + 1}:\n'
f' {previous_line_context}'
f'----> {line}')
if VERBOSE: print(error_message)
errors.append(error_message)
if MERGE_DIFF_REGEX.search(line):
error_message = str(f'{file_name}::{self.__class__.__name__} FAILED - Source file contains git merge '
f'conflict diff-marker on line {line_number + 1}:\n'
f' {previous_line_context}'
f'----> {line}')
if VERBOSE: print(error_message)
errors.append(error_message)
if MERGE_FROM_MARKER_REGEX.search(line):
error_message = str(f'{file_name}::{self.__class__.__name__} FAILED - Source file contains git merge '
f'conflict end-marker on line {line_number + 1}:\n'
f' {previous_line_context}'
f'----> {line}')
if VERBOSE: print(error_message)
errors.append(error_message)
previous_line_context = line
return (not errors)
def get_validator() -> Type[GitConflictValidator]:
"""Returns the validator class for this module"""
return GitConflictValidator
@@ -0,0 +1,74 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import fnmatch
import os
import re
from typing import Type, List
from commit_validation.commit_validation import Commit, CommitValidator, IsFileSkipped, SOURCE_AND_SCRIPT_FILE_EXTENSIONS, EXCLUDED_VALIDATION_PATTERNS, VERBOSE
_SINGLE_NEWLINE_ENDING_REGEX = re.compile(r'\n\Z', re.MULTILINE)
_MULTI_NEWLINE_ENDING_REGEX = re.compile(r'\n\s*\n\Z', re.MULTILINE)
_CRLF_REGEX = re.compile(r'^.*\r\n', re.MULTILINE)
_ONLY_LF_REGEX = re.compile(r'^[^\r]*\n', re.MULTILINE)
class NewlineValidator(CommitValidator):
"""A file-level validator that makes sure a file contains valid line-endings"""
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
file_identifier = f"{file_name}::{self.__class__.__name__}"
for pattern in EXCLUDED_VALIDATION_PATTERNS:
if fnmatch.fnmatch(file_name, pattern):
if VERBOSE: print(f'{file_identifier} SKIPPED - Validation pattern excluded on path.')
break
else:
file_extension = os.path.splitext(file_name)[1].lower()
if IsFileSkipped(file_name):
if VERBOSE: print(f'{file_identifier} SKIPPED - File excluded based on extension.')
continue
# since this validator focuses on newlines throughout the file, not just in diffs
# we use the real file data instead of a diff
with open(file_name, 'rt', encoding='utf8', errors='replace') as fh:
lines = fh.read()
if not _SINGLE_NEWLINE_ENDING_REGEX.search(lines):
error_message = str(f'{file_identifier} FAILED - Source file does not end with a trailing newline.')
if VERBOSE: print(error_message)
errors.append(error_message)
if _MULTI_NEWLINE_ENDING_REGEX.search(lines):
error_message = str(f'{file_identifier} FAILED - Source file ends in multiple trailing newlines.')
if VERBOSE: print(error_message)
errors.append(error_message)
crlf_result = _CRLF_REGEX.search(lines)
only_lf_result = _ONLY_LF_REGEX.search(lines)
if crlf_result and only_lf_result:
error_message = str(f'{file_identifier} FAILED - Source file contains mixed line endings (\\r\\n and \\n)')
if VERBOSE: print(error_message)
errors.append(error_message)
if crlf_result:
error_message = str(f'{file_identifier} FAILED - Source file incorrectly contains Windows-style line endings'
f' (\\r\\n), when Unix-style line endings (\\n) were expected. Enable git option '
f'"core.autocrlf" to avoid this error.')
if VERBOSE: print(error_message)
errors.append(error_message)
return (not errors)
def get_validator() -> Type[NewlineValidator]:
"""Returns the validator class for this module"""
return NewlineValidator
@@ -0,0 +1,86 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import fnmatch
import os
import re
from typing import Type, List
import commit_validation.pal_allowedlist as pal_allowedlist
from commit_validation.commit_validation import Commit, CommitValidator, SOURCE_FILE_EXTENSIONS, VERBOSE
platform_macros = [
'ANDROID',
'APPLE',
'APPLETV',
'DARWIN',
'IOS',
'LINUX',
'LINUX64',
'MAC',
'PROVO',
'SALEM',
'WIN32',
'WIN32_LEAN_AND_MEAN',
'WIN64',
'XENIA',
'_WIN32',
'_WIN32_WINDOWS',
'_WIN32_WINNT',
'linux',
]
platform_macro_regex = re.compile(r'^\+\s*#.*?\b(' + str.join('|', platform_macros) + r')\b')
class PlatformMacroValidator(CommitValidator):
"""A file-level validator that makes sure a file does not contain certain platform macros"""
def __init__(self) -> None:
self.pal_allowedlist = pal_allowedlist.load()
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
if os.path.splitext(file_name)[1].lower() not in SOURCE_FILE_EXTENSIONS:
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on extension.')
continue
if self.pal_allowedlist.is_match(file_name):
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on PAL allowedlist.')
continue
if fnmatch.fnmatch(file_name, '*/Platform/*'):
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded because it is in a "Platform" '
f'folder.')
continue
file_diff = commit.get_file_diff(file_name)
previous_line_context = ""
line_number = 1
for line in file_diff.splitlines():
# we only care about added lines.
if line.startswith('+'):
match = platform_macro_regex.search(line)
if match:
error_message = str(f'{file_name}:{line_number}::{self.__class__.__name__} FAILED - Source file contains a forbidden '
f'platform macro "{match.group(1)}" here:\n'
f' {previous_line_context}\n'
f'---> {line}')
if VERBOSE: print(error_message)
errors.append(error_message)
previous_line_context = line
line_number += 1
return (not errors)
def get_validator() -> Type[PlatformMacroValidator]:
"""Returns the validator class for this module"""
return PlatformMacroValidator
@@ -0,0 +1,51 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import fnmatch
import os.path
from typing import Type, List
from commit_validation.commit_validation import Commit, CommitValidator, SOURCE_FILE_EXTENSIONS, EXCLUDED_VALIDATION_PATTERNS, VERBOSE
class PragmaOptimizeValidator(CommitValidator):
"""A file-level validator that makes sure a file does not contain #pragma optimize directives"""
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
for pattern in EXCLUDED_VALIDATION_PATTERNS:
if fnmatch.fnmatch(file_name, pattern):
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - Validation pattern excluded on path.')
break
else:
if os.path.splitext(file_name)[1].lower() not in SOURCE_FILE_EXTENSIONS:
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED - File excluded based on extension.')
continue
file_diff = commit.get_file_diff(file_name)
previous_line_for_context = ""
for line in file_diff.splitlines():
# we only care about added lines in a diff
if line.startswith('+'):
if '#pragma optimize' in line:
error_message = str(f'{file_name}::{self.__class__.__name__} FAILED - Source file contains #pragma optimize!\n'
f' {previous_line_for_context}\n'
f'---> {line}')
if VERBOSE: print(error_message)
errors.append(error_message)
previous_line_for_context = line
return (not errors)
def get_validator() -> Type[PragmaOptimizeValidator]:
"""Returns the validator class for this module"""
return PragmaOptimizeValidator
@@ -0,0 +1,65 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import fnmatch
import os.path
from typing import Type, List
from commit_validation.commit_validation import Commit, CommitValidator, IsFileSkipped, SOURCE_AND_SCRIPT_FILE_EXTENSIONS, EXCLUDED_VALIDATION_PATTERNS, VERBOSE
class TabsValidator(CommitValidator):
"""A file-level validator that makes sure a file does not contain tabs"""
def run(self, commit: Commit, errors: List[str]) -> bool:
for file_name in commit.get_files():
if IsFileSkipped(file_name):
if VERBOSE: print(f'{file_name}::{self.__class__.__name__} SKIPPED TabsValidator - File excluded based on extension.')
continue
for pattern in EXCLUDED_VALIDATION_PATTERNS:
if fnmatch.fnmatch(file_name, pattern):
if VERBOSE: print(f'{file_name} SKIPPED TabsValidator - Validation pattern excluded on path.')
break
else:
tab_line_count = 0
file_diff = commit.get_file_diff(file_name)
# Usually, code either has a very small number of tabs in the file by accident,
# or the entire file is full of tabs.
# So we count the tabs, but we only print the first one in full.
first_tab_line_found = None
for line in file_diff.splitlines():
# we only care about added lines.
if line.startswith('+'):
if '\t' in line:
line = line.replace('\t','\\t') # make it obvious!
if not first_tab_line_found:
first_tab_line_found = str(
f' {previous_line_context}\n'
f'---> {line}\n')
tab_line_count = tab_line_count + 1
previous_line_context = line
if tab_line_count:
error_message = str(
f'{file_name}::{self.__class__.__name__} FAILED TabsValidator - {tab_line_count} tabs in this file\n'
f'First instance of a tab: \n'
f'{first_tab_line_found}')
errors.append(error_message)
if VERBOSE: print(error_message)
return (not errors)
def get_validator() -> Type[TabsValidator]:
"""Returns the validator class for this module"""
return TabsValidator