Skip to content

Commit

Permalink
fix init and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
willcl-ark committed Aug 23, 2024
1 parent 8381ebf commit d042f3a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
47 changes: 22 additions & 25 deletions src/warnet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,40 +77,37 @@ def quickstart():
return False


@cli.command()
@click.argument("directory", type=click.Path(file_okay=False, dir_okay=True, resolve_path=True))
def create(directory: Path):
"""Create a new warnet project in the specified directory"""
_create(directory)


def _create(directory: Path):
full_path = Path(directory)
if full_path.exists():
richprint(f"[red]Error: Directory {full_path} already exists[/red]")
return
def create_warnet_project(directory: Path, check_empty: bool = False):
"""Common function to create a warnet project"""
if check_empty and any(directory.iterdir()):
richprint("[yellow]Warning: Directory is not empty[/yellow]")
if not click.confirm("Do you want to continue?", default=True):
return

try:
copy_network_defaults(full_path)
copy_scenario_defaults(full_path)
richprint(f"[green]Copied network example files to {full_path / 'networks'}[/green]")
richprint(f"[green]Created warnet project structure in {full_path}[/green]")
copy_network_defaults(directory)
copy_scenario_defaults(directory)
richprint(f"[green]Copied network example files to {directory / 'networks'}[/green]")
richprint(f"[green]Created warnet project structure in {directory}[/green]")
except Exception as e:
richprint(f"[red]Error creating project: {e}[/red]")

@cli.command()
@click.argument(
"directory", type=click.Path(file_okay=False, dir_okay=True, resolve_path=True, path_type=Path)
)
def create(directory: Path):
"""Create a new warnet project in the specified directory"""
if directory.exists():
richprint(f"[red]Error: Directory {directory} already exists[/red]")
return
create_warnet_project(directory)

@cli.command()
def init():
"""Initialize a warnet project in the current directory"""
current_dir = os.getcwd()
if os.listdir(current_dir):
richprint("[yellow]Warning: Current directory is not empty[/yellow]")
if not click.confirm("Do you want to continue?", default=True):
return

copy_network_defaults(current_dir)
richprint(f"[green]Copied network example files to {Path(current_dir) / 'networks'}[/green]")
richprint(f"[green]Created warnet project structure in {current_dir}[/green]")
current_dir = Path.cwd()
create_warnet_project(current_dir, check_empty=True)


@cli.command()
Expand Down
35 changes: 16 additions & 19 deletions src/warnet/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,16 @@ def setup_logging_helm() -> bool:
return True


def copy_network_defaults(directory: Path):
"""Create the project structure for a warnet project"""
(directory / WAR_NETWORK_DIR / DEFAULT_NETWORK).mkdir(parents=True, exist_ok=True)
target_network_defaults = directory / WAR_NETWORK_DIR / DEFAULT_NETWORK / DEFAULTS_FILE
target_network_example = directory / WAR_NETWORK_DIR / DEFAULT_NETWORK / NETWORK_FILE
shutil.copy2(WAR_NETWORK_FILES / DEFAULT_NETWORK / DEFAULTS_FILE, target_network_defaults)
shutil.copy2(WAR_NETWORK_FILES / DEFAULT_NETWORK / NETWORK_FILE, target_network_example)


def copy_scenario_defaults(directory: Path):
"""Create the project structure for a warnet project"""
target_dir = directory / WAR_SCENARIOS_DIR
def copy_defaults(directory: Path, target_subdir: str, source_path: Path, exclude_list: list[str]):
"""Generic function to copy default files and directories"""
target_dir = directory / target_subdir
target_dir.mkdir(parents=True, exist_ok=True)
print(f"Creating scenarios directory: {target_dir}")

scenarios_path = WAR_SCENARIOS_FILES.joinpath()
print(f"Creating directory: {target_dir}")

def should_copy(item: Path) -> bool:
return item.name not in ["__init__.py", "__pycache__", "commander.py"]
return item.name not in exclude_list

for item in scenarios_path.iterdir():
for item in source_path.iterdir():
if should_copy(item):
if item.is_file():
shutil.copy2(item, target_dir)
Expand All @@ -97,7 +86,15 @@ def should_copy(item: Path) -> bool:
shutil.copytree(item, target_dir / item.name, dirs_exist_ok=True)
print(f"Copied directory: {item.name}")

print(f"Finished copying scenario files to {target_dir}")
print(f"Finished copying files to {target_dir}")

def copy_network_defaults(directory: Path):
"""Create the project structure for a warnet project's network"""
copy_defaults(directory, WAR_NETWORK_DIR, WAR_NETWORK_FILES.joinpath(), [])

def copy_scenario_defaults(directory: Path):
"""Create the project structure for a warnet project's scenarios"""
copy_defaults(directory, WAR_SCENARIOS_DIR, WAR_SCENARIOS_FILES.joinpath(), ["__init__.py", "__pycache__", "commander.py"])


@network.command()
Expand Down Expand Up @@ -179,7 +176,7 @@ def _connected():
for peer in peerinfo:
if peer["connection_type"] == "manual":
manuals += 1
# Even if more edges are specifed, bitcoind only allows
# Even if more edges are specified, bitcoind only allows
# 8 manual outbound connections

print("manual " + str(manuals))
Expand Down

0 comments on commit d042f3a

Please sign in to comment.