Beispiel #1
0
 def _add_recipes(self):
     """
     Add recipe location:
     - If a prefix was explicitly selected, install it there
     - Otherwise, use local config file
     - Check alias is not already used
     """
     alias = self.args.alias
     uri = self.args.uri[0]
     self.log.debug(
         "Preparing to add recipe location {name} -> {uri}".format(
             name=alias, uri=uri))
     # Check recipe location alias is valid:
     if re.match(r'[a-z][a-z0-9_]*', alias) is None:
         self.log.error("Invalid recipe alias: {alias}".format(alias=alias))
         exit(1)
     if self.cfg.get_named_recipe_dirs().has_key(alias):
         if self.args.force:
             self.log.info(
                 "Overwriting existing recipe alias `{0}'".format(alias))
         elif not confirm(
                 "Alias `{0}' already exists, overwrite?".format(alias)):
             self.log.warn('Aborting.')
             return -1
     store_to_prefix = self.prefix is not None and self.prefix.prefix_src in (
         "cli", "cwd")
     cfg_file = None
     recipe_cache = None
     if store_to_prefix:
         cfg_file = self.prefix.cfg_file
         recipe_cache_top_level = os.path.join(self.prefix.prefix_cfg_dir,
                                               self.cfg.recipe_cache_dir)
     else:
         cfg_file = self.cfg.local_cfg
         recipe_cache_top_level = os.path.join(self.cfg.local_cfg_dir,
                                               self.cfg.recipe_cache_dir)
     if not os.path.isdir(recipe_cache_top_level):
         self.log.debug(
             "Recipe cache dir does not exist, creating {0}".format(
                 recipe_cache_top_level))
         os.mkdir(recipe_cache_top_level)
     recipe_cache = os.path.join(recipe_cache_top_level, alias)
     self.log.debug("Storing new recipe location to {cfg_file}".format(
         cfg_file=cfg_file))
     assert cfg_file is not None
     assert os.path.isdir(recipe_cache_top_level)
     assert recipe_cache is not None
     assert alias
     # Now make sure we don't already have a cache dir
     if os.path.isdir(recipe_cache):
         self.log.warn(
             "Cache dir {cdir} for remote recipe location {alias} already exists! Deleting."
             .format(cdir=recipe_cache, alias=alias))
         shutil.rmtree(recipe_cache)
     # Let the fetcher download the location
     self.log.debug("Fetching into directory: {0}/{1}".format(
         recipe_cache_top_level, alias))
     Fetcher().fetch_url(uri, recipe_cache_top_level, alias, {})  # No args
     # Write this to config file
     self.cfg.update_cfg_file({'recipes': {alias: uri}}, cfg_file=cfg_file)
Beispiel #2
0
 def register_alias(alias, path):
     " Write the prefix alias to the config file "
     if self.prefix is not None and \
             self.prefix.prefix_aliases.get(alias) is not None \
             and not confirm("Alias `{0}' already exists, overwrite?".format(alias)):
         self.log.warn('Aborting.')
         raise PBException("Could not create alias.")
     self.cfg.update_cfg_file({'prefix_aliases': {alias: path}})
