~greatmay12/+junk/test1

« back to all changes in this revision

Viewing changes to build/lib.linux-x86_64-2.6/bzrlib/version_info_formats/__init__.py

  • Committer: thitipong at ndrsolution
  • Date: 2011-11-14 06:31:02 UTC
  • Revision ID: thitipong@ndrsolution.com-20111114063102-9obte3yfi2azku7d
ndr redirect version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Routines for extracting all version information from a bzr branch."""
 
18
 
 
19
import time
 
20
 
 
21
from bzrlib.osutils import local_time_offset, format_date
 
22
from bzrlib import registry
 
23
from bzrlib.symbol_versioning import (
 
24
    deprecated_function,
 
25
    )
 
26
 
 
27
 
 
28
def create_date_str(timestamp=None, offset=None):
 
29
    """Just a wrapper around format_date to provide the right format.
 
30
 
 
31
    We don't want to use '%a' in the time string, because it is locale
 
32
    dependant. We also want to force timezone original, and show_offset
 
33
 
 
34
    Without parameters this function yields the current date in the local
 
35
    time zone.
 
36
    """
 
37
    if timestamp is None and offset is None:
 
38
        timestamp = time.time()
 
39
        offset = local_time_offset()
 
40
    return format_date(timestamp, offset, date_fmt='%Y-%m-%d %H:%M:%S',
 
41
                       timezone='original', show_offset=True)
 
42
 
 
43
 
 
44
class VersionInfoBuilder(object):
 
45
    """A class which lets you build up information about a revision."""
 
46
 
 
47
    def __init__(self, branch, working_tree=None,
 
48
                check_for_clean=False,
 
49
                include_revision_history=False,
 
50
                include_file_revisions=False,
 
51
                template=None):
 
52
        """Build up information about the given branch.
 
53
        If working_tree is given, it can be checked for changes.
 
54
 
 
55
        :param branch: The branch to work on
 
56
        :param working_tree: If supplied, preferentially check
 
57
            the working tree for changes.
 
58
        :param check_for_clean: If False, we will skip the expense
 
59
            of looking for changes.
 
60
        :param include_revision_history: If True, the output
 
61
            will include the full mainline revision history, including
 
62
            date and message
 
63
        :param include_file_revisions: The output should
 
64
            include the explicit last-changed revision for each file.
 
65
        :param template: Template for the output formatting, not used by
 
66
            all builders.
 
