예제 #1
0
 def __init__(self, full_path, callback=None):
     self.full_path = full_path
     self.https_proxy_server = get_local_key("proxy_setting.https_proxy_server", "django_git")
     self.connectivity_manager = ConnectivityManager()
     self.sync_msg = None
     self.call_back = callback
예제 #2
0
class GitSynchronizer(object):
    def __init__(self, full_path, callback=None):
        self.full_path = full_path
        self.https_proxy_server = get_local_key("proxy_setting.https_proxy_server", "django_git")
        self.connectivity_manager = ConnectivityManager()
        self.sync_msg = None
        self.call_back = callback

    def pull_all_branches(self):
        r = git.Repo(self.full_path)
        print "processing: ", self.full_path
        local_active_branch = r.active_branch
        log.info('current branch: %s, %s' % (local_active_branch.name, local_active_branch.commit))

        for remote_repo in r.remotes:
            log.info("remote repo: %s" % unicode(remote_repo))
            if self.is_proxy_needed(remote_repo):
                self.set_proxy_env()
            else:
                self.unset_proxy_env()
            self.process_remote_repo(local_active_branch, remote_repo)

    def process_remote_repo(self, branch, remote_repo):
        if self.is_valid_url(remote_repo.url) and (not self.is_ignored(remote_repo.url)):
            if self.is_repo_ref_valid(remote_repo):
                for remote_ref in remote_repo.refs:
                    log.info("remote branch:" + unicode(remote_ref).encode('utf8', 'replace'))
                    # self.pull_and_push_changes(branch, remote_branch, remote_repo)
                    pulling_repo = RemoteRepo(remote_repo)
                    pulling_repo.pull_and_push_changes(branch, remote_ref)
                    sync_message = pulling_repo.sync_msg
                    self.show_notification(sync_message)
        else:
            log.error("No valid pulling_repo url, repo is not synchronized")

    @ignore_exc
    def show_notification(self, sync_message):
        if (not (self.call_back is None)) and (not (sync_message is None)):
            self.call_back(u"%s: %s" % (self.full_path, sync_message))

    # noinspection PyMethodMayBeStatic
    def get_server(self, url):
        r = urlparse.urlparse(url)
        return "%s://%s" % (r.scheme, r.hostname)

    def is_proxy_needed(self, repo):
        server_url = self.get_server(repo.url)
        return not self.connectivity_manager.is_connectable(server_url)

    def set_proxy_env(self):
        os.environ["https_proxy"] = self.https_proxy_server

    @staticmethod
    def unset_proxy_env():
        try:
            del os.environ["https_proxy"]
        except:
            pass

    @staticmethod
    def is_repo_ref_valid(remote_repo):
        # if hasattr(i, "refs"):
        #     print 'no refs attr'
        # print "length:", len(i.refs)
        is_ref_valid = True
        try:
            len(remote_repo.refs)
        except AssertionError, e:
            import traceback
            traceback.print_exc()
            print remote_repo
            is_ref_valid = False
        return is_ref_valid