2024-01-16 22:46:39 +03:00
|
|
|
import datetime
|
2024-01-16 20:45:06 +03:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import importlib
|
2024-01-16 22:46:39 +03:00
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
from fastapi import BackgroundTasks, FastAPI
|
|
|
|
import uvicorn
|
|
|
|
from fastapi_utils.tasks import repeat_every
|
2024-01-16 20:45:06 +03:00
|
|
|
|
|
|
|
from autorepacked import utils
|
2024-01-17 13:47:54 +03:00
|
|
|
from autorepacked.common_providers.base_provider import BaseProvider
|
2024-01-16 20:45:06 +03:00
|
|
|
from autorepacked.config import Config
|
|
|
|
|
|
|
|
|
2024-01-16 22:46:39 +03:00
|
|
|
def create_repo(config: Config):
|
2024-01-17 15:36:11 +03:00
|
|
|
utils.epm([
|
2024-01-16 20:45:06 +03:00
|
|
|
'repo',
|
|
|
|
'create',
|
|
|
|
config.get('repo_path')
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2024-01-21 12:41:50 +03:00
|
|
|
def update_cache():
|
|
|
|
utils.epm(['update'])
|
|
|
|
|
2024-01-17 13:47:54 +03:00
|
|
|
def update_epm():
|
2024-01-21 12:41:50 +03:00
|
|
|
utils.epm(['ei'])
|
2024-01-17 13:47:54 +03:00
|
|
|
|
|
|
|
|
2024-01-16 22:46:39 +03:00
|
|
|
@asynccontextmanager
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
await update_repeat()
|
|
|
|
yield
|
2024-01-16 20:45:06 +03:00
|
|
|
|
|
|
|
|
2024-01-16 22:46:39 +03:00
|
|
|
config = Config()
|
2024-01-21 12:41:50 +03:00
|
|
|
app = FastAPI(lifespan=lifespan, root_path=config.get('root_path'))
|
2024-01-16 20:45:06 +03:00
|
|
|
|
2024-01-16 22:46:39 +03:00
|
|
|
update_task_started = False
|
|
|
|
|
|
|
|
|
|
|
|
def update():
|
2024-01-16 20:45:06 +03:00
|
|
|
json_file_path = os.path.join(config.get('repo_path'), 'data.json')
|
|
|
|
if not os.path.exists(json_file_path):
|
|
|
|
with open(json_file_path, 'w') as data_file:
|
|
|
|
json.dump({'versions': {}}, data_file, indent=4)
|
|
|
|
with open(json_file_path, 'r') as data_file:
|
|
|
|
repo_data = json.load(data_file)
|
|
|
|
|
2024-01-16 22:46:39 +03:00
|
|
|
global update_task_started
|
|
|
|
|
|
|
|
if 'last_version_check' in repo_data:
|
|
|
|
last_version_check = datetime.datetime.fromisoformat(repo_data['last_version_check'])
|
|
|
|
|
|
|
|
if datetime.datetime.now() - datetime.timedelta(minutes=5) < last_version_check:
|
|
|
|
update_task_started = False
|
|
|
|
return
|
|
|
|
|
2024-01-21 12:41:50 +03:00
|
|
|
update_cache()
|
2024-01-17 13:47:54 +03:00
|
|
|
update_epm()
|
|
|
|
|
2024-01-16 22:46:39 +03:00
|
|
|
providers_path = os.path.join(os.getcwd(), 'autorepacked/providers')
|
|
|
|
modules = os.listdir(providers_path)
|
|
|
|
|
2024-01-16 20:45:06 +03:00
|
|
|
versions = repo_data['versions']
|
|
|
|
need_update_index = False
|
|
|
|
for module_name in modules:
|
|
|
|
module = importlib.import_module(f'providers.{module_name}')
|
|
|
|
if hasattr(module, 'get_provider'):
|
|
|
|
provider = module.get_provider(config) # type: BaseProvider
|
2024-01-17 15:36:11 +03:00
|
|
|
if not provider.disabled:
|
|
|
|
name = provider.get_name()
|
|
|
|
print(f"request version of {name}")
|
|
|
|
version = provider.get_version()
|
|
|
|
if name not in versions or versions[name] != version:
|
|
|
|
print(f'{name} has new version {version}')
|
|
|
|
versions[name] = version
|
|
|
|
provider.download()
|
|
|
|
need_update_index = True
|
2024-01-16 20:45:06 +03:00
|
|
|
|
|
|
|
repo_data['versions'] = versions
|
|
|
|
|
2024-01-16 22:46:39 +03:00
|
|
|
current_date = datetime.datetime.utcnow()
|
2024-01-16 20:45:06 +03:00
|
|
|
|
|
|
|
if need_update_index:
|
2024-01-17 15:36:11 +03:00
|
|
|
utils.epm(
|
2024-01-16 20:45:06 +03:00
|
|
|
args=[
|
|
|
|
'repo',
|
|
|
|
'index',
|
|
|
|
config.get('repo_path'),
|
|
|
|
]
|
|
|
|
)
|
2024-01-16 22:46:39 +03:00
|
|
|
repo_data['last_index_update'] = current_date.isoformat()
|
|
|
|
|
|
|
|
repo_data['last_version_check'] = current_date.isoformat()
|
|
|
|
|
|
|
|
with open(json_file_path, 'w') as data_file:
|
|
|
|
json.dump(repo_data, data_file, indent=4)
|
|
|
|
|
|
|
|
update_task_started = False
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/update")
|
|
|
|
def update_method(background_tasks: BackgroundTasks):
|
|
|
|
global update_task_started
|
|
|
|
if not update_task_started:
|
|
|
|
update_task_started = True
|
|
|
|
background_tasks.add_task(update)
|
|
|
|
return {'status': 'OK'}
|
|
|
|
|
|
|
|
|
|
|
|
@repeat_every(seconds=60 * 60 * 4)
|
|
|
|
def update_repeat():
|
|
|
|
global update_task_started
|
|
|
|
if not update_task_started:
|
|
|
|
update_task_started = True
|
|
|
|
update()
|
2024-01-16 20:45:06 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-01-16 22:46:39 +03:00
|
|
|
create_repo(config)
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|