# Path: rebasehelper/logger.py
# class CustomLogger(logging.Logger):
# 
#     TRACE: int = logging.DEBUG + 1
#     VERBOSE: int = logging.DEBUG + 2
#     SUCCESS: int = logging.INFO + 5
#     HEADING: int = logging.INFO + 6
#     IMPORTANT: int = logging.INFO + 7
# 
#     _nameToLevel: Dict[str, int] = {
#         'TRACE': TRACE,
#         'VERBOSE': VERBOSE,
#         'SUCCESS': SUCCESS,
#         'HEADING': HEADING,
#         'IMPORTANT': IMPORTANT,
#     }
# 
#     def __init__(self, name, level=logging.NOTSET):
#         super().__init__(name, level)
# 
#         for lev, severity in self._nameToLevel.items():
#             logging.addLevelName(severity, lev)
# 
#     def __getattr__(self, level):
#         severity = self._nameToLevel.get(level.upper())
# 
#         def log(message, *args, **kwargs):
#             if self.isEnabledFor(severity):
#                 self._log(severity, message, args, **kwargs)
# 
#         if severity:
#             return log
# 
#         raise AttributeError
# 
# Path: rebasehelper/helpers/path_helper.py
# class PathHelper:
# 
#     """Class for performing path related tasks."""
# 
#     @staticmethod
#     def find_first_dir_with_file(top_path, pattern):
#         """Recursively searches for a directory containing a file that matches the given pattern.
# 
#         Args:
#             top_path (str): Directory where to start the search.
#             pattern (str): Filename pattern.
# 
#         Returns:
#             str: Full path to the directory containing the first occurence of the searched file.
#             None if there is no file matching the pattern.
# 
#         """
#         for root, dirs, files in os.walk(top_path):
#             dirs.sort()
#             for f in files:
#                 if fnmatch.fnmatch(f, pattern):
#                     return os.path.abspath(root)
#         return None
# 
#     @staticmethod
#     def find_first_file(top_path, pattern, recursion_level=None):
#         """Recursively searches for a file that matches the given pattern.
# 
#         Args:
#             top_path (str): Directory where to start the search.
#             pattern (str): Filename pattern.
#             recursion_level (int): How deep in the directory tree the search can go.
# 
#         Returns:
#             str: Path to the file matching the pattern or None if there is no file matching the pattern.
# 
#         """
#         for loop, (root, dirs, files) in enumerate(os.walk(top_path)):
#             dirs.sort()
#             for f in files:
#                 if fnmatch.fnmatch(f, pattern):
#                     return os.path.join(os.path.abspath(root), f)
#             if recursion_level is not None:
#                 if loop == recursion_level:
#                     break
#         return None
# 
#     @staticmethod
#     def find_all_files(top_path, pattern):
#         """Recursively searches for all files matching the given pattern.
# 
#         Args:
#             top_path (str): Directory where to start the search.
#             pattern (str): Filename pattern.
# 
#         Returns:
#             list: List containing absolute paths to all found files.
# 
#         """
#         files_list = []
#         for root, dirs, files in os.walk(top_path):
#             dirs.sort()
#             for f in files:
#                 if fnmatch.fnmatch(f, pattern):
#                     files_list.append(os.path.join(os.path.abspath(root), f))
#         return files_list
# 
#     @staticmethod
#     def find_all_files_current_dir(top_path, pattern):
#         """Searches for all files that match the given pattern inside a directory.
# 
#         Args:
#             top_path (str): Directory where to start the search.
#             pattern (str): Filename pattern.
# 
#         Returns:
#             list: List containing absolute paths to all found files.
# 
#         """
#         files_list = []
#         for files in os.listdir(top_path):
#             if fnmatch.fnmatch(files, pattern):
#                 files_list.append(os.path.join(os.path.abspath(top_path), files))
#         return files_list
# 
#     @staticmethod
#     def get_temp_dir():
#         """Creates a new temporary directory.
# 
#         Return:
#             str: Path to the created directory.
# 
#         """
#         return tempfile.mkdtemp(prefix='rebase-helper-')
# 
#     @staticmethod
#     def file_available(filename):
#         """Checks if the given file exists.
# 
#         Args:
#             filename (str): Path to the file.
# 
#         Returns:
#             bool: Whether the file exists.
# 
#         """
#         if os.path.exists(filename) and os.path.getsize(filename) != 0:
#             return True
#         else:
#             return False


import logging
import shutil
from typing import Dict, cast
from rebasehelper.logger import CustomLogger
from rebasehelper.helpers.path_helper import PathHelper
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Authors: Petr Hráček <phracek@redhat.com>
#          Tomáš Hozza <thozza@redhat.com>
#          Nikola Forró <nforro@redhat.com>
#          František Nečas <fifinecas@seznam.cz>




logger: CustomLogger = cast(CustomLogger, logging.getLogger(__name__))


class TemporaryEnvironment:

    """Class representing a temporary environment (directory) that can be used as a workspace.

    Works as a context manager.

    """

    TEMPDIR: str = 'TEMPDIR'

    def __init__(self, exit_callback=None):
        self._env: Dict[str, str] = {}
        self._exit_callback = exit_callback

    def __enter__(self):

