Ejemplo n.º 1
0
 def write_new_md5sum(self):
     """Write new md5sum of ``meta.yaml`` to ``conda-build/meta.md5``."""
     message(text='write md5sum of meta.yaml', color='bold', special='row')
     command = ('echo "$(md5sum $PWD/conda-build/meta.yaml)" | '
                'cut -d\' \' -f1 > $PWD/conda-build/meta.md5')
     run_in_bash(cmd=command)
     message(text='updated', color='green', special='end', indent=2)
Ejemplo n.º 2
0
    def __attrs_post_init__(self):
        """Set the more complex attributes of the project class."""
        try:
            meta_yaml = read_meta_yaml(Path.cwd())
        except FileNotFoundError:
            message(text='project has no meta.yaml!', color='red')
            exit(1)
        settings = meta_yaml['extra']['cenv']
        dependencies = extract_dependencies_from_meta_yaml(meta_yaml)

        config = read_config()
        self.is_git = (Path.cwd() / self.rules.git_folder).exists()
        self.export_environment_yml = config['export_environment_yml']
        self.conda_folder = Path(config['conda_folder'])
        self.env_folder = Path(config['env_folder'])
        self.env_name = settings['env_name']
        self.dependencies = dependencies
        self.is_env = self.env_name in self.collect_available_envs()
        conda_bin = self.rules.conda_cmds.conda_bin(self.conda_folder)
        self.cmds = self.rules.conda_cmds
        self.cmd_kwargs = {
            'conda': conda_bin,
            'name': self.env_name,
            'pkgs': ' '.join([f'"{_}"' for _ in self.dependencies]),
        }
Ejemplo n.º 3
0
    def create_environment(self, cloned: bool) -> NoReturn:
        """Create the environment for the project.

        Try to create the environment for the project. If the environment
        already existed and a backup was made and any error occure, restore the
        backup environment.
        If everything worked correctly finally remove the backup (if one was
        made).

        Parameters:
            cloned: indicates if the environment already existed and a backup
                was created.

        """
        message(text='Create environment', color='bold', special='row')

        try:
            run_in_bash(cmd=self.cmds.create.format(**self.cmd_kwargs))
        except CenvProcessError:
            self._restore_environment_from_backup(cloned=cloned)
            exit(1)

        if cloned:
            message(text='Clear backup', color='bold', special='row', indent=2)
            run_in_bash(cmd=self.cmds.clean.format(**self.cmd_kwargs))
            message(text='Cleared', color='green', special='end', indent=3)

        message(text='Created', color='green', special='end', indent=2)
Ejemplo n.º 4
0
    def update(self) -> NoReturn:
        """Create / recreate the conda environment of the current project.

        If the conda environment already exists, clone the environment as a
        backup and then remove original environment. Then create the new
        conda environment. If a backup was created it is
        removed afterwards. If any errors occurs during creation of the new
        environment, recreate the old environment from backup and remove the
        backup afterwards. If activated in the config-file, export the
        environment-definition of the created environment to an
        ``environment.yml`` file. Finally store the md5sum of the meta.yaml for
        the autoupdate feature.
        """
        if self.is_env:
            message(text=f'Updating {self.env_name}', color='cyan')
        else:
            message(text=f'Creating {self.env_name}', color='cyan')

        cloned = self._handle_existing_environment()

        self.create_environment(cloned=cloned)

        if self.export_environment_yml:
            self.export_environment_definition()

        self.write_new_md5sum()

        message(text='Done', color='green', special='end')
Ejemplo n.º 5
0
    def clone_environment_as_backup(self) -> NoReturn:
        """Clone the existing environment as a backup.

        If the backup already exists, the previous backup is removed, then
        the new one is created by cloning the current project environment.
        """
        backup_name = f'{self.env_name}_backup'
        if backup_name in self.collect_available_envs():
            message(text='Clear old backup', color='bold', special='row')
            self._remove_backup_environment()
            message(text='Cleared', color='green', special='end', indent=2)
        message(text='Create backup', color='bold', special='row')
        run_in_bash(cmd=self.cmds.clone.format(**self.cmd_kwargs))
        message(text='Created', color='green', special='end', indent=2)
