diff options
author | Jauhien Piatlicki <jauhien@gentoo.org> | 2015-08-04 22:54:21 +0200 |
---|---|---|
committer | Jauhien Piatlicki <jauhien@gentoo.org> | 2015-08-05 20:32:11 +0200 |
commit | 29875e07c1749ddf895d2058046c17092d72732e (patch) | |
tree | 1b658004588265a09c979355ce1902c0128facf9 /g_sorcery/git_syncer | |
parent | [docs] update documentation (diff) | |
download | g-sorcery-29875e07c1749ddf895d2058046c17092d72732e.tar.gz g-sorcery-29875e07c1749ddf895d2058046c17092d72732e.tar.bz2 g-sorcery-29875e07c1749ddf895d2058046c17092d72732e.zip |
[g_sorcery/package_db] new DB syncing
Diffstat (limited to 'g_sorcery/git_syncer')
-rw-r--r-- | g_sorcery/git_syncer/__init__.py | 1 | ||||
-rw-r--r-- | g_sorcery/git_syncer/git_syncer.py | 67 |
2 files changed, 68 insertions, 0 deletions
diff --git a/g_sorcery/git_syncer/__init__.py b/g_sorcery/git_syncer/__init__.py new file mode 100644 index 0000000..4265cc3 --- /dev/null +++ b/g_sorcery/git_syncer/__init__.py @@ -0,0 +1 @@ +#!/usr/bin/env python diff --git a/g_sorcery/git_syncer/git_syncer.py b/g_sorcery/git_syncer/git_syncer.py new file mode 100644 index 0000000..0f6c58e --- /dev/null +++ b/g_sorcery/git_syncer/git_syncer.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + git_syncer.py + ~~~~~~~~~~~~~ + + git sync helper + + :copyright: (c) 2015 by Jauhien Piatlicki + :license: GPL-2, see LICENSE for more details. +""" + +import os + +from g_sorcery.compatibility import TemporaryDirectory + +from g_sorcery.exceptions import SyncError +from g_sorcery.syncer import Syncer, SyncedData, TmpSyncedData + + +class GITSyncer(Syncer): + """ + Class used to sync with git repos. + """ + + 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. + """ + if self.persistent_datadir is None: + tmp_dir = TemporaryDirectory() + 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): + #TODO: allow changing of remotes/branches + self.pull(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) |