/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
1
# Copyright (C) 2005 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
"""\
18
Routines for extracting all version information from a bzr branch.
19
"""
20
21
import time
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
22
import pprint
23
0.8.3 by John Arbash Meinel
Playing around with some formats
24
from StringIO import StringIO
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
25
0.8.3 by John Arbash Meinel
Playing around with some formats
26
from bzrlib.errors import NoWorkingTree
27
from bzrlib.log import show_log, log_formatter
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
28
from bzrlib.rio import RioReader, RioWriter, Stanza
29
from errors import UncleanError
30
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
31
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
32
def is_clean(branch):
0.8.3 by John Arbash Meinel
Playing around with some formats
33
    """Check if a branch is clean.
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
34
35
    :param branch: The branch to check for changes
36
    TODO: jam 20051228 This might be better to ask for a WorkingTree
37
            instead of a Branch.
0.8.3 by John Arbash Meinel
Playing around with some formats
38
    :return: (is_clean, message)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
39
    """
40
    try:
41
        new_tree = branch.working_tree()
42
    except NoWorkingTree:
43
        # Trees without a working tree can't be dirty :)
0.8.3 by John Arbash Meinel
Playing around with some formats
44
        return True, ''
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
45
46
    # Look for unknown files in the new tree
47
    for info in new_tree.list_files():
48
        path = info[0]
49
        file_class = info[1]
50
        if file_class == '?':
0.8.3 by John Arbash Meinel
Playing around with some formats
51
            return False, 'path %s is unknown' % (path,)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
52
53
    from bzrlib.diff import compare_trees
54
    # See if there is anything that has been changed
55
    old_tree = branch.basis_tree()
56
    delta = compare_trees(old_tree, new_tree, want_unchanged=False)
57
    if len(delta.added) > 0:
0.8.3 by John Arbash Meinel
Playing around with some formats
58
        return False, 'have added files: %r' % (delta.added,)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
59
    if len(delta.removed) > 0:
0.8.3 by John Arbash Meinel
Playing around with some formats
60
        return False, 'have removed files: %r' % (delta.removed,)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
61
    if len(delta.modified) > 0:
0.8.3 by John Arbash Meinel
Playing around with some formats
62
        return False, 'have modified files: %r' % (delta.modified,)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
63
    if len(delta.renamed) > 0:
0.8.3 by John Arbash Meinel
Playing around with some formats
64
        return False, 'have renamed files: %r' % (delta.renamed,)
65
66
    return True, ''
67
68
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
69
# This contains a map of format id => formatter
70
# None is considered the default formatter
0.8.3 by John Arbash Meinel
Playing around with some formats
71
version_formats = {}
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
72
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
73
0.8.2 by John Arbash Meinel
Have working rio output
74
def generate_rio_version(branch, to_file,
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
75
        check_for_clean=False,
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
76
        include_revision_history=False):
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
77
    """Create the version file for this project.
78
79
    :param branch: The branch to write information about
80
    :param to_file: The file to write the information
81
    :param check_for_clean: If true, check if the branch is clean.
82
        This can be expensive for large trees. This is also only
83
        valid for branches with working trees.
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
84
    :param include_revision_history: Write out the list of revisions, and
85
        the commit message associated with each
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
86
    """
87
    info = Stanza()
88
    # TODO: jam 20051228 This might be better as the datestamp 
89
    #       of the last commit
90
    info.add('date', time.strftime('%Y-%m-%d %H:%M:%S (%A, %B %d, %Y, %Z)'))
0.8.2 by John Arbash Meinel
Have working rio output
91
    info.add('revno', str(branch.revno()))
0.8.3 by John Arbash Meinel
Playing around with some formats
92
0.8.2 by John Arbash Meinel
Have working rio output
93
    last_rev = branch.last_revision()
94
    if last_rev is not None:
95
        info.add('revision_id', last_rev)
0.8.3 by John Arbash Meinel
Playing around with some formats
96
0.8.2 by John Arbash Meinel
Have working rio output
97
    if branch.nick is not None:
98
        info.add('branch_nick', branch.nick)
0.8.3 by John Arbash Meinel
Playing around with some formats
99
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
100
    if check_for_clean:
0.8.3 by John Arbash Meinel
Playing around with some formats
101
        clean, message = is_clean(branch)
