/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
16
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
17
"""A generator which creates a template-based output from the current
18
   tree info."""
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
19
7045.1.15 by Jelmer Vernooij
fix version info tests
20
import codecs
21
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
22
from breezy import errors
23
from breezy.revision import (
7143.15.2 by Jelmer Vernooij
Run autopep8.
24
    NULL_REVISION,
25
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
26
from breezy.lazy_regex import lazy_compile
27
from breezy.version_info_formats import (
7143.15.2 by Jelmer Vernooij
Run autopep8.
28
    create_date_str,
29
    VersionInfoBuilder,
30
    )
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
31
32
6734.1.17 by Jelmer Vernooij
Move format custom errors.
33
class MissingTemplateVariable(errors.BzrError):
34
35
    _fmt = 'Variable {%(name)s} is not available.'
36
37
    def __init__(self, name):
38
        self.name = name
39
40
41
class NoTemplate(errors.BzrError):
42
43
    _fmt = 'No template specified.'
44
45
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
46
class Template(object):
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
47
    """A simple template engine.
48
49
    >>> t = Template()
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
50
    >>> t.add('test', 'xxx')
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
51
    >>> print(list(t.process('{test}')))
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
52
    ['xxx']
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
53
    >>> print(list(t.process('{test} test')))
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
54
    ['xxx', ' test']
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
55
    >>> print(list(t.process('test {test}')))
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
56
    ['test ', 'xxx']
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
57
    >>> print(list(t.process('test {test} test')))
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
58
    ['test ', 'xxx', ' test']
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
59
    >>> print(list(t.process('{test}\\\\n')))
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
60
    ['xxx', '\\n']
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
61
    >>> print(list(t.process('{test}\\n')))
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
62
    ['xxx', '\\n']
63
    """
64
6800.1.1 by Jelmer Vernooij
Fix another regex.
65
    _tag_re = lazy_compile('{(\\w+)}')
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
66
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
67
    def __init__(self):
68
        self._data = {}
69
70
    def add(self, name, value):
71
        self._data[name] = value
72
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
73
    def process(self, tpl):
7045.1.15 by Jelmer Vernooij
fix version info tests
74
        unicode_escape = codecs.getdecoder("unicode_escape")
75
        tpl = unicode_escape(tpl)[0]
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
76
        pos = 0
77
        while True:
78
            match = self._tag_re.search(tpl, pos)
79
            if not match:
80
                if pos < len(tpl):
81
                    yield tpl[pos:]
82
                break
83
            start, end = match.span()
84
            if start > 0:
85
                yield tpl[pos:start]
86
            pos = end
87
            name = match.group(1)
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
88
            try:
89
                data = self._data[name]
90
            except KeyError:
6743 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more.
91
                raise MissingTemplateVariable(name)
6695.3.1 by Martin
Remove remaining uses of basestring from the codebase
92
            if not isinstance(data, str):
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
93
                data = str(data)
94
            yield data
95
96
97
class CustomVersionInfoBuilder(VersionInfoBuilder):
98
    """Create a version file based on a custom template."""
99
100
    def generate(self, to_file):
3207.1.1 by Lukáš Lalinský
Raise a proper error when 'version-info --custom' is used without a template
101
        if self._template is None:
6743 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more.
102
            raise NoTemplate()
3207.1.1 by Lukáš Lalinský
Raise a proper error when 'version-info --custom' is used without a template
103
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
104
        info = Template()
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
105
        info.add('build_date', create_date_str())
106
        info.add('branch_nick', self._branch.nick)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
107
108
        revision_id = self._get_revision_id()
4250.1.1 by Jelmer Vernooij
Fix version-info in empty branches.
109
        if revision_id == NULL_REVISION:
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
110
            info.add('revno', 0)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
111
        else:
6968.1.1 by Jelmer Vernooij
Support 'bzr version-info' in horizon branches.
112
            try:
113
                info.add('revno', self._get_revno_str(revision_id))
114
            except errors.GhostRevisionsHaveNoRevno:
115
                pass
7045.1.15 by Jelmer Vernooij
fix version info tests
116
            info.add('revision_id', revision_id.decode('utf-8'))
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
117
            rev = self._branch.repository.get_revision(revision_id)
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
118
            info.add('date', create_date_str(rev.timestamp, rev.timezone))
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
119
120
        if self._check:
121
            self._extract_file_revisions()
122
123
        if self._check:
124
            if self._clean:
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
125
                info.add('clean', 1)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
126
            else:
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
127
                info.add('clean', 0)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
128
129
        to_file.writelines(info.process(self._template))