Exemplo n.º 1
0
def run(ctx, host, port, disable_reloader, threaded, extra_files, processes):
    u"""Runs the Werkzeug development server"""
    use_reloader = not disable_reloader
    threaded = threaded or tk.asbool(config.get(u"ckan.devserver.threaded"))
    processes = processes or tk.asint(
        config.get(u"ckan.devserver.multiprocess", 1)
    )
    if threaded and processes > 1:
        tk.error_shout(u"Cannot have a multithreaded and multi process server")
        raise click.Abort()

    log.info(u"Running server {0} on port {1}".format(host, port))

    config_extra_files = tk.aslist(
        config.get(u"ckan.devserver.watch_patterns")
    )
    extra_files = list(extra_files) + [
        config[u"__file__"]
    ] + config_extra_files

    run_simple(
        host,
        port,
        ctx.obj.app,
        use_reloader=use_reloader,
        use_evalex=True,
        threaded=threaded,
        processes=processes,
        extra_files=extra_files,
    )
Exemplo n.º 2
0
def bye(name):
    """Command with optional argument.
    """
    if not name:
        tk.error_shout(u"I do not know your name.")
    else:
        click.secho(u"Bye, {}".format(name))
Exemplo n.º 3
0
def run(ctx, host, port, disable_reloader, threaded, extra_files, processes,
        ssl_cert, ssl_key):
    u"""Runs the Werkzeug development server"""

    if tk.asbool(config.get("debug")):
        warnings.filterwarnings("default", category=CkanDeprecationWarning)

    # Reloading
    use_reloader = not disable_reloader
    config_extra_files = tk.aslist(
        config.get(u"ckan.devserver.watch_patterns")
    )
    extra_files = list(extra_files) + [
        config[u"__file__"]
    ] + config_extra_files

    # Threads and processes
    threaded = threaded or tk.asbool(config.get(u"ckan.devserver.threaded"))
    processes = processes or tk.asint(
        config.get(u"ckan.devserver.multiprocess", 1)
    )
    if threaded and processes > 1:
        tk.error_shout(u"Cannot have a multithreaded and multi process server")
        raise click.Abort()

    # SSL
    cert_file = ssl_cert or config.get('ckan.devserver.ssl_cert')
    key_file = ssl_key or config.get('ckan.devserver.ssl_key')

    if cert_file and key_file:
        if cert_file == key_file == 'adhoc':
            ssl_context = 'adhoc'
        else:
            ssl_context = (ssl_cert, ssl_key)
    else:
        ssl_context = None

    host = host or config.get('ckan.devserver.host', DEFAULT_HOST)
    port = port or config.get('ckan.devserver.port', DEFAULT_PORT)
    try:
        port = int(port)
    except ValueError:
        tk.error_shout(u"Server port must be an integer, not {}".format(port))
        raise click.Abort()

    log.info(u"Running CKAN on {scheme}://{host}:{port}".format(
        scheme='https' if ssl_context else 'http', host=host, port=port))

    run_simple(
        host,
        port,
        ctx.obj.app,
        use_reloader=use_reloader,
        use_evalex=True,
        threaded=threaded,
        processes=processes,
        extra_files=extra_files,
        ssl_context=ssl_context,
    )
Exemplo n.º 4
0
def clean():
    """Clean the database.
    """
    try:
        model.repo.clean_db()
    except Exception as e:
        tk.error_shout(e)
    else:
        click.secho(u'Cleaning DB: SUCCESS', fg=u'green', bold=True)
Exemplo n.º 5
0
def init():
    """Initialize the database.
    """
    log.info(u"Initialize the Database")
    try:
        model.repo.init_db()
    except Exception as e:
        tk.error_shout(e)
    else:
        click.secho(u'Initialising DB: SUCCESS', fg=u'green', bold=True)
Exemplo n.º 6
0
def downgrade(version, plugin):
    """Downgrade the database.
    """
    try:
        import ckan.model as model
        model.repo._alembic_ini = _resolve_alembic_config(plugin)
        model.repo.downgrade_db(version)
    except Exception as e:
        tk.error_shout(e)
    else:
        click.secho(u'Downgrading DB: SUCCESS', fg=u'green', bold=True)
def rebuild(verbose, force, refresh, only_missing, quiet, commit_each):
    u''' Rebuild search index '''
    from ckan.lib.search import rebuild, commit
    try:
        rebuild(only_missing=only_missing,
                force=force,
                refresh=refresh,
                defer_commit=(not commit_each),
                quiet=quiet)
    except Exception as e:
        tk.error_shout(e)
    if not commit_each:
        commit()
