diff --git a/.github/workflows/backend-QA.yaml b/.github/workflows/backend-QA.yaml new file mode 100644 index 0000000..c1a0e0f --- /dev/null +++ b/.github/workflows/backend-QA.yaml @@ -0,0 +1,38 @@ +name: Backend QA + +on: + pull_request: + push: + branches: + - main + +jobs: + check-qa: + runs-on: ubuntu-22.04 + + steps: + - uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version-file: backend/pyproject.toml + architecture: x64 + + - name: Install dependencies (and project) + working-directory: backend + run: | + pip install -U pip + pip install -e .[lint,scripts,test,check] + + - name: Check black formatting + working-directory: backend + run: inv lint-black + + - name: Check ruff + working-directory: backend + run: inv lint-ruff + + - name: Check pyright + working-directory: backend + run: inv check-pyright diff --git a/.github/workflows/worker-QA.yaml b/.github/workflows/worker-QA.yaml new file mode 100644 index 0000000..0544c43 --- /dev/null +++ b/.github/workflows/worker-QA.yaml @@ -0,0 +1,38 @@ +name: Worker QA + +on: + pull_request: + push: + branches: + - main + +jobs: + check-qa: + runs-on: ubuntu-22.04 + + steps: + - uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version-file: worker/pyproject.toml + architecture: x64 + + - name: Install dependencies (and project) + working-directory: worker + run: | + pip install -U pip + pip install -e .[lint,scripts,test,check] + + - name: Check black formatting + working-directory: worker + run: inv lint-black + + - name: Check ruff + working-directory: worker + run: inv lint-ruff + + - name: Check pyright + working-directory: worker + run: inv check-pyright diff --git a/.gitignore b/.gitignore index 68bc17f..c366b7b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Editors +*.vim + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] @@ -25,6 +28,7 @@ share/python-wheels/ .installed.cfg *.egg MANIFEST +.python-version # PyInstaller # Usually these files are written by a python script from a template diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..28b5abc --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,27 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer +- repo: https://github.com/psf/black + rev: "24.1.1" + hooks: + - id: black +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.2.0 + hooks: + - id: ruff +- repo: https://github.com/RobertCraigie/pyright-python + rev: v1.1.349 + hooks: + - id: pyright + name: pyright (system) + description: 'pyright static type checker' + entry: pyright + language: system + 'types_or': [python, pyi] + require_serial: true + minimum_pre_commit_version: '2.9.2' diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..e69de29 diff --git a/backend/README.md b/backend/README.md new file mode 100644 index 0000000..894aee4 --- /dev/null +++ b/backend/README.md @@ -0,0 +1 @@ +# Mirrors-QA diff --git a/backend/alembic.ini b/backend/alembic.ini new file mode 100644 index 0000000..daef88c --- /dev/null +++ b/backend/alembic.ini @@ -0,0 +1,113 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = src/backend/migrations + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python>=3.9 or backports.zoneinfo library. +# Any required deps can installed by adding `alembic[tz]` to the pip requirements +# string value is passed to ZoneInfo() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to src/backend/migirations/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:src/backend/migirations/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# set to 'true' to search source files recursively +# in each "version_locations" directory +# new in Alembic version 1.10 +# recursive_version_locations = false + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = driver://user:pass@localhost/dbname + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +hooks = black ruff +black.type = console_scripts +black.entrypoint = black +black.options = REVISION_SCRIPT_FILENAME +ruff.type = exec +ruff.executable = ruff +ruff.options = format REVISION_SCRIPT_FILENAME + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/backend/pyproject.toml b/backend/pyproject.toml new file mode 100644 index 0000000..5aefed1 --- /dev/null +++ b/backend/pyproject.toml @@ -0,0 +1,234 @@ +[build-system] +requires = ["hatchling", "hatch-openzim"] +build-backend = "hatchling.build" + +[project] +name = "mirrors_qa_backend" +requires-python = ">=3.12,<3.13" +description = "mirrors-qa Backend API" +readme = "README.md" +authors = [ + { name = "Kiwix", email = "dev@kiwix.org" }, +] +keywords = ["mirrors"] +dependencies = [ + "alembic == 1.13.1", + "fastapi[all] == 0.111.0", + "pydantic == 2.7.2", + "SQLAlchemy == 2.0.30", + "psycopg[binary,pool] == 3.1.19", +] +license = {text = "GPL-3.0-or-later"} +classifiers = [ + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.12", + "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", +] + +dynamic = ["version"] + +[project.urls] +Homepage = "https://github.com/kiwix/mirrors-qa" + +[project.optional-dependencies] +scripts = [ + "invoke==2.2.0", +] +lint = [ + "black==24.1.1", + "ruff==0.2.0", +] +check = [ + "pyright==1.1.349", +] +test = [ + "pytest==8.0.0", + "coverage==7.4.1", +] +dev = [ + "pre-commit==3.6.0", + "debugpy==1.8.0", + "mirrors_qa_backend[scripts]", + "mirrors_qa_backend[lint]", + "mirrors_qa_backend[test]", + "mirrors_qa_backend[check]", +] + +[project.scripts] +mirrors-qa-backend = "backend:entrypoint" + +[tool.hatch.version] +path = "src/backend/__about__.py" + +[tool.hatch.build] +exclude = [ + "/.github", +] + +[tool.hatch.build.targets.wheel] +packages = ["src/backend"] + +[tool.hatch.envs.default] +features = ["dev"] + +[tool.hatch.envs.test] +features = ["scripts", "test"] + +[tool.hatch.envs.test.scripts] +run = "inv test --args '{args}'" +run-cov = "inv test-cov --args '{args}'" +report-cov = "inv report-cov" +coverage = "inv coverage --args '{args}'" +html = "inv coverage --html --args '{args}'" + +[tool.hatch.envs.lint] +template = "lint" +skip-install = false +features = ["scripts", "lint"] + +[tool.hatch.envs.lint.scripts] +black = "inv lint-black --args '{args}'" +ruff = "inv lint-ruff --args '{args}'" +all = "inv lintall --args '{args}'" +fix-black = "inv fix-black --args '{args}'" +fix-ruff = "inv fix-ruff --args '{args}'" +fixall = "inv fixall --args '{args}'" + +[tool.hatch.envs.check] +features = ["scripts", "check"] + +[tool.hatch.envs.check.scripts] +pyright = "inv check-pyright --args '{args}'" +all = "inv checkall --args '{args}'" + +[tool.black] +line-length = 88 +target-version = ['py312'] + +[tool.ruff] +target-version = "py312" +line-length = 88 +src = ["src"] + +[tool.ruff.lint] +select = [ + "A", # flake8-builtins + # "ANN", # flake8-annotations + "ARG", # flake8-unused-arguments + # "ASYNC", # flake8-async + "B", # flake8-bugbear + # "BLE", # flake8-blind-except + "C4", # flake8-comprehensions + "C90", # mccabe + # "COM", # flake8-commas + # "D", # pydocstyle + # "DJ", # flake8-django + "DTZ", # flake8-datetimez + "E", # pycodestyle (default) + "EM", # flake8-errmsg + # "ERA", # eradicate + # "EXE", # flake8-executable + "F", # Pyflakes (default) + # "FA", # flake8-future-annotations + "FBT", # flake8-boolean-trap + # "FLY", # flynt + # "G", # flake8-logging-format + "I", # isort + "ICN", # flake8-import-conventions + # "INP", # flake8-no-pep420 + # "INT", # flake8-gettext + "ISC", # flake8-implicit-str-concat + "N", # pep8-naming + # "NPY", # NumPy-specific rules + # "PD", # pandas-vet + # "PGH", # pygrep-hooks + # "PIE", # flake8-pie + # "PL", # Pylint + "PLC", # Pylint: Convention + "PLE", # Pylint: Error + "PLR", # Pylint: Refactor + "PLW", # Pylint: Warning + # "PT", # flake8-pytest-style + # "PTH", # flake8-use-pathlib + # "PYI", # flake8-pyi + "Q", # flake8-quotes + # "RET", # flake8-return + # "RSE", # flake8-raise + "RUF", # Ruff-specific rules + "S", # flake8-bandit + # "SIM", # flake8-simplify + # "SLF", # flake8-self + "T10", # flake8-debugger + "T20", # flake8-print + # "TCH", # flake8-type-checking + # "TD", # flake8-todos + "TID", # flake8-tidy-imports + # "TRY", # tryceratops + "UP", # pyupgrade + "W", # pycodestyle + "YTT", # flake8-2020 +] +ignore = [ + # Allow non-abstract empty methods in abstract base classes + "B027", + # Remove flake8-errmsg since we consider they bloat the code and provide limited value + "EM", + # Allow boolean positional values in function calls, like `dict.get(... True)` + "FBT003", + # Ignore checks for possible passwords + "S105", "S106", "S107", + # Ignore warnings on subprocess.run / popen + "S603", + # Ignore complexity + "C901", "PLR0911", "PLR0912", "PLR0913", "PLR0915", +] +unfixable = [ + # Don't touch unused imports + "F401", +] + +[tool.ruff.lint.isort] +known-first-party = ["backend"] + +[tool.ruff.lint.flake8-bugbear] +# add exceptions to B008 for fastapi. +extend-immutable-calls = ["fastapi.Depends", "fastapi.Query"] + +[tool.ruff.lint.flake8-tidy-imports] +ban-relative-imports = "all" + +[tool.ruff.lint.per-file-ignores] +# Tests can use magic values, assertions, and relative imports +"tests/**/*" = ["PLR2004", "S101", "TID252"] + +[tool.pytest.ini_options] +minversion = "7.3" +testpaths = ["tests"] +pythonpath = [".", "src"] + +[tool.coverage.paths] +backend = ["src/backend"] +tests = ["tests"] + +[tool.coverage.run] +source_pkgs = ["backend"] +branch = true +parallel = true +omit = [ + "src/backend/__about__.py", +] + +[tool.coverage.report] +exclude_lines = [ + "no cov", + "if __name__ == .__main__.:", + "if TYPE_CHECKING:", +] + +[tool.pyright] +include = ["src", "tests", "tasks.py"] +exclude = [".env/**", ".venv/**"] +extraPaths = ["src"] +pythonVersion = "3.12" +typeCheckingMode="strict" +disableBytesTypePromotions = true diff --git a/backend/src/backend/__about__.py b/backend/src/backend/__about__.py new file mode 100644 index 0000000..a237a3e --- /dev/null +++ b/backend/src/backend/__about__.py @@ -0,0 +1 @@ +__version__ = "0.0.1-dev0" diff --git a/backend/src/backend/__init__.py b/backend/src/backend/__init__.py new file mode 100644 index 0000000..fa4d8e1 --- /dev/null +++ b/backend/src/backend/__init__.py @@ -0,0 +1,5 @@ +from backend.__about__ import __version__ + + +def entrypoint(): + print(f"Hello from backend:{__version__}") # noqa: T201 diff --git a/backend/src/backend/db/__init__.py b/backend/src/backend/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/src/backend/db/models.py b/backend/src/backend/db/models.py new file mode 100644 index 0000000..25883c2 --- /dev/null +++ b/backend/src/backend/db/models.py @@ -0,0 +1,130 @@ +from __future__ import annotations + +from datetime import datetime +from ipaddress import IPv4Address +from uuid import UUID + +from sqlalchemy import DateTime, Enum, ForeignKey, String, UniqueConstraint, text +from sqlalchemy.dialects.postgresql import ARRAY, INET +from sqlalchemy.orm import ( + DeclarativeBase, + Mapped, + MappedAsDataclass, + mapped_column, + relationship, +) +from sqlalchemy.sql.schema import MetaData + +from backend.enums import StatusEnum + + +class Base(MappedAsDataclass, DeclarativeBase): + # This map details the specific transformation of types between Python and + # PostgreSQL. This is only needed for the case where a specific PostgreSQL + # type has to be used. + + type_annotation_map = { # noqa: RUF012 + list[str]: ARRAY( + item_type=String + ), # transform Python list[str] into PostgreSQL Array of strings + datetime: DateTime( + timezone=False + ), # transform Python datetime into PostgreSQL Datetime without timezone + IPv4Address: INET, # transform Python IPV4Address into PostgreSQL INET + } + + # This metadata specifies some naming conventions that will be used by + # alembic to generate constraints names (indexes, unique constraints, ...) + metadata = MetaData( + naming_convention={ + "ix": "ix_%(column_0_label)s", + "uq": "uq_%(table_name)s_%(column_0_name)s", + "ck": "ck_%(table_name)s_%(constraint_name)s", + "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", + "pk": "pk_%(table_name)s", + } + ) + pass + + +class Country(Base): + __tablename__ = "country" + + code: Mapped[str] = mapped_column( + primary_key=True + ) # two-letter country codes as defined in ISO 3166-1 + + name: Mapped[str] # full name of the country (in English) + + worker_id: Mapped[str | None] = mapped_column(ForeignKey("worker.id"), init=False) + worker: Mapped[Worker | None] = relationship(back_populates="countries", init=False) + mirrors: Mapped[list[Mirror]] = relationship( + back_populates="country", + init=False, + cascade="all, delete-orphan", + ) + + __table_args__ = (UniqueConstraint("name", "code"),) + + +class Mirror(Base): + __tablename__ = "mirror" + + id: Mapped[str] = mapped_column(primary_key=True) # hostname of a mirror URL + base_url: Mapped[str] + enabled: Mapped[bool] + # metadata of a mirror from MirroBrain (https://mirrorbrain-docs.readthedocs.io/en/latest/mirrors.html#displaying-details-about-a-mirror) + region: Mapped[str | None] + asn: Mapped[str | None] + score: Mapped[int | None] + latitude: Mapped[float | None] + longitude: Mapped[float | None] + country_only: Mapped[bool | None] + region_only: Mapped[bool | None] + as_only: Mapped[bool | None] + other_countries: Mapped[list[str] | None] + + country_code: Mapped[str] = mapped_column( + ForeignKey("country.code"), + init=False, + ) + country: Mapped[Country] = relationship(back_populates="mirrors", init=False) + + +class Worker(Base): + __tablename__ = "worker" + id: Mapped[str] = mapped_column(primary_key=True) + # RSA public key in PKCS8 format for generating access tokens required + # to make requests to the web server + pubkey_pkcs8: Mapped[str] + pubkey_fingerprint: Mapped[str | None] + + last_seen_on: Mapped[datetime | None] + countries: Mapped[list[Country]] = relationship(back_populates="worker", init=False) + + +class Test(Base): + __tablename__ = "test" + id: Mapped[UUID] = mapped_column( + init=False, primary_key=True, server_default=text("uuid_generate_v4()") + ) + requested_on: Mapped[datetime] + started_on: Mapped[datetime | None] + status: Mapped[StatusEnum | None] = mapped_column( + Enum( + native_enum=False, + validate_strings=True, + create_constraint=True, + name="status", + ) + ) + error: Mapped[str | None] + isp: Mapped[str | None] + ip_address: Mapped[IPv4Address | None] + asn: Mapped[str | None] # autonomous system based on IP + country: Mapped[str | None] # country based on IP + location: Mapped[str | None] # city based on IP + latency: Mapped[int | None] # milliseconds + download_size: Mapped[int | None] # bytes + duration: Mapped[int | None] # seconds + speed: Mapped[float | None] # bytes per second diff --git a/backend/src/backend/enums.py b/backend/src/backend/enums.py new file mode 100644 index 0000000..736658b --- /dev/null +++ b/backend/src/backend/enums.py @@ -0,0 +1,7 @@ +from enum import Enum + + +class StatusEnum(Enum): + MISSED = 0 + SUCCEEDED = 1 + ERRORED = 2 diff --git a/backend/src/backend/migrations/env.py b/backend/src/backend/migrations/env.py new file mode 100644 index 0000000..634c406 --- /dev/null +++ b/backend/src/backend/migrations/env.py @@ -0,0 +1,73 @@ +import os +from logging.config import fileConfig + +from alembic import context +from sqlalchemy import create_engine + +from backend.db.models import Base + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = os.getenv("POSTGRES_URI", "") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = create_engine(os.getenv("POSTGRES_URI", ""), echo=False) + + with connectable.connect() as connection: + context.configure(connection=connection, target_metadata=target_metadata) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/backend/src/backend/migrations/script.py.mako b/backend/src/backend/migrations/script.py.mako new file mode 100644 index 0000000..24db54b --- /dev/null +++ b/backend/src/backend/migrations/script.py.mako @@ -0,0 +1,25 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/backend/src/backend/migrations/versions/0c273daa1ab0_set_up_database_models.py b/backend/src/backend/migrations/versions/0c273daa1ab0_set_up_database_models.py new file mode 100644 index 0000000..b7e3f87 --- /dev/null +++ b/backend/src/backend/migrations/versions/0c273daa1ab0_set_up_database_models.py @@ -0,0 +1,99 @@ +"""set up database models + +Revision ID: 0c273daa1ab0 +Revises: +Create Date: 2024-06-04 11:56:53.888630 + +""" + +import sqlalchemy as sa +from alembic import op +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = "0c273daa1ab0" +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "test", + sa.Column( + "id", + sa.Uuid(), + server_default=sa.text("uuid_generate_v4()"), + nullable=False, + ), + sa.Column("requested_on", sa.DateTime(), nullable=False), + sa.Column("started_on", sa.DateTime(), nullable=True), + sa.Column( + "status", + sa.Enum(name="status", native_enum=False, create_constraint=True), + nullable=True, + ), + sa.Column("error", sa.String(), nullable=True), + sa.Column("isp", sa.String(), nullable=True), + sa.Column("ip_address", postgresql.INET(), nullable=True), + sa.Column("asn", sa.String(), nullable=True), + sa.Column("country", sa.String(), nullable=True), + sa.Column("location", sa.String(), nullable=True), + sa.Column("latency", sa.Integer(), nullable=True), + sa.Column("download_size", sa.Integer(), nullable=True), + sa.Column("duration", sa.Integer(), nullable=True), + sa.Column("speed", sa.Float(), nullable=True), + sa.PrimaryKeyConstraint("id", name=op.f("pk_test")), + ) + op.create_table( + "worker", + sa.Column("id", sa.String(), nullable=False), + sa.Column("pubkey_pkcs8", sa.String(), nullable=False), + sa.Column("pubkey_fingerprint", sa.String(), nullable=True), + sa.Column("last_seen_on", sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint("id", name=op.f("pk_worker")), + ) + op.create_table( + "country", + sa.Column("code", sa.String(), nullable=False), + sa.Column("name", sa.String(), nullable=False), + sa.Column("worker_id", sa.String(), nullable=True), + sa.ForeignKeyConstraint( + ["worker_id"], ["worker.id"], name=op.f("fk_country_worker_id_worker") + ), + sa.PrimaryKeyConstraint("code", name=op.f("pk_country")), + sa.UniqueConstraint("name", "code", name=op.f("uq_country_name")), + ) + op.create_table( + "mirror", + sa.Column("id", sa.String(), nullable=False), + sa.Column("base_url", sa.String(), nullable=False), + sa.Column("enabled", sa.Boolean(), nullable=False), + sa.Column("region", sa.String(), nullable=True), + sa.Column("asn", sa.String(), nullable=True), + sa.Column("score", sa.Integer(), nullable=True), + sa.Column("latitude", sa.Float(), nullable=True), + sa.Column("longitude", sa.Float(), nullable=True), + sa.Column("country_only", sa.Boolean(), nullable=True), + sa.Column("region_only", sa.Boolean(), nullable=True), + sa.Column("as_only", sa.Boolean(), nullable=True), + sa.Column("other_countries", postgresql.ARRAY(sa.String()), nullable=True), + sa.Column("country_code", sa.String(), nullable=False), + sa.ForeignKeyConstraint( + ["country_code"], + ["country.code"], + name=op.f("fk_mirror_country_code_country"), + ), + sa.PrimaryKeyConstraint("id", name=op.f("pk_mirror")), + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("mirror") + op.drop_table("country") + op.drop_table("worker") + op.drop_table("test") + # ### end Alembic commands ### diff --git a/backend/tasks.py b/backend/tasks.py new file mode 100644 index 0000000..a95c71a --- /dev/null +++ b/backend/tasks.py @@ -0,0 +1,109 @@ +# pyright: strict, reportUntypedFunctionDecorator=false +import os + +from invoke.context import Context +from invoke.tasks import task # pyright: ignore [reportUnknownVariableType] + +use_pty = not os.getenv("CI", "") + + +@task(optional=["args"], help={"args": "pytest additional arguments"}) +def test(ctx: Context, args: str = ""): + """run tests (without coverage)""" + ctx.run(f"pytest {args}", pty=use_pty) + + +@task(optional=["args"], help={"args": "pytest additional arguments"}) +def test_cov(ctx: Context, args: str = ""): + """run test vith coverage""" + ctx.run(f"coverage run -m pytest {args}", pty=use_pty) + + +@task(optional=["html"], help={"html": "flag to export html report"}) +def report_cov(ctx: Context, *, html: bool = False): + """report coverage""" + ctx.run("coverage combine", warn=True, pty=use_pty) + ctx.run("coverage report --show-missing", pty=use_pty) + if html: + ctx.run("coverage html", pty=use_pty) + + +@task( + optional=["args", "html"], + help={ + "args": "pytest additional arguments", + "html": "flag to export html report", + }, +) +def coverage(ctx: Context, args: str = "", *, html: bool = False): + """run tests and report coverage""" + test_cov(ctx, args=args) + report_cov(ctx, html=html) + + +@task(optional=["args"], help={"args": "black additional arguments"}) +def lint_black(ctx: Context, args: str = "."): + args = args or "." # needed for hatch script + ctx.run("black --version", pty=use_pty) + ctx.run(f"black --check --diff {args}", pty=use_pty) + + +@task(optional=["args"], help={"args": "ruff additional arguments"}) +def lint_ruff(ctx: Context, args: str = "."): + args = args or "." # needed for hatch script + ctx.run("ruff --version", pty=use_pty) + ctx.run(f"ruff check {args}", pty=use_pty) + + +@task( + optional=["args"], + help={ + "args": "linting tools (black, ruff) additional arguments, typically a path", + }, +) +def lintall(ctx: Context, args: str = "."): + """Check linting""" + args = args or "." # needed for hatch script + lint_black(ctx, args) + lint_ruff(ctx, args) + + +@task(optional=["args"], help={"args": "check tools (pyright) additional arguments"}) +def check_pyright(ctx: Context, args: str = ""): + """check static types with pyright""" + ctx.run("pyright --version") + ctx.run(f"pyright {args}", pty=use_pty) + + +@task(optional=["args"], help={"args": "check tools (pyright) additional arguments"}) +def checkall(ctx: Context, args: str = ""): + """check static types""" + check_pyright(ctx, args) + + +@task(optional=["args"], help={"args": "black additional arguments"}) +def fix_black(ctx: Context, args: str = "."): + """fix black formatting""" + args = args or "." # needed for hatch script + ctx.run(f"black {args}", pty=use_pty) + + +@task(optional=["args"], help={"args": "ruff additional arguments"}) +def fix_ruff(ctx: Context, args: str = "."): + """fix all ruff rules""" + args = args or "." # needed for hatch script + ctx.run(f"ruff check --fix {args}", pty=use_pty) + + +@task( + optional=["args"], + help={ + "args": "linting tools (black, ruff) additional arguments, typically a path", + }, +) +def fixall(ctx: Context, args: str = "."): + """Fix everything automatically""" + args = args or "." # needed for hatch script + fix_black(ctx, args) + fix_ruff(ctx, args) + lintall(ctx, args) diff --git a/backend/tests/test_basic.py b/backend/tests/test_basic.py new file mode 100644 index 0000000..e69de29 diff --git a/dev/docker-compose.yaml b/dev/docker-compose.yaml new file mode 100644 index 0000000..e69de29 diff --git a/frontend/README.md b/frontend/README.md new file mode 100644 index 0000000..e69de29 diff --git a/worker/Dockerfile b/worker/Dockerfile new file mode 100644 index 0000000..e69de29 diff --git a/worker/README.md b/worker/README.md new file mode 100644 index 0000000..819002b --- /dev/null +++ b/worker/README.md @@ -0,0 +1 @@ +# Worker diff --git a/worker/pyproject.toml b/worker/pyproject.toml new file mode 100644 index 0000000..478f8bf --- /dev/null +++ b/worker/pyproject.toml @@ -0,0 +1,231 @@ +[build-system] +requires = ["hatchling", "hatch-openzim"] +build-backend = "hatchling.build" + +[project] +name = "mirrors_qa_worker" +requires-python = ">=3.12,<3.13" +description = "mirrors-qa Worker" +readme = "README.md" +authors = [ + { name = "Kiwix", email = "dev@kiwix.org" }, +] +keywords = ["mirrors"] +dependencies = [ + "requests == 2.32.3", +] +license = {text = "GPL-3.0-or-later"} +classifiers = [ + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.12", + "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", +] + +dynamic = ["version"] + +[project.urls] +Homepage = "https://github.com/kiwix/mirrors-qa" + +[project.optional-dependencies] +scripts = [ + "invoke==2.2.0", +] +lint = [ + "black==24.1.1", + "ruff==0.2.0", +] +check = [ + "pyright==1.1.349", +] +test = [ + "pytest==8.0.0", + "coverage==7.4.1", +] +dev = [ + "pre-commit==3.6.0", + "debugpy==1.8.0", + "mirrors_qa_worker[scripts]", + "mirrors_qa_worker[lint]", + "mirrors_qa_worker[test]", + "mirrors_qa_worker[check]", +] + +[project.scripts] +mirrors-qa-worker = "worker:entrypoint" + +[tool.hatch.version] +path = "src/worker/__about__.py" + +[tool.hatch.build] +exclude = [ + "/.github", +] + +[tool.hatch.build.targets.wheel] +packages = ["src/worker"] + +[tool.hatch.envs.default] +features = ["dev"] + +[tool.hatch.envs.test] +features = ["scripts", "test"] + + +[tool.hatch.envs.test.scripts] +run = "inv test --args '{args}'" +run-cov = "inv test-cov --args '{args}'" +report-cov = "inv report-cov" +coverage = "inv coverage --args '{args}'" +html = "inv coverage --html --args '{args}'" + +[tool.hatch.envs.lint] +template = "lint" +skip-install = false +features = ["scripts", "lint"] + +[tool.hatch.envs.lint.scripts] +black = "inv lint-black --args '{args}'" +ruff = "inv lint-ruff --args '{args}'" +all = "inv lintall --args '{args}'" +fix-black = "inv fix-black --args '{args}'" +fix-ruff = "inv fix-ruff --args '{args}'" +fixall = "inv fixall --args '{args}'" + +[tool.hatch.envs.check] +features = ["scripts", "check"] + +[tool.hatch.envs.check.scripts] +pyright = "inv check-pyright --args '{args}'" +all = "inv checkall --args '{args}'" + +[tool.black] +line-length = 88 +target-version = ['py310'] + +[tool.ruff] +target-version = "py311" +line-length = 88 +src = ["src"] + +[tool.ruff.lint] +select = [ + "A", # flake8-builtins + # "ANN", # flake8-annotations + "ARG", # flake8-unused-arguments + # "ASYNC", # flake8-async + "B", # flake8-bugbear + # "BLE", # flake8-blind-except + "C4", # flake8-comprehensions + "C90", # mccabe + # "COM", # flake8-commas + # "D", # pydocstyle + # "DJ", # flake8-django + "DTZ", # flake8-datetimez + "E", # pycodestyle (default) + "EM", # flake8-errmsg + # "ERA", # eradicate + # "EXE", # flake8-executable + "F", # Pyflakes (default) + # "FA", # flake8-future-annotations + "FBT", # flake8-boolean-trap + # "FLY", # flynt + # "G", # flake8-logging-format + "I", # isort + "ICN", # flake8-import-conventions + # "INP", # flake8-no-pep420 + # "INT", # flake8-gettext + "ISC", # flake8-implicit-str-concat + "N", # pep8-naming + # "NPY", # NumPy-specific rules + # "PD", # pandas-vet + # "PGH", # pygrep-hooks + # "PIE", # flake8-pie + # "PL", # Pylint + "PLC", # Pylint: Convention + "PLE", # Pylint: Error + "PLR", # Pylint: Refactor + "PLW", # Pylint: Warning + # "PT", # flake8-pytest-style + # "PTH", # flake8-use-pathlib + # "PYI", # flake8-pyi + "Q", # flake8-quotes + # "RET", # flake8-return + # "RSE", # flake8-raise + "RUF", # Ruff-specific rules + "S", # flake8-bandit + # "SIM", # flake8-simplify + # "SLF", # flake8-self + "T10", # flake8-debugger + "T20", # flake8-print + # "TCH", # flake8-type-checking + # "TD", # flake8-todos + "TID", # flake8-tidy-imports + # "TRY", # tryceratops + "UP", # pyupgrade + "W", # pycodestyle + "YTT", # flake8-2020 +] +ignore = [ + # Allow non-abstract empty methods in abstract base classes + "B027", + # Remove flake8-errmsg since we consider they bloat the code and provide limited value + "EM", + # Allow boolean positional values in function calls, like `dict.get(... True)` + "FBT003", + # Ignore checks for possible passwords + "S105", "S106", "S107", + # Ignore warnings on subprocess.run / popen + "S603", + # Ignore complexity + "C901", "PLR0911", "PLR0912", "PLR0913", "PLR0915", +] +unfixable = [ + # Don't touch unused imports + "F401", +] + +[tool.ruff.lint.isort] +known-first-party = ["worker"] + +[tool.ruff.lint.flake8-bugbear] +# add exceptions to B008 for fastapi. +extend-immutable-calls = ["fastapi.Depends", "fastapi.Query"] + +[tool.ruff.lint.flake8-tidy-imports] +ban-relative-imports = "all" + +[tool.ruff.lint.per-file-ignores] +# Tests can use magic values, assertions, and relative imports +"tests/**/*" = ["PLR2004", "S101", "TID252"] + +[tool.pytest.ini_options] +minversion = "7.3" +testpaths = ["tests"] +pythonpath = [".", "src"] + +[tool.coverage.paths] +worker = ["src/worker"] +tests = ["tests"] + +[tool.coverage.run] +source_pkgs = ["worker"] +branch = true +parallel = true +omit = [ + "src/worker/__about__.py", +] + +[tool.coverage.report] +exclude_lines = [ + "no cov", + "if __name__ == .__main__.:", + "if TYPE_CHECKING:", +] + +[tool.pyright] +include = ["src", "tests", "tasks.py"] +exclude = [".env/**", ".venv/**"] +extraPaths = ["src"] +pythonVersion = "3.12" +typeCheckingMode="strict" +disableBytesTypePromotions = true diff --git a/worker/src/worker/__about__.py b/worker/src/worker/__about__.py new file mode 100644 index 0000000..a237a3e --- /dev/null +++ b/worker/src/worker/__about__.py @@ -0,0 +1 @@ +__version__ = "0.0.1-dev0" diff --git a/worker/src/worker/__init__.py b/worker/src/worker/__init__.py new file mode 100644 index 0000000..ed166eb --- /dev/null +++ b/worker/src/worker/__init__.py @@ -0,0 +1,5 @@ +from worker.__about__ import __version__ + + +def entrypoint(): + print(f"Hello from worker:{__version__}") # noqa: T201 diff --git a/worker/tasks.py b/worker/tasks.py new file mode 100644 index 0000000..a95c71a --- /dev/null +++ b/worker/tasks.py @@ -0,0 +1,109 @@ +# pyright: strict, reportUntypedFunctionDecorator=false +import os + +from invoke.context import Context +from invoke.tasks import task # pyright: ignore [reportUnknownVariableType] + +use_pty = not os.getenv("CI", "") + + +@task(optional=["args"], help={"args": "pytest additional arguments"}) +def test(ctx: Context, args: str = ""): + """run tests (without coverage)""" + ctx.run(f"pytest {args}", pty=use_pty) + + +@task(optional=["args"], help={"args": "pytest additional arguments"}) +def test_cov(ctx: Context, args: str = ""): + """run test vith coverage""" + ctx.run(f"coverage run -m pytest {args}", pty=use_pty) + + +@task(optional=["html"], help={"html": "flag to export html report"}) +def report_cov(ctx: Context, *, html: bool = False): + """report coverage""" + ctx.run("coverage combine", warn=True, pty=use_pty) + ctx.run("coverage report --show-missing", pty=use_pty) + if html: + ctx.run("coverage html", pty=use_pty) + + +@task( + optional=["args", "html"], + help={ + "args": "pytest additional arguments", + "html": "flag to export html report", + }, +) +def coverage(ctx: Context, args: str = "", *, html: bool = False): + """run tests and report coverage""" + test_cov(ctx, args=args) + report_cov(ctx, html=html) + + +@task(optional=["args"], help={"args": "black additional arguments"}) +def lint_black(ctx: Context, args: str = "."): + args = args or "." # needed for hatch script + ctx.run("black --version", pty=use_pty) + ctx.run(f"black --check --diff {args}", pty=use_pty) + + +@task(optional=["args"], help={"args": "ruff additional arguments"}) +def lint_ruff(ctx: Context, args: str = "."): + args = args or "." # needed for hatch script + ctx.run("ruff --version", pty=use_pty) + ctx.run(f"ruff check {args}", pty=use_pty) + + +@task( + optional=["args"], + help={ + "args": "linting tools (black, ruff) additional arguments, typically a path", + }, +) +def lintall(ctx: Context, args: str = "."): + """Check linting""" + args = args or "." # needed for hatch script + lint_black(ctx, args) + lint_ruff(ctx, args) + + +@task(optional=["args"], help={"args": "check tools (pyright) additional arguments"}) +def check_pyright(ctx: Context, args: str = ""): + """check static types with pyright""" + ctx.run("pyright --version") + ctx.run(f"pyright {args}", pty=use_pty) + + +@task(optional=["args"], help={"args": "check tools (pyright) additional arguments"}) +def checkall(ctx: Context, args: str = ""): + """check static types""" + check_pyright(ctx, args) + + +@task(optional=["args"], help={"args": "black additional arguments"}) +def fix_black(ctx: Context, args: str = "."): + """fix black formatting""" + args = args or "." # needed for hatch script + ctx.run(f"black {args}", pty=use_pty) + + +@task(optional=["args"], help={"args": "ruff additional arguments"}) +def fix_ruff(ctx: Context, args: str = "."): + """fix all ruff rules""" + args = args or "." # needed for hatch script + ctx.run(f"ruff check --fix {args}", pty=use_pty) + + +@task( + optional=["args"], + help={ + "args": "linting tools (black, ruff) additional arguments, typically a path", + }, +) +def fixall(ctx: Context, args: str = "."): + """Fix everything automatically""" + args = args or "." # needed for hatch script + fix_black(ctx, args) + fix_ruff(ctx, args) + lintall(ctx, args) diff --git a/worker/tests/test_basic.py b/worker/tests/test_basic.py new file mode 100644 index 0000000..e69de29