~bzr-pqm/bzr/bzr.dev

0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
1
# Copyright (C) 2006 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
0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
16
17
"""A generator which creates a python script from the current tree info"""
18
19
import pprint
20
4250.1.1 by Jelmer Vernooij
Fix version-info in empty branches.
21
from bzrlib.revision import (
22
    NULL_REVISION,
23
    )
2022.1.1 by John Arbash Meinel
[merge] version-info plugin, and cleanup for layout in bzr
24
from bzrlib.version_info_formats import (
0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
25
    create_date_str,
26
    VersionInfoBuilder,
27
    )
28
29
30
# Header and footer for the python format
31
_py_version_header = '''#!/usr/bin/env python
32
"""This file is automatically generated by generate_version_info
33
It uses the current working tree to determine the revision.
34
So don't edit it. :)
35
"""
36
37
'''
38
39
40
_py_version_footer = '''
41
42
if __name__ == '__main__':
43
    print 'revision: %(revno)d' % version_info
44
    print 'nick: %(branch_nick)s' % version_info
45
    print 'revision id: %(revision_id)s' % version_info
46
'''
47
48
49
class PythonVersionInfoBuilder(VersionInfoBuilder):
50
    """Create a version file which is a python source module."""
51
52
    def generate(self, to_file):
53
        info = {'build_date':create_date_str()
54
                  , 'revno':None
55
                  , 'revision_id':None
56
                  , 'branch_nick':self._branch.nick
57
                  , 'clean':None
58
                  , 'date':None
59
        }
60
        revisions = []
61
62
        revision_id = self._get_revision_id()
4250.1.1 by Jelmer Vernooij
Fix version-info in empty branches.
63
        if revision_id == NULL_REVISION:
0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
64
            info['revno'] = 0
65
        else:
66
            info['revno'] = self._branch.revision_id_to_revno(revision_id)
67
            info['revision_id'] = revision_id
68
            rev = self._branch.repository.get_revision(revision_id)
69
            info['date'] = create_date_str(rev.timestamp, rev.timezone)
70
71
        if self._check or self._include_file_revs:
72
            self._extract_file_revisions()
73
74
        if self._check:
75
            if self._clean:
76
                info['clean'] = True
77
            else:
78
                info['clean'] = False
79
80
        info_str = pprint.pformat(info)
81
        to_file.write(_py_version_header)
82
        to_file.write('version_info = ')
83
        to_file.write(info_str)
84
        to_file.write('\n\n')
85
86
        if self._include_history:
87
            self._extract_revision_history()
88
            revision_str = pprint.pformat(self._revision_history_info)
89
            to_file.write('revisions = ')
90
            to_file.write(revision_str)
91
            to_file.write('\n\n')
92
        else:
93
            to_file.write('revisions = {}\n\n')
94
95
        if self._include_file_revs:
96
            file_rev_str = pprint.pformat(self._file_revisions)
97
            to_file.write('file_revisions = ')
98
            to_file.write(file_rev_str)
99
            to_file.write('\n\n')
100
        else:
101
            to_file.write('file_revisions = {}\n\n')
102
103
        to_file.write(_py_version_footer)
104
105
106