Exemplo n.º 8
0
def _version_hash_to_ordinal(version):
    if u'base' == version:
        return 0
    versions_dir = os.path.join(os.path.dirname(migration_repo.__file__),
                                u'versions')
    versions = sorted(os.listdir(versions_dir))

    # latest version looks like `123abc (head)`
    if version.endswith(u'(head)'):
        return int(versions[-1].split(u'_')[0])
    for name in versions:
        if version in name:
            return int(name.split(u'_')[0])
    tk.error_shout(u'Version `{}` was not found in {}'.format(
        version, versions_dir))
Exemplo n.º 9
0
def run(ctx, host, port, disable_reloader, threaded, extra_files, processes,
        ssl_cert, ssl_key):
    u"""Runs the Werkzeug development server"""

    # Reloading
    use_reloader = not disable_reloader
    config_extra_files = tk.aslist(
        config.get(u"ckan.devserver.watch_patterns"))
    extra_files = list(extra_files) + [config[u"__file__"]
                                       ] + config_extra_files

    # Threads and processes
    threaded = threaded or tk.asbool(config.get(u"ckan.devserver.threaded"))
    processes = processes or tk.asint(
        config.get(u"ckan.devserver.multiprocess", 1))
    if threaded and processes > 1:
        tk.error_shout(u"Cannot have a multithreaded and multi process server")
        raise click.Abort()

    # SSL
    cert_file = ssl_cert or config.get('ckan.devserver.ssl_cert')
    key_file = ssl_key or config.get('ckan.devserver.ssl_key')

    if cert_file and key_file:
        if cert_file == key_file == 'adhoc':
            ssl_context = 'adhoc'
        else:
            ssl_context = (ssl_cert, ssl_key)
    else:
        ssl_context = None

    log.info(u"Running CKAN on {scheme}://{host}:{port}".format(
        scheme='https' if ssl_context else 'http', host=host, port=port))

    run_simple(
        host,
        port,
        ctx.obj.app,
        use_reloader=use_reloader,
        use_evalex=True,
        threaded=threaded,
        processes=processes,
        extra_files=extra_files,
        ssl_context=ssl_context,
    )
Exemplo n.º 10
0
def _resolve_alembic_config(plugin):
    if plugin:
        plugin_obj = p.get_plugin(plugin)
        if plugin_obj is None:
            tk.error_shout(u"Plugin '{}' cannot be loaded.".format(plugin))
            raise click.Abort()
        plugin_dir = os.path.dirname(inspect.getsourcefile(type(plugin_obj)))

        # if there is `plugin` folder instead of single_file, find
        # plugin's parent dir
        ckanext_idx = plugin_dir.rfind(u"/ckanext/") + 9
        idx = plugin_dir.find(u"/", ckanext_idx)
        if ~idx:
            plugin_dir = plugin_dir[:idx]
        migration_dir = os.path.join(plugin_dir, u"migration", plugin)
    else:
        import ckan.migration as _cm
        migration_dir = os.path.dirname(_cm.__file__)
    return os.path.join(migration_dir, u"alembic.ini")
Exemplo n.º 11
0
def show(extract_id):
    show = tk.get_action('extractor_show')
    ids = _get_ids(True)
    for i, id in enumerate(ids):
        try:
            result = show({}, {'id': id})
        except tk.NotFound as e:
            tk.error_shout(e)
            continue
        print('{}:'.format(id))
        for key in sorted(result):
            if key in ('resource_id', 'meta'):
                continue
            print('  {}: {!r}'.format(key, result[key]))
        print('  meta:')
        meta = result['meta']
        for key in sorted(meta):
            print('    {}: {!r}'.format(key, _compress(meta[key])))
        if i < len(ids) - 1:
            print('')
Exemplo n.º 12
0
def _get_ids(ids, only_with_metadata=False):
    """
    Get list of resource IDs from command line arguments.

    Returns the specific IDs listed or all IDs if ``all`` was passed.

    If ``only_with_metadata`` is true and ``all`` was passed then only
    IDs of resources which have metadata are returned.
    """

    if len(ids) < 1:
        tk.error_shout('Missing argument. Specify one or more resource IDs ' +
                       'or "all".')
    if len(ids) == 1 and ids[0].lower() == 'all':
        if only_with_metadata:
            return sorted(tk.get_action('extractor_list')({}, {}))
        else:
            return sorted(r.id for r in model.Resource.active())
    else:
        return ids[:]
