Skip to content

Commit

Permalink
Use Path operations in shutil.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-S-Rosen committed Sep 4, 2023
1 parent 98e88c0 commit ff9c3b7
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions monty/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .io import zopen


def copy_r(src: str | Path, dst: str | Path):
def copy_r(src: str | Path, dst: str | Path) -> None:
"""
Implements a recursive copy function similar to Unix's "cp -r" command.
Surprisingly, python does not have a real equivalent. shutil.copytree
Expand All @@ -20,21 +20,21 @@ def copy_r(src: str | Path, dst: str | Path):
src (str | Path): Source folder to copy.
dst (str | Path): Destination folder.
"""
src = str(src)
dst = str(dst)
abssrc = os.path.abspath(src)
absdst = os.path.abspath(dst)
src = Path(src)
dst = Path(dst)
abssrc = src.resolve()
absdst = dst.resolve()
try:
os.makedirs(absdst)
except OSError:
# If absdst exists, an OSError is raised. We ignore this error.
pass
for f in os.listdir(abssrc):
fpath = os.path.join(abssrc, f)
if os.path.isfile(fpath):
fpath = Path(abssrc, f)
if Path(fpath).is_file():
shutil.copy(fpath, absdst)
elif not absdst.startswith(fpath):
copy_r(fpath, os.path.join(absdst, f))
copy_r(fpath, Path(absdst, f))
else:
warnings.warn(f"Cannot copy {fpath} to itself")

Expand All @@ -51,7 +51,7 @@ def gzip_dir(path: str | Path, compresslevel=6):
compresslevel (int): Level of compression, 1-9. 9 is default for
GzipFile, 6 is default for gzip.
"""
path = str(path)
path = Path(path)
for root, _, files in os.walk(path):
for f in files:
full_f = os.path.abspath(os.path.join(root, f))
Expand All @@ -73,7 +73,7 @@ def compress_file(filepath: str | Path, compression="gz"):
compression (str): A compression mode. Valid options are "gz" or
"bz2". Defaults to "gz".
"""
filepath = str(filepath)
filepath = Path(filepath)
if compression not in ["gz", "bz2"]:
raise ValueError("Supported compression formats are 'gz' and 'bz2'.")
if not filepath.lower().endswith(f".{compression}"):
Expand All @@ -93,7 +93,7 @@ def compress_dir(path: str | Path, compression="gz"):
compression (str): A compression mode. Valid options are "gz" or
"bz2". Defaults to gz.
"""
path = str(path)
path = Path(path)
for parent, _, files in os.walk(path):
for f in files:
compress_file(os.path.join(parent, f), compression=compression)
Expand All @@ -110,10 +110,10 @@ def decompress_file(filepath: str | Path) -> str | None:
Returns:
str: The decompressed file path.
"""
filepath = str(filepath)
toks = filepath.split(".")
filepath = Path(filepath)
toks = str(filepath).split(".")
file_ext = toks[-1].upper()
if file_ext in ["BZ2", "GZ", "Z"] and os.path.isfile(filepath):
if file_ext in ["BZ2", "GZ", "Z"] and filepath.is_file():
decompressed_file = ".".join(toks[0:-1])
with zopen(filepath, "rb") as f_in, open(decompressed_file, "wb") as f_out:
f_out.writelines(f_in)
Expand All @@ -130,10 +130,10 @@ def decompress_dir(path: str | Path):
Args:
path (str | Path): Path to parent directory.
"""
path = str(path)
path = Path(path)
for parent, _, files in os.walk(path):
for f in files:
decompress_file(os.path.join(parent, f))
decompress_file(Path(parent, f))


def remove(path: str | Path, follow_symlink=False):
Expand All @@ -149,10 +149,10 @@ def remove(path: str | Path, follow_symlink=False):
path (str | Path): path to remove
follow_symlink(bool): follow symlinks and removes whatever is in them
"""
path = str(path)
if os.path.isfile(path):
path = Path(path)
if path.is_file():
os.remove(path)
elif os.path.islink(path):
elif path.is_symlink():
if follow_symlink:
remove(os.readlink(path))
os.unlink(path)
Expand Down

0 comments on commit ff9c3b7

Please sign in to comment.