summaryrefslogtreecommitdiff
path: root/morphcacheserver/repocache.py
blob: 7061508d09fe75208b4d47eaf930dc3ca898c146 (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
# Copyright (C) 2012  Codethink Limited
# 
# 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; version 2 of the License.
# 
# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.


import cliapp
import os
import string


class RepositoryNotFoundError(cliapp.AppException):

    def __init__(self, repo):
        cliapp.AppException.__init__(
                self, 'Repository %s does not exist in the cache' % repo)


class InvalidReferenceError(cliapp.AppException):
    
    def __init__(self, repo, ref):
        cliapp.AppException.__init__(
                self, 'Ref %s is an invalid reference for repo %s' %
                (ref, repo))


class UnresolvedNamedReferenceError(cliapp.AppException):

    def __init__(self, repo, ref):
        cliapp.AppException.__init__(
                self, 'Ref %s is not a SHA1 ref for repo %s' %
                (ref, repo))


class RepoCache(object):
    
    def __init__(self, app, repo_cache_dir, bundle_cache_dir):
        self.app = app
        self.repo_cache_dir = repo_cache_dir
        self.bundle_cache_dir = bundle_cache_dir

    def resolve_ref(self, repo_url, ref):
        quoted_url = self._quote_url(repo_url)
        repo_dir = os.path.join(self.repo_cache_dir, quoted_url)
        if not os.path.exists(repo_dir):
            raise RepositoryNotFoundError(repo_url)
        try:
            refs = self._show_ref(repo_dir, ref).split('\n')
            refs = [x.split() for x in refs if 'origin' in x]
            return refs[0][0]
        except cliapp.AppException:
            pass
        if not self._is_valid_sha1(ref):
            raise InvalidReferenceError(repo_url, ref)
        try:
            return self._rev_list(ref).strip()
        except:
            raise InvalidReferenceError(repo_url, ref)

    def cat_file(self, repo_url, ref, filename):
        quoted_url = self._quote_url(repo_url)
        repo_dir = os.path.join(self.repo_cache_dir, quoted_url)
        if not self._is_valid_sha1(ref):
            raise UnresolvedNamedReferenceError(repo_url, ref)
        if not os.path.exists(repo_dir):
            raise RepositoryNotFoundError(repo_url)
        try:
            sha1 = self._rev_list(repo_dir, ref).strip()
        except:
            raise InvalidReferenceError(repo_url, ref)

        return self._cat_file(repo_dir, sha1, filename)

    def ls_tree(self, repo_url, ref, path):
        quoted_url = self._quote_url(repo_url)
        repo_dir = os.path.join(self.repo_cache_dir, quoted_url)
        if not self._is_valid_sha1(ref):
            raise UnresolvedNamedReferenceError(repo_url, ref)
        if not os.path.exists(repo_dir):
            raise RepositoryNotFoundError(repo_url)

        try:
            sha1 = self._rev_list(repo_dir, ref).strip()
        except:
            raise InvalidReferenceError(repo_url, ref)

        lines = self._ls_tree(repo_dir, sha1, path).strip()
        lines = lines.splitlines()
        data = {}
        for line in lines:
            elements = line.split()
            basename = elements[3]
            data[basename] = {
                'mode': elements[0],
                'kind': elements[1],
                'sha1': elements[2],
            }
        return data

    def get_bundle_filename(self, repo_url):
        quoted_url = self._quote_url(repo_url)
        return os.path.join(self.bundle_cache_dir, '%s.bndl' % quoted_url)
        
    def _quote_url(self, url):
        valid_chars = string.digits + string.letters + '%_'
        transl = lambda x: x if x in valid_chars else '_'
        return ''.join([transl(x) for x in url])

    def _show_ref(self, repo_dir, ref):
        return self.app.runcmd(['git', 'show-ref', ref], cwd=repo_dir)

    def _rev_list(self, repo_dir, ref):
        return self.app.runcmd(
                ['git', 'rev-list', '--no-walk', ref], cwd=repo_dir)

    def _cat_file(self, repo_dir, sha1, filename):
        return self.app.runcmd(
                ['git', 'cat-file', 'blob', '%s:%s' % (sha1, filename)],
                cwd=repo_dir)

    def _ls_tree(self, repo_dir, sha1, path):
        return self.app.runcmd(['git', 'ls-tree', sha1, path], cwd=repo_dir)

    def _is_valid_sha1(self, ref):
        valid_chars = 'abcdefABCDEF0123456789'
        return len(ref) == 40 and all([x in valid_chars for x in ref])