From f0042f2a37d6453a8b2687f4c7ee94a0c8f74f65 Mon Sep 17 00:00:00 2001 From: Benjamin Bohard Date: Mon, 16 Dec 2019 16:33:23 +0100 Subject: [PATCH] Fetch releases.yml to assert if Source is valid --- src/risotto/services/source/source.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/risotto/services/source/source.py b/src/risotto/services/source/source.py index 9290f98..d017d0d 100644 --- a/src/risotto/services/source/source.py +++ b/src/risotto/services/source/source.py @@ -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)