Skip to content

Commit af21340

Browse files
Merge pull request #332 from choosehappy/bug/185-bug-shutil-move-does-not-sync-correctly-with-docker-bind-mount
[#185] BUG: shutil move does not sync correctly with docker bind mount
2 parents f2137e9 + eca690e commit af21340

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

‎histoqc/_pipeline.py‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,28 @@ def setup_logging(*, capture_warnings, filter_warnings):
6767
logging.captureWarnings(capture_warnings)
6868

6969

70+
7071
def move_logging_file_handler(logger, destination, debug=False):
71-
"""point the logging file handlers to the new destination
72+
"""
73+
Redirect FileHandlers to a new destination directory, moving the log file.
7274
7375
Parameters
7476
----------
75-
logger :
76-
the Logger instance for which the default file handler should be moved
77-
destination :
78-
destination directory for the new file handler
77+
logger : logging.Logger
78+
Logger instance whose FileHandlers will be moved.
79+
destination : str
80+
Directory path for the new log files.
81+
debug : bool
82+
If True, set the log level of the new handlers to DEBUG. If False, the new handlers retain the original handler's log level.
7983
"""
8084
for handler in reversed(logger.handlers):
8185
if not isinstance(handler, logging.FileHandler):
8286
continue
8387
if handler.baseFilename != os.path.join(os.getcwd(), DEFAULT_LOG_FN):
8488
continue
89+
if not os.path.exists(handler.baseFilename):
90+
logger.warning(f"Original log file {handler.baseFilename!r} does not exist, cannot move.")
91+
continue
8592

8693
if not destination.endswith(handler.baseFilename):
8794
destination = os.path.join(destination, os.path.relpath(handler.baseFilename, os.getcwd()))
@@ -90,9 +97,15 @@ def move_logging_file_handler(logger, destination, debug=False):
9097
# remove handler
9198
logger.removeHandler(handler)
9299
handler.close()
93-
# copy error log to destination
94-
new_filename = shutil.move(handler.baseFilename, destination)
95100

101+
new_filename = shutil.copy2(handler.baseFilename, destination)
102+
try:
103+
os.remove(handler.baseFilename)
104+
except OSError:
105+
# on WSL/Windows, file might still be locked briefly
106+
logger.warning(f"Could not remove original log file {handler.baseFilename!r}")
107+
108+
# create and attach new handler
96109
new_handler = logging.FileHandler(new_filename, mode='a')
97110
new_handler.setLevel(logging.DEBUG if debug else handler.level)
98111
new_handler.setFormatter(handler.formatter)

0 commit comments

Comments
 (0)