/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/version_info_formats/format_custom.py

First attempt to merge .dev and resolve the conflicts (but tests are 
failing)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""A generator which creates a template-based output from the current
 
18
   tree info."""
 
19
 
 
20
from bzrlib import errors
 
21
from bzrlib.lazy_regex import lazy_compile
 
22
from bzrlib.version_info_formats import (
 
23
   create_date_str,
 
24
   VersionInfoBuilder,
 
25
   )
 
26
 
 
27
 
 
28
class Template(object):
 
29
    """A simple template engine.
 
30
 
 
31
    >>> t = Template()
 
32
    >>> t.add('test', 'xxx')
 
33
    >>> print list(t.process('{test}'))
 
34
    ['xxx']
 
35
    >>> print list(t.process('{test} test'))
 
36
    ['xxx', ' test']
 
37
    >>> print list(t.process('test {test}'))
 
38
    ['test ', 'xxx']
 
39
    >>> print list(t.process('test {test} test'))
 
40
    ['test ', 'xxx', ' test']
 
41
    >>> print list(t.process('{test}\\\\n'))
 
42
    ['xxx', '\\n']
 
43
    >>> print list(t.process('{test}\\n'))
 
44
    ['xxx', '\\n']
 
45
    """
 
46
 
 
47
    _tag_re = lazy_compile('{(\w+)}')
 
48
 
 
49
    def __init__(self):
 
50
        self._data = {}
 
51
 
 
52
    def add(self, name, value):
 
53
        self._data[name] = value
 
54
 
 
55
    def process(self, tpl):
 
56
        tpl = tpl.decode('string_escape')
 
57
        pos = 0
 
58
        while True:
 
59
            match = self._tag_re.search(tpl, pos)
 
60
            if not match:
 
61
                if pos < len(tpl):
 
62
                    yield tpl[pos:]
 
63
                break
 
64
            start, end = match.span()
 
65
            if start > 0:
 
66
                yield tpl[pos:start]
 
67
            pos = end
 
68
            name = match.group(1)
 
69
            try:
 
70
                data = self._data[name]
 
71
            except KeyError:
 
72
                raise errors.MissingTemplateVariable(name)
 
73
            if not isinstance(data, basestring):
 
74
                data = str(data)
 
75
            yield data
 
76
 
 
77
 
 
78
class CustomVersionInfoBuilder(VersionInfoBuilder):
 
79
    """Create a version file based on a custom template."""
 
80
 
 
81
    def generate(self, to_file):
 
82
        if self._template is None:
 
83
            raise errors.NoTemplate()
 
84
 
 
85
        info = Template()
 
86
        info.add('build_date', create_date_str())
 
87
        info.add('branch_nick', self._branch.nick)
 
88
 
 
89
        revision_id = self._get_revision_id()
 
90
        if revision_id is None:
 
91
            info.add('revno', 0)
 
92
        else:
 
93
            info.add('revno', self._branch.revision_id_to_revno(revision_id))
 
94
            info.add('revision_id', revision_id)
 
95
            rev = self._branch.repository.get_revision(revision_id)
 
96
            info.add('date', create_date_str(rev.timestamp, rev.timezone))
 
97
 
 
98
        if self._check:
 
99
            self._extract_file_revisions()
 
100
 
 
101
        if self._check:
 
102
            if self._clean:
 
103
                info.add('clean', 1)
 
104
            else:
 
105
                info.add('clean', 0)
 
106
 
 
107
        to_file.writelines(info.process(self._template))