def branch_create(repo: Repository, name, startpoint): """Create a branch with NAME at STARTPOINT (short-digest or branch) If no STARTPOINT is provided, the new branch is positioned at the HEAD of the staging area branch, automatically. """ from hangar.records.commiting import expand_short_commit_digest from hangar.records.heads import get_branch_head_commit from hangar.records.heads import get_staging_branch_head branch_names = repo.list_branches() if name in branch_names: e = ValueError(f'branch name: {name} already exists') raise click.ClickException(e) try: if startpoint is None: branch = get_staging_branch_head(repo._env.branchenv) base_commit = get_branch_head_commit(repo._env.branchenv, branch) elif startpoint in branch_names: base_commit = get_branch_head_commit(repo._env.branchenv, startpoint) else: base_commit = expand_short_commit_digest(repo._env.refenv, startpoint) res = repo.create_branch(name, base_commit=base_commit) except (KeyError, ValueError, RuntimeError) as e: raise click.ClickException(e) click.echo(f'Created BRANCH: {res.name} HEAD: {res.digest}')
def view_data(ctx, repo: Repository, column, sample, startpoint, format_, plugin): """Use a plugin to view the data of some SAMPLE in COLUMN at STARTPOINT. """ from hangar.records.commiting import expand_short_commit_digest from hangar.records.heads import get_branch_head_commit, get_staging_branch_head from hangar import external kwargs = parse_custom_arguments(ctx.args) if startpoint in repo.list_branches(): base_commit = get_branch_head_commit(repo._env.branchenv, startpoint) elif startpoint: base_commit = expand_short_commit_digest(repo._env.refenv, startpoint) else: branch_name = get_staging_branch_head(repo._env.branchenv) base_commit = get_branch_head_commit(repo._env.branchenv, branch_name) co = repo.checkout(commit=base_commit) try: aset = co.columns.get(column) extension = format_.lstrip('.') if format_ else None data = aset[sample] try: external.show(data, plugin=plugin, extension=extension, **kwargs) except Exception as e: raise click.ClickException(e) except KeyError as e: raise click.ClickException(e) finally: co.close()
def branch_create(ctx, name, startpoint): """Create a branch with NAME at STARTPOINT (short-digest or branch) If no STARTPOINT is provided, the new branch is positioned at the HEAD of the staging area branch, automatically. """ from hangar.records.heads import get_branch_head_commit, get_staging_branch_head P = os.getcwd() repo = Repository(path=P) branch_names = repo.list_branches() if name in branch_names: raise ValueError(f'branch name: {name} already exists') if startpoint is None: branch = get_staging_branch_head(repo._env.branchenv) base_commit = get_branch_head_commit(repo._env.branchenv, branch) elif startpoint in branch_names: base_commit = get_branch_head_commit(repo._env.branchenv, startpoint) else: base_commit = expand_short_commit_digest(repo._env.refenv, startpoint) click.echo(f'BRANCH: ' + repo.create_branch(name, base_commit=base_commit) + f' HEAD: {base_commit}')
def fetch_data(repo: Repository, remote, startpoint, column, nbytes, all_): """Get data from REMOTE referenced by STARTPOINT (short-commit or branch). The default behavior is to only download a single commit's data or the HEAD commit of a branch. Please review optional arguments for other behaviors. """ from hangar.records.commiting import expand_short_commit_digest from hangar.records.heads import get_branch_head_commit from hangar.records.heads import get_staging_branch_head from hangar.utils import parse_bytes if startpoint is None: branch = get_staging_branch_head(repo._env.branchenv) commit = get_branch_head_commit(repo._env.branchenv, branch) elif startpoint in repo.list_branches(): commit = get_branch_head_commit(repo._env.branchenv, startpoint) else: commit = expand_short_commit_digest(repo._env.refenv, startpoint) click.echo(f'Fetching data for commit: {commit}') try: max_nbytes = parse_bytes(nbytes) except AttributeError: max_nbytes = None if len(column) == 0: column = None commits = repo.remote.fetch_data(remote=remote, commit=commit, column_names=column, max_num_bytes=max_nbytes, retrieve_all_history=all_) click.echo(f'completed data for commits: {commits}')
def export_data(ctx, repo: Repository, column, outdir, startpoint, sample, format_, plugin): """Export COLUMN sample data as it existed a STARTPOINT to some format and path. Specifying which sample to be exported is possible by using the switch ``--sample`` (without this, all the samples in the given column will be exported). Since hangar supports both int and str datatype for the sample name, specifying that while mentioning the sample name might be necessary at times. It is possible to do that by separating the name and type by a colon. Example: 1. if the sample name is string of numeric 10 - ``str:10`` or ``10`` 2. if the sample name is ``sample1`` - ``str:sample1`` or ``sample1`` 3. if the sample name is an int, let say 10 - ``int:10`` """ from hangar.records.commiting import expand_short_commit_digest from hangar.records.heads import get_branch_head_commit, get_staging_branch_head from hangar import external kwargs = parse_custom_arguments(ctx.args) if startpoint in repo.list_branches(): base_commit = get_branch_head_commit(repo._env.branchenv, startpoint) elif startpoint: base_commit = expand_short_commit_digest(repo._env.refenv, startpoint) else: branch_name = get_staging_branch_head(repo._env.branchenv) base_commit = get_branch_head_commit(repo._env.branchenv, branch_name) co = repo.checkout(commit=base_commit) try: aset = co.columns.get(column) sampleNames = [sample] if sample is not None else list(aset.keys()) extension = format_.lstrip('.') if format_ else None with aset, click.progressbar(sampleNames) as sNamesBar: for sampleN in sNamesBar: data = aset[sampleN] formated_sampleN = f'{type(sampleN).__name__}:{sampleN}' try: external.save(data, outdir, formated_sampleN, extension, plugin, **kwargs) except Exception as e: raise click.ClickException(e) except KeyError as e: raise click.ClickException(e) finally: co.close()
def test_branch_name_head_commit_digests_exist(self, diverse_repo): from hangar.records.heads import get_branch_names, get_branch_head_commit from hangar.records.parsing import commit_ref_db_key_from_raw_key from hangar.records.parsing import commit_parent_db_key_from_raw_key from hangar.records.parsing import commit_spec_db_key_from_raw_key from hangar.diagnostics.integrity import _verify_branch_integrity branch_names = get_branch_names(diverse_repo._env.branchenv) for bname in branch_names: bhead = get_branch_head_commit(diverse_repo._env.branchenv, branch_name=bname) with diverse_repo._env.refenv.begin(write=True) as txn: cmtRefKey = commit_ref_db_key_from_raw_key(bhead) cmtSpecKey = commit_spec_db_key_from_raw_key(bhead) cmtParentKey = commit_parent_db_key_from_raw_key(bhead) cmtRefVal = txn.get(cmtRefKey) cmtSpecVal = txn.get(cmtSpecKey) cmtParentVal = txn.get(cmtParentKey) txn.delete(cmtRefKey) txn.delete(cmtSpecKey) txn.delete(cmtParentKey) with pytest.raises( RuntimeError, match='Branch commit map compromised. Branch name'): _verify_branch_integrity(diverse_repo._env.branchenv, diverse_repo._env.refenv) with diverse_repo._env.refenv.begin(write=True) as txn: txn.put(cmtRefKey, cmtRefVal) txn.put(cmtSpecKey, cmtSpecVal) txn.put(cmtParentKey, cmtParentVal)
def fetch_data(ctx, remote, startpoint, aset, nbytes, all_): """Get data from REMOTE referenced by STARTPOINT (short-commit or branch). The default behavior is to only download a single commit's data or the HEAD commit of a branch. Please review optional arguments for other behaviors """ from hangar.records.heads import get_branch_head_commit, get_staging_branch_head from hangar.utils import parse_bytes P = os.getcwd() repo = Repository(path=P) if startpoint is None: branch = get_staging_branch_head(repo._env.branchenv) commit = get_branch_head_commit(repo._env.branchenv, branch) click.echo( f'No startpoint supplied, fetching data of HEAD: {commit} for BRANCH: {branch}' ) elif startpoint in repo.list_branches(): commit = get_branch_head_commit(repo._env.branchenv, startpoint) click.echo( f'Fetching data for HEAD: {commit} of STARTPOINT BRANCH: {startpoint}' ) else: commit = expand_short_commit_digest(repo._env.refenv, startpoint) click.echo(f'Fetching data for STARTPOINT HEAD: {commit}') click.echo(f'aset argument: {aset}') try: max_nbytes = parse_bytes(nbytes) click.echo(f'nbytes argument: {max_nbytes}') except AttributeError: max_nbytes = None if len(aset) == 0: aset = None commits = repo.remote.fetch_data(remote=remote, commit=commit, arrayset_names=aset, max_num_bytes=max_nbytes, retrieve_all_history=all_) click.echo(f'completed data for commits: {commits}')
def export_data(repo: Repository, startpoint, arrayset, out, sample, format_, plugin): """export ARRAYSET sample data as it existed a STARTPOINT to some format and path. """ from hangar.records.commiting import expand_short_commit_digest from hangar.records.heads import get_branch_head_commit from hangar.cli.io import imsave if startpoint in repo.list_branches(): base_commit = get_branch_head_commit(repo._env.branchenv, startpoint) else: base_commit = expand_short_commit_digest(repo._env.refenv, startpoint) try: co = repo.checkout(write=False, commit=base_commit) arrayset = co.arraysets[arrayset] if sample: sampleNames = [sample] else: sampleNames = list(arrayset.keys()) if format_: format_ = format_.lstrip('.') outP = os.path.expanduser(os.path.normpath(out)) with arrayset as aset, click.progressbar(sampleNames) as sNamesBar: for sampleN in sNamesBar: if format_: if sampleN.endswith(format_): outFP = os.path.join(outP, f'{sampleN}') else: outFP = os.path.join(outP, f'{sampleN}.{format_}') else: outFP = os.path.join(outP, f'{sampleN}') try: data = aset[sampleN] imsave(outFP, data) except KeyError as e: click.echo(e) finally: co.close()
def view_data(repo: Repository, startpoint, arrayset, sample, plugin): """Use a plugin to view the data of some SAMPLE in ARRAYSET at STARTPOINT. """ from hangar.records.commiting import expand_short_commit_digest from hangar.records.heads import get_branch_head_commit from hangar.cli.io import imshow, show if startpoint in repo.list_branches(): base_commit = get_branch_head_commit(repo._env.branchenv, startpoint) else: base_commit = expand_short_commit_digest(repo._env.refenv, startpoint) try: co = repo.checkout(write=False, commit=base_commit) arrayset = co.arraysets[arrayset] try: data = arrayset[sample] imshow(data, plugin=plugin) show() except KeyError as e: click.echo(e) finally: co.close()