From 37d8fe7e0a224c748e996658a2fd042148e5a9e1 Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Sep 17 2018 10:05:09 +0000 Subject: Remove import cycle The cycle was: utils policies resources utils --- diff --git a/greenwave/app_factory.py b/greenwave/app_factory.py index e5c9084..971fd72 100644 --- a/greenwave/app_factory.py +++ b/greenwave/app_factory.py @@ -1,13 +1,18 @@ # SPDX-License-Identifier: GPL-2.0+ +import logging + from flask import Flask from greenwave.api_v1 import api from greenwave.utils import json_error, load_config, sha1_mangle_key +from greenwave.policies import load_policies from dogpile.cache import make_region from requests import ConnectionError, Timeout from werkzeug.exceptions import default_exceptions +log = logging.getLogger(__name__) + # applicaiton factory http://flask.pocoo.org/docs/0.12/patterns/appfactories/ def create_app(config_obj=None): @@ -17,6 +22,10 @@ def create_app(config_obj=None): if app.config['PRODUCTION'] and app.secret_key == 'replace-me-with-something-random': raise Warning("You need to change the app.secret_key value for production") + policies_dir = app.config['POLICIES_DIR'] + log.debug("config: Loading policies from %r", policies_dir) + app.config['policies'] = load_policies(policies_dir) + # register error handlers for code in default_exceptions.keys(): app.register_error_handler(code, json_error) diff --git a/greenwave/policies.py b/greenwave/policies.py index 3f02e34..403c340 100644 --- a/greenwave/policies.py +++ b/greenwave/policies.py @@ -1,8 +1,10 @@ # SPDX-License-Identifier: GPL-2.0+ from fnmatch import fnmatch +import glob import itertools import logging +import os import re import greenwave.resources @@ -17,6 +19,23 @@ from greenwave.safe_yaml import ( log = logging.getLogger(__name__) +def load_policies(policies_dir): + """ + Load Greenwave policies from the given policies directory. + + :param str policies_dir: A path points to the policies directory. + :return: A list of policies. + + """ + policy_pathnames = glob.glob(os.path.join(policies_dir, '*.yaml')) + policies = [] + for policy_pathname in policy_pathnames: + with open(policy_pathname, 'r') as f: + policies.extend(greenwave.policies.Policy.safe_load_all(f)) + log.debug("Loaded %i policies from %s", len(policies), policies_dir) + return policies + + class DisallowedRuleError(RuntimeError): pass diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index d321490..a871068 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -8,6 +8,7 @@ from textwrap import dedent from greenwave.app_factory import create_app from greenwave.policies import ( + load_policies, summarize_answers, Policy, RemotePolicy, @@ -17,7 +18,6 @@ from greenwave.policies import ( InvalidGatingYaml ) from greenwave.resources import ResultsRetriever -from greenwave.utils import load_policies from greenwave.safe_yaml import SafeYAMLError diff --git a/greenwave/utils.py b/greenwave/utils.py index e32f44a..a45503b 100644 --- a/greenwave/utils.py +++ b/greenwave/utils.py @@ -1,7 +1,6 @@ # SPDX-License-Identifier: GPL-2.0+ import functools -import glob import logging import os import time @@ -11,7 +10,6 @@ from flask import jsonify, current_app, request from flask.config import Config from requests import ConnectionError, Timeout from werkzeug.exceptions import HTTPException -import greenwave.policies log = logging.getLogger(__name__) @@ -101,29 +99,9 @@ def load_config(config_obj=None): if os.environ.get('SECRET_KEY'): config['SECRET_KEY'] = os.environ['SECRET_KEY'] - log.debug("config: Loading policies from %r", config['POLICIES_DIR']) - config['policies'] = load_policies(config['POLICIES_DIR']) - return config -def load_policies(policies_dir): - """ - Load Greenwave policies from the given policies directory. - - :param str policies_dir: A path points to the policies directory. - :return: A list of policies. - - """ - policy_pathnames = glob.glob(os.path.join(policies_dir, '*.yaml')) - policies = [] - for policy_pathname in policy_pathnames: - with open(policy_pathname, 'r') as f: - policies.extend(greenwave.policies.Policy.safe_load_all(f)) - log.debug("Loaded %i policies from %s", len(policies), policies_dir) - return policies - - def insert_headers(response): """ Insert the CORS headers for the give reponse if there are any configured for the application.