From 7898b8e63d4d3c92dd0af3b826bb1bb688f0c762 Mon Sep 17 00:00:00 2001 From: "Shah, Karan" Date: Tue, 29 Oct 2024 15:01:12 +0530 Subject: [PATCH] Eliminate package install during import Signed-off-by: Shah, Karan --- openfl/interface/workspace.py | 44 +++++++---------------------------- 1 file changed, 9 insertions(+), 35 deletions(-) diff --git a/openfl/interface/workspace.py b/openfl/interface/workspace.py index c8b96e4d4a..e0871dc020 100644 --- a/openfl/interface/workspace.py +++ b/openfl/interface/workspace.py @@ -10,10 +10,7 @@ import sys import tempfile from hashlib import sha256 -from os import chdir -from os.path import basename, isfile from pathlib import Path -from shutil import copyfile, copytree, ignore_patterns, unpack_archive from subprocess import check_call # nosec from sys import executable from typing import Tuple, Union @@ -71,7 +68,7 @@ def create_dirs(prefix): (prefix / "save").mkdir(parents=True, exist_ok=True) # model weight saves / initialization (prefix / "src").mkdir(parents=True, exist_ok=True) # model code - copyfile(WORKSPACE / "workspace" / ".workspace", prefix / ".workspace") + shutil.copyfile(WORKSPACE / "workspace" / ".workspace", prefix / ".workspace") def create_temp(prefix, template): @@ -84,11 +81,11 @@ def create_temp(prefix, template): echo("Creating Workspace Templates") - copytree( + shutil.copytree( src=WORKSPACE / template, dst=prefix, dirs_exist_ok=True, - ignore=ignore_patterns("__pycache__"), + ignore=shutil.ignore_patterns("__pycache__"), ) # from template workspace @@ -140,7 +137,7 @@ def create(prefix, template): requirements_filename = "requirements.txt" - if isfile(f"{str(prefix)}/{requirements_filename}"): + if os.path.isfile(f"{str(prefix)}/{requirements_filename}"): check_call( [ executable, @@ -172,37 +169,14 @@ def create(prefix, template): @option( "--archive", required=True, - help="Zip file containing workspace to import", + help="Path to workspace archive.", type=ClickPath(exists=True), ) def import_(archive): - """Import federated learning workspace. - - Args: - archive: The archive file containing the workspace to import. - """ - - archive = Path(archive).absolute() - - dir_path = basename(archive).split(".")[0] - unpack_archive(archive, extract_dir=dir_path) - chdir(dir_path) - - requirements_filename = "requirements.txt" - - if isfile(requirements_filename): - check_call( - [executable, "-m", "pip", "install", "--upgrade", "pip"], - shell=False, - ) - check_call( - [executable, "-m", "pip", "install", "-r", "requirements.txt"], - shell=False, - ) - else: - echo("No " + requirements_filename + " file found.") - - echo(f"Workspace {archive} has been imported.") + """Import a federated learning workspace generated by `fx workspace export`.""" + dir_path = os.path.basename(os.path.abspath(archive)).split(".")[0] + shutil.unpack_archive(archive, extract_dir=dir_path) + echo(f"Imported workspace `{archive}`.") echo("You may need to copy your PKI certificates to join the federation.")