Exemplo n.º 13
0
def migration(plugin, message):
    """Create new alembic revision for DB migration.
    """
    import ckan.model
    if not tk.config:
        tk.error_shout(u'Config is not loaded')
        raise click.Abort()
    config = CKANAlembicConfig(_resolve_alembic_config(plugin))
    migration_dir = os.path.dirname(config.config_file_name)
    config.set_main_option(u"sqlalchemy.url",
                           str(ckan.model.repo.metadata.bind.url))
    config.set_main_option(u'script_location', migration_dir)

    if not os.path.exists(os.path.join(migration_dir, u'script.py.mako')):
        alembic.command.init(config, migration_dir)

    rev = alembic.command.revision(config, message)
    click.secho(
        u"Revision file created. Now, you need to update it: \n\t{}".format(
            rev.path),
        fg=u"green")
Exemplo n.º 14
0
def duplicate_emails():
    u'''Check users email for duplicate'''
    log.info(u"Searching for accounts with duplicate emails.")

    q = model.Session.query(model.User.email,
                            model.User.name) \
        .filter(model.User.state == u"active") \
        .filter(model.User.email != u"") \
        .order_by(model.User.email).all()

    if not q:
        log.info(u"No duplicate emails found")
    try:
        for k, grp in groupby(q, lambda x: x[0]):
            users = [user[1] for user in grp]
            if len(users) > 1:
                s = u"{} appears {} time(s). Users: {}"
                click.secho(s.format(k, len(users), u", ".join(users)),
                            fg=u"green",
                            bold=True)
    except Exception as e:
        tk.error_shout(e)
Exemplo n.º 15
0
def extension(output_dir):
    """Generate empty extension files to expand CKAN.
    """
    try:
        from cookiecutter.main import cookiecutter
    except ImportError:
        tk.error_shout(u"`cookiecutter` library is missing from import path.")
        tk.error_shout(u"Make sure you have dev-dependencies installed:")
        tk.error_shout(u"\tpip install -r dev-requirements.txt")
        raise click.Abort()

    cur_loc = os.path.dirname(os.path.abspath(__file__))
    os.chdir(cur_loc)
    os.chdir(u'../../contrib/cookiecutter/ckan_extension/')
    template_loc = os.getcwd()

    # Prompt user for information
    click.echo(u"\n")
    while True:
        name = click.prompt(u"Extension's name",
                            default=u"must begin 'ckanext-'")
        if not name.startswith(u"ckanext-"):
            print(u"ERROR: Project name must start with 'ckanext-' > {}\n".
                  format(name))
        else:
            break

    author = click.prompt(u"Author's name", default=u"")
    email = click.prompt(u"Author's email", default=u"")
    github = click.prompt(u"Your Github user or organization name",
                          default=u"")
    description = click.prompt(u"Brief description of the project",
                               default=u"")
    keywords = click.prompt(u"List of keywords (separated by spaces)",
                            default=u"CKAN")

    # Ensure one instance of 'CKAN' in keywords
    keywords = [u"CKAN"] + [
        k for k in keywords.strip().split() if k.lower() != u"ckan"
    ]
    keywords = u' '.join(keywords)

    # Set short name and plugin class name
    project_short = name[8:].lower().replace(u'-', u'_')
    plugin_class_name = project_short.title().replace(u'_', u'') + u'Plugin'

    include_examples = int(
        click.confirm("Do you want to include code examples?"))
    context = {
        u"project": name,
        u"description": description,
        u"author": author,
        u"author_email": email,
        u"keywords": keywords,
        u"github_user_name": github,
        u"project_shortname": project_short,
        u"plugin_class_name": plugin_class_name,
        u"include_examples": include_examples,
        u"_source": u"cli",
    }

    if output_dir == u'.':
        os.chdir(u'../../../..')
        output_dir = os.getcwd()

    cookiecutter(template_loc,
                 no_input=True,
                 extra_context=context,
                 output_dir=output_dir)

    if not include_examples:
        remove_code_examples(
            os.path.join(output_dir, context["project"], "ckanext",
                         project_short))

    print(u"\nWritten: {}/{}".format(output_dir, name))
Exemplo n.º 16
0
def init():
    """Creates new syndication table."""
    tk.error_shout(
        "`ckan syndicate init` is not required and takes no effect anymore"
    )