"""
Temporary directory and file creation utilities.
"""

import os
import tempfile
from pathlib import Path

from monty.shutil import copy_r, gzip_dir, remove


class ScratchDir:
    """
    .. note::

        With effect from Python 3.2, tempfile.TemporaryDirectory already
        implements much of the functionality of ScratchDir. However, it does
        not provide options for copying of files to and from (though it is
        possible to do this with other methods provided by shutil).

    Creates a "with" context manager that automatically handles creation of
    temporary directories (utilizing Python's build in temp directory
    functions) and cleanup when done. This improves on Python's built in
    functions by allowing for truly temporary workspace that are deleted
    when it is done. The way it works is as follows:

    1. Create a temp dir in specified root path.
    2. Optionally copy input files from current directory to temp dir.
    3. Change to temp dir.
    4. User performs specified operations.
    5. Optionally copy generated output files back to original directory.
    6. Change back to original directory.
    7. Delete temp dir.
    """

    SCR_LINK = "scratch_link"

    def __init__(
        self,
        rootpath,
        create_symbolic_link=False,
        copy_from_current_on_enter=False,
        copy_to_current_on_exit=False,
        gzip_on_exit=False,
        storedir=None,
    ):
        """
        Initializes scratch directory given a **root** path. There is no need
        to try to create unique directory names. The code will generate a
        temporary sub directory in the rootpath. The way to use this is using a
        with context manager. Example::

            with ScratchDir("/scratch"):
                do_something()

        If the root path does not exist or is None, this will function as a
        simple pass through, i.e., nothing happens.

        Args:
            rootpath (str/Path): Path in which to create temp subdirectories.
                If this is None, no temp directories will be created and
                this will just be a simple pass through.
            create_symbolic_link (bool): Whether to create a symbolic link in
                the current working directory to the scratch directory
                created.
            copy_from_current_on_enter (bool): Whether to copy all files from
                the current directory (recursively) to the temp dir at the
                start, e.g., if input files are needed for performing some
                actions. Defaults to False.
            copy_to_current_on_exit (bool): Whether to copy files from the
                scratch to the current directory (recursively) at the end. E
                .g., if output files are generated during the operation.
                Defaults to False.
            gzip_on_exist (bool): Whether to gzip the files generated in the
                ScratchDir before copying them back.
            storedir (str/Path): Path to directory to copy files from/to.
                Defaults to os.getcwd().
        """
        if Path is not None and isinstance(rootpath, Path):
            rootpath = str(rootpath)

        self.rootpath = os.path.abspath(rootpath) if rootpath is not None else None
        self.cwd = os.getcwd()
        self.create_symbolic_link = create_symbolic_link
        self.start_copy = copy_from_current_on_enter
        self.end_copy = copy_to_current_on_exit
        self.gzip_on_exist = gzip_on_exit
        self.storedir = storedir or os.getcwd()

    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(self.storedir, 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:

                # gzip files
                if self.gzip_on_exist:
                    gzip_dir(self.tempdir)

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

            os.chdir(self.cwd)
            remove(self.tempdir)

@@ -1424,16 +1424,51 @@
 r=None,%0A
+        delete_removed_files=True,%0A
     ):%0A 
@@ -2904,17 +2904,16 @@
 p_on_exi
-s
 t (bool)
@@ -3010,16 +3010,51 @@
 m back.%0A
+                Defaults to False.%0A
         
@@ -3160,16 +3160,176 @@
 tcwd().%0A
+            delete_removed_files (bool): Whether to delete files in the cwd%0A                that are removed from the tmp dir.%0A                Defaults to True%0A
         
@@ -3727,17 +3727,16 @@
 p_on_exi
-s
 t = gzip
@@ -3791,16 +3791,73 @@
 getcwd()
+%0A        self.delete_removed_files = delete_removed_files
 %0A%0A    de
@@ -4468,16 +4468,125 @@
 nd_copy:
+%0A                files = set(os.listdir(self.tempdir))%0A                orig_files = set(os.listdir(self.cwd))
 %0A%0A      
@@ -4643,17 +4643,16 @@
 p_on_exi
-s
 t:%0A     
@@ -4711,14 +4711,8 @@
    #
- First
  cop
@@ -4766,24 +4766,264 @@
 r, self.
-storedir
+cwd)%0A%0A                # Delete any files that are now gone%0A                if self.delete_removed_files:%0A                    for f in orig_files - files:%0A                        fpath = os.path.join(self.cwd, f)%0A                        remove(fpath
 )%0A%0A     

Add option to not delete files with tempfile