From 36083f9bff284bed5a652dd1207097bc61b91e3a Mon Sep 17 00:00:00 2001 From: willcl-ark Date: Fri, 23 Aug 2024 00:45:44 +0200 Subject: [PATCH] refactor out from cli dir --- pyproject.toml | 8 ++- .../cli => resources/scenarios}/__init__.py | 0 .../scenarios/commander.py | 0 .../warnet => resources}/scenarios/ln_init.py | 5 +- .../scenarios/miner_std.py | 5 +- .../scenarios/sens_relay.py | 5 +- .../scenarios/tx_flood.py | 5 +- src/warnet/{cli => }/admin.py | 0 src/warnet/{cli => }/bitcoin.py | 0 src/warnet/{cli => }/graph.py | 0 src/warnet/{cli => }/image.py | 0 src/warnet/{cli => }/image_build.py | 2 +- src/warnet/{cli => }/k8s.py | 2 - src/warnet/{cli => }/ln.py | 0 src/warnet/{cli => }/main.py | 2 +- src/warnet/{cli => }/namespaces.py | 4 +- src/warnet/{cli => }/network.py | 8 +-- src/warnet/{cli => }/process.py | 0 src/warnet/{cli => }/scenarios.py | 52 +++++++++++-------- src/warnet/scenarios/__init__.py | 0 src/warnet/{cli => }/util.py | 0 test/data/scenario_p2p_interface.py | 6 ++- test/scenarios_test.py | 10 ++-- test/test_base.py | 6 +-- 24 files changed, 72 insertions(+), 48 deletions(-) rename {src/warnet/cli => resources/scenarios}/__init__.py (100%) rename {src/warnet => resources}/scenarios/commander.py (100%) rename {src/warnet => resources}/scenarios/ln_init.py (98%) rename {src/warnet => resources}/scenarios/miner_std.py (95%) rename {src/warnet => resources}/scenarios/sens_relay.py (91%) rename {src/warnet => resources}/scenarios/tx_flood.py (95%) rename src/warnet/{cli => }/admin.py (100%) rename src/warnet/{cli => }/bitcoin.py (100%) rename src/warnet/{cli => }/graph.py (100%) rename src/warnet/{cli => }/image.py (100%) rename src/warnet/{cli => }/image_build.py (97%) rename src/warnet/{cli => }/k8s.py (97%) rename src/warnet/{cli => }/ln.py (100%) rename src/warnet/{cli => }/main.py (98%) rename src/warnet/{cli => }/namespaces.py (97%) rename src/warnet/{cli => }/network.py (96%) rename src/warnet/{cli => }/process.py (100%) rename src/warnet/{cli => }/scenarios.py (86%) delete mode 100644 src/warnet/scenarios/__init__.py rename src/warnet/{cli => }/util.py (100%) diff --git a/pyproject.toml b/pyproject.toml index 53866fd32..69e2e2b93 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ dependencies = [ ] [project.scripts] -warcli = "warnet.cli.main:cli" +warcli = "warnet.main:cli" [project.urls] Homepage = "https://warnet.dev" @@ -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" = ["**/*"] diff --git a/src/warnet/cli/__init__.py b/resources/scenarios/__init__.py similarity index 100% rename from src/warnet/cli/__init__.py rename to resources/scenarios/__init__.py diff --git a/src/warnet/scenarios/commander.py b/resources/scenarios/commander.py similarity index 100% rename from src/warnet/scenarios/commander.py rename to resources/scenarios/commander.py diff --git a/src/warnet/scenarios/ln_init.py b/resources/scenarios/ln_init.py similarity index 98% rename from src/warnet/scenarios/ln_init.py rename to resources/scenarios/ln_init.py index 4894e1dc0..e915ee721 100644 --- a/src/warnet/scenarios/ln_init.py +++ b/resources/scenarios/ln_init.py @@ -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(): diff --git a/src/warnet/scenarios/miner_std.py b/resources/scenarios/miner_std.py similarity index 95% rename from src/warnet/scenarios/miner_std.py rename to resources/scenarios/miner_std.py index 1f54f86f9..fcfea9841 100755 --- a/src/warnet/scenarios/miner_std.py +++ b/resources/scenarios/miner_std.py @@ -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(): diff --git a/src/warnet/scenarios/sens_relay.py b/resources/scenarios/sens_relay.py similarity index 91% rename from src/warnet/scenarios/sens_relay.py rename to resources/scenarios/sens_relay.py index 0fa4ed55a..210b43760 100644 --- a/src/warnet/scenarios/sens_relay.py +++ b/resources/scenarios/sens_relay.py @@ -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(): diff --git a/src/warnet/scenarios/tx_flood.py b/resources/scenarios/tx_flood.py similarity index 95% rename from src/warnet/scenarios/tx_flood.py rename to resources/scenarios/tx_flood.py index 69b00b460..5da5f8b53 100755 --- a/src/warnet/scenarios/tx_flood.py +++ b/resources/scenarios/tx_flood.py @@ -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(): diff --git a/src/warnet/cli/admin.py b/src/warnet/admin.py similarity index 100% rename from src/warnet/cli/admin.py rename to src/warnet/admin.py diff --git a/src/warnet/cli/bitcoin.py b/src/warnet/bitcoin.py similarity index 100% rename from src/warnet/cli/bitcoin.py rename to src/warnet/bitcoin.py diff --git a/src/warnet/cli/graph.py b/src/warnet/graph.py similarity index 100% rename from src/warnet/cli/graph.py rename to src/warnet/graph.py diff --git a/src/warnet/cli/image.py b/src/warnet/image.py similarity index 100% rename from src/warnet/cli/image.py rename to src/warnet/image.py diff --git a/src/warnet/cli/image_build.py b/src/warnet/image_build.py similarity index 97% rename from src/warnet/cli/image_build.py rename to src/warnet/image_build.py index 1cc7864f8..98f502e23 100644 --- a/src/warnet/cli/image_build.py +++ b/src/warnet/image_build.py @@ -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): diff --git a/src/warnet/cli/k8s.py b/src/warnet/k8s.py similarity index 97% rename from src/warnet/cli/k8s.py rename to src/warnet/k8s.py index 9b8f72ba3..22adb96e0 100644 --- a/src/warnet/cli/k8s.py +++ b/src/warnet/k8s.py @@ -1,6 +1,5 @@ import json import tempfile -from importlib.resources import files from pathlib import Path import yaml @@ -10,7 +9,6 @@ from .process import run_command, stream_command -WAR_MANIFESTS = files("manifests") DEFAULT_NAMESPACE = "warnet" diff --git a/src/warnet/cli/ln.py b/src/warnet/ln.py similarity index 100% rename from src/warnet/cli/ln.py rename to src/warnet/ln.py diff --git a/src/warnet/cli/main.py b/src/warnet/main.py similarity index 98% rename from src/warnet/cli/main.py rename to src/warnet/main.py index e43c22002..a79356baa 100644 --- a/src/warnet/cli/main.py +++ b/src/warnet/main.py @@ -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() diff --git a/src/warnet/cli/namespaces.py b/src/warnet/namespaces.py similarity index 97% rename from src/warnet/cli/namespaces.py rename to src/warnet/namespaces.py index 83c433bc9..0b14de4dd 100644 --- a/src/warnet/cli/namespaces.py +++ b/src/warnet/namespaces.py @@ -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): diff --git a/src/warnet/cli/network.py b/src/warnet/network.py similarity index 96% rename from src/warnet/cli/network.py rename to src/warnet/network.py index 1b4b1752d..d160587e3 100644 --- a/src/warnet/cli/network.py +++ b/src/warnet/network.py @@ -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") diff --git a/src/warnet/cli/process.py b/src/warnet/process.py similarity index 100% rename from src/warnet/cli/process.py rename to src/warnet/process.py diff --git a/src/warnet/cli/scenarios.py b/src/warnet/scenarios.py similarity index 86% rename from src/warnet/cli/scenarios.py rename to src/warnet/scenarios.py index 370b93ce9..8ce115444 100644 --- a/src/warnet/cli/scenarios.py +++ b/src/warnet/scenarios.py @@ -5,6 +5,7 @@ import sys import tempfile import time +from importlib.resources import files import click import yaml @@ -12,14 +13,12 @@ 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() @@ -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 @@ -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 @@ -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 from the Warnet Test Framework with optional arguments + Start with optional arguments """ if not scenario_path.endswith(".py"): print("Error. Currently only python scenarios are supported") @@ -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 diff --git a/src/warnet/scenarios/__init__.py b/src/warnet/scenarios/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/warnet/cli/util.py b/src/warnet/util.py similarity index 100% rename from src/warnet/cli/util.py rename to src/warnet/util.py diff --git a/test/data/scenario_p2p_interface.py b/test/data/scenario_p2p_interface.py index 54c044c7c..27508681f 100644 --- a/test/data/scenario_p2p_interface.py +++ b/test/data/scenario_p2p_interface.py @@ -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 diff --git a/test/scenarios_test.py b/test/scenarios_test.py index b5b940338..a56714ecd 100755 --- a/test/scenarios_test.py +++ b/test/scenarios_test.py @@ -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): @@ -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")) diff --git a/test/test_base.py b/test/test_base.py index 5e7c6953b..4753cf2d7 100644 --- a/test/test_base.py +++ b/test/test_base.py @@ -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: