~bzr-pqm/bzr/bzr.dev

5599.3.5 by John Arbash Meinel
Merge bzr.dev 5602 to, yet again, resolve a NEWS/bzr-2.3.txt conflict
1
# Copyright (C) 2006, 2009, 2011 Canonical Ltd
0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
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
if __name__ == '__main__':
42
    print 'revision: %(revno)d' % version_info
43
    print 'nick: %(branch_nick)s' % version_info
44
    print 'revision id: %(revision_id)s' % version_info
45
'''
46
47
48
class PythonVersionInfoBuilder(VersionInfoBuilder):
49
    """Create a version file which is a python source module."""
50
51
    def generate(self, to_file):
52
        info = {'build_date':create_date_str()
53
                  , 'revno':None
54
                  , 'revision_id':None
55
                  , 'branch_nick':self._branch.nick
56
                  , 'clean':None
57
                  , 'date':None
58
        }
59
        revisions = []
60
61
        revision_id = self._get_revision_id()
4250.1.1 by Jelmer Vernooij
Fix version-info in empty branches.
62
        if revision_id == NULL_REVISION:
0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
63
            info['revno'] = 0
64
        else:
65
            info['revno'] = self._branch.revision_id_to_revno(revision_id)
66
            info['revision_id'] = revision_id
67
            rev = self._branch.repository.get_revision(revision_id)
68
            info['date'] = create_date_str(rev.timestamp, rev.timezone)
69
70
        if self._check or self._include_file_revs:
71
            self._extract_file_revisions()
72
73
        if self._check:
74
            if self._clean:
75
                info['clean'] = True
76
            else:
77
                info['clean'] = False
78
79
        info_str = pprint.pformat(info)
80
        to_file.write(_py_version_header)
81
        to_file.write('version_info = ')
82
        to_file.write(info_str)
83
        to_file.write('\n\n')
84
85
        if self._include_history:
86
            self._extract_revision_history()
87
            revision_str = pprint.pformat(self._revision_history_info)
88
            to_file.write('revisions = ')
89
            to_file.write(revision_str)
90
            to_file.write('\n\n')
91
        else:
92
            to_file.write('revisions = {}\n\n')
93
94
        if self._include_file_revs:
95
            file_rev_str = pprint.pformat(self._file_revisions)
96
            to_file.write('file_revisions = ')
97
            to_file.write(file_rev_str)
98
            to_file.write('\n\n')
99
        else:
100
            to_file.write('file_revisions = {}\n\n')
101
102
        to_file.write(_py_version_footer)
103
104
105