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

Replace Apache license headers with SPDX ID #5808

Merged
Merged
Changes from 6 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
43 changes: 43 additions & 0 deletions scripts/update_license_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
import re
import sys

def update_license_header(file_path):
with open(file_path, 'r') as file:
content = file.read()

# Pattern to match copyright lines and Apache license
header_pattern = re.compile(
r'^(// Copyright.*\n)+' # Match one or more copyright lines
r'(//\s*\n)*' # Match zero or more empty comment lines
r'// Licensed under the Apache License, Version 2\.0.*?'
r'limitations under the License\.\n+',
re.MULTILINE | re.DOTALL
)

spdx_header = "// SPDX-License-Identifier: Apache-2.0\n"

new_content, count = header_pattern.subn(
lambda m: ''.join(re.findall(r'^// Copyright.*\n', m.group(0), re.MULTILINE)) + spdx_header,
content
)

if count > 0:
with open(file_path, 'w') as file:
file.write(new_content)
print(f"Updated {file_path}")
return True
else:
print(f"Skipping {file_path}: No Apache header found")
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
return False

def main():
if len(sys.argv) < 2:
print("Usage: python update_license_headers.py <file> [<file> ...]")
sys.exit(1)

total_updated = sum(update_license_header(file) for file in sys.argv[1:])
print(f"Total files updated: {total_updated}")

if __name__ == "__main__":
main()