~bzr-pqm/bzr/bzr.dev

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
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
20
from __future__ import absolute_import
21
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
22
from bzrlib import errors
4250.1.1 by Jelmer Vernooij
Fix version-info in empty branches.
23
from bzrlib.revision import (
24
   NULL_REVISION,
25
   )
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
26
from bzrlib.lazy_regex import lazy_compile
27
from bzrlib.version_info_formats import (
28
   create_date_str,
29
   VersionInfoBuilder,
30
   )
31
32
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
33
class Template(object):
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
34
    """A simple template engine.
35
36
    >>> t = Template()
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
37
    >>> t.add('test', 'xxx')
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
38
    >>> print list(t.process('{test}'))
39
    ['xxx']
40
    >>> print list(t.process('{test} test'))
41
    ['xxx', ' test']
42
    >>> print list(t.process('test {test}'))
43
    ['test ', 'xxx']
44
    >>> print list(t.process('test {test} test'))
45
    ['test ', 'xxx', ' test']
46
    >>> print list(t.process('{test}\\\\n'))
47
    ['xxx', '\\n']
48
    >>> print list(t.process('{test}\\n'))
49
    ['xxx', '\\n']
50
    """
51
52
    _tag_re = lazy_compile('{(\w+)}')
53
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
54
    def __init__(self):
55
        self._data = {}
56
57
    def add(self, name, value):
58
        self._data[name] = value
59
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
60
    def process(self, tpl):
61
        tpl = tpl.decode('string_escape')
62
        pos = 0
63
        while True:
64
            match = self._tag_re.search(tpl, pos)
65
            if not match:
66
                if pos < len(tpl):
67
                    yield tpl[pos:]
68
                break
69
            start, end = match.span()
70
            if start > 0:
71
                yield tpl[pos:start]
72
            pos = end
73
            name = match.group(1)
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
74
            try:
75
                data = self._data[name]
76
            except KeyError:
77
                raise errors.MissingTemplateVariable(name)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
78
            if not isinstance(data, basestring):
79
                data = str(data)
80
            yield data
81
82
83
class CustomVersionInfoBuilder(VersionInfoBuilder):
84
    """Create a version file based on a custom template."""
85
86
    def generate(self, to_file):
3207.1.1 by Lukáš Lalinský
Raise a proper error when 'version-info --custom' is used without a template
87
        if self._template is None:
88
            raise errors.NoTemplate()
89
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
90
        info = Template()
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
91
        info.add('build_date', create_date_str())
92
        info.add('branch_nick', self._branch.nick)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
93
94
        revision_id = self._get_revision_id()
4250.1.1 by Jelmer Vernooij
Fix version-info in empty branches.
95
        if revision_id == NULL_REVISION:
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
96
            info.add('revno', 0)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
97
        else:
5967.11.2 by Benoît Pierre
Update version-info formats to support dotted revnos.
98
            info.add('revno', self._get_revno_str(revision_id))
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
99
            info.add('revision_id', revision_id)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
100
            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.
101
            info.add('date', create_date_str(rev.timestamp, rev.timezone))
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
102
103
        if self._check:
104
            self._extract_file_revisions()
105
106
        if self._check:
107
            if self._clean:
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
108
                info.add('clean', 1)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
109
            else:
2948.4.6 by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable.
110
                info.add('clean', 0)
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
111
112
        to_file.writelines(info.process(self._template))