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 |
|
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 |
||
7045.1.15
by Jelmer Vernooij
fix version info tests |
22 |
import codecs |
23 |
||
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
24 |
from breezy import errors |
25 |
from breezy.revision import ( |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
26 |
NULL_REVISION, |
27 |
)
|
|
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
28 |
from breezy.lazy_regex import lazy_compile |
29 |
from breezy.version_info_formats import ( |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
30 |
create_date_str, |
31 |
VersionInfoBuilder, |
|
32 |
)
|
|
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
33 |
|
34 |
||
6734.1.17
by Jelmer Vernooij
Move format custom errors. |
35 |
class MissingTemplateVariable(errors.BzrError): |
36 |
||
37 |
_fmt = 'Variable {%(name)s} is not available.' |
|
38 |
||
39 |
def __init__(self, name): |
|
40 |
self.name = name |
|
41 |
||
42 |
||
43 |
class NoTemplate(errors.BzrError): |
|
44 |
||
45 |
_fmt = 'No template specified.' |
|
46 |
||
47 |
||
2948.4.6
by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable. |
48 |
class Template(object): |
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
49 |
"""A simple template engine. |
50 |
||
51 |
>>> t = Template()
|
|
2948.4.6
by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable. |
52 |
>>> t.add('test', 'xxx')
|
7045.1.1
by Jelmer Vernooij
Fix another 300 tests. |
53 |
>>> print(list(t.process('{test}')))
|
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
54 |
['xxx']
|
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 |
['xxx', ' test']
|
7045.1.1
by Jelmer Vernooij
Fix another 300 tests. |
57 |
>>> print(list(t.process('test {test}')))
|
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
58 |
['test ', 'xxx']
|
7045.1.1
by Jelmer Vernooij
Fix another 300 tests. |
59 |
>>> print(list(t.process('test {test} test')))
|
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
60 |
['test ', 'xxx', ' test']
|
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']
|
7045.1.1
by Jelmer Vernooij
Fix another 300 tests. |
63 |
>>> print(list(t.process('{test}\\n')))
|
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
64 |
['xxx', '\\n']
|
65 |
"""
|
|
66 |
||
6800.1.1
by Jelmer Vernooij
Fix another regex. |
67 |
_tag_re = lazy_compile('{(\\w+)}') |
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
68 |
|
2948.4.6
by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable. |
69 |
def __init__(self): |
70 |
self._data = {} |
|
71 |
||
72 |
def add(self, name, value): |
|
73 |
self._data[name] = value |
|
74 |
||
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
75 |
def process(self, tpl): |
7045.1.15
by Jelmer Vernooij
fix version info tests |
76 |
unicode_escape = codecs.getdecoder("unicode_escape") |
77 |
tpl = unicode_escape(tpl)[0] |
|
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
78 |
pos = 0 |
79 |
while True: |
|
80 |
match = self._tag_re.search(tpl, pos) |
|
81 |
if not match: |
|
82 |
if pos < len(tpl): |
|
83 |
yield tpl[pos:] |
|
84 |
break
|
|
85 |
start, end = match.span() |
|
86 |
if start > 0: |
|
87 |
yield tpl[pos:start] |
|
88 |
pos = end |
|
89 |
name = match.group(1) |
|
2948.4.6
by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable. |
90 |
try: |
91 |
data = self._data[name] |
|
92 |
except KeyError: |
|
6743
by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more. |
93 |
raise MissingTemplateVariable(name) |
6695.3.1
by Martin
Remove remaining uses of basestring from the codebase |
94 |
if not isinstance(data, str): |
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
95 |
data = str(data) |
96 |
yield data |
|
97 |
||
98 |
||
99 |
class CustomVersionInfoBuilder(VersionInfoBuilder): |
|
100 |
"""Create a version file based on a custom template.""" |
|
101 |
||
102 |
def generate(self, to_file): |
|
3207.1.1
by Lukáš Lalinský
Raise a proper error when 'version-info --custom' is used without a template |
103 |
if self._template is None: |
6743
by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more. |
104 |
raise NoTemplate() |
3207.1.1
by Lukáš Lalinský
Raise a proper error when 'version-info --custom' is used without a template |
105 |
|
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
106 |
info = Template() |
2948.4.6
by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable. |
107 |
info.add('build_date', create_date_str()) |
108 |
info.add('branch_nick', self._branch.nick) |
|
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
109 |
|
110 |
revision_id = self._get_revision_id() |
|
4250.1.1
by Jelmer Vernooij
Fix version-info in empty branches. |
111 |
if revision_id == NULL_REVISION: |
2948.4.6
by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable. |
112 |
info.add('revno', 0) |
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
113 |
else: |
6968.1.1
by Jelmer Vernooij
Support 'bzr version-info' in horizon branches. |
114 |
try: |
115 |
info.add('revno', self._get_revno_str(revision_id)) |
|
116 |
except errors.GhostRevisionsHaveNoRevno: |
|
117 |
pass
|
|
7045.1.15
by Jelmer Vernooij
fix version info tests |
118 |
info.add('revision_id', revision_id.decode('utf-8')) |
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
119 |
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. |
120 |
info.add('date', create_date_str(rev.timestamp, rev.timezone)) |
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
121 |
|
122 |
if self._check: |
|
123 |
self._extract_file_revisions() |
|
124 |
||
125 |
if self._check: |
|
126 |
if self._clean: |
|
2948.4.6
by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable. |
127 |
info.add('clean', 1) |
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
128 |
else: |
2948.4.6
by Lukáš Lalinský
Don't subclass dict in Template and raise an error on missing variable. |
129 |
info.add('clean', 0) |
2948.4.1
by Lukáš Lalinský
Custom template-based version info formatter. |
130 |
|
131 |
to_file.writelines(info.process(self._template)) |