Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/SK-944 + SK-934 | Add flag for deleting the virtual env #661

Merged
merged 19 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fedn/cli/client_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from fedn.common.exceptions import InvalidClientConfig
from fedn.network.clients.client import Client

from .main import main
from .shared import CONTROLLER_DEFAULTS, apply_config, get_api_url, get_token, print_response
from fedn.cli.main import main
from fedn.cli.shared import CONTROLLER_DEFAULTS, apply_config, get_api_url, get_token, print_response


def validate_client_config(config):
Expand Down
2 changes: 1 addition & 1 deletion fedn/cli/controller_cmd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from .main import main
from fedn.cli.main import main


@main.group("controller")
Expand Down
82 changes: 38 additions & 44 deletions fedn/cli/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
from fedn.network.combiner.combiner import Combiner
from fedn.utils.dispatcher import Dispatcher, _read_yaml_file

from .client_cmd import validate_client_config
from .main import main
from .shared import apply_config

from fedn.cli.client_cmd import validate_client_config
from fedn.cli.main import main
from fedn.cli.shared import apply_config

def get_statestore_config_from_file(init):
""":param init:
Expand All @@ -36,7 +35,22 @@ def check_helper_config_file(config):
exit(-1)
return helper


def check_yaml_exists(path):
"""Check if fedn.yaml exists in the given path."""
yaml_file = os.path.join(path, "fedn.yaml")
if not os.path.exists(yaml_file):
logger.error(f"Could not find fedn.yaml in {path}")
click.echo(f"Could not find fedn.yaml in {path}")
exit(-1)
return yaml_file
def delete_virtual_environment(remove_venv,dispatcher):
if remove_venv:
# delete the virtualenv
if dispatcher.python_env_path:
logger.info(f"Removing virtualenv {dispatcher.python_env_path}")
shutil.rmtree(dispatcher.python_env_path)
else:
logger.warning("No virtualenv found to remove.")
@main.group("run")
@click.pass_context
def run_cmd(ctx):
Expand All @@ -46,20 +60,18 @@ def run_cmd(ctx):
@run_cmd.command("validate")
@click.option("-p", "--path", required=True, help="Path to package directory containing fedn.yaml")
@click.option("-i", "--input", required=True, help="Path to input model" )
@click.option("-o", "--output", required=True,help="Path to write the output JSON containing validation metrics")
@click.option("-o", "--output", required=True, help="Path to write the output JSON containing validation metrics")
@click.option("-v", "--remove-venv", is_flag=True, default=True,required=False, help="flag if set to False doesn't remove venv")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need default=True when is_flag=True

@click.pass_context
def validate_cmd(ctx, path,input,output):
def validate_cmd(ctx, path,input,output,remove_venv):
"""Execute 'validate' entrypoint in fedn.yaml.

:param ctx:
:param path: Path to folder containing fedn.yaml
:type path: str
"""
path = os.path.abspath(path)
yaml_file = os.path.join(path, "fedn.yaml")
if not os.path.exists(yaml_file):
logger.error(f"Could not find fedn.yaml in {path}")
exit(-1)
yaml_file = check_yaml_exists(path)

config = _read_yaml_file(yaml_file)
# Check that validate is defined in fedn.yaml under entry_points
Expand All @@ -70,28 +82,22 @@ def validate_cmd(ctx, path,input,output):
dispatcher = Dispatcher(config, path)
_ = dispatcher._get_or_create_python_env()
dispatcher.run_cmd("validate {} {}".format(input, output))

# delete the virtualenv
if dispatcher.python_env_path:
logger.info(f"Removing virtualenv {dispatcher.python_env_path}")
shutil.rmtree(dispatcher.python_env_path)
delete_virtual_environment(remove_venv,dispatcher)
@run_cmd.command("train")
@click.option("-p", "--path", required=True, help="Path to package directory containing fedn.yaml")
@click.option("-i", "--input", required=True, help="Path to input model parameters" )
@click.option("-o", "--output", required=True,help="Path to write the updated model parameters ")
@click.option("-o", "--output", required=True, help="Path to write the updated model parameters ")
@click.option("-v", "--remove-venv", is_flag=True, default=True,required=False, help="flag if set to False doesn't remove venv")
@click.pass_context
def train_cmd(ctx, path,input,output):
def train_cmd(ctx, path,input,output,remove_venv):
"""Execute 'train' entrypoint in fedn.yaml.

