| |
| |
|
|
| import sys |
| from enum import Enum |
|
|
|
|
| class SPDXStatus(Enum): |
| """SPDX header status enumeration""" |
|
|
| EMPTY = "empty" |
| COMPLETE = "complete" |
| MISSING_LICENSE = "missing_license" |
| MISSING_COPYRIGHT = "missing_copyright" |
| MISSING_BOTH = "missing_both" |
|
|
|
|
| FULL_SPDX_HEADER = ( |
| "# SPDX-License-Identifier: Apache-2.0\n" |
| "# SPDX-FileCopyrightText: Copyright contributors to the vLLM project" |
| ) |
|
|
| LICENSE_LINE = "# SPDX-License-Identifier: Apache-2.0" |
| COPYRIGHT_LINE = "# SPDX-FileCopyrightText: Copyright contributors to the vLLM project" |
|
|
|
|
| def check_spdx_header_status(file_path): |
| """Check SPDX header status of the file""" |
| with open(file_path, encoding="UTF-8") as file: |
| lines = file.readlines() |
| if not lines: |
| |
| return SPDXStatus.EMPTY |
|
|
| |
| start_idx = 0 |
| if lines and lines[0].startswith("#!"): |
| start_idx = 1 |
|
|
| has_license = False |
| has_copyright = False |
|
|
| |
| for i in range(start_idx, len(lines)): |
| line = lines[i].strip() |
| if line == LICENSE_LINE: |
| has_license = True |
| elif line == COPYRIGHT_LINE: |
| has_copyright = True |
|
|
| |
| if has_license and has_copyright: |
| return SPDXStatus.COMPLETE |
| elif has_license and not has_copyright: |
| |
| return SPDXStatus.MISSING_COPYRIGHT |
| |
| elif not has_license and has_copyright: |
| return SPDXStatus.MISSING_LICENSE |
| else: |
| |
| return SPDXStatus.MISSING_BOTH |
|
|
|
|
| def add_header(file_path, status): |
| """Add or supplement SPDX header based on status""" |
| with open(file_path, "r+", encoding="UTF-8") as file: |
| lines = file.readlines() |
| file.seek(0, 0) |
| file.truncate() |
|
|
| if status == SPDXStatus.MISSING_BOTH: |
| |
| if lines and lines[0].startswith("#!"): |
| |
| file.write(lines[0]) |
| file.write(FULL_SPDX_HEADER + "\n") |
| file.writelines(lines[1:]) |
| else: |
| |
| file.write(FULL_SPDX_HEADER + "\n") |
| file.writelines(lines) |
|
|
| elif status == SPDXStatus.MISSING_COPYRIGHT: |
| |
| |
| for i, line in enumerate(lines): |
| if line.strip() == LICENSE_LINE: |
| |
| lines.insert( |
| i + 1, |
| f"{COPYRIGHT_LINE}\n", |
| ) |
| break |
|
|
| file.writelines(lines) |
|
|
| elif status == SPDXStatus.MISSING_LICENSE: |
| |
| |
| for i, line in enumerate(lines): |
| if line.strip() == COPYRIGHT_LINE: |
| |
| lines.insert(i, f"{LICENSE_LINE}\n") |
| break |
| file.writelines(lines) |
|
|
|
|
| def main(): |
| """Main function""" |
| files_missing_both = [] |
| files_missing_copyright = [] |
| files_missing_license = [] |
|
|
| for file_path in sys.argv[1:]: |
| status = check_spdx_header_status(file_path) |
|
|
| if status == SPDXStatus.MISSING_BOTH: |
| files_missing_both.append(file_path) |
| elif status == SPDXStatus.MISSING_COPYRIGHT: |
| files_missing_copyright.append(file_path) |
| elif status == SPDXStatus.MISSING_LICENSE: |
| files_missing_license.append(file_path) |
| else: |
| continue |
|
|
| |
| all_files_to_fix = ( |
| files_missing_both + files_missing_copyright + files_missing_license |
| ) |
| if all_files_to_fix: |
| print("The following files are missing the SPDX header:") |
| if files_missing_both: |
| for file_path in files_missing_both: |
| print(f" {file_path}") |
| add_header(file_path, SPDXStatus.MISSING_BOTH) |
|
|
| if files_missing_copyright: |
| for file_path in files_missing_copyright: |
| print(f" {file_path}") |
| add_header(file_path, SPDXStatus.MISSING_COPYRIGHT) |
| if files_missing_license: |
| for file_path in files_missing_license: |
| print(f" {file_path}") |
| add_header(file_path, SPDXStatus.MISSING_LICENSE) |
|
|
| sys.exit(1 if all_files_to_fix else 0) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|