def new(cls, path, ckan_version, port=None): """ Return a Environment object with settings for a new project. No directories or containers are created by this call. :params path: location for new project directory, may be relative :params ckan_version: release of CKAN to install :params port: preferred port for local instance Raises DatcatsError if directories or project with same name already exits. """ workdir, name = path_split(abspath(expanduser(path))) if not valid_name(name): raise DatacatsError('Please choose an environment name starting' ' with a letter and including only lowercase letters' ' and digits') if not isdir(workdir): raise DatacatsError('Parent directory for environment' ' does not exist') datadir = expanduser('~/.datacats/' + name) target = workdir + '/' + name if isdir(datadir): raise DatacatsError('Environment data directory {0} already exists', (datadir,)) if isdir(target): raise DatacatsError('Environment directory already exists') environment = cls(name, target, datadir, ckan_version, port) environment._generate_passwords() return environment
def new(cls, path, ckan_version, port=None): """ Return a Environment object with settings for a new project. No directories or containers are created by this call. :params path: location for new project directory, may be relative :params ckan_version: release of CKAN to install :params port: preferred port for local instance Raises DatcatsError if directories or project with same name already exits. """ workdir, name = path_split(abspath(expanduser(path))) if not valid_name(name): raise DatacatsError( 'Please choose an environment name starting' ' with a letter and including only lowercase letters' ' and digits') if not isdir(workdir): raise DatacatsError('Parent directory for environment' ' does not exist') datadir = expanduser('~/.datacats/' + name) target = workdir + '/' + name if isdir(datadir): raise DatacatsError( 'Environment data directory {0} already exists', (datadir, )) if isdir(target): raise DatacatsError('Environment directory already exists') environment = cls(name, target, datadir, ckan_version, port) environment._generate_passwords() return environment
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
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
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
def load_site(srcdir, datadir, site_name=None): """ Load configuration values for a site. Returns (port, address, site_url, passwords) """ if site_name is None: site_name = 'primary' if not validate.valid_name(site_name): raise DatacatsError('{} is not a valid site name.'.format(site_name)) cp = ConfigParser.SafeConfigParser() try: cp.read([srcdir + '/.datacats-environment']) except ConfigParser.Error: raise DatacatsError('Error reading environment information') site_section = 'site_' + site_name try: port = cp.getint(site_section, 'port') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): port = None try: address = cp.get(site_section, 'address') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): address = None try: site_url = cp.get(site_section, 'site_url') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): site_url = None passwords = {} cp = ConfigParser.SafeConfigParser() cp.read(datadir + '/sites/' + site_name + '/passwords.ini') try: pw_options = cp.options('passwords') except ConfigParser.NoSectionError: pw_options = [] for n in pw_options: passwords[n.upper()] = cp.get('passwords', n) return port, address, site_url, passwords
def load_site(srcdir, datadir, site_name=None): """ Load configuration values for a site. Returns (port, address, site_url, passwords) """ if site_name is None: site_name = "primary" if not validate.valid_name(site_name): raise DatacatsError("{} is not a valid site name.".format(site_name)) cp = ConfigParser.SafeConfigParser() try: cp.read([srcdir + "/.datacats-environment"]) except ConfigParser.Error: raise DatacatsError("Error reading environment information") site_section = "site_" + site_name try: port = cp.getint(site_section, "port") except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): port = None try: address = cp.get(site_section, "address") except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): address = None try: site_url = cp.get(site_section, "site_url") except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): site_url = None passwords = {} cp = ConfigParser.SafeConfigParser() cp.read(datadir + "/sites/" + site_name + "/passwords.ini") try: pw_options = cp.options("passwords") except ConfigParser.NoSectionError: pw_options = [] for n in pw_options: passwords[n.upper()] = cp.get("passwords", n) return port, address, site_url, passwords
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
def test_name_with_leading_numbers(self): self.assertFalse(valid_name('42seven'))
def test_name_with_numbers(self): self.assertTrue(valid_name('seven42'))
def test_good_name(self): self.assertTrue(valid_name('copper'))
def load(cls, environment_name=None, data_only=False): """ Return an Environment object based on an existing project. :param environment_name: exising environment name, path or None to look in current or parent directories for project :param data_only: set to True to only load from data dir, not the project dir; Used for purging environment data. Raises DatacatsError if environment can't be found or if there is an error parsing the environment information. """ if environment_name is None: environment_name = '.' extension_dir = 'ckan' if valid_name(environment_name) and isdir( expanduser('~/.datacats/' + environment_name)): used_path = False datadir = expanduser('~/.datacats/' + environment_name) with open(datadir + '/project-dir') as pd: wd = pd.read() if not data_only and not exists(wd + '/.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"') else: used_path = True wd = abspath(environment_name) if not isdir(wd): raise DatacatsError('No environment found with that name') first_wd = wd oldwd = None while not exists(wd + '/.datacats-environment'): oldwd = wd wd, ignore = path_split(wd) if wd == oldwd: raise DatacatsError( 'Environment not found in {0} or above', first_wd) if oldwd: ignore, extension_dir = path_split(oldwd) if data_only and not used_path: return cls(environment_name, None, datadir) cp = SafeConfigParser() try: cp.read([wd + '/.datacats-environment']) except ConfigParserError: raise DatacatsError('Error reading environment information') name = cp.get('datacats', 'name') datadir = expanduser('~/.datacats/' + name) ckan_version = cp.get('datacats', 'ckan_version') try: port = cp.getint('datacats', 'port') except NoOptionError: port = None try: site_url = cp.get('datacats', 'site_url') except NoOptionError: site_url = None try: always_prod = cp.getboolean('datacats', 'always_prod') except NoOptionError: always_prod = False try: deploy_target = cp.get('deploy', 'target', None) except NoSectionError: deploy_target = None passwords = {} try: # backwards compatibility FIXME: remove this pw_options = cp.options('passwords') except NoSectionError: cp = SafeConfigParser() cp.read(datadir + '/passwords.ini') try: pw_options = cp.options('passwords') except NoSectionError: pw_options = [] for n in pw_options: passwords[n.upper()] = cp.get('passwords', n) environment = cls(name, wd, datadir, ckan_version, port, deploy_target, site_url=site_url, always_prod=always_prod, extension_dir=extension_dir) if passwords: environment.passwords = passwords else: environment._generate_passwords() if not used_path: environment._update_saved_project_dir() return environment