diff --git a/antarest/core/utils/fastapi_sqlalchemy/LICENSE b/antarest/core/utils/fastapi_sqlalchemy/LICENSE new file mode 100644 index 0000000000..577fc21f6c --- /dev/null +++ b/antarest/core/utils/fastapi_sqlalchemy/LICENSE @@ -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. diff --git a/antarest/core/utils/fastapi_sqlalchemy/README.md b/antarest/core/utils/fastapi_sqlalchemy/README.md index 78c02a32a5..fae88f5abc 100644 --- a/antarest/core/utils/fastapi_sqlalchemy/README.md +++ b/antarest/core/utils/fastapi_sqlalchemy/README.md @@ -1,3 +1,4 @@ # FastAPI-SQLAlchemy -Forked from https://github.com/mfreeborn/fastapi-sqlalchemy \ No newline at end of file +Forked from https://github.com/mfreeborn/fastapi-sqlalchemy, +licensed under MIT license, see [LICENSE](LICENSE). diff --git a/antarest/core/utils/fastapi_sqlalchemy/__init__.py b/antarest/core/utils/fastapi_sqlalchemy/__init__.py index 66c4a96541..cdc6aaaaf5 100644 --- a/antarest/core/utils/fastapi_sqlalchemy/__init__.py +++ b/antarest/core/utils/fastapi_sqlalchemy/__init__.py @@ -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"] diff --git a/antarest/core/utils/fastapi_sqlalchemy/exceptions.py b/antarest/core/utils/fastapi_sqlalchemy/exceptions.py index 56f06f4c22..ad1eccff2c 100644 --- a/antarest/core/utils/fastapi_sqlalchemy/exceptions.py +++ b/antarest/core/utils/fastapi_sqlalchemy/exceptions.py @@ -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.""" diff --git a/antarest/core/utils/fastapi_sqlalchemy/middleware.py b/antarest/core/utils/fastapi_sqlalchemy/middleware.py index 89426d55a9..9a98b4ef1b 100644 --- a/antarest/core/utils/fastapi_sqlalchemy/middleware.py +++ b/antarest/core/utils/fastapi_sqlalchemy/middleware.py @@ -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 diff --git a/antarest/utils.py b/antarest/utils.py index 8c9b0dea70..eb2e5dc479 100644 --- a/antarest/utils.py +++ b/antarest/utils.py @@ -10,7 +10,6 @@ # # This file is part of the Antares project. -import datetime import logging from enum import Enum from pathlib import Path diff --git a/scripts/license_checker_and_adder.py b/scripts/license_checker_and_adder.py old mode 100644 new mode 100755 index e5ac2828ec..2eb388a20f --- a/scripts/license_checker_and_adder.py +++ b/scripts/license_checker_and_adder.py @@ -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( @@ -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")