Skip to content

Commit 4bf4822

Browse files
committed
black-ify everything
1 parent e51d5a0 commit 4bf4822

File tree

8 files changed

+307
-159
lines changed

8 files changed

+307
-159
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ classifiers = [
1313
"Programming Language :: Python :: 3",
1414
]
1515
dependencies = [
16+
"black==23.7.0",
1617
"click==8.1.7",
1718
"docker==6.1.3",
1819
"flask==2.3.3",

src/warnet/cli.py

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,28 @@ def rpc_call(rpc_method, params: Optional[Union[Dict[str, Any], Tuple[Any, ...]]
2929
def cli():
3030
pass
3131

32+
3233
@click.group(name="debug")
3334
def debug():
3435
"""Debug commands"""
36+
37+
3538
cli.add_command(debug)
3639

40+
3741
@click.group(name="scenarios")
3842
def scenarios():
3943
"""Scenario commands"""
44+
45+
4046
cli.add_command(scenarios)
4147

48+
4249
@click.group(name="network")
4350
def network():
4451
"""Network commands"""
52+
53+
4554
cli.add_command(network)
4655

4756

@@ -69,31 +78,42 @@ def help_command(ctx, command):
6978
help_info = cmd_obj.get_help(ctx).split("\n", 1)[-1].strip()
7079

7180
# Extract the arguments of the command
72-
arguments = [param.human_readable_name.upper() for param in cmd_obj.params if isinstance(param, click.Argument)]
81+
arguments = [
82+
param.human_readable_name.upper()
83+
for param in cmd_obj.params
84+
if isinstance(param, click.Argument)
85+
]
7386

7487
# Determine the correct usage string based on whether the command has subcommands
7588
if isinstance(cmd_obj, click.Group) and cmd_obj.list_commands(ctx):
76-
usage_str = f"Usage: warnet {command} [OPTIONS] COMMAND [ARGS...]\n\n{help_info}"
89+
usage_str = (
90+
f"Usage: warnet {command} [OPTIONS] COMMAND [ARGS...]\n\n{help_info}"
91+
)
7792
else:
7893
args_str = " ".join(arguments)
7994
usage_str = f"Usage: warnet {command} [OPTIONS] {args_str}\n\n{help_info}"
80-
95+
8196
print(usage_str)
8297

98+
8399
cli.add_command(help_command)
84100

85101

86102
@cli.command(context_settings={"ignore_unknown_options": True})
87-
@click.argument('node', type=int)
88-
@click.argument('method', type=str, nargs=-1) # this will capture all remaining arguments
89-
@click.option('--params', type=str, multiple=True, default=())
90-
@click.option('--network', default='warnet', show_default=True)
103+
@click.argument("node", type=int)
104+
@click.argument(
105+
"method", type=str, nargs=-1
106+
) # this will capture all remaining arguments
107+
@click.option("--params", type=str, multiple=True, default=())
108+
@click.option("--network", default="warnet", show_default=True)
91109
def rpc(node, method, params, network):
92110
"""
93111
Call bitcoin-cli <method> <params> on <node> in <--network>
94112
"""
95113
if len(method) > 2:
96-
raise click.BadArgumentUsage("You can provide at most two arguments for 'method'.")
114+
raise click.BadArgumentUsage(
115+
"You can provide at most two arguments for 'method'."
116+
)
97117

98118
# Convert tuple to space-separated string
99119
method_str = " ".join(method)
@@ -109,8 +129,8 @@ def rpc(node, method, params, network):
109129

110130

111131
@cli.command()
112-
@click.argument('node', type=int, required=True)
113-
@click.option('--network', default='warnet', show_default=True)
132+
@click.argument("node", type=int, required=True)
133+
@click.option("--network", default="warnet", show_default=True)
114134
def debug_log(node, network):
115135
"""
116136
Fetch the Bitcoin Core debug log from <node> in [network]
@@ -123,14 +143,15 @@ def debug_log(node, network):
123143

124144

125145
@cli.command()
126-
@click.argument('node_a', type=int, required=True)
127-
@click.argument('node_b', type=int, required=True)
128-
@click.option('--network', default='warnet', show_default=True)
146+
@click.argument("node_a", type=int, required=True)
147+
@click.argument("node_b", type=int, required=True)
148+
@click.option("--network", default="warnet", show_default=True)
129149
def messages(node_a, node_b, network):
130150
"""
131151
Fetch messages sent between <node_a> and <node_b> in <network>
132152
"""
133153
import logging
154+
134155
logging.warning(f"got args: {node_a}, {node_b}, {network}")
135156
try:
136157
result = rpc_call(
@@ -156,7 +177,7 @@ def list():
156177

157178

158179
@scenarios.command()
159-
@click.argument('scenario', type=str)
180+
@click.argument("scenario", type=str)
160181
def run(scenario):
161182
"""
162183
Run <scenario> from the Warnet Test Framework
@@ -169,49 +190,59 @@ def run(scenario):
169190

170191

171192
@debug.command()
172-
@click.argument('graph_file', type=str)
173-
@click.option('--network', default='warnet', show_default=True)
193+
@click.argument("graph_file", type=str)
194+
@click.option("--network", default="warnet", show_default=True)
174195
def generate_compose(graph_file: str, network: str = "warnet"):
175196
"""
176197
Generate the docker-compose file for a given <graph_file> and <--network> name and return it.
177198
"""
178199
try:
179-
result = rpc_call("generate_compose", {"graph_file": graph_file, "network": network})
200+
result = rpc_call(
201+
"generate_compose", {"graph_file": graph_file, "network": network}
202+
)
180203
print(result)
181204
except Exception as e:
182205
print(f"Error generating compose: {e}")
183206

207+
184208
@debug.command()
185-
@click.argument('graph_file', type=str)
186-
@click.option('--network', default='warnet', show_default=True)
209+
@click.argument("graph_file", type=str)
210+
@click.option("--network", default="warnet", show_default=True)
187211
def update_dns_seed(graph_file: Path = EXAMPLE_GRAPH_FILE, network: str = "warnet"):
188212
"""
189213
Update the dns seed database using a graph file
190214
"""
191215
try:
192-
result = rpc_call("update_dns_seeder", {"graph_file": str(graph_file), "network": network})
216+
result = rpc_call(
217+
"update_dns_seeder", {"graph_file": str(graph_file), "network": network}
218+
)
193219
print(result)
194220
except Exception as e:
195221
print(f"Error updating dns seed addresses: {e}")
196222

197223

198224
@network.command()
199-
@click.argument('graph_file', default=EXAMPLE_GRAPH_FILE, type=click.Path())
200-
@click.option('--force', default=False, is_flag=True, type=bool)
201-
@click.option('--network', default='warnet', show_default=True)
202-
def start(graph_file: Path = EXAMPLE_GRAPH_FILE, force: bool = False, network: str = "warnet"):
225+
@click.argument("graph_file", default=EXAMPLE_GRAPH_FILE, type=click.Path())
226+
@click.option("--force", default=False, is_flag=True, type=bool)
227+
@click.option("--network", default="warnet", show_default=True)
228+
def start(
229+
graph_file: Path = EXAMPLE_GRAPH_FILE, force: bool = False, network: str = "warnet"
230+
):
203231
"""
204232
Start a warnet with topology loaded from a <graph_file> into <--network> (default: "warnet")
205233
"""
206234
try:
207-
result = rpc_call("from_file", {"graph_file": str(graph_file), "force": force, "network": network})
235+
result = rpc_call(
236+
"from_file",
237+
{"graph_file": str(graph_file), "force": force, "network": network},
238+
)
208239
print(result)
209240
except Exception as e:
210241
print(f"Error creating network: {e}")
211242

212243

213244
@network.command()
214-
@click.option('--network', default='warnet', show_default=True)
245+
@click.option("--network", default="warnet", show_default=True)
215246
def up(network: str = "warnet"):
216247
"""
217248
Run 'docker-compose up' on a warnet named <--network> (default: "warnet").
@@ -224,7 +255,7 @@ def up(network: str = "warnet"):
224255

225256

226257
@network.command()
227-
@click.option('--network', default='warnet', show_default=True)
258+
@click.option("--network", default="warnet", show_default=True)
228259
def down(network: str = "warnet"):
229260
"""
230261
Run 'docker-compose down on a warnet named <--network> (default: "warnet").

src/warnet/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ def stop_container(c):
7272
logger.info(f"stopping container: {c.name}")
7373
c.stop()
7474

75+
7576
def stop_network(network="warnet") -> bool:
7677
"""
7778
Stop all containers in the network in parallel using a background thread
7879
"""
80+
7981
def thread_stop():
8082
d = docker.from_env()
8183
network_obj = d.networks.get(network)
@@ -87,6 +89,7 @@ def thread_stop():
8789
threading.Thread(target=thread_stop).start()
8890
return True
8991

92+
9093
def compose_down(network="warnet") -> bool:
9194
"""
9295
Run docker-compose down on a warnet

src/warnet/tank.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ def __init__(self):
4545
self.config_dir = Path()
4646

4747
def __str__(self) -> str:
48-
return (f"Tank(\n"
49-
f"\tIndex: {self.index}\n"
50-
f"\tVersion: {self.version}\n"
51-
f"\tConf: {self.conf}\n"
52-
f"\tConf File: {self.conf_file}\n"
53-
f"\tNetem: {self.netem}\n"
54-
f"\tIPv4: {self._ipv4}\n"
55-
f"\t)")
48+
return (
49+
f"Tank(\n"
50+
f"\tIndex: {self.index}\n"
51+
f"\tVersion: {self.version}\n"
52+
f"\tConf: {self.conf}\n"
53+
f"\tConf File: {self.conf_file}\n"
54+
f"\tNetem: {self.netem}\n"
55+
f"\tIPv4: {self._ipv4}\n"
56+
f"\t)"
57+
)
5658

5759
@classmethod
5860
def from_graph_node(cls, index, warnet):
@@ -67,14 +69,17 @@ def from_graph_node(cls, index, warnet):
6769
if "version" in node:
6870
if not "/" and "#" in self.version:
6971
if node["version"] not in SUPPORTED_TAGS:
70-
raise Exception(f"Unsupported version: can't be generated from Docker images: {node['version']}")
72+
raise Exception(
73+
f"Unsupported version: can't be generated from Docker images: {node['version']}"
74+
)
7175
self.version = node["version"]
7276
if "bitcoin_config" in node:
7377
self.conf = node["bitcoin_config"]
7478
if "tc_netem" in node:
7579
self.netem = node["tc_netem"]
7680
with open(self.warnet.fork_observer_config, "a") as f:
77-
f.write(f'''
81+
f.write(
82+
f"""
7883
[[networks.nodes]]
7984
id = {self.index}
8085
name = "Node {self.index}"
@@ -83,7 +88,8 @@ def from_graph_node(cls, index, warnet):
8388
rpc_port = {self.rpc_port}
8489
rpc_user = "{self.rpc_user}"
8590
rpc_password = "{self.rpc_password}"
86-
''')
91+
"""
92+
)
8793
self.config_dir = self.warnet.config_dir / str(self.suffix)
8894
self.config_dir.mkdir(parents=True, exist_ok=True)
8995
self.write_torrc()
@@ -133,8 +139,10 @@ def container(self) -> Container:
133139
def exec(self, cmd: str, user: str = "root"):
134140
result = self.container.exec_run(cmd=cmd, user=user)
135141
if result.exit_code != 0:
136-
raise Exception(f"Command failed with exit code {result.exit_code}: {result.output.decode('utf-8')}")
137-
return result.output.decode('utf-8')
142+
raise Exception(
143+
f"Command failed with exit code {result.exit_code}: {result.output.decode('utf-8')}"
144+
)
145+
return result.output.decode("utf-8")
138146

139147
def apply_network_conditions(self):
140148
if self.netem is None:
@@ -181,7 +189,7 @@ def write_bitcoin_conf(self, base_bitcoin_conf):
181189
self.conf_file = path
182190

183191
def write_torrc(self):
184-
src_tor_conf_file = TEMPLATES / 'torrc'
192+
src_tor_conf_file = TEMPLATES / "torrc"
185193

186194
dest_path = self.config_dir / "torrc"
187195
shutil.copyfile(src_tor_conf_file, dest_path)
@@ -214,23 +222,23 @@ def add_services(self, services):
214222
services[self.bitcoind_name].update({"entrypoint": "/warnet_entrypoint.sh"})
215223

216224
# Add the bitcoind service
217-
services[self.bitcoind_name].update({
218-
"container_name": self.bitcoind_name,
219-
"build": build,
220-
"volumes": [
221-
f"{self.conf_file}:/home/bitcoin/.bitcoin/bitcoin.conf",
222-
f"{self.torrc_file}:/etc/tor/torrc_original",
223-
],
224-
"networks": {
225-
self.docker_network: {
226-
"ipv4_address": f"{self.ipv4}",
227-
}
228-
},
229-
"labels": {
230-
"warnet": "tank"
231-
},
232-
"privileged": True,
233-
})
225+
services[self.bitcoind_name].update(
226+
{
227+
"container_name": self.bitcoind_name,
228+
"build": build,
229+
"volumes": [
230+
f"{self.conf_file}:/home/bitcoin/.bitcoin/bitcoin.conf",
231+
f"{self.torrc_file}:/etc/tor/torrc_original",
232+
],
233+
"networks": {
234+
self.docker_network: {
235+
"ipv4_address": f"{self.ipv4}",
236+
}
237+
},
238+
"labels": {"warnet": "tank"},
239+
"privileged": True,
240+
}
241+
)
234242

235243
# Add the prometheus data exporter in a neighboring container
236244
# services[self.exporter_name] = {

0 commit comments

Comments
 (0)