Skip to content

Commit e8829c6

Browse files
committed
Fix local target permission handling.
1 parent feef358 commit e8829c6

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

law/target/file.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,15 @@ def chmod(self, perm, *, silent: bool = False, **kwargs) -> bool:
324324
return self.fs.chmod(self.path, perm, silent=silent, **kwargs)
325325

326326
def makedirs(self, *args, **kwargs) -> None:
327+
# overwrites luigi's makedirs method
327328
parent = self.parent
328329
if parent:
329330
parent.touch(*args, **kwargs)
330331

332+
def _prepare_dir(self, **kwargs) -> None:
333+
dir_target = self if isinstance(self, self.directory_class) else self.parent
334+
dir_target.touch(**kwargs) # type: ignore[union-attr]
335+
331336
@abstractproperty
332337
def fs(self) -> FileSystem:
333338
...
@@ -478,8 +483,11 @@ def copy_to(
478483
dir_perm: int | None = None,
479484
**kwargs,
480485
) -> str:
486+
if isinstance(dst, FileSystemTarget):
487+
dst._prepare_dir(perm=dir_perm, **kwargs)
488+
481489
# TODO: complain when dst not local? forward to copy_from request depending on protocol?
482-
return self.fs.copy(self.path, get_path(dst), perm=perm, dir_perm=dir_perm, **kwargs)
490+
return self.fs.copy(self.path, get_path(dst), perm=perm, **kwargs)
483491

484492
def copy_from(
485493
self,
@@ -489,12 +497,14 @@ def copy_from(
489497
dir_perm: int | None = None,
490498
**kwargs,
491499
) -> str:
500+
self._prepare_dir(perm=dir_perm, **kwargs)
501+
492502
if isinstance(src, FileSystemFileTarget):
493-
return src.copy_to(self.abspath, perm=perm, dir_perm=dir_perm, **kwargs)
503+
return src.copy_to(self.abspath, perm=perm or self.fs.default_file_perm, **kwargs)
494504

495-
# when src is a plain string, let the fs handle it
496505
# TODO: complain when src not local? forward to copy_to request depending on protocol?
497-
return self.fs.copy(get_path(src), self.path, perm=perm, dir_perm=dir_perm, **kwargs)
506+
# when src is a plain string, let the fs handle it
507+
return self.fs.copy(get_path(src), self.path, perm=perm, **kwargs)
498508

499509
def move_to(
500510
self,
@@ -504,8 +514,11 @@ def move_to(
504514
dir_perm: int | None = None,
505515
**kwargs,
506516
) -> str:
517+
if isinstance(dst, FileSystemTarget):
518+
dst._prepare_dir(perm=dir_perm, **kwargs)
519+
507520
# TODO: complain when dst not local? forward to copy_from request depending on protocol?
508-
return self.fs.move(self.path, get_path(dst), perm=perm, dir_perm=dir_perm, **kwargs)
521+
return self.fs.move(self.path, get_path(dst), perm=perm, **kwargs)
509522

510523
def move_from(
511524
self,
@@ -515,12 +528,14 @@ def move_from(
515528
dir_perm: int | None = None,
516529
**kwargs,
517530
) -> str:
531+
self._prepare_dir(perm=dir_perm, **kwargs)
532+
518533
if isinstance(src, FileSystemFileTarget):
519-
return src.move_to(self.abspath, perm=perm, dir_perm=dir_perm, **kwargs)
534+
return src.move_to(self.abspath, perm=perm or self.fs.default_file_perm, **kwargs)
520535

521536
# when src is a plain string, let the fs handle it
522537
# TODO: complain when src not local? forward to copy_to request depending on protocol?
523-
return self.fs.move(get_path(src), self.path, perm=perm, dir_perm=dir_perm, **kwargs)
538+
return self.fs.move(get_path(src), self.path, perm=perm, **kwargs)
524539

525540

526541
class FileSystemDirectoryTarget(FileSystemTarget):

law/target/local.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def mkdir( # type: ignore[override]
200200

201201
# the mode passed to os.mkdir or os.makedirs is ignored on some systems, so the strategy
202202
# here is to disable the process' current umask, create the directories and use chmod again
203-
orig = os.umask(0) if perm is not None else None
203+
orig = os.umask(0o0770 - perm) if perm is not None else None
204204
func = os.makedirs if recursive else os.mkdir
205205
try:
206206
try:
@@ -578,7 +578,7 @@ def localize(
578578

579579
# move back again
580580
if tmp.exists():
581-
tmp.copy_to_local(self, perm=perm, dir_perm=dir_perm)
581+
self.copy_from_local(tmp, perm=perm, dir_perm=dir_perm)
582582
else:
583583
logger.warning(
584584
"cannot move non-existing localized target to actual representation "
@@ -663,7 +663,7 @@ def localize(
663663
# TODO: keep track of changed contents in "a" mode and copy only those?
664664
if tmp.exists():
665665
self.remove()
666-
tmp.copy_to_local(self, perm=perm, dir_perm=dir_perm)
666+
self.copy_from_local(tmp, perm=perm, dir_perm=dir_perm)
667667
else:
668668
logger.warning(
669669
"cannot move non-existing localized target to actual representation "

0 commit comments

Comments
 (0)