aboutsummaryrefslogtreecommitdiff
blob: beeffd49abb9936cf5f3bcd8fb6fbc53f1f01510 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    syncer.py
    ~~~~~~~~~

    sync helper

    :copyright: (c) 2013-2015 by Jauhien Piatlicki
    :license: GPL-2, see LICENSE for more details.
"""

import glob
import os

from .compatibility import TemporaryDirectory

from .exceptions import SyncError
from .fileutils import wget


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


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 TGZSyncer(Syncer):
    """
    Class used to download and unpack tarballs.
    """

    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.
        """
        download_dir = TemporaryDirectory()
        if wget(db_uri, download_dir.name):
            raise SyncError('sync failed: ' + db_uri)

        tmp_dir = TemporaryDirectory()
        for f_name in glob.iglob(os.path.join(download_dir.name, '*.tar.gz')):
            if os.system("tar -xvzf " + f_name + " -C " + tmp_dir.name):
                raise SyncError('sync failed (unpacking)')

        tmp_path = os.path.join(tmp_dir.name, os.listdir(tmp_dir.name)[0])
        del download_dir
        return TmpSyncedData(tmp_path, tmp_dir)


SUPPORTED_SYNCERS = {"tgz": TGZSyncer}

# git_syncer module is optional, we should check if it is installed
try:
    from .git_syncer.git_syncer import GITSyncer
    SUPPORTED_SYNCERS["git"] = GITSyncer

except ImportError as e:
    pass