Fetch releases.yml to assert if Source is valid

This commit is contained in:
Benjamin Bohard 2019-12-16 16:33:23 +01:00
parent 7f0411da4d
commit f0042f2a37
1 changed files with 22 additions and 0 deletions

View File

@ -2,6 +2,11 @@ from typing import Dict, List
from ...controller import Controller
from ...register import register
from ...context import Context
import requests
import yaml
import os
from ...utils import _
from ...config import get_config
class Risotto(Controller):
@ -14,6 +19,23 @@ class Risotto(Controller):
ON CONFLICT (SourceName) DO UPDATE SET SourceURL = $2
RETURNING SourceId
"""
# If given url is not 'none' (a.k.a internal source)
# Look for file releases.yml at given url
# If such a file exists, consider source a valid one and create source in database.
if source_url != 'none':
try:
releases = yaml.load(requests.get(source_url.rstrip('/') + '/releases.yml').content, Loader=yaml.SafeLoader)
except requests.exceptions.ConnectionError as err:
raise Exception(_('Invalid URL'))
except yaml.scanner.ScannerError as err:
raise Exception(_('Invalid releases.yml file'))
except:
raise Exception(_('Invalid source'))
else:
releases = {'1.0.0': {'distribution': 'stable'}}
os.makedirs(os.path.join(get_config().get('source').get('root_path'), source_name))
with open(os.path.join(get_config().get('source').get('root_path'), source_name, 'releases.yml')) as release_file:
yaml.dump(releases, release_file)
source_id = await risotto_context.connection.fetchval(source_upsert,
source_name,
source_url)