imports.py 4.16 KB
Newer Older
Mustafa Gezen's avatar
Mustafa Gezen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#  Copyright (c) 2021 The Distrobuild Authors
#
#  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,
Mustafa Gezen's avatar
Mustafa Gezen committed
18
19
20
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#  SOFTWARE.

21
from typing import Optional, Dict
22
23
24

from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi_pagination import pagination_params, Page
Mustafa Gezen's avatar
Mustafa Gezen committed
25
from fastapi_pagination.ext.tortoise import paginate
26
from pydantic import BaseModel, validator
Mustafa Gezen's avatar
Mustafa Gezen committed
27
28
from starlette.responses import PlainTextResponse

29
from distrobuild.common import gen_body_filters, create_import_order, get_user
30
31
from distrobuild.models import Import, Package, Repo, ImportStatus
from distrobuild.serialize import Import_Pydantic, ImportGeneral_Pydantic
32
from distrobuild.settings import settings
Mustafa Gezen's avatar
Mustafa Gezen committed
33
34
35
36
37
from distrobuild_scheduler import import_package_task

router = APIRouter(prefix="/imports")


38
39
40
41
42
43
44
45
46
47
48
49
50
class ImportRequest(BaseModel):
    full_history: bool = False
    single_tag: Optional[str]
    package_id: Optional[int]
    package_name: Optional[str]

    @validator("package_name")
    def validate(cls, package_name, values):
        if (not values.get("package_id") and not package_name) or (values.get("package_id") and package_name):
            raise ValueError("either package_id or package_name is required")
        return package_name


51
@router.get("/", response_model=Page[ImportGeneral_Pydantic], dependencies=[Depends(pagination_params)])
Mustafa Gezen's avatar
Mustafa Gezen committed
52
async def list_imports():
53
    return await paginate(Import.all().order_by("-created_at").prefetch_related("package", "commits"))
54
55
56
57
58


@router.get("/{import_id}", response_model=Import_Pydantic)
async def get_import(import_id: int):
    return await Import_Pydantic.from_queryset_single(Import.filter(id=import_id).prefetch_related("package").first())
Mustafa Gezen's avatar
Mustafa Gezen committed
59
60
61
62
63
64
65
66


@router.get("/{import_id}/logs", response_class=PlainTextResponse)
async def get_import_logs(import_id: int):
    import_obj = await Import.filter(id=import_id).get_or_none()
    if not import_obj:
        raise HTTPException(404, detail="import does not exist")
    try:
67
        with open(f"{settings.import_logs_dir}/import-{import_obj.id}.log") as f:
Mustafa Gezen's avatar
Mustafa Gezen committed
68
69
70
71
72
            return f.read()
    except FileNotFoundError:
        raise HTTPException(412, detail="import not started or log has expired")


73
74
75
76
77
78
79
80
81
82
83
84
@router.post("/{import_id}/cancel", status_code=202)
async def cancel_import(import_id: int):
    import_obj = await Import.filter(id=import_id, cancelled=False).get_or_none()
    if not import_obj:
        raise HTTPException(404, detail="import does not exist or is already cancelled")

    import_obj.status = ImportStatus.CANCELLED
    await import_obj.save()

    return {}


Mustafa Gezen's avatar
Mustafa Gezen committed
85
@router.post("/", status_code=202)
86
async def import_package_route(request: Request, body: Dict[str, ImportRequest], batch_import_id: Optional[int] = None):
87
88
    user = get_user(request)

Mustafa Gezen's avatar
Mustafa Gezen committed
89
90
91
92
93
    filters = gen_body_filters(body)
    package = await Package.filter(**filters).get_or_none()
    if not package:
        raise HTTPException(404, detail="package does not exist")

94
95
96
    if package.repo == Repo.MODULAR_CANDIDATE:
        raise HTTPException(401, detail="modular subpackages cannot be imported")

97
    build_order = await create_import_order(package, user["preferred_username"], batch_import_id)
Mustafa Gezen's avatar
Mustafa Gezen committed
98
99
100
    await import_package_task(build_order[0][0], build_order[0][1], build_order[1:])

    return {}