/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/controllers/directory_ui.py

  • Committer: Ubuntu One Auto Copilot
  • Author(s): Jelmer Vernooij
  • Date: 2022-08-30 10:28:23 UTC
  • mfrom: (533.1.1 trunk)
  • Revision ID: otto-copilot@canonical.com-20220830102823-u3w6efosxw5s086s
Cope with moved errors NoSuchFile and FileExists in newer versions of Breezy.

Merged from https://code.launchpad.net/~jelmer/loggerhead/moved-errors/+merge/429073

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008  Canonical Ltd.
 
2
#                     (Authored by Martin Albisetti <argentina@gmail.com>)
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
 
17
#
 
18
 
 
19
import datetime
 
20
import logging
 
21
import stat
 
22
 
 
23
from breezy import branch, errors, urlutils
 
24
 
 
25
try:
 
26
    from breezy.transport import NoSuchFile
 
27
except ImportError:
 
28
    from breezy.errors import NoSuchFile
 
29
 
 
30
from .. import util
 
31
from ..controllers import TemplatedBranchView
 
32
 
 
33
 
 
34
class DirEntry(object):
 
35
 
 
36
    def __init__(self, dirname, parity, branch):
 
37
        self.dirname = urlutils.unquote(dirname)
 
38
        self.parity = parity
 
39
        self.branch = branch
 
40
        if branch is not None:
 
41
            # If a branch is empty, bzr raises an exception when trying this
 
42
            try:
 
43
                self.last_revision = branch.repository.get_revision(branch.last_revision())
 
44
                self.last_change_time = datetime.datetime.utcfromtimestamp(self.last_revision.timestamp)
 
45
            except errors.NoSuchRevision:
 
46
                self.last_revision = None
 
47
                self.last_change_time = None
 
48
 
 
49
 
 
50
class DirectoryUI(TemplatedBranchView):
 
51
    """
 
52
    """
 
53
 
 
54
    template_name = 'directory'
 
55
 
 
56
    def __init__(self, static_url_base, transport, name):
 
57
 
 
58
        class _branch(object):
 
59
            context_url = 1
 
60
 
 
61
            @staticmethod
 
62
            def static_url(path):
 
63
                return self._static_url_base + path
 
64
        self._branch = _branch
 
65
        self._history_callable = lambda: None
 
66
        self._name = name
 
67
        self._static_url_base = static_url_base
 
68
        self.transport = transport
 
69
        self.log = logging.getLogger('')
 
70
 
 
71
    def get_values(self, path, kwargs, response):
 
72
        listing = [d for d in self.transport.list_dir('.')
 
73
                   if not d.startswith('.')]
 
74
        listing.sort(key=lambda x: x.lower())
 
75
        dirs = []
 
76
        parity = 0
 
77
        for d in listing:
 
78
            try:
 
79
                b = branch.Branch.open_from_transport(self.transport.clone(d))
 
80
            except:
 
81
                # TODO(jelmer): don't catch all exceptions here
 
82
                try:
 
83
                    if not stat.S_ISDIR(self.transport.stat(d).st_mode):
 
84
                        continue
 
85
                except NoSuchFile:
 
86
                    continue
 
87
                b = None
 
88
            else:
 
89
                if not b.get_config().get_user_option_as_bool('http_serve', default=True):
 
90
                    continue
 
91
            dirs.append(DirEntry(d, parity, b))
 
92
            parity = 1 - parity
 
93
        # Create breadcrumb trail
 
94
        directory_breadcrumbs = util.directory_breadcrumbs(
 
95
                self._name,
 
96
                False,
 
97
                'directory')
 
98
        return {
 
99
            'dirs': dirs,
 
100
            'name': self._name,
 
101
            'directory_breadcrumbs': directory_breadcrumbs,
 
102
            }