Exemplo n.º 1
0
    def get_repo(path: Optional[str] = None,
                 experiment_name: Optional[str] = None) -> AimRepo:
        # Auto commit
        if os.getenv(AIM_AUTOMATED_EXEC_ENV_VAR):
            # Get Aim environment variables
            branch_name = os.getenv(AIM_BRANCH_ENV_VAR)
            commit_hash = os.getenv(AIM_COMMIT_ENV_VAR)
        else:
            commit_hash = AimRepo.generate_commit_hash()
            if experiment_name is not None:
                branch_name = experiment_name
            else:
                # FIXME: Get active experiment name from given repo
                #  if path is specified. Currently active experiment name of
                #  the highest repo in the hierarchy will be returned.
                branch_name = AimRepo.get_active_branch_if_exists() \
                              or AIM_DEFAULT_BRANCH_NAME

        if path is not None:
            repo = AimRepo(path)
            if not repo.exists():
                if not repo.init():
                    raise ValueError('can not create repo `{}`'.format(path))
            repo = AimRepo(path, branch_name, commit_hash)
        else:
            if AimRepo.get_working_repo() is None:
                path = os.getcwd()
                repo = AimRepo(path)
                if not repo.init():
                    raise ValueError('can not create repo `{}`'.format(path))
                repo = AimRepo(path, branch_name, commit_hash)
            else:
                repo = AimRepo.get_working_repo(branch_name, commit_hash)

        return repo
Exemplo n.º 2
0
    def _parse_env_vars(self, env_vars):
        env_vars = env_vars or ''
        env_vars_arr = env_vars.split(' ')
        filtered_env_vars = []

        automated = False
        automated_branch = None
        automated_commit = None
        for e in env_vars_arr:
            if AIM_AUTOMATED_EXEC_ENV_VAR in e:
                automated = True
            elif AIM_BRANCH_ENV_VAR in e:
                _, _, automated_branch = e.rpartition('=')
            else:
                filtered_env_vars.append(e)

        if automated:
            if not automated_branch:
                automated_branch = AIM_DEFAULT_BRANCH_NAME
            automated_commit = AimRepo.generate_commit_hash()

            filtered_env_vars.append('{}={}'.format(AIM_BRANCH_ENV_VAR,
                                                    automated_branch))
            filtered_env_vars.append('{}={}'.format(AIM_COMMIT_ENV_VAR,
                                                    automated_commit))
            filtered_env_vars.append('{}={}'.format(AIM_PROCESS_ENV_VAR,
                                                    self.process_uuid))
            filtered_env_vars.append('{}=1'.format(AIM_AUTOMATED_EXEC_ENV_VAR))

        return {
            'env_vars': ' '.join(filtered_env_vars),
            'automated': automated,
            'automated_branch': automated_branch,
            'automated_commit': automated_commit,
        }
Exemplo n.º 3
0
    def test_run_archivation(self):
        """
        Test aim repo can be removed correctly.
        """
        prefix = os.getcwd() + '/'
        with tempfile.TemporaryDirectory(prefix=prefix) as tmp_dir_path:
            AimRepo(tmp_dir_path).init()

            experiment = 'test'
            run_hash = AimRepo.generate_commit_hash()
            repo = AimRepo(tmp_dir_path, experiment, run_hash)
            repo.commit_init()
            repo.commit_finish()

            self.assertFalse(repo.is_archived(experiment, run_hash))

            repo.archive(experiment, run_hash)
            self.assertTrue(repo.is_archived(experiment, run_hash))

            repo.unarchive(experiment, run_hash)
            self.assertFalse(repo.is_archived(experiment, run_hash))
Exemplo n.º 4
0
def commit(repo, message, code):
    if repo is None:
        click.echo('Repository does not exist')
        return

    commit_hash = AimRepo.generate_commit_hash()
    message = message.strip() or int(time.time())

    # Check if there is anything to commit
    if repo.is_index_empty():
        click.echo('Nothing to commit')
        return

    branch_name = branch_hash = None
    if code:
        try:
            from aim.version_control.factory import Factory
        except Exception:
            click.echo('Git executable not found')
            return

        vc = Factory.create(Factory.GIT)

        # Check if version control repo exists
        if vc is None or vc.get_repo() is None:
            click.echo('No git repository (or any parent up to mount ' +
                       'point /) found. Initialize git repository ' +
                       'by running `git init`')
            return

        # Check untracked files
        if len(vc.get_untracked_files()):
            click.echo('You have untracked files, please add them to the ' +
                       'index before committing your changes by running ' +
                       '`git add -A`')
            return

        # Check if HEAD exists
        if not vc.get_head_hash():
            click.echo('Needed a single revision. ' +
                       'You do not have the git initial commit yet')
            return

        # Commit changes to a new created branch and return branch name
        branch_name, branch_hash = vc.commit_changes_to_branch(
            message, commit_hash)

        # Get the latest branch
        latest_branch = repo.get_latest_vc_branch() or {}
        latest_branch = latest_branch.get('branch')
        if latest_branch is None:
            latest_branch = vc.get_head_hash()

        # Get diff between current commit and latest commit(or HEAD)
        diff = vc.get_diff_text(latest_branch, branch_name)
        repo.save_diff(diff)

    # Commit
    commit_res = repo.commit(commit_hash, message, branch_name, branch_hash)

    click.echo(
        click.style('[{b}/{c} commit]'.format(b=commit_res['branch'],
                                              c=commit_res['commit']),
                    fg='yellow'))
    click.echo(message)