Beispiel #3
0
 def add_recipe_dir(self, alias, uri):
     """
     Add recipe location:
     - If a prefix was explicitly selected, install it there
     - Otherwise, use local config file
     - Check alias is not already used
     """
     self.log.debug("Preparing to add recipe location {name} -> {uri}".format(
         name=alias, uri=uri
     ))
     # Check recipe location alias is valid:
     if re.match(r'[a-z][a-z0-9_]*', alias) is None:
         self.log.error("Invalid recipe alias: {alias}".format(alias=alias))
         return False
     if alias in self.cfg.get_named_recipe_dirs():
         if self.args.force:
             self.log.info("Overwriting existing recipe alias `{0}'".format(alias))
         elif not confirm("Alias `{0}' already exists, overwrite?".format(alias)):
             self.log.warn('Aborting.')
             return False
     store_to_prefix = self.prefix is not None and self.prefix.prefix_src in ("cli", "cwd")
     cfg_file = None
     recipe_cache = None
     if store_to_prefix:
         cfg_file = self.prefix.cfg_file
         recipe_cache_top_level = os.path.join(self.prefix.prefix_cfg_dir, self.cfg.recipe_cache_dir)
     else:
         cfg_file = self.cfg.local_cfg
         recipe_cache_top_level = os.path.join(self.cfg.local_cfg_dir, self.cfg.recipe_cache_dir)
     if not os.path.isdir(recipe_cache_top_level):
         self.log.debug("Recipe cache dir does not exist, creating {0}".format(recipe_cache_top_level))
         os.mkdir(recipe_cache_top_level)
     recipe_cache = os.path.join(recipe_cache_top_level, alias)
     self.log.debug("Storing new recipe location to {cfg_file}".format(cfg_file=cfg_file))
     assert cfg_file is not None
     assert os.path.isdir(recipe_cache_top_level)
     assert recipe_cache is not None
     assert alias
     # Now make sure we don't already have a cache dir
     if os.path.isdir(recipe_cache):
         self.log.warn("Cache dir {cdir} for remote recipe location {alias} already exists! Deleting.".format(
             cdir=recipe_cache, alias=alias
         ))
         shutil.rmtree(recipe_cache)
     if not os.path.isdir(os.path.normpath(os.path.expanduser(uri))):
         # Let the fetcher download the location
         self.log.debug("Fetching into directory: {0}/{1}".format(recipe_cache_top_level, alias))
         try:
             Fetcher().fetch_url(uri, recipe_cache_top_level, alias, {}) # No args
         except PBException as ex:
             self.log.error("Could not fetch recipes: {s}".format(str(ex)))
             return False
     # Write this to config file
     self.cfg.update_cfg_file({'recipes': {alias: uri}}, cfg_file=cfg_file)
     return True
Beispiel #4
0
 def check_path_is_valid(path):
     " Returns True if we can use path to init our prefix "
     try:
         if not sysutils.mkdir_writable(path, self.log):
             raise PBException("Could not create writable directory.")
     except PBException:
         self.log.error("Cannot write to prefix path `{0}'.".format(path))
         return False
     from pybombs import config_manager
     if op.exists(op.join(path, config_manager.PrefixInfo.prefix_conf_dir)):
         self.log.warn("There already is a prefix in `{0}'.".format(path))
         if not confirm("Continue using this path?"):
             self.log.error("Aborting. A prefix already exists in `{0}'".format(path))
             return False
     return True
Beispiel #5
0
    def _run_init(self):
        """
        pybombs prefix init
        """
        # Make sure the directory is writable
        path = op.abspath(op.normpath(self.args.path))
        if not op.isdir(path):
            self.log.info("Creating directory `{0}'".format(path))
            os.mkdir(path)
        assert op.isdir(path)
        if not os.access(path, os.W_OK | os.X_OK):
            self.log.error("Cannot write to prefix path `{0}'.".format(path))
            exit(1)

        # Make sure that a pybombs directory doesn't already exist
        test_path = op.join(path, ".pybombs")
        if op.exists(test_path):
            self.log.error(
                "Ignoring. A prefix already exists in `{0}'".format(path))
            return

        # Copy template
        # TODO: I'm not too happy about this, all the hard coded stuff. Needs
        # cleaning up, for sure. Especially that setup_env.sh stuff at the end.
        # Ideally, we could switch prefix templates.
        self.log.info("Initializing PyBOMBS prefix in `{0}'...".format(path))
        skel_dir = op.join(self.cfg.module_dir, 'skel')
        for p in os.listdir(skel_dir):
            if op.exists(op.join(path, p)):
                self.log.obnoxious("Skipping {0}".format(p))
                continue
            self.log.obnoxious("Copying {0}".format(p))
            p_full = op.join(skel_dir, p)
            if op.isdir(p_full):
                shutil.copytree(p_full,
                                op.join(path, p),
                                ignore=shutil.ignore_patterns('.ignore'))
            else:
                shutil.copy(p_full, path)
        open(op.join(path, 'setup_env.sh'), 'w').write(
            open(op.join(skel_dir,
                         'setup_env.sh')).read().format(prefix_dir=path, ))
        # Register alias
        if self.args.alias is not None:
            if self.prefix is not None and \
                self.prefix.prefix_aliases.get(self.args.alias) is not None \
                and not confirm("Alias `{0}' already exists, overwrite?".format(self.args.alias)):
                self.log.warn('Aborting.')
                return 1
            self.cfg.update_cfg_file(
                {'prefix_aliases': {
                    self.args.alias: path
                }})
        # Create virtualenv if so desired
        if self.args.virtualenv:
            self.log.info("Creating Python virtualenv in prefix...")
            venv_args = ['virtualenv']
            venv_args.append(path)
            subproc.monitor_process(args=venv_args)
        # Install SDK if so desired
        if self.args.sdkname is not None:
            self.log.info("Reloading configuration...")
            self.cfg.load(select_prefix=path)
            self.prefix = self.cfg.get_active_prefix()
            self.inventory = self.prefix.inventory
            self._install_sdk_to_prefix(self.args.sdkname)
