Skip to content

Commit 5db7a4c

Browse files
authored
tree: local: remove misc redundant logic (#5139)
Non-lazy logging, fspath, redundant exist check, etc.
1 parent dcea83e commit 5db7a4c

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

dvc/tree/local.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,10 @@ def walk_files(self, path_info, **kwargs):
128128
yield PathInfo(f"{root}{os.sep}{file}")
129129

130130
def is_empty(self, path_info):
131-
path = path_info.fspath
132-
133-
if self.isfile(path_info) and os.path.getsize(path) == 0:
131+
if self.isfile(path_info) and os.path.getsize(path_info) == 0:
134132
return True
135133

136-
if self.isdir(path_info) and len(os.listdir(path)) == 0:
134+
if self.isdir(path_info) and len(os.listdir(path_info)) == 0:
137135
return True
138136

139137
return False
@@ -142,12 +140,7 @@ def remove(self, path_info):
142140
if isinstance(path_info, PathInfo):
143141
if path_info.scheme != "local":
144142
raise NotImplementedError
145-
path = path_info.fspath
146-
else:
147-
path = path_info
148-
149-
if self.exists(path):
150-
remove(path)
143+
remove(path_info)
151144

152145
def makedirs(self, path_info):
153146
makedirs(path_info, exist_ok=True, mode=self.dir_mode)
@@ -226,11 +219,7 @@ def hardlink(self, from_info, to_info):
226219
if self.getsize(from_info) == 0:
227220
self.open(to_info, "w").close()
228221

229-
logger.debug(
230-
"Created empty file: {src} -> {dest}".format(
231-
src=str(from_info), dest=str(to_info)
232-
)
233-
)
222+
logger.debug("Created empty file: %s -> %s", from_info, to_info)
234223
return
235224

236225
System.hardlink(from_info, to_info)
@@ -248,9 +237,8 @@ def reflink(self, from_info, to_info):
248237
os.rename(tmp_info, to_info)
249238

250239
def chmod(self, path_info, mode):
251-
path = os.fspath(path_info)
252240
try:
253-
os.chmod(path, mode)
241+
os.chmod(path_info, mode)
254242
except OSError as exc:
255243
# There is nothing we need to do in case of a read-only file system
256244
if exc.errno == errno.EROFS:
@@ -261,13 +249,13 @@ def chmod(self, path_info, mode):
261249
if exc.errno not in [errno.EPERM, errno.EACCES]:
262250
raise
263251

264-
actual = stat.S_IMODE(os.stat(path).st_mode)
252+
actual = stat.S_IMODE(os.stat(path_info).st_mode)
265253
if actual != mode:
266254
raise
267255

268256
def _unprotect_file(self, path):
269257
if System.is_symlink(path) or System.is_hardlink(path):
270-
logger.debug(f"Unprotecting '{path}'")
258+
logger.debug("Unprotecting '%s'", path)
271259
tmp = os.path.join(os.path.dirname(path), "." + uuid())
272260

273261
# The operations order is important here - if some application
@@ -281,8 +269,9 @@ def _unprotect_file(self, path):
281269

282270
else:
283271
logger.debug(
284-
"Skipping copying for '{}', since it is not "
285-
"a symlink or a hardlink.".format(path)
272+
"Skipping copying for '%s', since it is not "
273+
"a symlink or a hardlink.",
274+
path,
286275
)
287276

288277
os.chmod(path, self.file_mode)
@@ -292,14 +281,15 @@ def _unprotect_dir(self, path):
292281
self._unprotect_file(fname)
293282

294283
def unprotect(self, path_info):
295-
path = path_info.fspath
296-
if not os.path.exists(path):
297-
raise DvcException(f"can't unprotect non-existing data '{path}'")
284+
if not os.path.exists(path_info):
285+
raise DvcException(
286+
f"can't unprotect non-existing data '{path_info}'"
287+
)
298288

299-
if os.path.isdir(path):
300-
self._unprotect_dir(path)
289+
if os.path.isdir(path_info):
290+
self._unprotect_dir(path_info)
301291
else:
302-
self._unprotect_file(path)
292+
self._unprotect_file(path_info)
303293

304294
def protect(self, path_info):
305295
self.chmod(path_info, self.CACHE_MODE)

0 commit comments

Comments
 (0)