# Path: pybombs/pb_logging.py
# BOLD = str('\033[1m')
# TRACE = 1
# class ColoredConsoleHandler(logging.StreamHandler):
# class PBLogger(logging.getLoggerClass()):
#     def emit(self, record):
#     def __init__(self, *args, **kwargs):
#     def trace(self, *args, **kwargs):
# 
# Path: pybombs/utils/sysutils.py
# def which(program, env=None):
#     def is_exe(fpath):
# def dir_is_writable(dir_path):
# def mkdir_writable(dir_path, log=None):
# def mkdirp_writable(dir_path, log=None):
# def require_subdirs(base_path, subdirs, log=None):
# def write_file_in_subdir(base_path, file_path, content):
# def is_virtualenv(path):
# def get_interpreter_version():
# 
# Path: pybombs/utils/subproc.py
# READ_TIMEOUT = 0.1 # s
# def check_output(*args, **kwargs):
# def get_child_pids(pid):
# def kill_process_tree(process, pid=None):
# def run_with_output_processing(p, o_proc, event, cleanup=None):
#     def enqueue_output(stdout_data, stdout_queue, stderr_data, stderr_queue):
#     def poll_queue(q):
# def _process_thread(event, args, kwargs):
#     def elevate_command(args, elevate_pre_args):
#     def pretty_print_cmd(args):
# def monitor_process(args, **kwargs):
# def match_output(command, pattern, match_key=None, **kwargs):


import os
from six import iteritems
from pybombs import pb_logging
from pybombs.utils import sysutils
from pybombs.utils import subproc
# You should have received a copy of the GNU General Public License
# along with PyBOMBS; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
""" Git cache manager """

from __future__ import print_function

class GitCacheManager(object):
    " Git cache manager "
    def __init__(self, path):
        self.path = path
        self.log = pb_logging.logger.getChild("GitCacheManager")
        self.ensure_repo_exists(path)
        self.remotes = self.get_existing_remotes()

    def run_git_command(self, args):
        " Run a git command in path, return to previous cwd, return output "
        git_cmd = ['git'] + args
        cwd = os.getcwd()
        os.chdir(self.path)
        output = subproc.check_output(git_cmd)
        os.chdir(cwd)
        return output

    def ensure_repo_exists(self, path):
        " Guarantee that path is a writable git repo. "
        if not sysutils.dir_is_writable(path):
            self.log.info("Creating new git cache in {path}".format(path=path))

# Path: pybombs/pb_logging.py
# BOLD = str('\033[1m')
# TRACE = 1
# class ColoredConsoleHandler(logging.StreamHandler):
# class PBLogger(logging.getLoggerClass()):
#     def emit(self, record):
#     def __init__(self, *args, **kwargs):
#     def trace(self, *args, **kwargs):
# 
# Path: pybombs/utils/sysutils.py
# def which(program, env=None):
#     def is_exe(fpath):
# def dir_is_writable(dir_path):
# def mkdir_writable(dir_path, log=None):
# def mkdirp_writable(dir_path, log=None):
# def require_subdirs(base_path, subdirs, log=None):
# def write_file_in_subdir(base_path, file_path, content):
# def is_virtualenv(path):
# def get_interpreter_version():
# 
# Path: pybombs/utils/subproc.py
# READ_TIMEOUT = 0.1 # s
# def check_output(*args, **kwargs):
# def get_child_pids(pid):
# def kill_process_tree(process, pid=None):
# def run_with_output_processing(p, o_proc, event, cleanup=None):
#     def enqueue_output(stdout_data, stdout_queue, stderr_data, stderr_queue):
#     def poll_queue(q):
# def _process_thread(event, args, kwargs):
#     def elevate_command(args, elevate_pre_args):
#     def pretty_print_cmd(args):
# def monitor_process(args, **kwargs):
# def match_output(command, pattern, match_key=None, **kwargs):


# Path: pybombs/gitcache_manager.py
import os
from six import iteritems
from pybombs import pb_logging
from pybombs.utils import sysutils
from pybombs.utils import subproc

# You should have received a copy of the GNU General Public License
# along with PyBOMBS; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
""" Git cache manager """

from __future__ import print_function

class GitCacheManager(object):
    " Git cache manager "
    def __init__(self, path):
        self.path = path
        self.log = pb_logging.logger.getChild("GitCacheManager")
        self.ensure_repo_exists(path)
        self.remotes = self.get_existing_remotes()

    def run_git_command(self, args):
        " Run a git command in path, return to previous cwd, return output "
        git_cmd = ['git'] + args
        cwd = os.getcwd()
        os.chdir(self.path)
        output = subproc.check_output(git_cmd)
        os.chdir(cwd)
        return output

    def ensure_repo_exists(self, path):
        " Guarantee that path is a writable git repo. "
        if not sysutils.dir_is_writable(path):
            self.log.info("Creating new git cache in {path}".format(path=path))

            sysutils.mkdir_writable(path)