Skip to content
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

LineEndingsCheck: improve performance #513

Merged
merged 5 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 17 additions & 37 deletions .pytool/Plugin/LineEndingCheck/LineEndingCheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ def _get_files_ignored_in_config(self,
Callable[[None], None]: A test case function.
"""
ignored_files = []
if pkg_config.get("IgnoreFilesWithNoExtension", False):
ignored_files.extend(['*', '!*.*', '!*/'])
if "IgnoreFiles" in pkg_config:
ignored_files = pkg_config["IgnoreFiles"]
ignored_files.extend(pkg_config["IgnoreFiles"])

# Pass "Package configuration file" as the source file path since
# the actual configuration file name is unknown to this plugin and
Expand Down Expand Up @@ -260,47 +262,25 @@ def RunBuildPlugin(self, package_rel_path: str, edk2_path: Edk2Path,
tc.LogStdError(f"Package folder not found {self._abs_pkg_path}")
return 0

all_files = [Path(n) for n in glob.glob(
os.path.join(self._abs_pkg_path, '**/*.*'),
recursive=True)]
ignored_files = list(filter(
self._get_files_ignored_in_config(
package_config, self._abs_pkg_path), all_files))
ignored_files = [Path(f) for f in ignored_files]

all_files = list(set(all_files) - set(ignored_files))
if not all_files:
tc.SetSuccess()
return 0

all_files_before_git_removal = set(all_files)
git_ignored_paths = set(self._get_git_ignored_paths() + self._get_git_submodule_paths())
all_files = list(all_files_before_git_removal - git_ignored_paths)
git_ignored_paths = git_ignored_paths - (all_files_before_git_removal - set(all_files))
if not all_files:
tc.SetSuccess()
return 0

git_ignored_paths = {p for p in git_ignored_paths if p.is_dir()}

ignored_files = []
for file in all_files:
for ignored_path in git_ignored_paths:
if Path(file).is_relative_to(ignored_path):
ignored_files.append(file)
break

all_files = list(set(all_files) - set(ignored_files))
if not all_files:
tc.SetSuccess()
return 0
ignore_files = set(self._get_git_ignored_paths())
ignore_dirs = set(self._get_git_submodule_paths())
ignore_filter = self._get_files_ignored_in_config(package_config, self._abs_pkg_path)

file_count = 0
line_ending_count = dict.fromkeys(LINE_ENDINGS, 0)

for file in all_files:
for file in Path(self._abs_pkg_path).rglob('*'):
if file.is_dir():
continue

if any(file.is_relative_to(ignore_dir) for ignore_dir in ignore_dirs):
continue

if ignore_filter(file):
continue

if file in ignore_files:
continue

with open(file.resolve(), 'rb') as fb:
if not fb.readable() or _is_binary_string(fb.read(1024)):
continue
Expand Down
6 changes: 6 additions & 0 deletions .pytool/Plugin/LineEndingCheck/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ The plugin can be configured to ignore certain files.
``` yaml
"LineEndingCheck": {
"IgnoreFiles": []
"IgnoreFilesWithNoExtension": False
}
```

### IgnoreFiles

An **optional** list of git ignore patterns relative to the package root used to exclude files from being checked.

### IgnoreFilesWithNoExtension

An **optional** value that, if True, will insert the gitignore rules necessary to have this check ignore files
that do not contain a file extension. Necessary for binary files and/or POSIX like executables.
2 changes: 1 addition & 1 deletion BaseTools/BaseTools.ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"AuditOnly": True,
},
"LineEndingCheck": {
"IgnoreFiles": ['*.*']
"IgnoreFiles": ["*"],
}
# MU_CHANGE [END]
}