Skip to content

Commit 56b8c3a

Browse files
WillemJiangCopilot
andauthored
feat(tool): Adding license header check and apply tool (#857)
* feat(tool): Adding license header check and apply tool * Update docs/LICENSE_HEADERS.md Co-authored-by: Copilot <[email protected]> * Update scripts/license_header.py Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent ba45c1a commit 56b8c3a

6 files changed

Lines changed: 475 additions & 1 deletion

File tree

LICENSE_HEADER

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
2+
# SPDX-License-Identifier: MIT

LICENSE_HEADER_TS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
2+
// SPDX-License-Identifier: MIT

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: help lint format install-dev serve test coverage langgraph-dev lint-frontend
1+
.PHONY: help lint format install-dev serve test coverage langgraph-dev lint-frontend add-license add-license-ts add-license-all check-license check-license-ts check-license-all
22

33
help: ## Show this help message
44
@echo "Deer Flow - Available Make Targets:"
@@ -34,3 +34,11 @@ langgraph-dev: ## Start langgraph development server
3434

3535
coverage: ## Run tests with coverage report
3636
uv run pytest --cov=src tests/ --cov-report=term-missing --cov-report=xml
37+
38+
add-license-all: ## Add license headers to all Python and TypeScript files
39+
@echo "Adding license headers to all source files..."
40+
@uv run python scripts/license_header.py src/ tests/ server.py main.py web/src/ web/tests/ --verbose
41+
42+
check-license-all: ## Check if all Python and TypeScript files have license headers
43+
@echo "Checking license headers in all source files..."
44+
@uv run python scripts/license_header.py src/ tests/ server.py main.py web/src/ web/tests/ --check

docs/LICENSE_HEADERS.md

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# License Header Management
2+
3+
This document explains how to manage license headers in the DeerFlow project.
4+
5+
## License Header Format
6+
7+
All source files in this project should include license headers.
8+
9+
### Python Files
10+
11+
```python
12+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
13+
# SPDX-License-Identifier: MIT
14+
```
15+
16+
For files with a shebang (`#!/usr/bin/env python3`), the header is placed after the shebang:
17+
18+
```python
19+
#!/usr/bin/env python3
20+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
21+
# SPDX-License-Identifier: MIT
22+
23+
import something
24+
```
25+
26+
### TypeScript Files
27+
28+
```typescript
29+
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
30+
// SPDX-License-Identifier: MIT
31+
32+
import { something } from "somewhere";
33+
```
34+
35+
## Makefile Targets
36+
37+
### Check License Headers
38+
39+
Check if all Python and TypeScript files have the required license header:
40+
41+
```bash
42+
# Check all files (Python and TypeScript)
43+
make check-license-all
44+
45+
# Check only Python files
46+
make check-license
47+
48+
# Check only TypeScript files
49+
make check-license-ts
50+
```
51+
52+
These commands:
53+
- Scan all source files in `src/`, `tests/`, `web/src/`, `web/tests/`, and root-level files
54+
- Report files missing the license header
55+
- Return exit code 1 if any files are missing headers (useful for CI/CD)
56+
- Return exit code 0 if all files have headers
57+
58+
### Add License Headers
59+
60+
Automatically add license headers to files that don't have them:
61+
62+
```bash
63+
# Add to all files (Python and TypeScript)
64+
make add-license-all
65+
66+
# Add only to Python files
67+
make add-license
68+
69+
# Add only to TypeScript files
70+
make add-license-ts
71+
```
72+
73+
These commands:
74+
- Add the appropriate license header to files that don't have it
75+
- Preserve shebangs at the top of Python files
76+
- Add appropriate spacing after headers
77+
- Show vTypeScript files
78+
uv run python scripts/license_header.py web/src/components/ --check
79+
80+
# Check a single file (works for both .py and .ts/.tsx)
81+
uv run python scripts/license_header.py src/workflow.py --check
82+
uv run python scripts/license_header.py web/src/core/api/chat.ts --check
83+
```
84+
85+
### Script Options
86+
87+
- `--check`: Check mode - verify headers without modifying files
88+
- `--verbose` / `-v`: Show detailed output for each file processed
89+
- `paths`: One or more paths (files or directories) to process
90+
91+
### Supported File Types
92+
93+
The script automatically detects and processes:
94+
- Python files (`.py`)
95+
- TypeScript files (`.ts`)
96+
- TypeScript React files (`.tsx`)
97+
98+
## Pre-commit Hook
99+
100+
The license header check is integrated into the pre-commit hook. Before allowing a commit, it will:
101+
102+
1. Run linting (`make lint`)
103+
2. Run formatting (`make format`)
104+
105+
This ensures all merged code has proper license headers for both Python and TypeScript fileill be blocked. Run `make add-license` to fix.
106+
107+
## CI/CD Integration
108+
109+
For continuous integration, add the license check to your workflow:
110+
111+
```bash
112+
# In your CI script or GitHub Actions
113+
ma`.next` (Next.js build directory)
114+
- ke check-license
115+
```
116+
117+
This ensures all merged code has proper license headers.
118+
119+
## Files Excluded
120+
121+
The license header tool automatically skips:
122+
- `__pycache__` directories
123+
- `.pytest_cache`, `.ruff_cache`, `.mypy_cache`
124+
- `node_modules`
125+
- Virtual environment directories (`.venv`, `venv`, `.tox`)
126+
- Build artifacts (`build`, `dist`)
127+
- `.git` directory
128+
129+
## Customization
130+
131+
### Changing the License Header
132+
S` dictionary in `scripts/license_header.py`:
133+
134+
```python
135+
LICENSE_HEADERS: Dict[str, str] = {
136+
"python": """# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
137+
# SPDX-License-Identifier: MIT
138+
""",
139+
"typescript": """// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
140+
// SPDX-License-Identifier: MIT
141+
""",
142+
}
143+
```
144+
145+
### Adding Mo-all:
146+
@uv run python scripts/license_header.py src/ tests/ scripts/ web/src/ web/test
147+
1. Add the extension to `FILE_TYPE_MAP` in `scripts/license_header.py`
148+
2. Add the corresponding header format to `LICENSE_HEADERS`
149+
150+
```python
151+
FILE_TYPE_MAP = {
152+
".py": "python",
153+
".ts": "typescript",
154+
".tsx": "typescript",
155+
".js": "javascript", # Example: adding JavaScript support
156+
}
157+
158+
LICENSE_HEADERS = {
159+
# ... existing headers ...
160+
"javascript": """// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
161+
// SPDX-License-Identifier: MIT
162+
""",
163+
}PDX-License-Identifier: MIT
164+
"""
165+
```
166+
-all
167+
Checking license headers in all source files...
168+
✅ All 289 source file(s) have license headers.
169+
```
170+
171+
### Example 2: Check Only TypeScript Files
172+
```bash
173+
$ make check-license-ts
174+
Checking license headers in TypeScript files...
175+
❌ 3 file(s) missing license header:
176+
- web/src/components/new-component.tsx
177+
- web/src/core/api/new-api.ts
178+
- web/tests/new-test.test.ts
179+
180+
Run 'make add-license-ts' to add headers.
181+
```
182+
183+
### Example 3: Add Headers to New Module
184+
```bash
185+
$ make add-license-all
186+
Adding license headers to all source files...
187+
✅ Added license header to 11 file(s).
188+
```
189+
190+
### Example 4: Check Specific Directory
191+
```bash
192+
$ uv run python scripts/license_header.py web/src/components/ --check --verbose
193+
Header already present: web/src/components/deer-flow/logo.tsx
194+
Header already present: web/src/components/deer-flow/markdown.tsx
195+
Header already present: web/src/components/editor/index.tsx
196+
✅ All 24 sourceooks for exact matches (ignoring leading/trailing whitespace)
197+
198+
### "Pre-commit hook blocks my commit"
199+
- Run `make add-license` to add headers to all files
200+
- Or disable the check temporarily by editing the `pre-commit` file
201+
202+
## Examples
203+
204+
### Example 1: Check All Files
205+
```bash
206+
$ make check-license
207+
Checking license headers in Python files...
208+
✅ All 156 Python file(s) have license headers.
209+
```
210+
211+
### Example 2: Add Headers to New Module
212+
```bash
213+
$ make add-license
214+
Adding license headers to Python files...
215+
✅ Added license header to 11 file(s).
216+
```
217+
218+
### Example 3: Check Specific Directory
219+
```bash
220+
$ uv run python scripts/license_header.py src/agents/ --check --verbose
221+
Header already present: src/agents/base.py
222+
Header already present: src/agents/coordinator.py
223+
✅ All 8 Python file(s) have license headers.
224+
```

pre-commit

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ if [ $FORMAT_RESULT -ne 0 ]; then
2020
exit 1
2121
fi
2222

23+
# Check license headers
24+
echo "Checking license headers..."
25+
make check-license-all
26+
LICENSE_RESULT=$?
27+
28+
if [ $LICENSE_RESULT -ne 0 ]; then
29+
echo "❌ Some files are missing license headers."
30+
echo "Run 'make add-license-all' to add them automatically."
31+
exit 1
32+
fi
33+
2334
# If any files were reformatted, add them back to staging
2435
git diff --name-only | xargs -I {} git add "{}"
2536

0 commit comments

Comments
 (0)