# Path: g_sorcery/compatibility.py
# class TemporaryDirectory(object):
#     def __init__(self):
#         self.name = mkdtemp()
# 
#     def __del__(self):
#         shutil.rmtree(self.name)
# 
# Path: g_sorcery/exceptions.py
# class SyncError(DBError):
#     pass
# 
# Path: g_sorcery/syncer.py
# class Syncer(object):
#     """
#     Class used to sync data with remote source.
#     """
# 
#     def __init__(self, persistent_datadir):
#         self.persistent_datadir = persistent_datadir
# 
#     def sync(self, db_uri, repository_config):
#         """
#         Synchronize local directory with remote source.
# 
#         Args:
#             db_uri: URI for synchronization with remote source.
#             repository_config: repository config.
# 
#         Returns:
#             SyncedData object that gives access to the directory with data.
#         """
#         raise NotImplementedError
# 
# class SyncedData(object):
#     """
#     Synced data.
# 
#     Directory with sync data is guaranted to exist only as long as this
#     object does.
#     """
#     def __init__(self, directory):
#         self.directory = os.path.abspath(directory)
# 
#     def get_path(self):
#         return self.directory
# 
# class TmpSyncedData(SyncedData):
#     """
#     Synced data that lives in a temporary directory.
#     """
# 
#     def __init__(self, directory, tmpdirobj):
#         super(TmpSyncedData, self).__init__(directory)
#         self.tmpdirobj = tmpdirobj


import os
import shutil
import subprocess
from g_sorcery.compatibility import TemporaryDirectory
from g_sorcery.exceptions import SyncError
from g_sorcery.syncer import Syncer, SyncedData, TmpSyncedData
            path = os.path.join(tmp_dir.name, "remote")
        else:
            path = self.persistent_datadir
        try:
            branch = repository_config["branch"]
        except KeyError:
            branch = "master"

        if os.path.exists(path):
            if self.branch_not_changed(path, branch) and self.remote_url_not_changed(path, db_uri):
                self.pull(path)
            else:
                shutil.rmtree(path)
                self.clone(db_uri, branch, path)
        else:
            self.clone(db_uri, branch, path)

        if self.persistent_datadir is None:
            return TmpSyncedData(path, tmp_dir)
        else:
            return SyncedData(path)


    def clone(self, db_uri, branch, path):
        if os.system("git clone --depth 1 --branch " + branch + " " + db_uri + " " + path):
            raise SyncError("sync failed (clonning): " + db_uri)


    def pull(self, path):
        if os.system("cd " + path + " && git pull"):

# Path: g_sorcery/compatibility.py
# class TemporaryDirectory(object):
#     def __init__(self):
#         self.name = mkdtemp()
# 
#     def __del__(self):
#         shutil.rmtree(self.name)
# 
# Path: g_sorcery/exceptions.py
# class SyncError(DBError):
#     pass
# 
# Path: g_sorcery/syncer.py
# class Syncer(object):
#     """
#     Class used to sync data with remote source.
#     """
# 
#     def __init__(self, persistent_datadir):
#         self.persistent_datadir = persistent_datadir
# 
#     def sync(self, db_uri, repository_config):
#         """
#         Synchronize local directory with remote source.
# 
#         Args:
#             db_uri: URI for synchronization with remote source.
#             repository_config: repository config.
# 
#         Returns:
#             SyncedData object that gives access to the directory with data.
#         """
#         raise NotImplementedError
# 
# class SyncedData(object):
#     """
#     Synced data.
# 
#     Directory with sync data is guaranted to exist only as long as this
#     object does.
#     """
#     def __init__(self, directory):
#         self.directory = os.path.abspath(directory)
# 
#     def get_path(self):
#         return self.directory
# 
# class TmpSyncedData(SyncedData):
#     """
#     Synced data that lives in a temporary directory.
#     """
# 
#     def __init__(self, directory, tmpdirobj):
#         super(TmpSyncedData, self).__init__(directory)
#         self.tmpdirobj = tmpdirobj


# Path: g_sorcery/git_syncer/git_syncer.py
import os
import shutil
import subprocess
from g_sorcery.compatibility import TemporaryDirectory
from g_sorcery.exceptions import SyncError
from g_sorcery.syncer import Syncer, SyncedData, TmpSyncedData

            path = os.path.join(tmp_dir.name, "remote")
        else:
            path = self.persistent_datadir
        try:
            branch = repository_config["branch"]
        except KeyError:
            branch = "master"

        if os.path.exists(path):
            if self.branch_not_changed(path, branch) and self.remote_url_not_changed(path, db_uri):
                self.pull(path)
            else:
                shutil.rmtree(path)
                self.clone(db_uri, branch, path)
        else:
            self.clone(db_uri, branch, path)

        if self.persistent_datadir is None:
            return TmpSyncedData(path, tmp_dir)
        else:
            return SyncedData(path)


    def clone(self, db_uri, branch, path):
        if os.system("git clone --depth 1 --branch " + branch + " " + db_uri + " " + path):
            raise SyncError("sync failed (clonning): " + db_uri)


    def pull(self, path):
        if os.system("cd " + path + " && git pull"):

            raise SyncError("sync failed (pulling): " + path)