Skip to content

Commit f642c56

Browse files
committed
Refactor: Migrate os.path to pathlib.Path in file_processor_handler.py (#68757)
1 parent a1daaf1 commit f642c56

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

airflow-core/src/airflow/utils/log/file_processor_handler.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,22 @@ def _symlink_latest_log_directory(self):
115115
116116
:return: None
117117
"""
118-
log_directory = self._get_log_directory()
119-
latest_log_directory_path = os.path.join(self.base_log_folder, "latest")
120-
if os.path.isdir(log_directory):
121-
rel_link_target = Path(log_directory).relative_to(Path(latest_log_directory_path).parent)
118+
log_directory = Path(self._get_log_directory())
119+
latest_log_directory_path = Path(self.base_log_folder) / "latest"
120+
if log_directory.is_dir():
121+
rel_link_target = log_directory.relative_to(latest_log_directory_path.parent)
122122
try:
123123
# if symlink exists but is stale, update it
124-
if os.path.islink(latest_log_directory_path):
125-
if os.path.realpath(latest_log_directory_path) != log_directory:
126-
os.unlink(latest_log_directory_path)
127-
os.symlink(rel_link_target, latest_log_directory_path)
128-
elif os.path.isdir(latest_log_directory_path) or os.path.isfile(latest_log_directory_path):
124+
if latest_log_directory_path.is_symlink():
125+
if latest_log_directory_path.resolve() != log_directory.resolve():
126+
latest_log_directory_path.unlink()
127+
latest_log_directory_path.symlink_to(rel_link_target)
128+
elif latest_log_directory_path.is_dir() or latest_log_directory_path.is_file():
129129
logger.warning(
130130
"%s already exists as a dir/file. Skip creating symlink.", latest_log_directory_path
131131
)
132132
else:
133-
os.symlink(rel_link_target, latest_log_directory_path)
133+
latest_log_directory_path.symlink_to(rel_link_target)
134134
except OSError:
135135
logger.warning("OSError while attempting to symlink the latest log directory")
136136

@@ -141,13 +141,13 @@ def _init_file(self, filename):
141141
:param filename: task instance object
142142
:return: relative log path of the given task instance
143143
"""
144-
relative_log_file_path = os.path.join(self._get_log_directory(), self._render_filename(filename))
145-
log_file_path = os.path.abspath(relative_log_file_path)
146-
directory = os.path.dirname(log_file_path)
144+
relative_log_file_path = Path(self._get_log_directory()) / self._render_filename(filename)
145+
log_file_path = relative_log_file_path.resolve()
146+
directory = log_file_path.parent
147147

148-
Path(directory).mkdir(parents=True, exist_ok=True)
148+
directory.mkdir(parents=True, exist_ok=True)
149149

150-
if not os.path.exists(log_file_path):
151-
open(log_file_path, "a").close()
150+
if not log_file_path.exists():
151+
log_file_path.touch()
152152

153-
return log_file_path
153+
return str(log_file_path)

0 commit comments

Comments
 (0)