mirror of
https://github.com/Kludex/awesome-fastapi-projects.git
synced 2025-05-14 13:17:04 +00:00
Create index.json
This commit is contained in:
parent
fc4f709189
commit
f608ff0500
53
app/create_index.py
Normal file
53
app/create_index.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
"""Create index.json file from database."""
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import aiofiles
|
||||||
|
import sqlalchemy.orm
|
||||||
|
import typer
|
||||||
|
|
||||||
|
from app.database import Repo
|
||||||
|
from app.models import RepoDetail
|
||||||
|
from app.uow import async_session_uow
|
||||||
|
|
||||||
|
INDEX_PATH = Path(__file__).parent.parent / "index.json"
|
||||||
|
|
||||||
|
|
||||||
|
async def create_index() -> None:
|
||||||
|
"""
|
||||||
|
Create index.json file from database.
|
||||||
|
|
||||||
|
Creates an index which is going to be used by the frontend.
|
||||||
|
:return: None
|
||||||
|
"""
|
||||||
|
async with async_session_uow() as session, aiofiles.open(
|
||||||
|
INDEX_PATH, "w"
|
||||||
|
) as index_file:
|
||||||
|
await index_file.write(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"repos": [
|
||||||
|
RepoDetail.model_validate(repo).model_dump()
|
||||||
|
async for repo in (
|
||||||
|
await session.stream_scalars(
|
||||||
|
sqlalchemy.select(Repo)
|
||||||
|
.order_by(Repo.id)
|
||||||
|
.options(sqlalchemy.orm.selectinload(Repo.dependencies))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
],
|
||||||
|
},
|
||||||
|
indent=4,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""Create index.json file from database."""
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
asyncio.run(create_index())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
typer.run(main)
|
@ -2,13 +2,12 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import subprocess
|
import subprocess
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from typing import NewType
|
|
||||||
|
|
||||||
import aiofiles.tempfile
|
import aiofiles.tempfile
|
||||||
import stamina
|
import stamina
|
||||||
|
|
||||||
from app.database import Repo
|
from app.database import Repo
|
||||||
from app.models import DependencyCreateData
|
from app.models import DependencyCreateData, RevisionHash
|
||||||
|
|
||||||
|
|
||||||
async def run_command(*cmd: str, cwd: str | None = None) -> str:
|
async def run_command(*cmd: str, cwd: str | None = None) -> str:
|
||||||
@ -38,10 +37,6 @@ async def run_command(*cmd: str, cwd: str | None = None) -> str:
|
|||||||
return stdout.decode()
|
return stdout.decode()
|
||||||
|
|
||||||
|
|
||||||
# TODO: Organize the types
|
|
||||||
RevisionHash = NewType("RevisionHash", str)
|
|
||||||
|
|
||||||
|
|
||||||
async def acquire_dependencies_data_for_repository(
|
async def acquire_dependencies_data_for_repository(
|
||||||
repo: Repo,
|
repo: Repo,
|
||||||
) -> tuple[RevisionHash, list[DependencyCreateData]]:
|
) -> tuple[RevisionHash, list[DependencyCreateData]]:
|
||||||
|
@ -1,13 +1,43 @@
|
|||||||
"""Module contains the models for the application."""
|
"""Module contains the models for the application."""
|
||||||
from typing import NewType
|
from typing import NewType
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, ConfigDict, NonNegativeInt
|
||||||
|
|
||||||
|
# TODO: Organize the types
|
||||||
|
|
||||||
RepoId = NewType("RepoId", int)
|
RepoId = NewType("RepoId", int)
|
||||||
DependencyId = NewType("DependencyId", int)
|
DependencyId = NewType("DependencyId", int)
|
||||||
|
RevisionHash = NewType("RevisionHash", str)
|
||||||
|
|
||||||
|
|
||||||
class DependencyCreateData(BaseModel):
|
class DependencyCreateData(BaseModel):
|
||||||
"""A dependency of a repository."""
|
"""A dependency of a repository."""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
|
||||||
|
class DependencyDetail(BaseModel):
|
||||||
|
"""A dependency of a repository."""
|
||||||
|
|
||||||
|
model_config = ConfigDict(
|
||||||
|
from_attributes=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
id: DependencyId
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
|
class RepoDetail(BaseModel):
|
||||||
|
"""A repository that is being tracked."""
|
||||||
|
|
||||||
|
model_config = ConfigDict(
|
||||||
|
from_attributes=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
id: RepoId
|
||||||
|
url: str
|
||||||
|
description: str
|
||||||
|
stars: NonNegativeInt
|
||||||
|
source_graph_repo_id: int
|
||||||
|
dependencies: list[DependencyDetail]
|
||||||
|
last_checked_revision: RevisionHash | None
|
||||||
|
73024
index.json
Normal file
73024
index.json
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user