Beispiel #6
0
    def _run_init(self):
        """
        pybombs prefix init
        """
        # Make sure the directory is writable
        path = op.abspath(op.normpath(self.args.path))
        if not op.isdir(path):
            self.log.info("Creating directory `{0}'".format(path))
            os.mkdir(path)
        assert op.isdir(path)
        if not os.access(path, os.W_OK|os.X_OK):
            self.log.error("Cannot write to prefix path `{0}'.".format(path))
            exit(1)

        # Make sure that a pybombs directory doesn't already exist
        test_path = op.join(path, ".pybombs")
        if op.exists(test_path):
            self.log.error("Ignoring. A prefix already exists in `{0}'".format(path))
            return

        # Copy template
        # TODO: I'm not too happy about this, all the hard coded stuff. Needs
        # cleaning up, for sure. Especially that setup_env.sh stuff at the end.
        # Ideally, we could switch prefix templates.
        self.log.info("Initializing PyBOMBS prefix in `{0}'...".format(path))
        skel_dir = op.join(self.cfg.module_dir, 'skel')
        for p in os.listdir(skel_dir):
            if op.exists(op.join(path, p)):
                self.log.obnoxious("Skipping {0}".format(p))
                continue
            self.log.obnoxious("Copying {0}".format(p))
            p_full = op.join(skel_dir, p)
            if op.isdir(p_full):
                shutil.copytree(
                    p_full, op.join(path, p),
                    ignore=shutil.ignore_patterns('.ignore')
                )
            else:
                shutil.copy(p_full, path)
        open(op.join(path, 'setup_env.sh'), 'w').write(
            open(op.join(skel_dir, 'setup_env.sh')).read().format(
                prefix_dir=path,
            )
        )
        # Register alias
        if self.args.alias is not None:
            if self.prefix is not None and \
                self.prefix.prefix_aliases.get(self.args.alias) is not None \
                and not confirm("Alias `{0}' already exists, overwrite?".format(self.args.alias)):
                    self.log.warn('Aborting.')
                    return 1
            self.cfg.update_cfg_file({'prefix_aliases': {self.args.alias: path}})
        # Create virtualenv if so desired
        if self.args.virtualenv:
            self.log.info("Creating Python virtualenv in prefix...")
            venv_args = ['virtualenv']
            venv_args.append(path)
            subproc.monitor_process(args=venv_args)
        # Install SDK if so desired
        if self.args.sdkname is not None:
            self.log.info("Reloading configuration...")
            self.cfg.load(select_prefix=path)
            self.prefix = self.cfg.get_active_prefix()
            self.inventory = self.prefix.inventory
            self._install_sdk_to_prefix(self.args.sdkname)