Skip to content

Commit

Permalink
Merge pull request #480 from willcl-ark/remove-cli-subtree
Browse files Browse the repository at this point in the history
  • Loading branch information
willcl-ark authored Aug 22, 2024
2 parents 6bafb2c + 36083f9 commit f722c94
Show file tree
Hide file tree
Showing 24 changed files with 72 additions and 48 deletions.
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies = [
]

[project.scripts]
warcli = "warnet.cli.main:cli"
warcli = "warnet.main:cli"

[project.urls]
Homepage = "https://warnet.dev"
Expand All @@ -42,4 +42,8 @@ build-backend = "setuptools.build_meta"
include-package-data = true

[tool.setuptools.packages.find]
where = ["src", "resources"]
where = ["src", "."]
include = ["warnet*", "test_framework*", "resources*"]

[tool.setuptools.package-data]
"resources" = ["**/*"]
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from time import sleep

# The base class exists inside the commander container
from commander import Commander
try:
from commander import Commander
except ImportError:
from resources.scenarios.commander import Commander


def cli_help():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from time import sleep

# The base class exists inside the commander container
from commander import Commander
try:
from commander import Commander
except ImportError:
from resources.scenarios.commander import Commander


def cli_help():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env python3

# The base class exists inside the commander container
from commander import Commander
try:
from commander import Commander
except ImportError:
from resources.scenarios.commander import Commander


def cli_help():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from time import sleep

# The base class exists inside the commander container
from commander import Commander
try:
from commander import Commander
except ImportError:
from resources.scenarios.commander import Commander


def cli_help():
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

ARCHES = ["amd64", "arm64", "armhf"]

dockerfile_path = files("images.bitcoin").joinpath("Dockerfile")
dockerfile_path = files("resources.images.bitcoin").joinpath("Dockerfile")


def run_command(command):
Expand Down
2 changes: 0 additions & 2 deletions src/warnet/cli/k8s.py → src/warnet/k8s.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import tempfile
from importlib.resources import files
from pathlib import Path

import yaml
Expand All @@ -10,7 +9,6 @@

from .process import run_command, stream_command

WAR_MANIFESTS = files("manifests")
DEFAULT_NAMESPACE = "warnet"


Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/warnet/cli/main.py → src/warnet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .network import copy_network_defaults, network
from .scenarios import scenarios

QUICK_START_PATH = files("scripts").joinpath("quick_start.sh")
QUICK_START_PATH = files("resources.scripts").joinpath("quick_start.sh")


@click.group()
Expand Down
4 changes: 2 additions & 2 deletions src/warnet/cli/namespaces.py → src/warnet/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

from .process import run_command, stream_command

WARNET_NAMESPACES_DIR = files("namespaces")
WARNET_NAMESPACES_DIR = files("resources").joinpath("namespaces")
NAMESPACES_DIR = Path("namespaces")
DEFAULT_NAMESPACES = Path("two_namespaces_two_users")
NAMESPACES_FILE = "namespaces.yaml"
DEFAULTS_FILE = "namespace-defaults.yaml"
HELM_COMMAND = "helm upgrade --install"
BITCOIN_CHART_LOCATION = Path(str(files("charts").joinpath("namespaces")))
BITCOIN_CHART_LOCATION = Path(str(files("resources.charts").joinpath("namespaces")))


def copy_namespaces_defaults(directory: Path):
Expand Down
8 changes: 4 additions & 4 deletions src/warnet/cli/network.py → src/warnet/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
from .k8s import delete_namespace, get_default_namespace, get_mission, get_pods
from .process import stream_command

WAR_MANIFESTS = files("manifests")
WARNET_NETWORK_DIR = files("networks")
NETWORK_DIR = Path("networks")
WAR_MANIFESTS = files("resources.manifests")
WARNET_NETWORK_DIR = files("resources.networks")
NETWORK_DIR = Path("resources.networks")
DEFAULT_NETWORK = Path("6_node_bitcoin")
NETWORK_FILE = "network.yaml"
DEFAULTS_FILE = "node-defaults.yaml"
HELM_COMMAND = "helm upgrade --install --create-namespace"
BITCOIN_CHART_LOCATION = str(files("charts").joinpath("bitcoincore"))
BITCOIN_CHART_LOCATION = str(files("resources.charts").joinpath("bitcoincore"))


@click.group(name="network")
Expand Down
File renamed without changes.
52 changes: 29 additions & 23 deletions src/warnet/cli/scenarios.py → src/warnet/scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
import sys
import tempfile
import time
from importlib.resources import files

