Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/local_document/cache.py
blob: 79e7cbfe6e820b0155c1a6c604e67a8f4ed39ae8 (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
# Copyright (C) 2012, Aleksey Lim
#
# 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 3 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, see <http://www.gnu.org/licenses/>.

import os
import shutil
import logging
import tempfile
from os.path import isdir, exists, dirname, join

from sweets_recipe import Bundle

from local_document import env, http
from local_document.sockets import BUFFER_SIZE


_logger = logging.getLogger('local_document.cache')


def get_cached_blob(document, guid, prop):
    path = _path(document, guid, prop)
    mime_path = path + '.mime'

    if not exists(path) or not exists(mime_path):
        return None

    with file(mime_path) as f:
        mime_type = f.read().strip()

    if not os.stat(path).st_size:
        path = None

    return path, mime_type


def get_blob(document, guid, prop):
    cache = get_cached_blob(document, guid, prop)
    if cache is not None:
        return cache

    path = _ensure_path(document, guid, prop)
    mime_path = path + '.mime'

    if isdir(path):
        shutil.rmtree(path)

    response = http.raw_request('GET', [document, guid, prop],
            allow_redirects=True)

    if not exists(dirname(path)):
        os.makedirs(dirname(path))

    mime_type = response.headers.get('Content-Type') or \
            'application/octet-stream'
    with file(mime_path, 'w') as f:
        f.write(mime_type)

    def download(f):
        _logger.debug('Download %s/%s/%s BLOB to %r file',
                document, guid, prop, path)

        length = int(response.headers.get('Content-Length', BUFFER_SIZE))
        chunk_size = min(length, BUFFER_SIZE)
        empty = True

        for chunk in response.iter_content(chunk_size=chunk_size):
            empty = False
            f.write(chunk)

        return not empty

    if document == 'implementation' and prop == 'bundle':
        tmp_file = tempfile.NamedTemporaryFile(delete=False)
        try:
            if download(tmp_file):
                tmp_file.close()
                with Bundle(tmp_file.name, 'application/zip') as bundle:
                    bundle.extractall(path)
            else:
                path = None
        finally:
            if exists(tmp_file.name):
                os.unlink(tmp_file.name)
    else:
        with file(path, 'w') as f:
            if not download(f):
                path = None

    return path, mime_type


def set_blob(document, guid, prop, stream,
        mime_type='application/octet-stream'):
    path = _ensure_path(document, guid, prop)
    mime_path = path + '.mime'

    with file(mime_path, 'w') as f:
        f.write(mime_type)

    with file(path, 'wb') as f:
        while True:
            chunk = stream.read(BUFFER_SIZE)
            if not chunk:
                break
            f.write(chunk)


def _path(document, guid, *args):
    return join(env.local_root.value, 'cache', document, guid[:2], guid, *args)


def _ensure_path(document, guid, *args):
    return env.ensure_path('cache', document, guid[:2], guid, *args)