Files
tabby-web/backend/tabby/app/management/commands/add_version.py

66 lines
2.1 KiB
Python
Raw Normal View History

2021-07-26 17:56:35 +02:00
import fsspec
2021-07-11 16:13:18 +02:00
import logging
import requests
import shutil
import subprocess
import tempfile
from django.core.management.base import BaseCommand
from django.conf import settings
from pathlib import Path
2021-07-26 17:56:35 +02:00
from urllib.parse import urlparse
2021-07-11 16:13:18 +02:00
class Command(BaseCommand):
help = 'Downloads a new app version'
def add_arguments(self, parser):
parser.add_argument('version', type=str)
def handle(self, *args, **options):
version = options['version']
2021-07-26 17:56:35 +02:00
target = f'{settings.APP_DIST_STORAGE}/{version}'
fs = fsspec.filesystem(urlparse(settings.APP_DIST_STORAGE).scheme)
2021-07-11 16:13:18 +02:00
plugin_list = [
'tabby-web-container',
'tabby-core',
'tabby-settings',
'tabby-terminal',
'tabby-ssh',
'tabby-community-color-schemes',
2021-07-11 16:31:27 +02:00
'tabby-serial',
2021-07-11 23:10:57 +02:00
'tabby-telnet',
2021-07-11 16:13:18 +02:00
'tabby-web',
'tabby-web-demo',
]
with tempfile.TemporaryDirectory() as tempdir:
tempdir = Path(tempdir)
for plugin in plugin_list:
logging.info(f'Resolving {plugin}@{version}')
response = requests.get(f'{settings.NPM_REGISTRY}/{plugin}/{version}')
response.raise_for_status()
info = response.json()
url = info['dist']['tarball']
logging.info(f'Downloading {plugin}@{version} from {url}')
response = requests.get(url)
with tempfile.NamedTemporaryFile('wb') as f:
f.write(response.content)
plugin_final_target = Path(tempdir) / plugin
with tempfile.TemporaryDirectory() as extraction_tmp:
subprocess.check_call(
['tar', '-xzf', f.name, '-C', str(extraction_tmp)]
)
2021-07-26 17:56:35 +02:00
shutil.move(
Path(extraction_tmp) / 'package', plugin_final_target
)
2021-07-11 16:13:18 +02:00
2021-07-26 17:56:35 +02:00
if fs.exists(target):
fs.rm(target, recursive=True)
fs.mkdir(target)
fs.put(str(tempdir), target, recursive=True)