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