Example #1
0
def main(args=sys.argv[1:]):
    """Entry point for the adr module.

    When the adr module is called, this method is run.

    The argument list is parsed, and the appropriate parser or subparser is created.

    Using the argument list, arguments are parsed and grouped into a Namespace object
    representing known arguments, and a remainder list representing unknown arguments.

    The method then calls the appropriate method for the action specified.

    Supported use cases:

    $ adr recipe <recipe_name>
    $ adr query <query_name>
    $ adr <recipe_name>

    :param list args: command-line arguments.
    """
    # load config from file
    config = Configuration(os.path.join(here, 'config.yml'))

    # create parsers and subparsers.
    parser = ArgumentParser(description='Runs adr recipes and/or queries.')

    # check that adr is invoked with at least a recipe or subcommand.
    _check_tasks_exist(args)

    # determine if subparser are necessary.
    if 'query' in args or 'recipe' in args:
        subparser = parser.add_subparsers()

    if args[0] != 'query':
        # if subcommand query is not specified, default to recipe.
        if args[0] == 'recipe':
            recipe_parser = subparser.add_parser('recipe', help='Recipe subcommand.')
            recipe_parser = _build_parser_arguments(recipe_parser, config)
        else:
            parser = _build_parser_arguments(parser, config)
        parser.set_defaults(func=recipe_handler)
    else:
        query_parser = subparser.add_parser('query', help='Query subcommand.')
        query_parser = _build_parser_arguments(query_parser, config)
        query_parser.set_defaults(func=query_handler)

    # parse all arguments, then pass to appropriate handler.
    parsed_args, remainder = parser.parse_known_args()

    # Temporary disable debug if not query
    if not ('query' in args):
        parsed_args.debug = False

    # store all non-recipe/query args into config
    # From this point, only config stores all non-recipe/query args
    # Additional args will go to remainder
    config.update(vars(parsed_args))

    parsed_args.func(parsed_args, remainder, config)
Example #2
0
def test_recipe(patch_active_data, recipe_test, validate):
    try:
        patch_active_data(recipe_test)

        config = Configuration()
        config.fmt = "json"
        result = json.loads(
            run_recipe(recipe_test['recipe'], recipe_test['args'], config))

        validate(recipe_test, result)
    except Exception as e:
        if is_fail(recipe_test['recipe']):
            xfail(str(e))
        else:
            raise e
Example #3
0
def cli(args=sys.argv[1:]):
    parser = ArgumentParser()
    parser.add_argument('recipe', help='Recipe to generate test for')
    args, remainder = parser.parse_known_args(args)

    orig_run_query = query.run_query
    query_results = []

    def new_run_query(name, config, **context):
        context['limit'] = 10
        qgen = orig_run_query(name, config, **context)

        for result in qgen:
            mock_result = deepcopy(result)
            mock_result.pop('meta')
            query_results.append(mock_result)
            yield result

    query.run_query = new_run_query
    cfg = Configuration(os.path.join(os.path.dirname(here), 'config.yml'))
    cfg.fmt = 'json'

    result = run_recipe(args.recipe, remainder, cfg)
    test = OrderedDict()
    test['recipe'] = args.recipe
    test['args'] = remainder
    test['queries'] = query_results
    test['expected'] = json.loads(result)

    # get pyyaml to preserve ordering of dict keys
    def represent_ordereddict(dumper, data):
        value = []
        for item_key, item_value in data.items():
            node_key = dumper.represent_data(item_key)
            node_value = dumper.represent_data(item_value)

            value.append((node_key, node_value))
        return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', value)

    yaml.add_representer(OrderedDict, represent_ordereddict)

    path = os.path.join(test_dir, '{}.test'.format(args.recipe))
    with open(path, 'a') as fh:
        yaml.dump(test, fh)
def test_recipe(monkeypatch, recipe_test):
    monkeypatch.setattr(query, 'query_activedata', new_run_query(recipe_test))
    config = Configuration()
    config.fmt = "json"
    module = 'adr.recipes.{}'.format(recipe_test['recipe'])
    if module in sys.modules:
        reload(sys.modules[module])

    result = json.loads(
        run_recipe(recipe_test['recipe'], recipe_test['args'], config))

    buf = IO()
    yaml.dump(result, buf)
    print("Yaml formatted result for copy/paste:")
    print(buf.getvalue())

    buf = IO()
    yaml.dump(recipe_test['expected'], buf)
    print("\nYaml formatted expected:")
    print(buf.getvalue())
    assert result == recipe_test['expected']
Example #5
0
from flask import Flask, render_template, Markup
import os
import markdown
from adr import recipes
from adr.recipe import run_recipe
from adr.util.config import Configuration
import adr


app = Flask(__name__)
recipe_names = []
recipe_path = os.path.dirname(recipes.__file__)
config_path = os.path.join(os.path.dirname(adr.__file__), 'config.yml')
config = Configuration(config_path)


def get_recipes():
    """
    Return a list of recipes located in /adr/recipes path
    """
    for file in os.listdir(recipe_path):
        if file.endswith('.py') and file != '__init__.py':
            recipe_names.append(file.rsplit('.', 1)[0])


get_recipes()


@app.route('/')
def home():
    """
def config():
    config = Configuration()
    config.fmt = 'json'
    config.debug_url = "https://activedata.allizom.org/tools/query.html#query_id={}"
    return config