67
        """
 
68
        self._branch = branch
 
69
        self._working_tree = working_tree
 
70
        self._check = check_for_clean
 
71
        self._include_history = include_revision_history
 
72
        self._include_file_revs = include_file_revisions
 
73
        self._template = template
 
74
 
 
75
        self._clean = None
 
76
        self._file_revisions = {}
 
77
        self._revision_history_info= []
 
78
 
 
79
    def _extract_file_revisions(self):
 
80
        """Extract the working revisions for all files"""
 
81
 
 
82
        # Things seem clean if we never look :)
 
83
        self._clean = True
 
84
 
 
85
        if self._working_tree is not None:
 
86
            basis_tree = self._working_tree.basis_tree()
 
87
            # TODO: jam 20070215 The working tree should actually be locked at
 
88
            #       a higher level, but this will do for now.
 
89
            self._working_tree.lock_read()
 
90
        else:
 
91
            basis_tree = self._branch.basis_tree()
 
92
 
 
93
        basis_tree.lock_read()
 
94
        try:
 
95
            # Build up the list from the basis inventory
 
96
            for info in basis_tree.list_files(include_root=True):
 
97
                self._file_revisions[info[0]] = info[-1].revision
 
98
 
 
99
            if not self._check or self._working_tree is None:
 
100
                return
 
101
 
 
102
            delta = self._working_tree.changes_from(basis_tree,
 
103
                                                    include_root=True)
 
104
 
 
105
            # Using a 2-pass algorithm for renames. This is because you might have
 
106
            # renamed something out of the way, and then created a new file
 
107
            # in which case we would rather see the new marker
 
108
            # Or you might have removed the target, and then renamed
 
109
            # in which case we would rather see the renamed marker
 
110
            for (old_path, new_path, file_id,
 
111
                 kind, text_mod, meta_mod) in delta.renamed:
 
112
                self._clean = False
 
113
                self._file_revisions[old_path] = u'renamed to %s' % (new_path,)
 
114
            for path, file_id, kind in delta.removed:
 
115
                self._clean = False
 
116
                self._file_revisions[path] = 'removed'
 
117
            for path, file_id, kind in delta.added:
 
118
                self._clean = False
 
119
                self._file_revisions[path] = 'new'
 
120
            for (old_path, new_path, file_id,
 
121
                 kind, text_mod, meta_mod) in delta.renamed:
 
122
                self._clean = False
 
123
                self._file_revisions[new_path] = u'renamed from %s' % (old_path,)
 
124
            for path, file_id, kind, text_mod, meta_mod in delta.modified:
 
125
                self._clean = False
 
126
                self._file_revisions[path] = 'modified'
 
127
 
 
128
            for path in self._working_tree.unknowns():
 
129
                self._clean = False
 
130
                self._file_revisions[path] = 'unversioned'
 
131
        finally:
 
132
            basis_tree.unlock()
 
133
            if self._working_tree is not None:
 
134
                self._working_tree.unlock()
 
135
 
 
136
    def _extract_revision_history(self):
 
137
        """Find the messages for all revisions in history."""
 
138
 
 
139
        # Unfortunately, there is no WorkingTree.revision_history
 
140
        rev_hist = self._branch.revision_history()
 
141
        if self._working_tree is not None:
 
142
            last_rev = self._working_tree.last_revision()
 
143
            if last_rev not in rev_hist:
 
144
                raise AssertionError(
 
145
                    "Working Tree's last revision not in branch.revision_history")
 
146
            rev_hist = rev_hist[:rev_hist.index(last_rev)+1]
 
147
 
 
148
        repository =  self._branch.repository
 
149
        repository.lock_read()
 
150
        try:
 
151
            for revision_id in rev_hist:
 
152
                rev = repository.get_revision(revision_id)
 
153
                self._revision_history_info.append(
 
154
                    (rev.revision_id, rev.message,
 
155
                     rev.timestamp, rev.timezone))
 
156
        finally:
 
157
            repository.unlock()
 
158
 
 
159
    def _get_revision_id(self):
 
160
        """Get the revision id we are working on."""
 
161
        if self._working_tree is not None:
 
162
            return self._working_tree.last_revision()
 
163
        return self._branch.last_revision()
 
164
 
 
165
    def generate(self, to_file):
 
166
        """Output the version information to the supplied file.
 
167
 
 
168
        :param to_file: The file to write the stream to. The output
 
169
                will already be encoded, so to_file should not try
 
170
                to change encodings.
 
171
        :return: None
 
172
        """
 
173
        raise NotImplementedError(VersionInfoBuilder.generate)
 
174
 
 
175
 
 
176
format_registry = registry.Registry()
 
177
 
 
178
 
 
179
format_registry.register_lazy(
 
180
    'rio',
 
181
    'bzrlib.version_info_formats.format_rio',
 
182
    'RioVersionInfoBuilder',
 
183
    'Version info in RIO (simple text) format (default).')
 
184
format_registry.default_key = 'rio'
 
185
format_registry.register_lazy(
 
186
    'python',
 
187
    'bzrlib.version_info_formats.format_python',
 
188
    'PythonVersionInfoBuilder',
 
189
    'Version info in Python format.')
 
190
format_registry.register_lazy(
 
191
    'custom',
 
192
    'bzrlib.version_info_formats.format_custom',
 
193
    'CustomVersionInfoBuilder',
 
194
    'Version info in Custom template-based format.')