Skip to content

Commit 7d65002

Browse files
authored
Merge pull request #3178 from efiop/1843
logger: remote: use lazy formatting
2 parents 3987086 + 19acded commit 7d65002

File tree

5 files changed

+59
-52
lines changed

5 files changed

+59
-52
lines changed

dvc/logger.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ class ColorFormatter(logging.Formatter):
5858
}
5959

6060
def format(self, record):
61+
msg = record.msg.format(*record.args) if record.args else record.msg
62+
6163
if record.levelname == "INFO":
62-
return record.msg
64+
return msg
6365

6466
if record.levelname == "ERROR" or record.levelname == "CRITICAL":
6567
exception, stack_trace = self._parse_exc(record)
@@ -70,16 +72,16 @@ def format(self, record):
7072
color=self.color_code.get(record.levelname, ""),
7173
nc=colorama.Fore.RESET,
7274
levelname=record.levelname,
73-
description=self._description(record.msg, exception),
74-
msg=record.msg,
75+
description=self._description(msg, exception),
76+
msg=msg,
7577
stack_trace=stack_trace,
7678
)
7779

7880
return "{color}{levelname}{nc}: {msg}".format(
7981
color=self.color_code.get(record.levelname, ""),
8082
nc=colorama.Fore.RESET,
8183
levelname=record.levelname,
82-
msg=record.msg,
84+
msg=msg,
8385
)
8486

8587
def _current_level(self):

dvc/remote/base.py

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from dvc.progress import Tqdm
2525
from dvc.remote.slow_link_detection import slow_link_guard
2626
from dvc.state import StateNoop
27-
from dvc.utils import relpath, tmp_fname
27+
from dvc.utils import tmp_fname
2828
from dvc.utils.fs import move, makedirs
2929
from dvc.utils.http import open_url
3030

@@ -275,8 +275,10 @@ def load_dir_cache(self, checksum):
275275
raise DirCacheError(checksum) from exc
276276

277277
if not isinstance(d, list):
278-
msg = "dir cache file format error '{}' [skipping the file]"
279-
logger.error(msg.format(relpath(path_info)))
278+
logger.error(
279+
"dir cache file format error '{}' [skipping the file]",
280+
path_info,
281+
)
280282
return []
281283

