Skip to content

Commit

Permalink
Eliminate package install during import (securefederatedai#1109)
Browse files Browse the repository at this point in the history
Signed-off-by: Shah, Karan <[email protected]>
  • Loading branch information
MasterSkepticista authored and tanwarsh committed Nov 18, 2024
1 parent 79099b8 commit 8d4b8ff
Showing 1 changed file with 9 additions and 35 deletions.
44 changes: 9 additions & 35 deletions openfl/interface/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.")


Expand Down

0 comments on commit 8d4b8ff

Please sign in to comment.