Ejemplo n.º 6
0
    def _restore_environment_from_backup(self, cloned: bool) -> NoReturn:
        """Restore the environment from the cloned backup environment.

        After restore the backup environment is removed.

        Parameters:
            cloned: indicates if the environment already existed and a backup
                was created.

        """
        message(text='Error during creation!', color='red', special='row')
        if self.is_env and cloned:
            message(text='Recreating backup', color='bold', special='row')
            run_in_bash(cmd=self.cmds.restore.format(**self.cmd_kwargs))
            self._remove_backup_environment()
            message(text='Recreated', color='green', special='end', indent=2)
        message(text='Exit', color='red', special='end')
Ejemplo n.º 7
0
    def _remove_previous_environment(self) -> NoReturn:
        """Remove old version of project environment.

        If the old environment can't be removed, the backup made is removed.
        """
        try:
            message(text='Remove existing env', color='bold', special='row')
            run_in_bash(cmd=self.cmds.remove.format(**self.cmd_kwargs))
            message(text='Removed', color='green', special='end', indent=2)
        except CenvProcessError:
            self._remove_backup_environment()
            message(
                text=('Could not remove environment because it is '
                      'activated! Please deactivate it first.'),
                color='red',
            )
            exit(1)
Ejemplo n.º 8
0
 def export_environment_definition(self) -> NoReturn:
     """Export projects environment definition to an ``environment.yml``."""
     message(text='Export environment.yml ...', color='bold', special='row')
     run_in_bash(cmd=self.cmds.export.format(**self.cmd_kwargs))
     message(text='Exported', color='green', special='end', indent=2)
Ejemplo n.º 9
0
def initialize_cenv(
    config_path: Path,
    autoenv_script_path: Path,
    autoenv_script_source_path: Path,
    config_file: Path,
    config_file_source: Path,
    zshrc: Path,
    bashrc: Path,
) -> NoReturn:
    """
    Install user-config and cenv.sh for autoactivate and autoupdate.

    Parameters:
        config_path: the path for cenv config-stuff.
        autoenv_script_path: the path to install the cenv.sh script to.
        autoenv_script_source_path: the path where to get the cenv.sh script from
        config_file: the path to install the user-config into.
        config_file_source: the path where to get the config file from.
        zshrc: the path to the users .zshrc
        bashrc: the path to the users .bashrc

    """
    if not config_path.exists():
        message(text='creating cenv-config folder', color='cyan')
        config_path.mkdir(parents=True)
        message(text='created cenv-config folder', color='green')
    else:
        message(text='cenv-config folder already exists', color='green')

    if not autoenv_script_path.exists():
        message(
            text=('copying script for autoactivation and autoupdate to '
                  'config-folder'),
            color='cyan',
        )
        autoenv_script_path.write_text(autoenv_script_source_path.read_text())
        message(
            text='copied script for autoactivateion and autoupdate',
            color='green',
        )
    else:
        message(
            text='script for autoactivation and autupdate already copied',
            color='green',
        )

    if not config_file.exists():
        message(
            text='copying config-file to cenv-config-folder ...',
            color='cyan',
        )
        config_file.write_text(config_file_source.read_text())
        message(text='copied config-file', color='green')
    else:
        message(text='config-file already exists', color='green')

    for shell_config in (zshrc, bashrc):
        if shell_config.exists():
            if RC_CONTENT not in shell_config.read_text():
                message(
                    text=f'initilized cenv in {shell_config}',
                    color='green',
                )
                with open(shell_config, 'a') as opened_shell_config:
                    opened_shell_config.write(RC_CONTENT)
            else:
                message(
                    text=f'cenv already initialized in {shell_config}',
                    color='green',
                )