Skip to content

Commit f722c94

Browse files
authored
Merge pull request #480 from willcl-ark/remove-cli-subtree
2 parents 6bafb2c + 36083f9 commit f722c94

24 files changed

+72
-48
lines changed

pyproject.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies = [
2121
]
2222

2323
[project.scripts]
24-
warcli = "warnet.cli.main:cli"
24+
warcli = "warnet.main:cli"
2525

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

4444
[tool.setuptools.packages.find]
45-
where = ["src", "resources"]
45+
where = ["src", "."]
46+
include = ["warnet*", "test_framework*", "resources*"]
47+
48+
[tool.setuptools.package-data]
49+
"resources" = ["**/*"]
File renamed without changes.
File renamed without changes.

src/warnet/scenarios/ln_init.py renamed to resources/scenarios/ln_init.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from time import sleep
44

55
# The base class exists inside the commander container
6-
from commander import Commander
6+
try:
7+
from commander import Commander
8+
except ImportError:
9+
from resources.scenarios.commander import Commander
710

811

912
def cli_help():

src/warnet/scenarios/miner_std.py renamed to resources/scenarios/miner_std.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from time import sleep
44

55
# The base class exists inside the commander container
6-
from commander import Commander
6+
try:
7+
from commander import Commander
8+
except ImportError:
9+
from resources.scenarios.commander import Commander
710

811

912
def cli_help():

src/warnet/scenarios/sens_relay.py renamed to resources/scenarios/sens_relay.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/usr/bin/env python3
22

33
# The base class exists inside the commander container
4-
from commander import Commander
4+
try:
5+
from commander import Commander
6+
except ImportError:
7+
from resources.scenarios.commander import Commander
58

69

710
def cli_help():

src/warnet/scenarios/tx_flood.py renamed to resources/scenarios/tx_flood.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from time import sleep
55

66
# The base class exists inside the commander container
7-
from commander import Commander
7+
try:
8+
from commander import Commander
9+
except ImportError:
10+
from resources.scenarios.commander import Commander
811

912

1013
def cli_help():
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/warnet/cli/image_build.py renamed to src/warnet/image_build.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

6-
dockerfile_path = files("images.bitcoin").joinpath("Dockerfile")
6+
dockerfile_path = files("resources.images.bitcoin").joinpath("Dockerfile")
77

88

99
def run_command(command):

src/warnet/cli/k8s.py renamed to src/warnet/k8s.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
import tempfile
3-
from importlib.resources import files
43
from pathlib import Path
54

65
import yaml
@@ -10,7 +9,6 @@
109

1110
from .process import run_command, stream_command
1211

13-
WAR_MANIFESTS = files("manifests")
1412
DEFAULT_NAMESPACE = "warnet"
1513

1614

File renamed without changes.

src/warnet/cli/main.py renamed to src/warnet/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .network import copy_network_defaults, network
1414
from .scenarios import scenarios
1515

16-
QUICK_START_PATH = files("scripts").joinpath("quick_start.sh")
16+
QUICK_START_PATH = files("resources.scripts").joinpath("quick_start.sh")
1717

1818

1919
@click.group()

src/warnet/cli/namespaces.py renamed to src/warnet/namespaces.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
from .process import run_command, stream_command
1010

11-
WARNET_NAMESPACES_DIR = files("namespaces")
11+
WARNET_NAMESPACES_DIR = files("resources").joinpath("namespaces")
1212
NAMESPACES_DIR = Path("namespaces")
1313
DEFAULT_NAMESPACES = Path("two_namespaces_two_users")
1414
NAMESPACES_FILE = "namespaces.yaml"
1515
DEFAULTS_FILE = "namespace-defaults.yaml"
1616
HELM_COMMAND = "helm upgrade --install"
17-
BITCOIN_CHART_LOCATION = Path(str(files("charts").joinpath("namespaces")))
17+
BITCOIN_CHART_LOCATION = Path(str(files("resources.charts").joinpath("namespaces")))
1818

1919

2020
def copy_namespaces_defaults(directory: Path):

src/warnet/cli/network.py renamed to src/warnet/network.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
from .k8s import delete_namespace, get_default_namespace, get_mission, get_pods
1313
from .process import stream_command
1414

15-
WAR_MANIFESTS = files("manifests")
16-
WARNET_NETWORK_DIR = files("networks")
17-
NETWORK_DIR = Path("networks")
15+
WAR_MANIFESTS = files("resources.manifests")
16+
WARNET_NETWORK_DIR = files("resources.networks")
17+
NETWORK_DIR = Path("resources.networks")
1818
DEFAULT_NETWORK = Path("6_node_bitcoin")
1919
NETWORK_FILE = "network.yaml"
2020
DEFAULTS_FILE = "node-defaults.yaml"
2121
HELM_COMMAND = "helm upgrade --install --create-namespace"
22-
BITCOIN_CHART_LOCATION = str(files("charts").joinpath("bitcoincore"))
22+
BITCOIN_CHART_LOCATION = str(files("resources.charts").joinpath("bitcoincore"))
2323

2424

2525
@click.group(name="network")
File renamed without changes.

src/warnet/cli/scenarios.py renamed to src/warnet/scenarios.py

