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