import click
import yaml
from rich import print
from rich.console import Console
from rich.table import Table

from warnet import scenarios as SCENARIOS

from .k8s import apply_kubernetes_yaml, get_default_namespace, get_mission


@click.group(name="scenarios")
def scenarios():
"""Manage scenarios on a running network"""
"""Manage scenarios on a network"""


@scenarios.command()
Expand All @@ -41,24 +40,22 @@ def available():


def _available():
# This ugly hack temporarily allows us to import the scenario modules
# in the context in which they run: as __main__ from
# the root directory of the commander container.
scenarios_path = SCENARIOS.__path__
sys.path.insert(0, scenarios_path[0])

scenario_list = []
for s in pkgutil.iter_modules(scenarios_path):
module_name = f"warnet.scenarios.{s.name}"
try:
m = importlib.import_module(module_name)
if hasattr(m, "cli_help"):
scenario_list.append((s.name, m.cli_help()))
except Exception as e:
print(f"Ignoring module: {module_name} because {e}")

# Clean up that ugly hack
sys.path.pop(0)
scenarios_dir = files("resources.scenarios")
sys.path.append(scenarios_dir)

try:
scenario_list = []
package_name = "resources.scenarios"
for _, name, _ in pkgutil.iter_modules([scenarios_dir]):
module_name = f"{package_name}.{name}"
try:
m = importlib.import_module(module_name)
if hasattr(m, "cli_help"):
scenario_list.append((name, m.cli_help()))
except Exception as e:
print(f"Error importing module {module_name}: {e}")
finally:
sys.path.remove(scenarios_dir)

return scenario_list

Expand All @@ -72,7 +69,7 @@ def run(scenario: str, additional_args: tuple[str]):
"""

# Use importlib.resources to get the scenario path
scenario_package = "warnet.scenarios"
scenario_package = "resources.scenarios"
scenario_filename = f"{scenario}.py"

# Ensure the scenario file exists within the package
Expand All @@ -86,7 +83,7 @@ def run(scenario: str, additional_args: tuple[str]):
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
def run_file(scenario_path: str, additional_args: tuple[str]):
"""
Run <scenario_path> from the Warnet Test Framework with optional arguments
Start <scenario_path> with optional arguments
"""
if not scenario_path.endswith(".py"):
print("Error. Currently only python scenarios are supported")
Expand Down Expand Up @@ -206,3 +203,12 @@ def active():
def _active() -> list[str]:
commanders = get_mission("commander")
return [{"commander": c.metadata.name, "status": c.status.phase.lower()} for c in commanders]


@scenarios.command()
@click.argument("pid", type=int)
def stop(pid: int):
"""
Stop scenario
"""
pass
Empty file removed src/warnet/scenarios/__init__.py
Empty file.
File renamed without changes.
6 changes: 5 additions & 1 deletion test/data/scenario_p2p_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from collections import defaultdict

# The base class exists inside the commander container
from commander import Commander
try:
from commander import Commander
except Exception:
from resources.scenarios.commander import Commander


from test_framework.messages import CInv, msg_getdata
from test_framework.p2p import P2PInterface
Expand Down
10 changes: 5 additions & 5 deletions test/scenarios_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from test_base import TestBase

from warnet.cli.k8s import delete_pod
from warnet.cli.process import run_command
from warnet.cli.scenarios import _active as scenarios_active
from warnet.cli.scenarios import _available as scenarios_available
from warnet.k8s import delete_pod
from warnet.process import run_command
from warnet.scenarios import _active as scenarios_active
from warnet.scenarios import _available as scenarios_available


class ScenariosTest(TestBase):
Expand Down Expand Up @@ -69,7 +69,7 @@ def run_and_check_miner_scenario(self):
self.stop_scenario()

def run_and_check_miner_scenario_from_file(self):
scenario_file = "src/warnet/scenarios/miner_std.py"
scenario_file = "resources/scenarios/miner_std.py"
self.log.info(f"Running scenario from file: {scenario_file}")
self.warcli(f"scenarios run-file {scenario_file} --allnodes --interval=1")
start = int(self.warcli("bitcoin rpc tank-0000 getblockcount"))
Expand Down
6 changes: 3 additions & 3 deletions test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from time import sleep

from warnet import SRC_DIR
from warnet.cli.network import _connected as network_connected
from warnet.cli.network import _status as network_status
from warnet.cli.scenarios import _active as scenarios_active
from warnet.network import _connected as network_connected
from warnet.network import _status as network_status
from warnet.scenarios import _active as scenarios_active


class TestBase:
Expand Down

0 comments on commit f722c94

Please sign in to comment.