Skip to content

Commit

Permalink
Merge pull request #346 from DARMA-tasking/341-header-guards-script
Browse files Browse the repository at this point in the history
#341: Add script for generating license and header guards
  • Loading branch information
lifflander authored Jun 6, 2024
2 parents 4e98dde + cffcbc0 commit f6cf0af
Show file tree
Hide file tree
Showing 61 changed files with 267 additions and 87 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/check_license_and_guards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Check License and Header Guards

on: pull_request

jobs:
check:
name: Check License/Guards
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master

- name: Install dependencies
run: sudo apt-get update && sudo apt-get install libfile-find-rule-perl

- name: Check license and header guards
shell: bash
run: ./scripts/check_guards.sh $(pwd) true
2 changes: 1 addition & 1 deletion examples/checkpoint_example_1_nonintrusive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_example_1_nonintrusive.cc
// checkpoint_example_1_nonintrusive.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion examples/checkpoint_example_2_nonintrusive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_example_2_nonintrusive.cc
// checkpoint_example_2_nonintrusive.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion examples/checkpoint_example_3_nonintrusive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_example_3_nonintrusive.cc
// checkpoint_example_3_nonintrusive.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion examples/checkpoint_example_polymorphic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_example_polymorphic.cc
// checkpoint_example_polymorphic.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion examples/checkpoint_example_polymorphic_macro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_example_polymorphic_macro.cc
// checkpoint_example_polymorphic_macro.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_example_polymorphic_macro_nonintrusive.cc
// checkpoint_example_polymorphic_macro_nonintrusive.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion examples/checkpoint_example_polymorphic_nonintrusive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_example_polymorphic_nonintrusive.cc
// checkpoint_example_polymorphic_nonintrusive.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion examples/checkpoint_example_to_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_example_to_file.cc
// checkpoint_example_to_file.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion examples/checkpoint_example_to_file_nonintrusive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_example_to_file_nonintrusive.cc
// checkpoint_example_to_file_nonintrusive.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion examples/checkpoint_example_traversal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_traversal.cc
// checkpoint_example_traversal.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion examples/checkpoint_example_traversal_nonintrusive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// checkpoint_traversal_nonintrusive.cc
// checkpoint_example_traversal_nonintrusive.cc
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion scripts/add-license-perl.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use strict;
use warnings;

my ($path, $template, $extension) = ($ARGV[0], "license-template", "*.*");
my ($path, $template, $extension) = ($ARGV[0], "license-template", ["*.h", "*.cc"]);

$template = $ARGV[1] if (@ARGV > 1);
$extension = $ARGV[2] if (@ARGV > 2);
Expand Down
24 changes: 24 additions & 0 deletions scripts/check_guards.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

path_to_magistrate=${1}
show_fix_command=${2}
cd "$path_to_magistrate" || exit 1

for sub_dir in "src" "tests" "examples"
do
python3 "${path_to_magistrate}/scripts/generate_header_guards_and_license.py" -s=${sub_dir} -l="${path_to_magistrate}/scripts/license-template"
done

# Check for modified files
modified_files=$(git ls-files -m)

if [ -n "$modified_files" ]; then
echo "The following files require an update to the license or header guards:"
echo "$modified_files"

if [ "$show_fix_command" == true ]; then
echo "Please run the following command from main magistrate directory to fix them:"
echo "./scripts/check_guards.sh ."
fi
exit 1
fi
135 changes: 135 additions & 0 deletions scripts/generate_header_guards_and_license.py
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()
4 changes: 2 additions & 2 deletions src/checkpoint/container/atomic_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//@HEADER
// *****************************************************************************
//
// atomic_serialize.h
// atomic_serialize.h
// DARMA/checkpoint => Serialization Library
//
// Copyright 2020 National Technology & Engineering Solutions of Sandia, LLC
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
Expand Down
4 changes: 2 additions & 2 deletions src/checkpoint/container/chrono_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//@HEADER
// *****************************************************************************
//
// chrono_serialize.h
// chrono_serialize.h
// DARMA/checkpoint => Serialization Library
//
// Copyright 2020 National Technology & Engineering Solutions of Sandia, LLC
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
Expand Down
4 changes: 2 additions & 2 deletions src/checkpoint/container/function_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//@HEADER
// *****************************************************************************
//
// function_serialize.h
// function_serialize.h
// DARMA/checkpoint => Serialization Library
//
// Copyright 2020 National Technology & Engineering Solutions of Sandia, LLC
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/container/kokkos_complex_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// kokkos_complex_serialize.h
// kokkos_complex_serialize.h
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/container/kokkos_pair_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// kokkos_pair_serialize.h
// kokkos_pair_serialize.h
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/container/kokkos_unordered_map_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@HEADER
// *****************************************************************************
//
// kokkos_unordered_map_serialize.h
// kokkos_unordered_map_serialize.h
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
Expand Down
4 changes: 2 additions & 2 deletions src/checkpoint/container/queue_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//@HEADER
// *****************************************************************************
//
// queue_serialize.h
// queue_serialize.h
// DARMA/checkpoint => Serialization Library
//
// Copyright 2020 National Technology & Engineering Solutions of Sandia, LLC
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/container/raw_ptr_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// raw_ptr_serialize.h
// DARMA/checkpoint => Serialization Library
//
// Copyright 2020 National Technology & Engineering Solutions of Sandia, LLC
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
Expand Down
4 changes: 2 additions & 2 deletions src/checkpoint/container/shared_ptr_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//@HEADER
// *****************************************************************************
//
// shared_ptr_serialize.h
// shared_ptr_serialize.h
// DARMA/checkpoint => Serialization Library
//
// Copyright 2020 National Technology & Engineering Solutions of Sandia, LLC
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
Expand Down
Loading

0 comments on commit f6cf0af

Please sign in to comment.