Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix licensing-related issues #2132

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion antarest/eventbus/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import List, Optional

from fastapi import Depends, FastAPI, HTTPException, Query
from fastapi_jwt_auth import AuthJWT # type: ignore
from pydantic import BaseModel
from starlette.websockets import WebSocket, WebSocketDisconnect

Expand All @@ -27,7 +28,6 @@
from antarest.core.model import PermissionInfo, StudyPermissionType
from antarest.core.permissions import check_permission
from antarest.login.auth import Auth
from fastapi_jwt_auth import AuthJWT # type: ignore

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion antarest/login/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
from typing import Any, Callable, Coroutine, Dict, Optional, Tuple, Union

from fastapi import Depends
from fastapi_jwt_auth import AuthJWT # type: ignore
from pydantic import BaseModel
from ratelimit.types import Scope # type: ignore
from starlette.requests import Request

from antarest.core.config import Config
from antarest.core.jwt import DEFAULT_ADMIN_USER, JWTUser
from fastapi_jwt_auth import AuthJWT # type: ignore

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion antarest/login/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import Any, Optional

from fastapi import FastAPI
from fastapi_jwt_auth import AuthJWT # type: ignore
from fastapi_jwt_auth.exceptions import AuthJWTException # type: ignore
from starlette.requests import Request
from starlette.responses import JSONResponse
Expand All @@ -26,7 +27,6 @@
from antarest.login.repository import BotRepository, GroupRepository, RoleRepository, UserLdapRepository, UserRepository
from antarest.login.service import LoginService
from antarest.login.web import create_login_api
from fastapi_jwt_auth import AuthJWT # type: ignore


def build_login(
Expand Down
2 changes: 1 addition & 1 deletion antarest/login/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import Any, List, Optional, Union

from fastapi import APIRouter, Depends, HTTPException
from fastapi_jwt_auth import AuthJWT # type: ignore
from markupsafe import escape
from pydantic import BaseModel

Expand All @@ -41,7 +42,6 @@
UserInfo,
)
from antarest.login.service import LoginService
from fastapi_jwt_auth import AuthJWT # type: ignore

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion antarest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from fastapi import FastAPI, HTTPException
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi_jwt_auth import AuthJWT # type: ignore
from ratelimit import RateLimitMiddleware # type: ignore
from ratelimit.backends.redis import RedisBackend # type: ignore
from ratelimit.backends.simple import MemoryBackend # type: ignore
Expand Down Expand Up @@ -53,7 +54,6 @@
from antarest.study.storage.rawstudy.watcher import Watcher
from antarest.tools.admin_lib import clean_locks
from antarest.utils import SESSION_ARGS, Module, create_services, init_db_engine
from fastapi_jwt_auth import AuthJWT # type: ignore

logger = logging.getLogger(__name__)

Expand Down
3 changes: 1 addition & 2 deletions 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 All @@ -20,6 +19,7 @@
import sqlalchemy.ext.baked # type: ignore
import uvicorn # type: ignore
from fastapi import FastAPI
from fastapi_jwt_auth import AuthJWT # type: ignore
from ratelimit import RateLimitMiddleware # type: ignore
from ratelimit.backends.redis import RedisBackend # type: ignore
from ratelimit.backends.simple import MemoryBackend # type: ignore
Expand Down Expand Up @@ -53,7 +53,6 @@
from antarest.worker.archive_worker import ArchiveWorker
from antarest.worker.simulator_worker import SimulatorWorker
from antarest.worker.worker import AbstractWorker
from fastapi_jwt_auth import AuthJWT # type: ignore

logger = logging.getLogger(__name__)

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
Loading