# Path: rebasehelper/logger.py
# class CustomLogger(logging.Logger):
# 
#     TRACE: int = logging.DEBUG + 1
#     VERBOSE: int = logging.DEBUG + 2
#     SUCCESS: int = logging.INFO + 5
#     HEADING: int = logging.INFO + 6
#     IMPORTANT: int = logging.INFO + 7
# 
#     _nameToLevel: Dict[str, int] = {
#         'TRACE': TRACE,
#         'VERBOSE': VERBOSE,
#         'SUCCESS': SUCCESS,
#         'HEADING': HEADING,
#         'IMPORTANT': IMPORTANT,
#     }
# 
#     def __init__(self, name, level=logging.NOTSET):
#         super().__init__(name, level)
# 
#         for lev, severity in self._nameToLevel.items():
#             logging.addLevelName(severity, lev)
# 
#     def __getattr__(self, level):
#         severity = self._nameToLevel.get(level.upper())
# 
#         def log(message, *args, **kwargs):
#             if self.isEnabledFor(severity):
#                 self._log(severity, message, args, **kwargs)
# 
#         if severity:
#             return log
# 
#         raise AttributeError
# 
# Path: rebasehelper/helpers/path_helper.py
# class PathHelper:
# 
#     """Class for performing path related tasks."""
# 
#     @staticmethod
#     def find_first_dir_with_file(top_path, pattern):
#         """Recursively searches for a directory containing a file that matches the given pattern.
# 
#         Args:
#             top_path (str): Directory where to start the search.
#             pattern (str): Filename pattern.
# 
#         Returns:
#             str: Full path to the directory containing the first occurence of the searched file.
#             None if there is no file matching the pattern.
# 
#         """
#         for root, dirs, files in os.walk(top_path):
#             dirs.sort()
#             for f in files:
#                 if fnmatch.fnmatch(f, pattern):
#                     return os.path.abspath(root)
#         return None
# 
#     @staticmethod
#     def find_first_file(top_path, pattern, recursion_level=None):
#         """Recursively searches for a file that matches the given pattern.
# 
#         Args:
#             top_path (str): Directory where to start the search.
#             pattern (str): Filename pattern.
#             recursion_level (int): How deep in the directory tree the search can go.
# 
#         Returns:
#             str: Path to the file matching the pattern or None if there is no file matching the pattern.
# 
#         """
#         for loop, (root, dirs, files) in enumerate(os.walk(top_path)):
#             dirs.sort()
#             for f in files:
#                 if fnmatch.fnmatch(f, pattern):
#                     return os.path.join(os.path.abspath(root), f)
#             if recursion_level is not None:
#                 if loop == recursion_level:
#                     break
#         return None
# 
#     @staticmethod
#     def find_all_files(top_path, pattern):
#         """Recursively searches for all files matching the given pattern.
# 
#         Args:
#             top_path (str): Directory where to start the search.
#             pattern (str): Filename pattern.
# 
#         Returns:
#             list: List containing absolute paths to all found files.
# 
#         """
#         files_list = []
#         for root, dirs, files in os.walk(top_path):
#             dirs.sort()
#             for f in files:
#                 if fnmatch.fnmatch(f, pattern):
#                     files_list.append(os.path.join(os.path.abspath(root), f))
#         return files_list
# 
#     @staticmethod
#     def find_all_files_current_dir(top_path, pattern):
#         """Searches for all files that match the given pattern inside a directory.
# 
#         Args:
#             top_path (str): Directory where to start the search.
#             pattern (str): Filename pattern.
# 
#         Returns:
#             list: List containing absolute paths to all found files.
# 
#         """
#         files_list = []
#         for files in os.listdir(top_path):
#             if fnmatch.fnmatch(files, pattern):
#                 files_list.append(os.path.join(os.path.abspath(top_path), files))
#         return files_list
# 
#     @staticmethod
#     def get_temp_dir():
#         """Creates a new temporary directory.
# 
#         Return:
#             str: Path to the created directory.
# 
#         """
#         return tempfile.mkdtemp(prefix='rebase-helper-')
# 
#     @staticmethod
#     def file_available(filename):
#         """Checks if the given file exists.
# 
#         Args:
#             filename (str): Path to the file.
# 
#         Returns:
#             bool: Whether the file exists.
# 
#         """
#         if os.path.exists(filename) and os.path.getsize(filename) != 0:
#             return True
#         else:
#             return False


# Path: rebasehelper/temporary_environment.py
import logging
import shutil
from typing import Dict, cast
from rebasehelper.logger import CustomLogger
from rebasehelper.helpers.path_helper import PathHelper

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Authors: Petr Hráček <phracek@redhat.com>
#          Tomáš Hozza <thozza@redhat.com>
#          Nikola Forró <nforro@redhat.com>
#          František Nečas <fifinecas@seznam.cz>




logger: CustomLogger = cast(CustomLogger, logging.getLogger(__name__))


class TemporaryEnvironment:

    """Class representing a temporary environment (directory) that can be used as a workspace.

    Works as a context manager.

    """

    TEMPDIR: str = 'TEMPDIR'

    def __init__(self, exit_callback=None):
        self._env: Dict[str, str] = {}
        self._exit_callback = exit_callback

    def __enter__(self):

        self._env[self.TEMPDIR] = PathHelper.get_temp_dir()