Beispiel #1
0
def replace(obj,
            run_id,
            user_namespace,
            tags_to_add=None,
            tags_to_remove=None):
    _set_current(obj)
    # While run.replace_tag() can accept 0 additions or 0 removals, we want to encourage
    # the *obvious* way to achieve their goals. E.g. if they are only adding tags, use "tag add"
    # over more obscure "tag replace --add ... --add ..."
    if not tags_to_add and not tags_to_remove:
        raise CommandException(
            "Specify at least one tag to add (--add) and one tag to remove (--remove)"
        )
    if not tags_to_remove:
        raise CommandException(
            "Specify at least one tag to remove; else please use *tag add*.")
    if not tags_to_add:
        raise CommandException(
            "Specify at least one tag to add, else please use *tag remove*.")
    user_namespace = resolve_identity(
    ) if user_namespace is None else user_namespace
    run = _get_client_run_obj(obj, run_id, user_namespace)

    run.replace_tags(tags_to_remove, tags_to_add)

    obj.echo("Operation successful. New tags:")
    _print_tags_for_one_run(obj, run)
Beispiel #2
0
def remove(obj, run_id, user_namespace, tags):
    _set_current(obj)
    user_namespace = resolve_identity(
    ) if user_namespace is None else user_namespace
    run = _get_client_run_obj(obj, run_id, user_namespace)

    run.remove_tags(tags)

    obj.echo("Operation successful. New tags:")
    _print_tags_for_one_run(obj, run)
Beispiel #3
0
 def _tags(self):
     env = self._environment.get_environment_info()
     tags = [
         resolve_identity(), 'runtime:' + env['runtime'],
         'python_version:' + env['python_version_code'],
         'date:' + datetime.utcnow().strftime('%Y-%m-%d')
     ]
     if env['metaflow_version']:
         tags.append('metaflow_version:' + env['metaflow_version'])
     return tags
Beispiel #4
0
def default_namespace():
    """
    Sets or resets the namespace used to filter objects.

    The default namespace is in the form 'user:<username>' and is intended to filter
    objects belonging to the user.

    Returns
    -------
    string
        The result of get_namespace() after
    """
    global current_namespace
    current_namespace = resolve_identity()
    return get_namespace()
Beispiel #5
0
 def _tags(self):
     env = self._environment.get_environment_info()
     tags = [
         resolve_identity(),
         "runtime:" + env["runtime"],
         "python_version:" + env["python_version_code"],
         "date:" + datetime.utcnow().strftime("%Y-%m-%d"),
     ]
     if env["metaflow_version"]:
         tags.append("metaflow_version:" + env["metaflow_version"])
     if "metaflow_r_version" in env:
         tags.append("metaflow_r_version:" + env["metaflow_r_version"])
     if "r_version_code" in env:
         tags.append("r_version:" + env["r_version_code"])
     return tags
Beispiel #6
0
 def _tags(self):
     env = self._environment.get_environment_info()
     tags = [
         resolve_identity(), 'runtime:' + env['runtime'],
         'python_version:' + env['python_version_code'],
         'date:' + datetime.utcnow().strftime('%Y-%m-%d')
     ]
     if env['metaflow_version']:
         tags.append('metaflow_version:' + env['metaflow_version'])
     if 'metaflow_r_version' in env:
         tags.append('metaflow_r_version:' + env['metaflow_r_version'])
     if 'r_version_code' in env:
         tags.append('r_version:' + env['r_version_code'])
     if 'project_name' in current:
         tags.append('project:' + current.project_name)
         tags.append('project_branch:' + current.branch_name)
     return tags
Beispiel #7
0
def tag_list(
    obj,
    run_id,
    hide_system_tags,
    list_all,
    my_runs,
    group_by_tag,
    group_by_run,
    flat,
    arg_run_id,
):
    _set_current(obj)
    if run_id is None and arg_run_id is None and not list_all and not my_runs:
        # Assume list_all by default
        list_all = True

    if list_all and my_runs:
        raise CommandException(
            "Option --all cannot be used together with --my-runs.")

    if run_id is not None and arg_run_id is not None:
        raise CommandException(
            "Specify a run either using --run-id or as an argument but not both"
        )

    if arg_run_id is not None:
        run_id = arg_run_id

    if group_by_run and group_by_tag:
        raise CommandException(
            "Option --group-by-tag cannot be used with --group-by-run")

    if flat and (group_by_run or group_by_tag):
        raise CommandException(
            "Option --flat cannot be used with any --group-by-* option")

    system_tags_by_some_grouping = dict()
    all_tags_by_some_grouping = dict()

    def _populate_tag_groups_from_run(_run):
        if group_by_run:
            if hide_system_tags:
                all_tags_by_some_grouping[
                    _run.pathspec] = _run.tags - _run.system_tags
            else:
                system_tags_by_some_grouping[_run.pathspec] = _run.system_tags
                all_tags_by_some_grouping[_run.pathspec] = _run.tags
        elif group_by_tag:
            for t in _run.tags - _run.system_tags:
                all_tags_by_some_grouping.setdefault(t,
                                                     []).append(_run.pathspec)
            if not hide_system_tags:
                for t in _run.system_tags:
                    system_tags_by_some_grouping.setdefault(t, []).append(
                        _run.pathspec)
        else:
            if hide_system_tags:
                all_tags_by_some_grouping.setdefault("_", set()).update(
                    _run.tags.difference(_run.system_tags))
            else:
                system_tags_by_some_grouping.setdefault("_", set()).update(
                    _run.system_tags)
                all_tags_by_some_grouping.setdefault("_",
                                                     set()).update(_run.tags)

    pathspecs = []
    if list_all or my_runs:
        user_namespace = resolve_identity() if my_runs else None
        namespace(user_namespace)
        try:
            flow = Flow(pathspec=obj.flow.name)
        except MetaflowNotFound:
            raise CommandException(
                "Cannot list tags because the flow %s has never been run." %
                (obj.flow.name, ))
        for run in flow.runs():
            _populate_tag_groups_from_run(run)
            pathspecs.append(run.pathspec)
    else:
        run = _get_client_run_obj(obj, run_id, None)
        _populate_tag_groups_from_run(run)
        pathspecs.append(run.pathspec)

    if not group_by_run and not group_by_tag:
        # We list all the runs that match to print them out if needed.
        system_tags_by_some_grouping[",".join(
            pathspecs)] = system_tags_by_some_grouping.get("_", set())
        all_tags_by_some_grouping[",".join(
            pathspecs)] = all_tags_by_some_grouping.get("_", set())
        if "_" in system_tags_by_some_grouping:
            del system_tags_by_some_grouping["_"]
        if "_" in all_tags_by_some_grouping:
            del all_tags_by_some_grouping["_"]

    if flat:
        if len(all_tags_by_some_grouping) != 1:
            raise MetaflowInternalError("Failed to flatten tag set")
        for v in all_tags_by_some_grouping.values():
            for tag in v:
                obj.echo(tag)
            return

    _print_tags_for_runs_by_groups(obj, system_tags_by_some_grouping,
                                   all_tags_by_some_grouping, group_by_tag)