ESP32/add_license.py

89 lines
3.2 KiB
Python

import os
# Configuration
SEARCH_DIRS = ["main", "components"] # Directories to scan relative to script location
EXTENSIONS = (".c", ".h")
COPYRIGHT_MARKER = "Copyright (c) 2025 Umber Networks & Robert McMahon"
# The License Template ( {filename} will be replaced )
LICENSE_TEMPLATE = """/*
* {filename}
*
* Copyright (c) 2025 Umber Networks & Robert McMahon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
"""
def add_license_to_file(filepath):
filename = os.path.basename(filepath)
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Check if license is already present
if COPYRIGHT_MARKER in content:
print(f"Skipping (License exists): {filepath}")
return
# Prepare the license text with the specific filename
license_text = LICENSE_TEMPLATE.format(filename=filename)
# Prepend license to content
new_content = license_text + content
with open(filepath, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Updated: {filepath}")
except Exception as e:
print(f"Error processing {filepath}: {e}")
def main():
root_dir = os.getcwd()
print(f"Scanning directories from: {root_dir}")
for search_dir in SEARCH_DIRS:
target_path = os.path.join(root_dir, search_dir)
if not os.path.exists(target_path):
print(f"Warning: Directory '{search_dir}' not found. Skipping.")
continue
for dirpath, _, filenames in os.walk(target_path):
for filename in filenames:
if filename.endswith(EXTENSIONS):
full_path = os.path.join(dirpath, filename)
add_license_to_file(full_path)
if __name__ == "__main__":
main()