:param ctx:
:param path: Path to folder containing fedn.yaml
:type path: str
"""
path = os.path.abspath(path)
yaml_file = os.path.join(path, "fedn.yaml")
if not os.path.exists(yaml_file):
logger.error(f"Could not find fedn.yaml in {path}")
exit(-1)
yaml_file = check_yaml_exists(path)

config = _read_yaml_file(yaml_file)
# Check that train is defined in fedn.yaml under entry_points
Expand All @@ -102,26 +108,21 @@ def train_cmd(ctx, path,input,output):
dispatcher = Dispatcher(config, path)
_ = dispatcher._get_or_create_python_env()
dispatcher.run_cmd("train {} {}".format(input, output))
delete_virtual_environment(remove_venv,dispatcher)

# delete the virtualenv
if dispatcher.python_env_path:
logger.info(f"Removing virtualenv {dispatcher.python_env_path}")
shutil.rmtree(dispatcher.python_env_path)
@run_cmd.command("startup")
@click.option("-p", "--path", required=True, help="Path to package directory containing fedn.yaml")
@click.option("-v", "--remove-venv", is_flag=True, default=True,required=False, help="flag if set to False doesn't remove venv")
@click.pass_context
def startup_cmd(ctx, path):
def startup_cmd(ctx, path,remove_venv):
"""Execute 'startup' entrypoint in fedn.yaml.

:param ctx:
:param path: Path to folder containing fedn.yaml
:type path: str
"""
path = os.path.abspath(path)
yaml_file = os.path.join(path, "fedn.yaml")
if not os.path.exists(yaml_file):
logger.error(f"Could not find fedn.yaml in {path}")
exit(-1)
yaml_file = check_yaml_exists(path)

config = _read_yaml_file(yaml_file)
# Check that startup is defined in fedn.yaml under entry_points
Expand All @@ -132,27 +133,22 @@ def startup_cmd(ctx, path):
dispatcher = Dispatcher(config, path)
_ = dispatcher._get_or_create_python_env()
dispatcher.run_cmd("startup")
delete_virtual_environment(remove_venv,dispatcher)

# delete the virtualenv
if dispatcher.python_env_path:
logger.info(f"Removing virtualenv {dispatcher.python_env_path}")
shutil.rmtree(dispatcher.python_env_path)

@run_cmd.command("build")
@click.option("-p", "--path", required=True, help="Path to package directory containing fedn.yaml")
@click.option("-v", "--remove-venv", is_flag=True, default=True,required=False, help="flag if set to False doesn't remove venv")
@click.pass_context
def build_cmd(ctx, path):
def build_cmd(ctx, path,remove_venv):
"""Execute 'build' entrypoint in fedn.yaml.

:param ctx:
:param path: Path to folder containing fedn.yaml
:type path: str
"""
path = os.path.abspath(path)
yaml_file = os.path.join(path, "fedn.yaml")
if not os.path.exists(yaml_file):
logger.error(f"Could not find fedn.yaml in {path}")
exit(-1)
yaml_file = check_yaml_exists(path)

config = _read_yaml_file(yaml_file)
# Check that build is defined in fedn.yaml under entry_points
Expand All @@ -163,11 +159,9 @@ def build_cmd(ctx, path):
dispatcher = Dispatcher(config, path)
_ = dispatcher._get_or_create_python_env()
dispatcher.run_cmd("build")
print(remove_venv)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove print

delete_virtual_environment(remove_venv,dispatcher)

# delete the virtualenv
if dispatcher.python_env_path:
logger.info(f"Removing virtualenv {dispatcher.python_env_path}")
shutil.rmtree(dispatcher.python_env_path)


@run_cmd.command("client")
Expand Down
Loading
Loading