Skip to content

Commit df97d0c

Browse files
chore: Remove print statements (canonical#326)
As a library, pycloudlib really shouldn't be printing anything ever. Convert all non-test print statements to the appropriate log message.
1 parent b329dd9 commit df97d0c

File tree

7 files changed

+25
-23
lines changed

7 files changed

+25
-23
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1!5.9.0
1+
1!5.10.0

pycloudlib/cloud.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pycloudlib.key import KeyPair
1818
from pycloudlib.util import (
1919
get_timestamped_tag,
20-
print_exception_list,
20+
log_exception_list,
2121
validate_tag,
2222
)
2323

@@ -82,7 +82,7 @@ def __enter__(self):
8282
def __exit__(self, _type, _value, _trackback):
8383
"""Cleanup context manager for this class."""
8484
exceptions = self.clean()
85-
print_exception_list(exceptions)
85+
log_exception_list(exceptions)
8686
if exceptions:
8787
raise CleanupError(exceptions)
8888

pycloudlib/instance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from pycloudlib.errors import CleanupError, PycloudlibTimeoutError
2121
from pycloudlib.result import Result
22-
from pycloudlib.util import print_exception_list, shell_pack, shell_quote
22+
from pycloudlib.util import log_exception_list, shell_pack, shell_quote
2323

2424

2525
class BaseInstance(ABC):
@@ -48,7 +48,7 @@ def __enter__(self):
4848
def __exit__(self, _type, _value, _traceback):
4949
"""Exit context manager for this class."""
5050
exceptions = self.delete()
51-
print_exception_list(exceptions)
51+
log_exception_list(exceptions)
5252
if exceptions:
5353
raise CleanupError(exceptions)
5454

pycloudlib/lxd/cloud.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ def create_profile(self, profile_name, profile_config, force=False):
7171
]
7272

7373
if profile_name in profile_list and not force:
74-
msg = "The profile named {} already exists".format(profile_name)
74+
msg = f"The profile named {profile_name} already exists"
7575
self._log.debug(msg)
76-
print(msg)
7776
return
7877

7978
if force:
@@ -250,7 +249,7 @@ def init(
250249
config_dict=config_dict,
251250
)
252251

253-
print(cmd)
252+
self._log.info(cmd)
254253
result = subp(cmd)
255254

256255
if not name:

pycloudlib/oci/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This file is part of pycloudlib. See LICENSE file for license information.
22
"""Utilities for OCI images and instances."""
3+
import logging
34
import time
45
from typing import TYPE_CHECKING, Optional
56

@@ -9,6 +10,9 @@
910
import oci
1011

1112

13+
log = logging.getLogger(__name__)
14+
15+
1216
def wait_till_ready(func, current_data, desired_state, sleep_seconds=1000):
1317
"""Wait until the results of function call reach a desired lifecycle state.
1418
@@ -72,9 +76,9 @@ def get_subnet_id(
7276
subnet_id = None
7377
for subnet in subnets:
7478
if subnet.prohibit_internet_ingress: # skip subnet if it's private
75-
print("Ignoring private subnet: " + subnet.id)
79+
log.debug("Ignoring private subnet: %s", subnet.id)
7680
continue
77-
print("Using public subnet: " + subnet.id)
81+
log.debug("Using public subnet: %s", subnet.id)
7882
if subnet.availability_domain == availability_domain:
7983
subnet_id = subnet.id
8084
break

pycloudlib/util.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import base64
55
import collections.abc
66
import datetime
7+
import logging
78
import os
89
import platform
910
import re
1011
import shlex
1112
import subprocess
12-
import sys
1313
import tempfile
1414
import traceback
1515
from errno import ENOENT
@@ -32,6 +32,8 @@
3232

3333
LTS_RELEASES = ["xenial", "bionic", "focal", "jammy"]
3434

35+
log = logging.getLogger(__name__)
36+
3537

3638
def chmod(path, mode):
3739
"""Run chmod on a file or directory.
@@ -398,11 +400,10 @@ def add_key_to_cloud_config(
398400
return "#cloud-config\n" + new_data
399401

400402

401-
def print_exception_list(exceptions: List[Exception]):
403+
def log_exception_list(exceptions: List[Exception]):
402404
"""Print a list of exceptions (including traceback) to stderr."""
403405
if exceptions:
404-
print("Encountered exception(s) during cleanup!\n", file=sys.stderr)
406+
log.error("Encountered exception(s) during cleanup!")
405407
for i, e in enumerate(exceptions, start=1):
406-
print(f"===== EXCEPTION {i} =====", file=sys.stderr)
407-
traceback.print_exception(type(e), e, e.__traceback__)
408-
print("", file=sys.stderr)
408+
tb = traceback.format_exception(type(e), e, e.__traceback__)
409+
log.error("===== EXCEPTION %s =====\n%s", i, "".join(tb))

tests/unit_tests/lxd/test_cloud.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,19 @@ class TestProfileCreation:
8484
"""Tests covering pycloudlib.lxd.cloud.create_profile method."""
8585

8686
@mock.patch("pycloudlib.lxd.cloud.subp")
87-
def test_create_profile_that_already_exists(self, m_subp):
87+
def test_create_profile_that_already_exists(self, m_subp, caplog):
8888
"""Tests creating a profile that already exists."""
8989
m_subp.return_value = """
9090
- name: test_profile
9191
"""
9292
cloud = LXDContainer(tag="test", config_file=io.StringIO(CONFIG))
9393

94-
fake_stdout = io.StringIO()
95-
with contextlib.redirect_stdout(fake_stdout):
96-
cloud.create_profile(
97-
profile_name="test_profile", profile_config="profile_config"
98-
)
94+
cloud.create_profile(
95+
profile_name="test_profile", profile_config="profile_config"
96+
)
9997

10098
expected_msg = "The profile named test_profile already exists"
101-
assert expected_msg in fake_stdout.getvalue().strip()
99+
assert expected_msg in caplog.text
102100
assert m_subp.call_args_list == [
103101
mock.call(["lxc", "profile", "list", "--format", "yaml"])
104102
]

0 commit comments

Comments
 (0)