1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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)
|