Skip to content

Instantly share code, notes, and snippets.

@mmusich
Created April 11, 2023 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmusich/956f230a45ac05b6e8435c3eeabf38ea to your computer and use it in GitHub Desktop.
Save mmusich/956f230a45ac05b6e8435c3eeabf38ea to your computer and use it in GitHub Desktop.
import os
import re
# Define the base folder where the search should start
base_folder = '.'
# Define the regular expression pattern to match the inclusion guard
guard_pattern = r'#ifndef\s+(\w+)\n#define\s+\1\n'
# Compile the regular expression pattern
guard_regex = re.compile(guard_pattern)
# Loop through all the subdirectories and files in the base folder
for root, dirs, files in os.walk(base_folder):
for file in files:
# Check if the file has a .h extension
if file.endswith('.h'):
# Generate the new inclusion guard based on the file's path
package = os.path.basename(os.path.dirname(root))
subpackage = os.path.basename(root)
filename = os.path.splitext(file)[0]
if ("interface" not in subpackage):
new_guard = f'RecoTracker_{package}_{subpackage}_{filename}_h'
else:
new_guard = f'RecoTracker_{package}_{filename}_h'
print(package,subpackage,filename," =>" , new_guard)
# Replace the existing inclusion guard with the new one
filepath = os.path.join(root, file)
with open(filepath, 'r+') as f:
content = f.read()
match = guard_regex.search(content)
if match:
old_guard = match.group(1)
new_content = content.replace(old_guard, new_guard)
f.seek(0)
f.write(new_content)
f.truncate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment