Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/carquinyol/layoutmanager.py
blob: afb9e42680e4820afd7411a9b3c9376e16550fbc (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright (C) 2008, One Laptop Per Child
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import logging

MAX_QUERY_LIMIT = 40960
CURRENT_LAYOUT_VERSION = 6


class LayoutManager(object):
    """Provide the logic about how entries are stored inside the datastore
    directory
    """

    def __init__(self):
        profile = os.environ.get('SUGAR_PROFILE', 'default')
        base_dir = os.path.join(os.path.expanduser('~'), '.sugar', profile)

        self._root_path = os.path.join(base_dir, 'datastore')

        if not os.path.exists(self._root_path):
            os.makedirs(self._root_path)

        self._create_if_needed(self.get_checksums_dir())
        self._create_if_needed(self.get_queue_path())

    def _create_if_needed(self, path):
        if not os.path.exists(path):
            os.makedirs(path)

    def get_version(self):
        version_path = os.path.join(self._root_path, 'version')
        version = 0
        if os.path.exists(version_path):
            try:
                version = int(open(version_path, 'r').read())
            except ValueError:
                logging.exception('Can not read layout version')
                version = 0

        return version

    def set_version(self, version):
        version_path = os.path.join(self._root_path, 'version')
        open(version_path, 'w').write(str(version))

    def get_entry_path(self, object_id):
        # os.path.join() is just too slow
        tree_id, version_id = object_id
        return '%s/%s/%s/%s' % (self._root_path, tree_id[:2], tree_id,
            version_id)

    def get_data_path(self, object_id):
        tree_id, version_id = object_id
        return '%s/%s/%s/%s/data' % (self._root_path, tree_id[:2], tree_id,
            version_id)

    def get_metadata_path(self, object_id):
        tree_id, version_id = object_id
        return '%s/%s/%s/%s/metadata' % (self._root_path, tree_id[:2], tree_id,
            version_id)

    def get_root_path(self):
        return self._root_path

    def get_index_path(self):
        return os.path.join(self._root_path, 'index')

    def get_checksums_dir(self):
        return os.path.join(self._root_path, 'checksums')

    def get_queue_path(self):
        return os.path.join(self.get_checksums_dir(), 'queue')

    def find_all(self):
        object_ids = []
        for tree_2 in os.listdir(self._root_path):
            tree_2_path = os.path.join(self._root_path, tree_2)
            if not (os.path.isdir(tree_2_path) and len(tree_2) == 2):
                continue

            for tree_id in os.listdir(tree_2_path):
                if len(tree_id) != 36:
                    continue

                tree_path = os.path.join(tree_2_path, tree_id)
                for version_id in os.listdir(tree_path):
                    if len(version_id) != 36:
                        continue

                    object_ids.append((tree_id, version_id))

        return object_ids

    def is_empty(self):
        """Check if there is any existing entry.

        All data store layout versions are handled. Will err on the safe
        side (i.e. return False if there might be any entry)."""
        if os.path.exists(os.path.join(self._root_path, 'store')):
            # unmigrated 0.82 data store
            return False

        for tree_2 in os.listdir(self._root_path):
            tree_2_path = os.path.join(self._root_path, tree_2)
            if not (os.path.isdir(tree_2_path) and len(tree_2) == 2):
                continue

            for tree_id in os.listdir(tree_2_path):
                if len(tree_id) != 36:
                    continue

                tree_path = os.path.join(tree_2_path, tree_id)
                return False

        return True


_instance = None


def get_instance():
    global _instance
    if _instance is None:
        _instance = LayoutManager()
    return _instance