Skip to content

Commit 76cfcbc

Browse files
committed
Code update
Signed-off-by: refai06 <refai.ahamed06@gmail.com>
1 parent f0a3775 commit 76cfcbc

5 files changed

Lines changed: 13 additions & 29 deletions

File tree

openfl/experimental/workflow/component/director/director.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async def start_experiment_execution_loop(self) -> None:
105105
private_key=self.private_key,
106106
tls=self.tls,
107107
director_config=self.director_config,
108-
install_requirements=False,
108+
install_requirements=self.install_requirements,
109109
)
110110
)
111111
# Adding the experiment to collaborators queues

openfl/experimental/workflow/component/director/experiment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async def start(
8888
private_key: Optional[Union[Path, str]] = None,
8989
certificate: Optional[Union[Path, str]] = None,
9090
director_config: Path = None,
91-
install_requirements: bool = False,
91+
install_requirements: bool = True,
9292
) -> Tuple[bool, Any]:
9393
"""Run experiment.
9494
@@ -103,7 +103,7 @@ async def start(
103103
certificate for TLS. Defaults to None.
104104
director_config (Path): Path to director's config file
105105
install_requirements (bool, optional): A flag indicating if the
106-
requirements should be installed. Defaults to False.
106+
requirements should be installed. Defaults to True.
107107
108108
Returns:
109109
List[Union[bool, Any]]:

openfl/experimental/workflow/interface/cli/director.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def start(director_config_path, tls, root_certificate, private_key, certificate)
9797
validators=[
9898
Validator("settings.listen_host", default="localhost"),
9999
Validator("settings.listen_port", default=50051, gte=1024, lte=65535),
100-
Validator("settings.install_requirements", default=False),
100+
Validator("settings.install_requirements", default=True),
101101
Validator(
102102
"settings.envoy_health_check_period",
103103
default=60, # in seconds

openfl/experimental/workflow/notebooktools/code_analyzer.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
from importlib import import_module
99
from pathlib import Path
10-
from typing import Any, Dict, List, Optional, Tuple
10+
from typing import Any, Dict, List, Optional
1111

1212
import nbformat
1313
from nbdev.export import nb_export
@@ -18,8 +18,9 @@ class CodeAnalyzer:
1818
Provides code extraction and transformation functionality
1919
2020
Attributes:
21-
script_path (Path): Absolute path to the python script generated.
2221
script_name (str): Name of the generated python script.
22+
script_path (Path): Absolute path to the python script generated.
23+
requirements (List[str]): List of pip libraries found in the script.
2324
exported_script_module (ModuleType): The imported module object of the generated script.
2425
available_modules_in_exported_script (list): List of available attributes in the
2526
exported script.
@@ -43,6 +44,7 @@ def __init__(self, notebook_path: Path, output_path: Path) -> None:
4344
f"{self.script_name}.py",
4445
)
4546
).resolve()
47+
self.requirements = self._get_requirements()
4648
self.__modify_experiment_script()
4749

4850
def __get_exp_name(self, notebook_path: Path) -> str:
@@ -292,41 +294,25 @@ def _clean_value(self, value: str) -> str:
292294
value = value.lstrip("[").rstrip("]")
293295
return value
294296

295-
def get_requirements(self) -> Tuple[List[str], List[int], List[str]]:
297+
def _get_requirements(self) -> List[str]:
296298
"""Extract pip libraries from the script
297299
298300
Returns:
299-
tuple: A tuple containing:
300-
requirements (list of str): List of pip libraries found in the script.
301-
line_nos (list of int): List of line numbers where "pip install" commands are found.
302-
data (list of str): The entire script data as a list of lines.
301+
requirements (List[str]): List of pip libraries found in the script.
303302
"""
304303
data = None
305304
with self.script_path.open("r") as f:
306305
requirements = []
307-
line_nos = []
308306
data = f.readlines()
309-
for i, line in enumerate(data):
307+
for _, line in enumerate(data):
310308
line = line.strip()
311309
if "pip install" in line:
312-
line_nos.append(i)
313310
# Avoid commented lines, libraries from *.txt file, or openfl.git
314311
# installation
315312
if not line.startswith("#") and "-r" not in line and "openfl.git" not in line:
316313
requirements.append(f"{line.split(' ')[-1].strip()}\n")
317314

318-
return requirements, line_nos, data
319-
320-
def remove_lines(self, data: List[str], line_nos: List[int]) -> None:
321-
"""Removes pip install lines from the script
322-
Args:
323-
data (List[str]): The entire script data as a list of lines.
324-
line_nos (List[int]): List of line numbers where "pip install" commands are found.
325-
"""
326-
with self.script_path.open("w") as f:
327-
for i, line in enumerate(data):
328-
if i not in line_nos:
329-
f.write(line)
315+
return requirements
330316

331317
def get_flow_class_details(self, parent_class) -> Dict[str, Any]:
332318
"""

openfl/experimental/workflow/notebooktools/notebook_tools.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,11 @@ def _generate_requirements(self) -> None:
7676
and append to workspace/requirements.txt
7777
"""
7878
try:
79-
requirements, requirements_line_numbers, data = self.code_analyzer.get_requirements()
8079
requirements_filepath = str(
8180
self.output_workspace_path.joinpath("requirements.txt").resolve()
8281
)
8382
with open(requirements_filepath, "a") as f:
84-
f.writelines(requirements)
85-
self.code_analyzer.remove_lines(data, requirements_line_numbers)
83+
f.writelines(self.code_analyzer.requirements)
8684

8785
print(f"Successfully generated {requirements_filepath}")
8886

0 commit comments

Comments
 (0)