Ejemplo n.º 1
0
def new_environment_check(srcpath, site_name, ckan_version):
    """
    Check if a new environment or site can be created at the given path.

    Returns (name, datadir, sitedir, srcdir) or raises DatacatsError
    """
    docker.require_images()

    workdir, name = path.split(path.abspath(path.expanduser(srcpath)))

    if not validate.valid_name(name):
        raise DatacatsError(
            "Please choose an environment name starting"
            " with a letter and including only lowercase letters"
            " and digits"
        )
    if not path.isdir(workdir):
        raise DatacatsError("Parent directory for environment" " does not exist")

    datadir = path.expanduser("~/.datacats/" + name)
    sitedir = datadir + "/sites/" + site_name
    # We track through the datadir to the target if we are just making a
    # site
    if path.isdir(datadir):
        with open(datadir + "/project-dir") as pd:
            srcdir = pd.read()
    else:
        srcdir = workdir + "/" + name

    if ckan_version not in SUPPORTED_PRELOADS:
        raise DatacatsError(
            """Datacats does not currently support CKAN version {}.
Versions that are currently supported are: {}""".format(
                ckan_version, ", ".join(SUPPORTED_PRELOADS)
            )
        )

    preload_name = str(ckan_version)

    # Get all the versions from the tags
    downloaded_versions = [tag for tag in docker.get_tags("datacats/ckan")]

    if ckan_version not in downloaded_versions:
        retrying_pull_image("datacats/ckan:{}".format(preload_name))

    if path.isdir(sitedir):
        raise DatacatsError("Site data directory {0} already exists".format(sitedir))
    # This is the case where the data dir has been removed,
    if path.isdir(srcdir) and not path.isdir(datadir):
        raise DatacatsError(
            "Environment directory exists, but data directory does not.\n"
            "If you simply want to recreate the data directory, run "
            '"datacats init" in the environment directory.'
        )

    return name, datadir, srcdir
Ejemplo n.º 2
0
def new_environment_check(srcpath, site_name, ckan_version):
    """
    Check if a new environment or site can be created at the given path.

    Returns (name, datadir, sitedir, srcdir) or raises DatacatsError
    """
    docker.require_images()

    workdir, name = path.split(path.abspath(path.expanduser(srcpath)))

    if not validate.valid_name(name):
        raise DatacatsError(
            'Please choose an environment name starting'
            ' with a letter and including only lowercase letters'
            ' and digits')
    if not path.isdir(workdir):
        raise DatacatsError('Parent directory for environment'
                            ' does not exist')

    datadir = path.expanduser('~/.datacats/' + name)
    sitedir = datadir + '/sites/' + site_name
    # We track through the datadir to the target if we are just making a
    # site
    if path.isdir(datadir):
        with open(datadir + '/project-dir') as pd:
            srcdir = pd.read()
    else:
        srcdir = workdir + '/' + name

    if ckan_version not in SUPPORTED_PRELOADS:
        raise DatacatsError(
            '''Datacats does not currently support CKAN version {}.
Versions that are currently supported are: {}'''.format(
                ckan_version, ', '.join(SUPPORTED_PRELOADS)))

    preload_name = str(ckan_version)

    # Get all the versions from the tags
    downloaded_versions = [tag for tag in docker.get_tags('datacats/ckan')]

    if ckan_version not in downloaded_versions:
        retrying_pull_image('datacats/ckan:{}'.format(preload_name))

    if path.isdir(sitedir):
        raise DatacatsError(
            'Site data directory {0} already exists'.format(sitedir))
    # This is the case where the data dir has been removed,
    if path.isdir(srcdir) and not path.isdir(datadir):
        raise DatacatsError(
            'Environment directory exists, but data directory does not.\n'
            'If you simply want to recreate the data directory, run '
            '"datacats init" in the environment directory.')

    return name, datadir, srcdir
Ejemplo n.º 3
0
def find_environment_dirs(environment_name=None, data_only=False):
    """
    :param environment_name: exising environment name, path or None to
        look in current or parent directories for project

    returns (srcdir, extension_dir, datadir)

    extension_dir is the  name of extension directory user was in/referenced,
    default: 'ckan'. This value is used by the paster cli command.

    datadir will be None if environment_name was a path or None (not a name)
    """
    docker.require_images()

    if environment_name is None:
        environment_name = '.'

    extension_dir = 'ckan'
    if validate.valid_name(environment_name) and path.isdir(
            path.expanduser('~/.datacats/' + environment_name)):
        # loading from a name
        datadir = path.expanduser('~/.datacats/' + environment_name)
        with open(datadir + '/project-dir') as pd:
            srcdir = pd.read()

        if not data_only and not path.exists(srcdir +
                                             '/.datacats-environment'):
            raise DatacatsError(
                'Environment data found but environment directory is'
                ' missing. Try again from the new environment directory'
                ' location or remove the environment data with'
                ' "datacats purge"')

        return srcdir, extension_dir, datadir

    # loading from a path
    srcdir = path.abspath(environment_name)
    if not path.isdir(srcdir):
        raise DatacatsError('No environment found with that name')

    wd = srcdir
    oldwd = None
    while not path.exists(wd + '/.datacats-environment'):
        oldwd = wd
        wd, _ = path.split(wd)
        if wd == oldwd:
            raise DatacatsError(
                'Environment not found in {0} or above'.format(srcdir))
    srcdir = wd

    if oldwd:
        _, extension_dir = path.split(oldwd)

    return srcdir, extension_dir, None