+29-23
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
import sys
66
import tempfile
77
import time
8+
from importlib.resources import files
89

910
import click
1011
import yaml
1112
from rich import print
1213
from rich.console import Console
1314
from rich.table import Table
1415

15-
from warnet import scenarios as SCENARIOS
16-
1716
from .k8s import apply_kubernetes_yaml, get_default_namespace, get_mission
1817

1918

2019
@click.group(name="scenarios")
2120
def scenarios():
22-
"""Manage scenarios on a running network"""
21+
"""Manage scenarios on a network"""
2322

2423

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

4241

4342
def _available():
44-
# This ugly hack temporarily allows us to import the scenario modules
45-
# in the context in which they run: as __main__ from
46-
# the root directory of the commander container.
47-
scenarios_path = SCENARIOS.__path__
48-
sys.path.insert(0, scenarios_path[0])
49-
50-
scenario_list = []
51-
for s in pkgutil.iter_modules(scenarios_path):
52-
module_name = f"warnet.scenarios.{s.name}"
53-
try:
54-
m = importlib.import_module(module_name)
55-
if hasattr(m, "cli_help"):
56-
scenario_list.append((s.name, m.cli_help()))
57-
except Exception as e:
58-
print(f"Ignoring module: {module_name} because {e}")
59-
60-
# Clean up that ugly hack
61-
sys.path.pop(0)
43+
scenarios_dir = files("resources.scenarios")
44+
sys.path.append(scenarios_dir)
45+
46+
try:
47+
scenario_list = []
48+
package_name = "resources.scenarios"
49+
for _, name, _ in pkgutil.iter_modules([scenarios_dir]):
50+
module_name = f"{package_name}.{name}"
51+
try:
52+
m = importlib.import_module(module_name)
53+
if hasattr(m, "cli_help"):
54+
scenario_list.append((name, m.cli_help()))
55+
except Exception as e:
56+
print(f"Error importing module {module_name}: {e}")
57+
finally:
58+
sys.path.remove(scenarios_dir)
6259

6360
return scenario_list
6461

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

7471
# Use importlib.resources to get the scenario path
75-
scenario_package = "warnet.scenarios"
72+
scenario_package = "resources.scenarios"
7673
scenario_filename = f"{scenario}.py"
7774

7875
# Ensure the scenario file exists within the package
@@ -86,7 +83,7 @@ def run(scenario: str, additional_args: tuple[str]):
8683
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
8784
def run_file(scenario_path: str, additional_args: tuple[str]):
8885
"""
89-
Run <scenario_path> from the Warnet Test Framework with optional arguments
86+
Start <scenario_path> with optional arguments
9087
"""
9188
if not scenario_path.endswith(".py"):
9289
print("Error. Currently only python scenarios are supported")
@@ -206,3 +203,12 @@ def active():
206203
def _active() -> list[str]:
207204
commanders = get_mission("commander")
208205
return [{"commander": c.metadata.name, "status": c.status.phase.lower()} for c in commanders]
206+
207+
208+
@scenarios.command()
209+
@click.argument("pid", type=int)
210+
def stop(pid: int):
211+
"""
212+
Stop scenario
213+
"""
214+
pass

src/warnet/scenarios/__init__.py

Whitespace-only changes.
File renamed without changes.

test/data/scenario_p2p_interface.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
from collections import defaultdict
33

44
# The base class exists inside the commander container
5-
from commander import Commander
5+
try:
6+
from commander import Commander
7+
except Exception:
8+
from resources.scenarios.commander import Commander
9+
610

711
from test_framework.messages import CInv, msg_getdata
812
from test_framework.p2p import P2PInterface

test/scenarios_test.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
from test_base import TestBase
77

8-
from warnet.cli.k8s import delete_pod
9-
from warnet.cli.process import run_command
10-
from warnet.cli.scenarios import _active as scenarios_active
11-
from warnet.cli.scenarios import _available as scenarios_available
8+
from warnet.k8s import delete_pod
9+
from warnet.process import run_command
10+
from warnet.scenarios import _active as scenarios_active
11+
from warnet.scenarios import _available as scenarios_available
1212

1313

1414
class ScenariosTest(TestBase):
@@ -69,7 +69,7 @@ def run_and_check_miner_scenario(self):
6969
self.stop_scenario()
7070

7171
def run_and_check_miner_scenario_from_file(self):
72-
scenario_file = "src/warnet/scenarios/miner_std.py"
72+
scenario_file = "resources/scenarios/miner_std.py"
7373
self.log.info(f"Running scenario from file: {scenario_file}")
7474
self.warcli(f"scenarios run-file {scenario_file} --allnodes --interval=1")
7575
start = int(self.warcli("bitcoin rpc tank-0000 getblockcount"))

test/test_base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from time import sleep
1212

1313
from warnet import SRC_DIR
14-
from warnet.cli.network import _connected as network_connected
15-
from warnet.cli.network import _status as network_status
16-
from warnet.cli.scenarios import _active as scenarios_active
14+
from warnet.network import _connected as network_connected
15+
from warnet.network import _status as network_status
16+
from warnet.scenarios import _active as scenarios_active
1717

1818

1919
class TestBase:

0 commit comments

Comments
 (0)