|
| 1 | +# License Header Management Tool |
| 2 | + |
| 3 | +A comprehensive tool for managing license headers across the torch-xpu-ops repository. This tool automates the process of adding, verifying, and replacing license headers in source files. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Automatic header detection**: Identifies existing license headers in source files |
| 8 | +- **Multi-format support**: Handles Python, C/C++, CUDA, YAML, and CMake files with appropriate comment styles |
| 9 | +- **Configurable headers**: YAML-based configuration for default and custom headers per file |
| 10 | +- **Multiple operations**: Check, add, replace, or force-update headers |
| 11 | +- **Dry-run mode**: Preview changes before applying them |
| 12 | +- **Glob pattern support**: Flexible file matching for custom header assignments |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +No additional dependencies required beyond Python 3.6+ standard library. |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +### Basic Commands |
| 21 | + |
| 22 | +```bash |
| 23 | +# Check all files for correct headers (no modifications) |
| 24 | +python tools/fixheaders/fixheaders.py check . |
| 25 | + |
| 26 | +# Add headers only to files missing them |
| 27 | +python tools/fixheaders/fixheaders.py add . |
| 28 | + |
| 29 | +# Replace incorrect headers with correct ones |
| 30 | +python tools/fixheaders/fixheaders.py replace . |
| 31 | + |
| 32 | +# Force replace all headers regardless of current state |
| 33 | +python tools/fixheaders/fixheaders.py force . |
| 34 | +``` |
| 35 | + |
| 36 | +### Options |
| 37 | + |
| 38 | +| Option | Description | |
| 39 | +|--------|-------------| |
| 40 | +| `--config`, `-c` | Path to YAML configuration file (default: `default.yaml` in script directory) | |
| 41 | +| `--dry-run`, `-n` | Show what would be done without making changes | |
| 42 | +| `--verbose`, `-v` | Enable verbose output | |
| 43 | + |
| 44 | +### Examples |
| 45 | + |
| 46 | +```bash |
| 47 | +# Dry-run check with verbose output |
| 48 | +python tools/fixheaders/fixheaders.py check . --dry-run --verbose |
| 49 | + |
| 50 | +# Use custom config for CUDA-derived files |
| 51 | +python tools/fixheaders/fixheaders.py replace . --config tools/fixheaders/cuda.yaml |
| 52 | + |
| 53 | +# Add headers to specific directory |
| 54 | +python tools/fixheaders/fixheaders.py add src/ATen/native/xpu/ |
| 55 | +``` |
| 56 | + |
| 57 | +## Configuration |
| 58 | + |
| 59 | +Configuration files use YAML format with the following structure: |
| 60 | + |
| 61 | +```yaml |
| 62 | +# Default header applied to all files unless specified otherwise |
| 63 | +default_header: | |
| 64 | + Copyright (c) 2025 Intel Corporation |
| 65 | + SPDX-License-Identifier: Apache-2.0 |
| 66 | +
|
| 67 | +# Files/patterns to exclude from processing entirely |
| 68 | +exclude: |
| 69 | + - ".git/*" |
| 70 | + - "__pycache__/*" |
| 71 | + - "**/__init__.py" |
| 72 | + |
| 73 | +# Custom headers for specific files |
| 74 | +custom_headers: |
| 75 | + - header: | |
| 76 | + Copyright (c) 2025 Intel Corporation |
| 77 | + SPDX-License-Identifier: Apache-2.0 |
| 78 | +
|
| 79 | + Portions derived from Third Party Project |
| 80 | + Original Copyright Notice Here |
| 81 | + files: |
| 82 | + - "path/to/specific/file.cpp" |
| 83 | + - "src/some/pattern/*.h" |
| 84 | +``` |
| 85 | +
|
| 86 | +### Configuration Files |
| 87 | +
|
| 88 | +| File | Purpose | |
| 89 | +|------|---------| |
| 90 | +| `default.yaml` | Default Intel Apache-2.0 header and common exclusions | |
| 91 | +| `*.yaml` | Headers for files derived from third-party sources | |
| 92 | + |
| 93 | +## Supported File Types |
| 94 | + |
| 95 | +| Extension | Comment Style | |
| 96 | +|-----------|---------------| |
| 97 | +| `.py`, `.pyi` | `# comment` | |
| 98 | +| `.yml`, `.yaml` | `# comment` | |
| 99 | +| `.cmake` | `# comment` | |
| 100 | +| `.c`, `.h` | `/* block comment */` | |
| 101 | +| `.cpp`, `.hpp`, `.cxx`, `.hxx`, `.cc` | `/* block comment */` | |
| 102 | +| `.cu`, `.cuh` | `/* block comment */` | |
| 103 | + |
| 104 | +## Pre-Release License Compliance Process |
| 105 | + |
| 106 | +Before each release, follow this process to ensure all files have correct license headers: |
| 107 | + |
| 108 | +### Step 1: Run Protex Scan |
| 109 | + |
| 110 | +Launch a Protex (Black Duck) scan on the repository to identify all third-party code. |
| 111 | + |
| 112 | +### Step 2: Identify Matches |
| 113 | + |
| 114 | +Review the Protex scan results to identify: |
| 115 | + |
| 116 | +- Files containing third-party code |
| 117 | +- The original project/source of the code |
| 118 | +- The applicable license for each match |
| 119 | + |
| 120 | +### Step 3: Create/Update YAML Configuration |
| 121 | + |
| 122 | +For each third-party project identified: |
| 123 | + |
| 124 | +1. **Create a separate YAML file** for each project (if not already existing): |
| 125 | + ``` |
| 126 | + tools/fixheaders/<project_name>.yaml |
| 127 | + ``` |
| 128 | + |
| 129 | +2. **Copy the exact copyright header** from the original project. The header must be reproduced exactly as it appears in the source: |
| 130 | + |
| 131 | + ```yaml |
| 132 | + # filepath: tools/fixheaders/<project_name>.yaml |
| 133 | + # Copyright (c) 2025 Intel Corporation |
| 134 | + # SPDX-License-Identifier: Apache-2.0 |
| 135 | +
|
| 136 | + custom_headers: |
| 137 | + - header: | |
| 138 | + Copyright (c) 2025 Intel Corporation |
| 139 | + SPDX-License-Identifier: Apache-2.0 |
| 140 | +
|
| 141 | + <PASTE EXACT ORIGINAL COPYRIGHT HEADER HERE> |
| 142 | + <PRESERVE ALL ORIGINAL TEXT, FORMATTING, AND NOTICES> |
| 143 | + files: |
| 144 | + - "path/to/derived/file1.cpp" |
| 145 | + - "path/to/derived/file2.h" |
| 146 | + ``` |
| 147 | + |
| 148 | +3. **Important**: Always include the Intel copyright first, followed by the original project's copyright/license text. |
| 149 | + |
| 150 | +### Step 4: Apply Headers |
| 151 | + |
| 152 | +Run the tool to apply correct headers: |
| 153 | + |
| 154 | +```bash |
| 155 | +# First, do a dry-run to verify changes |
| 156 | +python tools/fixheaders/fixheaders.py replace . --config tools/fixheaders/<project_name>.yaml --dry-run |
| 157 | +
|
| 158 | +# If everything looks correct, apply the changes |
| 159 | +python tools/fixheaders/fixheaders.py replace . --config tools/fixheaders/<project_name>.yaml |
| 160 | +``` |
| 161 | + |
| 162 | +### Step 5: Verify |
| 163 | + |
| 164 | +Run a final check to ensure all files have correct headers: |
| 165 | + |
| 166 | +```bash |
| 167 | +python tools/fixheaders/fixheaders.py check . |
| 168 | +``` |
| 169 | + |
| 170 | +## Example: Adding PyTorch-Derived Code |
| 171 | + |
| 172 | +For files derived from PyTorch: |
| 173 | + |
| 174 | +```yaml |
| 175 | +# filepath: tools/fixheaders/pytorch.yaml |
| 176 | +custom_headers: |
| 177 | + - header: | |
| 178 | + Copyright (c) 2025 Intel Corporation |
| 179 | + SPDX-License-Identifier: Apache-2.0 |
| 180 | +
|
| 181 | + Portions of this file are derived from PyTorch |
| 182 | + Copyright (c) Meta Platforms, Inc. and affiliates. |
| 183 | + SPDX-License-Identifier: BSD-3-Clause |
| 184 | + files: |
| 185 | + - "test/xpu/test_modules_xpu.py" |
| 186 | + - "src/derived_from_pytorch/*.cpp" |
| 187 | +``` |
| 188 | + |
| 189 | +## Troubleshooting |
| 190 | + |
| 191 | +### File shows as needing replacement but content looks correct |
| 192 | + |
| 193 | +The tool normalizes headers for comparison, ignoring whitespace differences. However, if you still see false positives: |
| 194 | + |
| 195 | +1. Check for trailing whitespace in your YAML file |
| 196 | +2. Ensure the header text in YAML exactly matches what should be in the file |
| 197 | +3. Use `--verbose` flag to see detailed comparison information |
| 198 | + |
| 199 | +### File type not recognized |
| 200 | + |
| 201 | +Add the extension to `FILE_TYPE_MAP` in `fixheaders.py`: |
| 202 | + |
| 203 | +```python |
| 204 | +FILE_TYPE_MAP = { |
| 205 | + # ... existing entries ... |
| 206 | + ".new_ext": "cpp", # Use appropriate comment style |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | +### Excluding files from processing |
| 211 | + |
| 212 | +Add patterns to the `exclude` section in your YAML config: |
| 213 | + |
| 214 | +```yaml |
| 215 | +exclude: |
| 216 | + - "vendor/*" # Exclude vendor directory |
| 217 | + - "**/*.generated.py" # Exclude generated files |
| 218 | + - "specific/file.cpp" # Exclude specific file |
| 219 | +``` |
| 220 | + |
| 221 | +## Exit Codes |
| 222 | + |
| 223 | +| Code | Meaning | |
| 224 | +|------|---------| |
| 225 | +| 0 | Success (all files OK or changes applied) | |
| 226 | +| 1 | Check mode: some files have incorrect/missing headers | |
| 227 | + |
| 228 | +## Contributing |
| 229 | + |
| 230 | +When adding support for new file types or features: |
| 231 | + |
| 232 | +1. Update `COMMENT_STYLES` for new comment syntax |
| 233 | +2. Update `FILE_TYPE_MAP` for new extensions |
| 234 | +3. Add tests for the new functionality |
| 235 | +4. Update this README with new information |
0 commit comments