Ejemplo n.º 4
0
def find_environment_dirs(environment_name=None, data_only=False):
    """
    :param environment_name: exising environment name, path or None to
        look in current or parent directories for project

    returns (srcdir, extension_dir, datadir)

    extension_dir is the  name of extension directory user was in/referenced,
    default: 'ckan'. This value is used by the paster cli command.

    datadir will be None if environment_name was a path or None (not a name)
    """
    docker.require_images()

    if environment_name is None:
        environment_name = '.'

    extension_dir = 'ckan'
    if validate.valid_name(environment_name) and path.isdir(
            path.expanduser('~/.datacats/' + environment_name)):
        # loading from a name
        datadir = path.expanduser('~/.datacats/' + environment_name)
        with open(datadir + '/project-dir') as pd:
            srcdir = pd.read()

        if not data_only and not path.exists(srcdir + '/.datacats-environment'):
            raise DatacatsError(
                'Environment data found but environment directory is'
                ' missing. Try again from the new environment directory'
                ' location or remove the environment data with'
                ' "datacats purge"')

        return srcdir, extension_dir, datadir

    # loading from a path
    srcdir = path.abspath(environment_name)
    if not path.isdir(srcdir):
        raise DatacatsError('No environment found with that name')

    wd = srcdir
    oldwd = None
    while not path.exists(wd + '/.datacats-environment'):
        oldwd = wd
        wd, _ = path.split(wd)
        if wd == oldwd:
            raise DatacatsError(
                'Environment not found in {0} or above'.format(srcdir))
    srcdir = wd

    if oldwd:
        _, extension_dir = path.split(oldwd)

    return srcdir, extension_dir, None
Ejemplo n.º 5
0
def convert_environment(datadir, version, always_yes):
    """
    Converts an environment TO the version specified by `version`.
    :param datadir: The datadir to convert.
    :param version: The version to convert TO.
    :param always_yes: True if the user shouldn't be prompted about the migration.
    """
    # Since we don't call either load() or new() we have to call require_images ourselves.
    require_images()

    inp = None
    old_version = _get_current_format(datadir)
    migration_func = migrations[(old_version, version)]

    if version > CURRENT_FORMAT_VERSION:
        raise DatacatsError('Cannot migrate to a version higher than the '
                            'current one.')
    if version < 1:
        raise DatacatsError('Datadir versioning starts at 1.')

    if not always_yes:
        while inp != 'y' and inp != 'n':
            inp = raw_input(migration_func.__doc__.format(version))

        if inp == 'n':
            sys.exit(1)

    lockfile = LockFile(path_join(datadir, '.migration_lock'))
    lockfile.acquire()

    try:
        # FIXME: If we wanted to, we could find a set of conversions which
        # would bring us up to the one we want if there's no direct path.
        # This isn't necessary with just two formats, but it may be useful
        # at 3.
        # Call the appropriate conversion function
        migration_func(datadir)
    finally:
        lockfile.release()
Ejemplo n.º 6
0
def new_environment_check(srcpath, site_name):
    """
    Check if a new environment or site can be created at the given path.

    Returns (name, datadir, sitedir, srcdir) or raises DatacatsError
    """
    docker.require_images()

    workdir, name = path.split(path.abspath(path.expanduser(srcpath)))

    if not validate.valid_name(name):
        raise DatacatsError('Please choose an environment name starting'
                            ' with a letter and including only lowercase letters'
                            ' and digits')
    if not path.isdir(workdir):
        raise DatacatsError('Parent directory for environment'
                            ' does not exist')

    datadir = path.expanduser('~/.datacats/' + name)
    sitedir = datadir + '/sites/' + site_name
    # We track through the datadir to the target if we are just making a
    # site
    if path.isdir(datadir):
        with open(datadir + '/project-dir') as pd:
            srcdir = pd.read()
    else:
        srcdir = workdir + '/' + name

    if path.isdir(sitedir):
        raise DatacatsError('Site data directory {0} already exists'.format(
                            sitedir))
    # This is the case where the data dir has been removed,
    if path.isdir(srcdir) and not path.isdir(datadir):
        raise DatacatsError('Environment directory exists, but data directory does not.\n'
                            'If you simply want to recreate the data directory, run '
                            '"datacats init" in the environment directory.')

    return name, datadir, srcdir