# Path: monty/shutil.py
# def copy_r(src, dst):
#     """
#     Implements a recursive copy function similar to Unix's "cp -r" command.
#     Surprisingly, python does not have a real equivalent. shutil.copytree
#     only works if the destination directory is not present.
# 
#     Args:
#         src (str): Source folder to copy.
#         dst (str): Destination folder.
#     """
# 
#     abssrc = os.path.abspath(src)
#     absdst = os.path.abspath(dst)
#     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):
#             shutil.copy(fpath, absdst)
#         elif not absdst.startswith(fpath):
#             copy_r(fpath, os.path.join(absdst, f))
#         else:
#             warnings.warn(f"Cannot copy {fpath} to itself")
# 
# def remove(path, follow_symlink=False):
#     """
#     Implements an remove function that will delete files, folder trees and
#     symlink trees
# 
#     1.) Remove a file
#     2.) Remove a symlink and follow into with a recursive rm if follow_symlink
#     3.) Remove directory with rmtree
# 
#     Args:
#         path (str): path to remove
#         follow_symlink(bool): follow symlinks and removes whatever is in them
#     """
#     if os.path.isfile(path):
#         os.remove(path)
#     elif os.path.islink(path):
#         if follow_symlink:
#             remove(os.readlink(path))
#         os.unlink(path)
#     else:
#         shutil.rmtree(path)


import os
import tempfile
from pathlib import Path
from monty.shutil import copy_r, remove
        self.start_copy = copy_from_current_on_enter
        self.end_copy = copy_to_current_on_exit

    def __enter__(self):
        tempdir = self.cwd
        if self.rootpath is not None and os.path.exists(self.rootpath):
            tempdir = tempfile.mkdtemp(dir=self.rootpath)
            self.tempdir = os.path.abspath(tempdir)
            if self.start_copy:
                copy_r(".", tempdir)
            if self.create_symbolic_link:
                os.symlink(tempdir, ScratchDir.SCR_LINK)
            os.chdir(tempdir)
        return tempdir

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.rootpath is not None and os.path.exists(self.rootpath):
            if self.end_copy:
                files = set(os.listdir(self.tempdir))
                orig_files = set(os.listdir(self.cwd))

                # First copy files over
                copy_r(self.tempdir, self.cwd)

                # Delete any files that are now gone
                for f in orig_files - files:
                    fpath = os.path.join(self.cwd, f)
                    remove(fpath)

            os.chdir(self.cwd)

# Path: monty/shutil.py
# def copy_r(src, dst):
#     """
#     Implements a recursive copy function similar to Unix's "cp -r" command.
#     Surprisingly, python does not have a real equivalent. shutil.copytree
#     only works if the destination directory is not present.
# 
#     Args:
#         src (str): Source folder to copy.
#         dst (str): Destination folder.
#     """
# 
#     abssrc = os.path.abspath(src)
#     absdst = os.path.abspath(dst)
#     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):
#             shutil.copy(fpath, absdst)
#         elif not absdst.startswith(fpath):
#             copy_r(fpath, os.path.join(absdst, f))
#         else:
#             warnings.warn(f"Cannot copy {fpath} to itself")
# 
# def remove(path, follow_symlink=False):
#     """
#     Implements an remove function that will delete files, folder trees and
#     symlink trees
# 
#     1.) Remove a file
#     2.) Remove a symlink and follow into with a recursive rm if follow_symlink
#     3.) Remove directory with rmtree
# 
#     Args:
#         path (str): path to remove
#         follow_symlink(bool): follow symlinks and removes whatever is in them
#     """
#     if os.path.isfile(path):
#         os.remove(path)
#     elif os.path.islink(path):
#         if follow_symlink:
#             remove(os.readlink(path))
#         os.unlink(path)
#     else:
#         shutil.rmtree(path)


# Path: monty/tempfile.py
import os
import tempfile
from pathlib import Path
from monty.shutil import copy_r, remove

        self.start_copy = copy_from_current_on_enter
        self.end_copy = copy_to_current_on_exit

    def __enter__(self):
        tempdir = self.cwd
        if self.rootpath is not None and os.path.exists(self.rootpath):
            tempdir = tempfile.mkdtemp(dir=self.rootpath)
            self.tempdir = os.path.abspath(tempdir)
            if self.start_copy:
                copy_r(".", tempdir)
            if self.create_symbolic_link:
                os.symlink(tempdir, ScratchDir.SCR_LINK)
            os.chdir(tempdir)
        return tempdir

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.rootpath is not None and os.path.exists(self.rootpath):
            if self.end_copy:
                files = set(os.listdir(self.tempdir))
                orig_files = set(os.listdir(self.cwd))

                # First copy files over
                copy_r(self.tempdir, self.cwd)

                # Delete any files that are now gone
                for f in orig_files - files:
                    fpath = os.path.join(self.cwd, f)
                    remove(fpath)

            os.chdir(self.cwd)

            remove(self.tempdir)