102
        if clean:
103
            info.add('clean', 'True')
104
        else:
0.8.2 by John Arbash Meinel
Have working rio output
105
            info.add('clean', 'False')
0.8.3 by John Arbash Meinel
Playing around with some formats
106
107
    if include_revision_history:
108
        revs = branch.revision_history()
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
109
        log = Stanza()
110
        for rev_id in revs:
111
            rev = branch.get_revision(rev_id)
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
112
            log.add('id', rev_id)
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
113
            log.add('message', rev.message)
114
        sio = StringIO()
115
        log_writer = RioWriter(to_file=sio)
116
        log_writer.write_stanza(log)
117
        info.add('revisions', sio.getvalue())
0.8.3 by John Arbash Meinel
Playing around with some formats
118
0.8.2 by John Arbash Meinel
Have working rio output
119
    writer = RioWriter(to_file=to_file)
120
    writer.write_stanza(info)
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
121
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
122
0.8.3 by John Arbash Meinel
Playing around with some formats
123
version_formats['rio'] = generate_rio_version
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
124
# Default format is rio
0.8.3 by John Arbash Meinel
Playing around with some formats
125
version_formats[None] = generate_rio_version
126
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
127
128
# Header and footer for the python format
129
_py_version_header = '''#!/usr/bin/env python
0.8.3 by John Arbash Meinel
Playing around with some formats
130
"""\\
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
131
This file is automatically generated by generate_version_info
0.8.3 by John Arbash Meinel
Playing around with some formats
132
It uses the current working tree to determine the revision.
133
So don't edit it. :)
134
"""
135
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
136
'''
137
138
139
_py_version_footer = '''
0.8.3 by John Arbash Meinel
Playing around with some formats
140
141
if __name__ == '__main__':
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
142
    print 'revision: %(revno)d' % version_info
143
    print 'nick: %(branch_nick)s' % version_info
144
    print 'revision id: %(revision_id)s' % version_info
0.8.3 by John Arbash Meinel
Playing around with some formats
145
'''
146
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
147
0.8.3 by John Arbash Meinel
Playing around with some formats
148
def generate_python_version(branch, to_file,
149
        check_for_clean=False,
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
150
        include_revision_history=False):
0.8.3 by John Arbash Meinel
Playing around with some formats
151
    """Create a python version file for this project.
152
153
    :param branch: The branch to write information about
154
    :param to_file: The file to write the information
155
    :param check_for_clean: If true, check if the branch is clean.
156
        This can be expensive for large trees. This is also only
157
        valid for branches with working trees.
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
158
    :param include_revision_history: Write out the list of revisions, and
159
        the commit message associated with each
0.8.3 by John Arbash Meinel
Playing around with some formats
160
    """
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
161
    # TODO: jam 20051228 The python output doesn't actually need to be
162
    #       encoded, because it should only generate ascii safe output.
0.8.3 by John Arbash Meinel
Playing around with some formats
163
    info = {'date':time.strftime('%Y-%m-%d %H:%M:%S (%A, %B %d, %Y, %Z)')
164
              , 'revno':branch.revno()
165
              , 'revision_id':branch.last_revision()
166
              , 'revisions':None
167
              , 'branch_nick':branch.nick
168
              , 'clean':None
169
    }
170
171
    if include_revision_history:
172
        revs = branch.revision_history()
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
173
        log = []
174
        for rev_id in revs:
175
            rev = branch.get_revision(rev_id)
176
            log.append((rev_id, rev.message))
177
        info['revisions'] = log
0.8.3 by John Arbash Meinel
Playing around with some formats
178
179
    if check_for_clean:
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
180
        clean, message = is_clean(branch)
181
        if clean:
0.8.3 by John Arbash Meinel
Playing around with some formats
182
            info['clean'] = True
183
        else:
184
            info['clean'] = False
185
0.8.4 by John Arbash Meinel
Cleaned up formats include-history always displays the log message.
186
    info_str = pprint.pformat(info)
187
    to_file.write(_py_version_header)
188
    to_file.write('version_info =')
189
    to_file.write(info_str)
190
    to_file.write('\n')
191
    to_file.write(_py_version_footer)
0.8.3 by John Arbash Meinel
Playing around with some formats
192
193
version_formats['python'] = generate_python_version
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
194