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