-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2290 from DARMA-tasking/2288-header-guards-genera…
…tion #2288: Add new script to generate/fix header guards and license
- Loading branch information
Showing
78 changed files
with
250 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
#!/usr/bin/env bash | ||
|
||
path_to_vt=${1} | ||
wrong_files="" | ||
|
||
cd "$path_to_vt" || exit 1 | ||
|
||
for sub_dir in "src" "tests/unit" "tests/perf" | ||
for sub_dir in "src" "tests" "tutorial" "examples" "tools" | ||
do | ||
wrong_files+=$(checkguard -r "$sub_dir" -p "path -1 | prepend INCLUDED_ | upper") | ||
python3 "${path_to_vt}/scripts/generate_header_guards_and_license.py" -s=${sub_dir} -l="${path_to_vt}/scripts/license-template" | ||
done | ||
|
||
if [[ $wrong_files ]]; then | ||
echo "Files with wrong header guard:" | ||
printf "%s\n" "$wrong_files" | ||
exit 1 | ||
# Check for modified files | ||
modified_files=$(git ls-files -m) | ||
|
||
if [ -n "$modified_files" ]; then | ||
echo "The following files have been modified by generate_header_guards_and_license.py:" | ||
echo "$modified_files" | ||
echo "Please run path_to_vt/scripts/generate_header_guards_and_license.py -s=your_sub_dir -l=path_to_vt/scripts/license-template to fix them" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import os | ||
import re | ||
import argparse as ap | ||
|
||
def has_license_header(lines): | ||
""" | ||
Check if the first two lines contain the license header start. | ||
""" | ||
if len(lines) >= 2 and lines[0] == "/*\n" and lines[1] == "//@HEADER\n": | ||
return True | ||
return False | ||
|
||
def find_existing_guards(lines): | ||
""" | ||
Find the range of lines containing existing header guards. | ||
""" | ||
ifndef_pattern = re.compile(r'#(?:if !defined|ifndef)\s+([A-Z0-9_]+)') | ||
define_pattern = re.compile(r'#define\s+([A-Z0-9_]+)') | ||
endif_pattern = re.compile(r'#endif') | ||
|
||
ifndef_start = None | ||
define_line = None | ||
endif_line = None | ||
|
||
for i, line in enumerate(lines): | ||
if ifndef_start is None: | ||
match = ifndef_pattern.match(line) | ||
if match: | ||
ifndef_start = i | ||
if ifndef_start is not None and define_line is None: | ||
match = define_pattern.match(line) | ||
if match: | ||
define_line = i | ||
if ifndef_start is not None and define_line is not None: | ||
match = endif_pattern.match(line) | ||
if match: | ||
endif_line = i | ||
|
||
if ifndef_start is not None and define_line is not None and endif_line is not None: | ||
return ifndef_start, endif_line | ||
return None, None | ||
|
||
|
||
def generate_license(file_path, license_path): | ||
with open(file_path, 'r') as file: | ||
lines = file.readlines() | ||
|
||
with open(license_path, 'r') as license_file: | ||
license_file = license_file.readlines() | ||
|
||
file_name = os.path.basename(file_path) | ||
lenright = int((80 - 2 - len(file_name))/2) | ||
license_file[3] = f"//{' ' :<{lenright}}{file_name}\n" | ||
|
||
# Remove leading empty lines | ||
non_empty_lines_start = 0 | ||
for i, line in enumerate(lines): | ||
if line.strip() != '': | ||
non_empty_lines_start = i | ||
break | ||
|
||
trimmed_lines = lines[non_empty_lines_start:] | ||
|
||
existing_license_start = -1 | ||
existing_license_end = -1 | ||
for i, line in enumerate(trimmed_lines): | ||
if line.startswith("//@HEADER"): | ||
if existing_license_start == -1: | ||
existing_license_start = i | ||
elif existing_license_end == -1: | ||
existing_license_end = i | ||
break | ||
|
||
if existing_license_start != -1: | ||
updated_file = trimmed_lines[:existing_license_start] + license_file + trimmed_lines[existing_license_end + 1:] | ||
else: | ||
updated_file = ['/*\n'] + license_file + ['*/\n'] + trimmed_lines | ||
|
||
with open(file_path, 'w') as file: | ||
file.writelines(updated_file) | ||
|
||
|
||
|
||
def generate_header_guard(file_path, root): | ||
with open(file_path, 'r') as file: | ||
lines = file.readlines() | ||
|
||
has_license = has_license_header(lines) | ||
|
||
# Create a header guard macro name based on the file name | ||
base_name = os.path.relpath(file_path, root).upper().replace('/', '_').replace('.', '_').replace('-', '_') | ||
guard_name = f'INCLUDED_{base_name}' | ||
|
||
# Add header guards | ||
ifndef_guard = f'#if !defined {guard_name}\n#define {guard_name}\n' | ||
endif_guard = f'#endif /*{guard_name}*/\n' | ||
|
||
# Detect existing guards | ||
start_line, end_line = find_existing_guards(lines) | ||
|
||
if has_license: | ||
license_content = ''.join(lines[:42]) | ||
file_content = ''.join(lines[42:]) | ||
else: | ||
license_content = '' | ||
file_content = ''.join(lines) | ||
|
||
if start_line is not None and end_line is not None: | ||
# Replace existing guards | ||
new_content = license_content + "\n" + ifndef_guard + ''.join(lines[start_line+2:end_line]) + endif_guard | ||
else: | ||
# Insert new guards | ||
new_content = license_content + "\n" + ifndef_guard + file_content + endif_guard | ||
|
||
with open(file_path, 'w') as file: | ||
file.write(new_content) | ||
|
||
def main(): | ||
parser = ap.ArgumentParser() | ||
parser.add_argument("--src_dir", "-s", dest='src_dir', required=True) | ||
parser.add_argument("--license", "-l", dest='license_path', required=True) | ||
args = parser.parse_args() | ||
|
||
src_dir_abs = os.path.abspath(os.path.expanduser(args.src_dir)) | ||
license_path_abs = os.path.abspath(os.path.expanduser(args.license_path)) | ||
for root, _, files in os.walk(src_dir_abs): | ||
if "extern" not in root.split(os.sep): # skip googletest | ||
for file in files: | ||
if file.endswith('.h') or file.endswith('.cc'): | ||
generate_license(os.path.join(root, file), license_path_abs) | ||
if file.endswith('.h'): | ||
generate_header_guard(os.path.join(root, file), src_dir_abs) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,4 +189,3 @@ extern ctx::Context* theContext(); | |
} // end namespace vt | ||
|
||
#endif /*INCLUDED_VT_CONTEXT_CONTEXT_H*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.