Skip to content

Commit

Permalink
Merge branch 'dev' into ci/parallel-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvlecl authored Sep 3, 2024
2 parents 332b7be + 81780e3 commit 8898a2c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 84 deletions.
21 changes: 21 additions & 0 deletions antarest/core/utils/fastapi_sqlalchemy/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Michael Freeborn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 2 additions & 1 deletion antarest/core/utils/fastapi_sqlalchemy/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# FastAPI-SQLAlchemy

Forked from https://github.com/mfreeborn/fastapi-sqlalchemy
Forked from https://github.com/mfreeborn/fastapi-sqlalchemy,
licensed under MIT license, see [LICENSE](LICENSE).
12 changes: 0 additions & 12 deletions antarest/core/utils/fastapi_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
# 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 antarest.core.utils.fastapi_sqlalchemy.middleware import DBSessionMiddleware, db

__all__ = ["db", "DBSessionMiddleware"]
Expand Down
13 changes: 0 additions & 13 deletions antarest/core/utils/fastapi_sqlalchemy/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# 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.


class MissingSessionError(Exception):
"""Exception raised for when the user tries to access a database session before it is created."""

Expand Down
12 changes: 0 additions & 12 deletions antarest/core/utils/fastapi_sqlalchemy/middleware.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
# 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 contextvars import ContextVar, Token
from typing import Any, Dict, Optional, Type, Union

Expand Down
1 change: 0 additions & 1 deletion antarest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# This file is part of the Antares project.

import datetime
import logging
from enum import Enum
from pathlib import Path
Expand Down
117 changes: 72 additions & 45 deletions scripts/license_checker_and_adder.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,82 @@
import glob
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.*")

ANTARES_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.
"""

LICENSE_AS_LIST = ANTARES_LICENSE_HEADER.splitlines()
LICENSE_TO_SAVE = [header + "\n" for header in LICENSE_AS_LIST] + ["\n"]


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()
if len(file_content) >= 11 and file_content[:11] == 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.suffixes != [".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),
type=click.Path(exists=True, path_type=Path),
help="Path to check",
)
@click.option(
Expand All @@ -25,49 +91,10 @@ 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}'")

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.
"""
license_as_list = license_header.splitlines()
license_to_save = [header + "\n" for header in license_as_list]
license_to_save.append("\n")

file_count = 0
all_files_and_folder = glob.glob(os.path.join(path, "**"), recursive=True)
for file in all_files_and_folder:
file_path = Path(file)
if not file_path.is_file():
continue
if file_path.suffixes != [".py"]:
continue
file_content = file_path.read_text().splitlines()
if len(file_content) < 11 or file_content[:11] != license_as_list:
file_count += 1
click.echo(f"{file_path.relative_to(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.relative_to(path)} already licensed.")
else:
new_lines = license_to_save + lines
if new_lines:
with open(file_path, "w") as f:
f.writelines(new_lines)
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")
Expand Down

0 comments on commit 8898a2c

Please sign in to comment.