Skip to content

Commit

Permalink
fix: set up automatic license header for source files
Browse files Browse the repository at this point in the history
  • Loading branch information
vschaffn committed Nov 15, 2024
1 parent 429c4c9 commit 6dd4f84
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,12 @@ repos:
files: ^(environment.yml|requirements.txt)$
pass_filenames: false
additional_dependencies: [tomli, pyyaml]

# Add license header to the source files
- repo: local
hooks:
- id: add-license_header
name: Add License Header
entry: python apply_license_header.py
language: python
files: \.py$
39 changes: 39 additions & 0 deletions apply_license_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os

# Path to the license header
HEADER_FILE = "license-header.txt"

# read license header
with open(HEADER_FILE) as file:
license_header = file.read()


# Add license header to a file
def add_license_header(file_path, header):
with open(file_path) as f:
content = f.read()

# Check if the header is already there
if content.startswith(header):
return

# If not, add it
with open(file_path, "w") as f:
f.write(header + "\n" + content)
print(f"Header added to {file_path}")


# Check the header in every file in root_dir
def apply_license_header_to_all_py_files(root_dir):
for subdir, _, files in os.walk(root_dir):
for file in files:
if file.endswith(".py"):
file_path = os.path.join(subdir, file)
add_license_header(file_path, license_header)


# Source repertory
PROJECT_SRC = "xdem"

# Add header to every source files
apply_license_header_to_all_py_files(PROJECT_SRC)
16 changes: 16 additions & 0 deletions license-header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2024 xDEM developers
#
# This file is part of xDEM project:
# https://github.com/glaciohack/xdem
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

0 comments on commit 6dd4f84

Please sign in to comment.