Skip to content

Commit

Permalink
chore(license): add license header and gh action to check it
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Sep 20, 2024
1 parent 75e9870 commit c4f39f9
Show file tree
Hide file tree
Showing 31 changed files with 478 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/workflows/license_header.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: check license headers
on:
push:
branches:
- "**"

jobs:
check-license-headers:
runs-on: ubuntu-20.04
steps:
- name: Checkout github repo (+ download lfs dependencies)
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install click
- name: Check licenses header
run: |
python license_checker_and_adder.py --path=../src/ --action=check-strict
python license_checker_and_adder.py --path=../tests/ --action=check-strict
working-directory: scripts
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pandas-stubs ~=2.2.2
pytest~=7.2.1
python-dateutil~=2.9.0
pydantic==2.7.1
configparser~=5.0.2
configparser~=5.0.2
click~=8.1.7
115 changes: 115 additions & 0 deletions scripts/license_checker_and_adder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import os
import re
from pathlib import Path
from typing import List

import click

# Use to skip subtrees that have their own licenses (forks)
LICENSE_FILE_PATTERN = re.compile("LICENSE.*")

LICENSE_HEADER = """# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.
"""


def is_license_file(filename: str) -> bool:
return LICENSE_FILE_PATTERN.match(filename) is not None


def check_file(file_path: Path, action: str) -> bool:
file_content = file_path.read_text().splitlines()
license_as_list = LICENSE_HEADER.splitlines()
license_to_save = [header + "\n" for header in license_as_list] + ["\n"]
n = len(license_as_list)
if len(file_content) >= n and file_content[:n] == license_as_list:
return True
click.echo(f"{file_path} has no valid header.")
new_lines = []
if action == "fix":
with open(file_path, "r") as f: # doesn't seem really optimal as I read the file twice.
already_licensed = False
lines = f.readlines()
first_line = lines[0].lower() if len(lines) > 0 else []
if "copyright" in first_line or "license" in first_line: # assumes license follows this
already_licensed = True
if already_licensed: # I don't really know what to do here
raise ValueError(f"File {file_path} already licensed.")
else:
new_lines = license_to_save + lines
if new_lines:
with open(file_path, "w") as f:
f.writelines(new_lines)


def check_dir(cwd: Path, dir_path: Path, action: str, invalid_files: List[Path]) -> None:
_, dirnames, filenames = next(os.walk(dir_path))
for f in filenames:
if dir_path != cwd and is_license_file(f):
click.echo(f"Found third party license file, skipping folder: {dir_path / f}")
return

for f in filenames:
file_path = dir_path / f

if file_path.suffix not in [".py"]:
continue

if not check_file(file_path, action):
invalid_files.append(file_path)

for d in dirnames:
check_dir(cwd, dir_path / d, action, invalid_files)


@click.command("license_checker_and_adder")
@click.option(
"--path",
nargs=1,
required=True,
type=click.Path(exists=True, path_type=Path),
help="Path to check",
)
@click.option(
"--action",
nargs=1,
required=False,
default="check",
type=str,
help="Action to realise. Can either be check or fix",
)
def cli(path: Path, action: str) -> None:
if action not in ["check", "check-strict", "fix"]:
raise ValueError(f"Parameter --action should be 'check', 'check-strict' or 'fix' and was '{action}'")

invalid_files = []
cwd = Path.cwd()
check_dir(cwd, path, action, invalid_files)
file_count = len(invalid_files)
if file_count > 0:
if action == "fix":
click.echo(f"{file_count} files have been fixed")
else:
click.echo(f"{file_count} files have an invalid header. Use --action=fix to fix them")
if action == "check-strict":
raise ValueError("Some files have invalid headers")

else:
click.echo("All good !")


def main():
cli(prog_name="cli")


if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions src/antares/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

12 changes: 12 additions & 0 deletions src/antares/api_conf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

12 changes: 12 additions & 0 deletions src/antares/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

12 changes: 12 additions & 0 deletions src/antares/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

12 changes: 12 additions & 0 deletions src/antares/model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

12 changes: 12 additions & 0 deletions src/antares/model/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from typing import Optional, Dict

from pydantic import BaseModel
Expand Down
12 changes: 12 additions & 0 deletions src/antares/service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

12 changes: 12 additions & 0 deletions src/antares/service/api_services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

12 changes: 12 additions & 0 deletions src/antares/service/local_services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

12 changes: 12 additions & 0 deletions src/antares/service/local_services/binding_constraint_local.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from typing import Optional, List, Any

import pandas as pd
Expand Down
12 changes: 12 additions & 0 deletions src/antares/service/local_services/link_local.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

import configparser
import os
from types import MappingProxyType
Expand Down
12 changes: 12 additions & 0 deletions src/antares/service/local_services/renewable_local.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from typing import Any

import pandas as pd
Expand Down
12 changes: 12 additions & 0 deletions src/antares/service/local_services/st_storage_local.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from typing import Any

from antares.config.local_configuration import LocalConfiguration
Expand Down
12 changes: 12 additions & 0 deletions src/antares/service/local_services/study_local.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from typing import Optional, Any

from antares.config.local_configuration import LocalConfiguration
Expand Down
12 changes: 12 additions & 0 deletions src/antares/service/local_services/thermal_local.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from typing import Any

import pandas as pd
Expand Down
12 changes: 12 additions & 0 deletions src/antares/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

12 changes: 12 additions & 0 deletions src/antares/tools/prepro_folder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from pathlib import Path

import numpy as np
Expand Down
Loading

0 comments on commit c4f39f9

Please sign in to comment.