def check_for_missing_profile(profile): path = get_config_path(profile) # Absence of default config is equivalent to running locally. if profile and not os.path.exists(path): raise click.ClickException("Couldn't find configuration for profile " + click.style('"%s"' % profile, fg="red") + " in " + click.style('"%s"' % path, fg="red"))
def validate_episode(episode): src_dir = os.path.join(get_tutorials_dir(), episode) if not os.path.isdir(src_dir): raise click.BadArgumentUsage( "Episode " + click.style('"{0}"'.format(episode), fg="red") + " does not exist." " To see a list of available episodes, " "type:\n" + click.style("metaflow tutorials list", fg="cyan"))
def pull(episode): tutorials_dir = get_tutorials_dir() if not episode: episodes = get_all_episodes() else: episodes = [episode] # Validate that the list is valid. for episode in episodes: validate_episode(episode) # Create destination `metaflow-tutorials` dir. dst_parent = os.path.join(os.getcwd(), "metaflow-tutorials") makedirs(dst_parent) # Pull specified episodes. for episode in episodes: dst_dir = os.path.join(dst_parent, episode) # Check if episode has already been pulled before. if os.path.exists(dst_dir): if click.confirm("Episode " + click.style('"{0}"'.format(episode), fg="red") + " has already been pulled before. Do you wish " "to delete the existing version?"): shutil.rmtree(dst_dir) else: continue echo("Pulling episode ", nl=False) echo('"{0}"'.format(episode), fg="cyan", nl=False) # TODO: Is the following redundant? echo(" into your current working directory.") # Copy from (local) metaflow package dir to current. src_dir = os.path.join(tutorials_dir, episode) shutil.copytree(src_dir, dst_dir) echo("\nTo know more about an episode, type:\n", nl=False) echo("metaflow tutorials info [EPISODE]", fg="cyan")
def log(msg, formatter=None, context=None, real_bad=False, real_good=False): cstr = "" fstr = "" if context: cstr = " in context '%s'" % context["name"] if formatter: fstr = " %s" % formatter if cstr or fstr: line = "###%s%s: %s ###" % (fstr, cstr, msg) else: line = "### %s ###" % msg if real_bad: line = click.style(line, fg="red", bold=True) elif real_good: line = click.style(line, fg="green", bold=True) else: line = click.style(line, fg="white", bold=True) pid = os.getpid() click.echo("[pid %s] %s" % (pid, line))
def reset(profile): check_for_missing_profile(profile) path = get_config_path(profile) if os.path.exists(path): if click.confirm( "Do you really wish to reset the configuration in " + click.style('"%s"' % path, fg="cyan"), abort=True, ): os.remove(path) echo("Configuration successfully reset to run locally.") else: echo("Configuration is already reset to run locally.")
def overwrite_config(profile): path = get_config_path(profile) if os.path.exists(path): if not click.confirm( click.style( "We found an existing configuration for your " + "profile. Do you want to modify the existing " + "configuration?", fg="red", bold=True, )): echo( "You can configure a different named profile by using the " "--profile argument. You can activate this profile by setting " "the environment variable METAFLOW_PROFILE to the named " "profile.", fg="yellow", ) return False return True
def export(profile, output_filename): check_for_missing_profile(profile) # Export its contents to a new file. path = get_config_path(profile) env_dict = {} if os.path.exists(path): with open(path, "r") as f: env_dict = json.load(f) # resolve_path doesn't expand `~` in `path`. output_path = expanduser(output_filename) if os.path.exists(output_path): if click.confirm( "Do you wish to overwrite the contents in " + click.style('"%s"' % output_path, fg="cyan") + "?", abort=True, ): pass # Write to file. with open(output_path, "w") as f: json.dump(env_dict, f, indent=4, sort_keys=True) echo("Configuration successfully exported to: ", nl=False) echo('"%s"' % output_path, fg="cyan")
def status(): from metaflow.client import get_metadata res = get_metadata() if res: res = res.split("@") else: raise click.ClickException( "Unknown status: cannot find a Metadata provider") if res[0] == "service": echo("Using Metadata provider at: ", nl=False) echo('"%s"\n' % res[1], fg="cyan") echo("To list available flows, type:\n") echo("1. python") echo("2. from metaflow import Metaflow") echo("3. list(Metaflow())") return from metaflow.client import namespace, metadata, Metaflow # Get the local data store path path = LocalStorage.get_datastore_root_from_config(echo, create_on_absent=False) # Throw an exception if path is None: raise click.ClickException( "Could not find " + click.style('"%s"' % DATASTORE_LOCAL_DIR, fg="red") + " in the current working tree.") stripped_path = os.path.dirname(path) namespace(None) metadata("local@%s" % stripped_path) echo("Working tree found at: ", nl=False) echo('"%s"\n' % stripped_path, fg="cyan") echo("Available flows:", fg="cyan", bold=True) for flow in Metaflow(): echo("* %s" % flow, fg="cyan")
def red(string): return click.style(string, fg="red")
def yellow(string): return click.style(string, fg="yellow")
def cyan(string): return click.style(string, fg="cyan")