282284
for info in d:
@@ -342,36 +344,35 @@ def changed(self, path_info, checksum_info):
342344
"""
343345

344346
logger.debug(
345-
"checking if '{}'('{}') has changed.".format(
346-
path_info, checksum_info
347-
)
347+
"checking if '{}'('{}') has changed.", path_info, checksum_info
348348
)
349349

350350
if not self.exists(path_info):
351-
logger.debug("'{}' doesn't exist.".format(path_info))
351+
logger.debug("'{}' doesn't exist.", path_info)
352352
return True
353353

354354
checksum = checksum_info.get(self.PARAM_CHECKSUM)
355355
if checksum is None:
356-
logger.debug("checksum for '{}' is missing.".format(path_info))
356+
logger.debug("checksum for '{}' is missing.", path_info)
357357
return True
358358

359359
if self.changed_cache(checksum):
360360
logger.debug(
361-
"cache for '{}'('{}') has changed.".format(path_info, checksum)
361+
"cache for '{}'('{}') has changed.", path_info, checksum
362362
)
363363
return True
364364

365365
actual = self.get_checksum(path_info)
366366
if checksum != actual:
367367
logger.debug(
368-
"checksum '{}'(actual '{}') for '{}' has changed.".format(
369-
checksum, actual, path_info
370-
)
368+
"checksum '{}'(actual '{}') for '{}' has changed.",
369+
checksum,
370+
actual,
371+
path_info,
371372
)
372373
return True
373374

374-
logger.debug("'{}' hasn't changed.".format(path_info))
375+
logger.debug("'{}' hasn't changed.", path_info)
375376
return False
376377

377378
def link(self, from_info, to_info):
@@ -394,8 +395,9 @@ def _try_links(self, from_info, to_info, link_types):
394395
return
395396

396397
except DvcException as exc:
397-
msg = "Cache type '{}' is not supported: {}"
398-
logger.debug(msg.format(link_types[0], str(exc)))
398+
logger.debug(
399+
"Cache type '{}' is not supported: {}", link_types[0], exc
400+
)
399401
del link_types[0]
400402

401403
raise DvcException("no possible cache types left to try out.")
@@ -409,13 +411,13 @@ def _do_link(self, from_info, to_info, link_method):
409411
if self.protected:
410412
self.protect(to_info)
411413

412-
msg = "Created {}'{}': {} -> {}".format(
414+
logger.debug(
415+
"Created {}'{}': {} -> {}",
413416
"protected " if self.protected else "",
414417
self.cache_types[0],
415418
from_info,
416419
to_info,
417420
)
418-
logger.debug(msg)
419421

420422
def _save_file(self, path_info, checksum, save_link=True):
421423
assert checksum
@@ -517,7 +519,7 @@ def save(self, path_info, checksum_info):
517519

518520
def _save(self, path_info, checksum):
519521
to_info = self.checksum_to_path_info(checksum)
520-
logger.debug("Saving '{}' to '{}'.".format(path_info, to_info))
522+
logger.debug("Saving '{}' to '{}'.", path_info, to_info)
521523
if self.isdir(path_info):
522524
self._save_dir(path_info, checksum)
523525
return
@@ -529,8 +531,9 @@ def _handle_transfer_exception(
529531
if isinstance(exception, OSError) and exception.errno == errno.EMFILE:
530532
raise exception
531533

532-
msg = "failed to {} '{}' to '{}'".format(operation, from_info, to_info)
533-
logger.exception(msg)
534+
logger.exception(
535+
"failed to {} '{}' to '{}'", operation, from_info, to_info
536+
)
534537
return 1
535538

536539
def upload(self, from_info, to_info, name=None, no_progress_bar=False):
@@ -543,7 +546,7 @@ def upload(self, from_info, to_info, name=None, no_progress_bar=False):
543546
if from_info.scheme != "local":
544547
raise NotImplementedError
545548

546-
logger.debug("Uploading '{}' to '{}'".format(from_info, to_info))
549+
logger.debug("Uploading '{}' to '{}'", from_info, to_info)
547550

548551
name = name or from_info.name
549552

@@ -622,7 +625,7 @@ def _download_file(
622625
):
623626
makedirs(to_info.parent, exist_ok=True, mode=dir_mode)
624627

625-
logger.debug("Downloading '{}' to '{}'".format(from_info, to_info))
628+
logger.debug("Downloading '{}' to '{}'", from_info, to_info)
626629
name = name or to_info.name
627630

628631
tmp_file = tmp_fname(to_info)
@@ -728,9 +731,10 @@ def changed_cache_file(self, checksum):
728731
actual = self.get_checksum(cache_info)
729732

730733
logger.debug(
731-
"cache '{}' expected '{}' actual '{}'".format(
732-
str(cache_info), checksum, actual
733-
)
734+
"cache '{}' expected '{}' actual '{}'",
735+
cache_info,
736+
checksum,
737+
actual,
734738
)
735739

736740
if not checksum or not actual:
@@ -740,7 +744,7 @@ def changed_cache_file(self, checksum):
740744
return False
741745

742746
if self.exists(cache_info):
743-
logger.warning("corrupted cache file '{}'.".format(cache_info))
747+
logger.warning("corrupted cache file '{}'.", cache_info)
744748
self.remove(cache_info)
745749

746750
return True
@@ -850,8 +854,9 @@ def _checkout_file(
850854
"""The file is changed we need to checkout a new copy"""
851855
cache_info = self.checksum_to_path_info(checksum)
852856
if self.exists(path_info):
853-
msg = "data '{}' exists. Removing before checkout."
854-
logger.warning(msg.format(str(path_info)))
857+
logger.warning(
858+
"data '{}' exists. Removing before checkout.", path_info
859+
)
855860
self.safe_remove(path_info, force=force)
856861

857862
self.link(cache_info, path_info)
@@ -882,7 +887,7 @@ def _checkout_dir(
882887

883888
dir_info = self.get_dir_cache(checksum)
884889

885-
logger.debug("Linking directory '{}'.".format(path_info))
890+
logger.debug("Linking directory '{}'.", path_info)
886891

887892
for entry in dir_info:
888893
relative_path = entry[self.PARAM_RELPATH]
@@ -933,22 +938,24 @@ def checkout(
933938
skip = False
934939
if not checksum:
935940
logger.warning(
936-
"No checksum info found for '{}'. "
937-
"It won't be created.".format(str(path_info))
941+
"No checksum info found for '{}'. " "It won't be created.",
942+
path_info,
938943
)
939944
self.safe_remove(path_info, force=force)
940945
failed = path_info
941946

942947
elif not relink and not self.changed(path_info, checksum_info):
943-
msg = "Data '{}' didn't change."
944-
logger.debug(msg.format(str(path_info)))
948+
logger.debug("Data '{}' didn't change.", path_info)
945949
skip = True
946950

947951
elif self.changed_cache(
948952
checksum, path_info=path_info, filter_info=filter_info
949953
):
950-
msg = "Cache '{}' not found. File '{}' won't be created."
951-
logger.warning(msg.format(checksum, str(path_info)))
954+
logger.warning(
955+
"Cache '{}' not found. File '{}' won't be created.",
956+
checksum,
957+
path_info,
958+
)
952959
self.safe_remove(path_info, force=force)
953960
failed = path_info
954961

@@ -962,8 +969,7 @@ def checkout(
962969
)
963970
return failed
964971

965-
msg = "Checking out '{}' with cache '{}'."
966-
logger.debug(msg.format(str(path_info), checksum))
972+
logger.debug("Checking out '{}' with cache '{}'.", path_info, checksum)
967973

968974
self._checkout(
969975
path_info, checksum, force, progress_callback, relink, filter_info

dvc/rwlock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from .exceptions import DvcException
1111
from .lock import LockError
12-
from .utils.fs import relpath
12+
from .utils import relpath
1313

1414
INFO_SCHEMA = {"pid": int, "cmd": str}
1515

dvc/state.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _execute(self, cmd, parameters=()):
141141

142142
def _fetchall(self):
143143
ret = self.cursor.fetchall()
144-
logger.debug("fetched: {}".format(ret))
144+
logger.debug("fetched: {}", ret)
145145
return ret
146146

147147
def _to_sqlite(self, num):
@@ -181,11 +181,12 @@ def _prepare_db(self, empty=False):
181181
__version__, self.VERSION, version
182182
)
183183
elif version < self.VERSION:
184-
msg = (
184+
logger.warning(
185185
"State file version '{}' is too old. "
186-
"Reformatting to the current version '{}'."
186+
"Reformatting to the current version '{}'.",
187+
version,
188+
self.VERSION,
187189
)
188-
logger.warning(msg.format(version, self.VERSION))
189190
cmd = "DROP TABLE IF EXISTS {};"
190191
self._execute(cmd.format(self.STATE_TABLE))
191192
self._execute(cmd.format(self.STATE_INFO_TABLE))
@@ -466,7 +467,7 @@ def remove_unused_links(self, used):
466467
actual_mtime, _ = get_mtime_and_size(path, self.repo.tree)
467468

468469
if inode == actual_inode and mtime == actual_mtime:
469-
logger.debug("Removing '{}' as unused link.".format(path))
470+
logger.debug("Removing '{}' as unused link.", path)
470471
remove(path)
471472
unused.append(relpath)
472473

dvc/utils/fs.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from dvc.utils import dict_md5
1414
from dvc.utils import fspath
1515
from dvc.utils import fspath_py35
16-
from dvc.utils import relpath
1716

1817

1918
logger = logging.getLogger(__name__)
@@ -30,7 +29,7 @@ def fs_copy(src, dst):
3029

3130
def get_inode(path):
3231
inode = System.inode(path)
33-
logger.debug("Path {} inode {}".format(path, inode))
32+
logger.debug("Path {} inode {}", path, inode)
3433
return inode
3534

3635

@@ -131,10 +130,9 @@ def _chmod(func, p, excinfo):
131130

132131

133132
def remove(path):
134-
path = fspath_py35(path)
135-
136-
logger.debug("Removing '{}'".format(relpath(path)))
133+
logger.debug("Removing '{}'", path)
137134

135+
path = fspath_py35(path)
138136
try:
139137
if os.path.isdir(path):
140138
shutil.rmtree(path, onerror=_chmod)

0 commit comments

Comments
 (0)