mirror of
https://github.com/Maks1mS/altlinux-autorepacked.git
synced 2025-03-22 10:13:49 +03:00
Compare commits
2 Commits
250fe07bf0
...
657c424bf1
Author | SHA1 | Date | |
---|---|---|---|
657c424bf1 | |||
233c11efec |
@ -0,0 +1 @@
|
|||||||
|
REPO_PATH=
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
.env
|
.env
|
||||||
|
venv
|
@ -2,9 +2,15 @@ FROM registry.altlinux.org/alt/python:p10
|
|||||||
|
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
eepm \
|
eepm \
|
||||||
wget
|
wget \
|
||||||
|
apt-repo-tools \
|
||||||
|
alien \
|
||||||
|
pip
|
||||||
# && rm -rf /var/lib/apt/lists/*
|
# && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip --no-cache-dir install -r requirements.txt
|
||||||
|
|
||||||
COPY ./autorepacked ./autorepacked
|
COPY ./autorepacked ./autorepacked
|
||||||
|
|
||||||
ENV PYTHONPATH "${PYTHONPATH}:."
|
ENV PYTHONPATH "${PYTHONPATH}:."
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from autorepacked import utils
|
||||||
|
|
||||||
|
|
||||||
def _get_file(directory):
|
def _get_file(directory):
|
||||||
files_and_dirs = os.listdir(directory)
|
files_and_dirs = os.listdir(directory)
|
||||||
files = [f for f in files_and_dirs if os.path.isfile(os.path.join(directory, f))]
|
files = [f for f in files_and_dirs if os.path.isfile(os.path.join(directory, f))]
|
||||||
return os.path.join(directory, files[0]) if files else None
|
return os.path.join(directory, files[0]) if files else None
|
||||||
|
|
||||||
|
|
||||||
class BaseProvider:
|
class BaseProvider:
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.config = config
|
self.config = config
|
||||||
@ -21,14 +23,14 @@ class BaseProvider:
|
|||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
def get_version(self):
|
def get_version(self):
|
||||||
url = subprocess.run([
|
url = utils.run([
|
||||||
'epm',
|
'epm',
|
||||||
'--silent',
|
'--silent',
|
||||||
'tool',
|
'tool',
|
||||||
'eget',
|
'eget',
|
||||||
'--get-real-url',
|
'--get-real-url',
|
||||||
self.get_download_url(),
|
self.get_download_url(),
|
||||||
], stdout=subprocess.PIPE).stdout.decode('utf-8')
|
])
|
||||||
|
|
||||||
pattern = r'\d+\.\d+\.\d+'
|
pattern = r'\d+\.\d+\.\d+'
|
||||||
match = re.search(pattern, url)
|
match = re.search(pattern, url)
|
||||||
@ -44,48 +46,36 @@ class BaseProvider:
|
|||||||
download_directory = tempfile.mkdtemp()
|
download_directory = tempfile.mkdtemp()
|
||||||
repacked_directory = tempfile.mkdtemp()
|
repacked_directory = tempfile.mkdtemp()
|
||||||
|
|
||||||
subprocess.run(
|
utils.run([
|
||||||
args=[
|
'epm',
|
||||||
'epm',
|
'--silent',
|
||||||
'--silent',
|
'tool',
|
||||||
'tool',
|
'eget',
|
||||||
'eget',
|
'-q',
|
||||||
'-q',
|
self.get_download_url(),
|
||||||
self.get_download_url(),
|
], cwd=download_directory)
|
||||||
],
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
cwd=download_directory,
|
|
||||||
).stdout.decode('utf-8')
|
|
||||||
|
|
||||||
file = _get_file(download_directory)
|
file = _get_file(download_directory)
|
||||||
|
|
||||||
subprocess.run(
|
utils.run([
|
||||||
args=[
|
'epm',
|
||||||
'epm',
|
'--silent',
|
||||||
'--silent',
|
'-y',
|
||||||
'-y',
|
'repack',
|
||||||
'repack',
|
file,
|
||||||
file,
|
], cwd=repacked_directory)
|
||||||
],
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
cwd=repacked_directory,
|
|
||||||
).stdout.decode('utf-8')
|
|
||||||
|
|
||||||
shutil.rmtree(download_directory)
|
shutil.rmtree(download_directory)
|
||||||
|
|
||||||
file = _get_file(repacked_directory)
|
file = _get_file(repacked_directory)
|
||||||
|
|
||||||
subprocess.run(
|
utils.run([
|
||||||
args=[
|
'epm',
|
||||||
'epm',
|
'--silent',
|
||||||
'--silent',
|
'repo',
|
||||||
'repo',
|
'pkgadd',
|
||||||
'pkgadd',
|
self.config.get('repo_path'),
|
||||||
self.config.get('repo_path'),
|
file,
|
||||||
file,
|
], cwd=repacked_directory)
|
||||||
],
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
cwd=repacked_directory,
|
|
||||||
).stdout.decode('utf-8')
|
|
||||||
|
|
||||||
shutil.rmtree(repacked_directory)
|
shutil.rmtree(repacked_directory)
|
114
autorepacked/main.py
Normal file
114
autorepacked/main.py
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import importlib
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
from fastapi import BackgroundTasks, FastAPI
|
||||||
|
import uvicorn
|
||||||
|
from fastapi_utils.tasks import repeat_every
|
||||||
|
|
||||||
|
from autorepacked import utils
|
||||||
|
from autorepacked.base_provider import BaseProvider
|
||||||
|
from autorepacked.config import Config
|
||||||
|
|
||||||
|
|
||||||
|
def create_repo(config: Config):
|
||||||
|
utils.run([
|
||||||
|
'epm',
|
||||||
|
'repo',
|
||||||
|
'create',
|
||||||
|
config.get('repo_path')
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
await update_repeat()
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
config = Config()
|
||||||
|
|
||||||
|
update_task_started = False
|
||||||
|
|
||||||
|
|
||||||
|
def update():
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
providers_path = os.path.join(os.getcwd(), 'autorepacked/providers')
|
||||||
|
modules = os.listdir(providers_path)
|
||||||
|
|
||||||
|
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
|
||||||
|
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
|
||||||
|
|
||||||
|
repo_data['versions'] = versions
|
||||||
|
|
||||||
|
current_date = datetime.datetime.utcnow()
|
||||||
|
|
||||||
|
if need_update_index:
|
||||||
|
utils.run(
|
||||||
|
args=[
|
||||||
|
'epm',
|
||||||
|
'repo',
|
||||||
|
'index',
|
||||||
|
config.get('repo_path'),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
create_repo(config)
|
||||||
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
9
autorepacked/utils.py
Normal file
9
autorepacked/utils.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def run(args, cwd=None):
|
||||||
|
return subprocess.run(
|
||||||
|
args=args,
|
||||||
|
cwd=cwd,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
).stdout.decode('utf-8')
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fastapi==0.109.0
|
||||||
|
uvicorn==0.26.0
|
||||||
|
fastapi-utils==0.2.1
|
Loading…
Reference in New Issue
Block a user