Skip to content

Commit 6ee096c

Browse files
authored
Switch to click for argument parsing and output (#162)
1 parent bf6b39c commit 6ee096c

22 files changed

Lines changed: 321 additions & 252 deletions

.github/workflows/python_verify.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ on:
33
push:
44
branches:
55
- main
6-
paths: &paths
7-
- 'client/smoke-tests/**'
6+
paths:
7+
- 'client/smoke_tests/**'
88
- 'client/src/**'
99
- 'environment/**'
1010
- 'tests/**'
1111
pull_request:
1212
branches:
1313
- "**"
14-
paths: *paths
14+
paths:
15+
- 'client/smoke_tests/**'
16+
- 'client/src/**'
17+
- 'environment/**'
18+
- 'tests/**'
1519

1620
jobs:
1721
mypy:

.github/workflows/verify_python.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
python3 -m venv venv
44
source ./venv/bin/activate
5+
pip install --upgrade pip
56
pip install mypy
67
pip install pytest pytest-asyncio
7-
pip install types-requests types-Deprecated types-tqdm types-paramiko types-netifaces types-psutil types-termcolor
8+
pip install types-requests types-Deprecated types-tqdm types-paramiko types-netifaces types-psutil click
89
pip install ./client
910
echo "Checking tests files..."
1011
python -m mypy tests --exclude=venv

environment/aws/common/docker.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from pathlib import Path
44
from typing import List, Optional
55

6+
import click
7+
68
from environment.aws.common.output import header
79

810

@@ -130,14 +132,14 @@ def start_container(
130132

131133
if container_check.stdout.strip() != "":
132134
if container_check.stdout.startswith("Up"):
133-
print(f"{name} already running, returning...")
135+
click.echo(f"{name} already running, returning...")
134136
return
135137

136-
print(f"Restarting existing {name} container...")
138+
click.echo(f"Restarting existing {name} container...")
137139
subprocess.run(["docker", "start", name], check=False, env=env)
138140
return
139141

140-
print(f"Starting new {name} container...")
142+
click.echo(f"Starting new {name} container...")
141143
args = [
142144
"docker",
143145
"run",

environment/aws/common/io.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import zipfile
2222
from pathlib import Path
2323

24+
import click
2425
import paramiko
2526
from requests import Response
2627
from tqdm import tqdm
2728

29+
LIGHT_GRAY = (128, 128, 128)
30+
2831

2932
def download_progress_bar(response: Response, output_path: Path) -> None:
3033
"""
@@ -83,14 +86,14 @@ def zip_directory(input: Path, output: Path) -> None:
8386
if not input.exists():
8487
raise RuntimeError(f"{input} does not exist...")
8588

86-
print("Zipping...")
89+
click.echo("Zipping...")
8790
with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as zipf:
8891
for root, _, files in os.walk(input):
8992
for file in tqdm(files, desc="Zipping"):
9093
file_path = Path(root) / file
9194
zipf.write(file_path, file_path.relative_to(input))
9295

93-
print("Done")
96+
click.echo("Done")
9497

9598

9699
def unzip_directory(input: Path, output: Path) -> None:
@@ -128,7 +131,7 @@ def unzip_directory(input: Path, output: Path) -> None:
128131
if perm:
129132
extracted_path.chmod(perm)
130133

131-
print("Done")
134+
click.echo("Done")
132135

133136

134137
def tar_directory(input: Path, output: Path) -> None:
@@ -145,14 +148,14 @@ def tar_directory(input: Path, output: Path) -> None:
145148
if not input.exists():
146149
raise RuntimeError(f"{input} does not exist...")
147150

148-
print("Compressing")
151+
click.echo("Compressing")
149152
with tarfile.open(output, "w:gz") as tar:
150153
for root, _, files in os.walk(input):
151154
for file in tqdm(files, desc="Archiving"):
152155
file_path = Path(root) / file
153156
tar.add(file_path, arcname=file_path.relative_to(input))
154157

155-
print("Done")
158+
click.echo("Done")
156159

157160

158161
def untar_directory(input: Path, output: Path) -> None:
@@ -169,7 +172,7 @@ def untar_directory(input: Path, output: Path) -> None:
169172
if not input.exists():
170173
raise RuntimeError(f"{input} does not exist...")
171174

172-
print("Extracting")
175+
click.echo("Extracting")
173176
with tarfile.open(input, "r:gz") as tar:
174177
for member in tqdm(tar.getmembers(), desc="Extracting"):
175178
tar.extract(member, path=output)
@@ -179,4 +182,4 @@ def untar_directory(input: Path, output: Path) -> None:
179182
if member.mode and not member.islnk() and not member.issym():
180183
extracted_path.chmod(member.mode)
181184

182-
print("Done")
185+
click.echo("Done")

environment/aws/common/output.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Print a green text header that makes the message very obvious
77
"""
88

9-
from termcolor import colored
9+
import click
1010

1111

1212
def header(text: str) -> None:
@@ -18,6 +18,7 @@ def header(text: str) -> None:
1818
Args:
1919
text (str): The text to print
2020
"""
21-
print()
22-
print(colored(f"=== {text} ===", "green"))
23-
print()
21+
22+
click.echo()
23+
click.secho(f"=== {text} ===", fg="green")
24+
click.echo()

environment/aws/lb_setup/setup_load_balancers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
from pathlib import Path
2222
from typing import List, Optional
2323

24+
import click
2425
import paramiko
25-
from termcolor import colored
2626

2727
from environment.aws.common.docker import (
2828
check_aws_key_checking,
2929
get_ec2_hostname,
3030
start_container,
3131
)
32-
from environment.aws.common.io import sftp_progress_bar
32+
from environment.aws.common.io import LIGHT_GRAY, sftp_progress_bar
3333
from environment.aws.common.output import header
3434
from environment.aws.topology_setup.setup_topology import TopologyConfig
3535

@@ -56,15 +56,15 @@ def remote_exec(
5656

5757
_, stdout, stderr = ssh.exec_command(command, get_pty=True)
5858
for line in iter(stdout.readline, ""):
59-
print(colored(f"[{current_ssh}] {line}", "light_grey"), end="")
59+
click.secho(f"[{current_ssh}] {line}", fg=LIGHT_GRAY, nl=False) # type: ignore
6060

6161
exit_status = stdout.channel.recv_exit_status()
6262
if fail_on_error and exit_status != 0:
63-
print(stderr.read().decode())
63+
click.secho(stderr.read().decode(), fg="red")
6464
raise Exception(f"Command '{command}' failed with exit status {exit_status}")
6565

6666
header("Done!")
67-
print()
67+
click.echo()
6868

6969

7070
def create_nginx_config(upstreams: List[str]) -> None:

environment/aws/logslurp_setup/setup_logslurp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
from pathlib import Path
2222
from typing import Optional
2323

24+
import click
2425
import paramiko
25-
from termcolor import colored
2626

2727
from environment.aws.common.docker import (
2828
check_aws_key_checking,
2929
get_ec2_hostname,
3030
start_container,
3131
)
32-
from environment.aws.common.io import sftp_progress_bar
32+
from environment.aws.common.io import LIGHT_GRAY, sftp_progress_bar
3333
from environment.aws.common.output import header
3434
from environment.aws.topology_setup.setup_topology import TopologyConfig
3535

@@ -56,15 +56,15 @@ def remote_exec(
5656

5757
_, stdout, stderr = ssh.exec_command(command, get_pty=True)
5858
for line in iter(stdout.readline, ""):
59-
print(colored(f"[{current_ssh}] {line}", "light_grey"), end="")
59+
click.secho(f"[{current_ssh}] {line}", fg=LIGHT_GRAY, nl=False) # type: ignore
6060

6161
exit_status = stdout.channel.recv_exit_status()
6262
if fail_on_error and exit_status != 0:
63-
print(stderr.read().decode())
63+
click.secho(stderr.read().decode(), fg="red")
6464
raise Exception(f"Command '{command}' failed with exit status {exit_status}")
6565

6666
header("Done!")
67-
print()
67+
click.echo()
6868

6969

7070
def main(topology: TopologyConfig, private_key: Optional[str] = None) -> None:

environment/aws/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ bcrypt==4.2.1
22
certifi==2024.12.14
33
cffi==1.17.1
44
charset-normalizer==3.4.1
5+
click==8.1.8
56
colorama==0.4.6
67
cryptography==44.0.0
78
idna==3.10
@@ -12,6 +13,5 @@ pycparser==2.22
1213
PyNaCl==1.5.0
1314
requests==2.32.3
1415
tdqm==0.0.1
15-
termcolor==2.5.0
1616
tqdm==4.67.1
1717
urllib3==2.3.0

environment/aws/server_setup/setup_server.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
from pathlib import Path
2323
from typing import Optional
2424

25+
import click
2526
import paramiko
26-
from termcolor import colored
2727

28-
from environment.aws.common.io import sftp_progress_bar
28+
from environment.aws.common.io import LIGHT_GRAY, sftp_progress_bar
2929
from environment.aws.common.output import header
3030
from environment.aws.topology_setup.setup_topology import TopologyConfig
3131

@@ -52,15 +52,15 @@ def remote_exec(
5252

5353
_, stdout, stderr = ssh.exec_command(command)
5454
for line in iter(stdout.readline, ""):
55-
print(colored(f"[{current_ssh}] {line}", "light_grey"), end="")
55+
click.secho(f"[{current_ssh}] {line}", fg=LIGHT_GRAY, nl=False) # type: ignore
5656

5757
exit_status = stdout.channel.recv_exit_status()
5858
if fail_on_error and exit_status != 0:
59-
print(stderr.read().decode())
59+
click.secho(stderr.read().decode(), fg="red")
6060
raise Exception(f"Command '{command}' failed with exit status {exit_status}")
6161

6262
header("Done!")
63-
print()
63+
click.echo()
6464

6565

6666
def setup_node(

environment/aws/sgw_setup/setup_sgw.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
from pathlib import Path
3838
from typing import Dict, Final, Optional, cast
3939

40+
import click
4041
import paramiko
4142
import requests
42-
from termcolor import colored
4343
from tqdm import tqdm
4444

45-
from environment.aws.common.io import sftp_progress_bar
45+
from environment.aws.common.io import LIGHT_GRAY, sftp_progress_bar
4646
from environment.aws.common.output import header
4747
from environment.aws.topology_setup.setup_topology import TopologyConfig
4848

@@ -174,7 +174,10 @@ def download_sgw_package(download_info: SgwDownloadInfo) -> None:
174174
size = f.write(chunk)
175175
bar.update(size)
176176
else:
177-
print(f"File {download_info.local_filename} already exists, skipping download.")
177+
click.secho(
178+
f"File {download_info.local_filename} already exists, skipping download.",
179+
fg="yellow",
180+
)
178181

179182

180183
def setup_config(server_hostname: str) -> None:
@@ -219,15 +222,15 @@ def remote_exec(
219222

220223
_, stdout, stderr = ssh.exec_command(command, get_pty=True)
221224
for line in iter(stdout.readline, ""):
222-
print(colored(f"[{current_ssh}] {line}", "light_grey"), end="")
225+
click.secho(f"[{current_ssh}] {line}", fg=LIGHT_GRAY, nl=False) # type: ignore
223226

224227
exit_status = stdout.channel.recv_exit_status()
225228
if fail_on_error and exit_status != 0:
226-
print(stderr.read().decode())
229+
click.secho(stderr.read().decode(), fg="red")
227230
raise Exception(f"Command '{command}' failed with exit status {exit_status}")
228231

229232
header("Done!")
230-
print()
233+
click.echo()
231234

232235

233236
def setup_server(
@@ -242,9 +245,9 @@ def setup_server(
242245
sgw_info (SgwDownloadInfo): The download information for Sync Gateway.
243246
"""
244247
if sgw_info.is_release:
245-
print(f"Setting up server {hostname} with SGW {sgw_info.version}")
248+
click.echo(f"Setting up server {hostname} with SGW {sgw_info.version}")
246249
else:
247-
print(
250+
click.echo(
248251
f"Setting up server {hostname} with SGW {sgw_info.version}-{sgw_info.build_no}"
249252
)
250253

0 commit comments

Comments
 (0)