~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/testament.py

  • Committer: John Arbash Meinel
  • Date: 2005-11-23 15:44:24 UTC
  • mto: (1185.50.19 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: john@arbash-meinel.com-20051123154424-a02f8bf990a1fed5
Renamed all of the tests from selftest/foo.py to tests/test_foo.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
70
70
# revisions can be serialized.
71
71
 
72
72
from copy import copy
 
73
from cStringIO import StringIO
 
74
import string
73
75
from sha import sha
74
76
 
75
77
from bzrlib.osutils import contains_whitespace, contains_linebreaks
76
78
 
77
 
 
78
79
class Testament(object):
79
80
    """Reduced summary of a revision.
80
81
 
86
87
      - compared to a revision
87
88
    """
88
89
 
89
 
    long_header = 'bazaar-ng testament version 1\n'
90
 
    short_header = 'bazaar-ng testament short form 1\n'
91
 
 
92
90
    @classmethod
93
 
    def from_revision(cls, repository, revision_id):
 
91
    def from_revision(cls, branch, revision_id):
94
92
        """Produce a new testament from a historical revision"""
95
 
        rev = repository.get_revision(revision_id)
96
 
        inventory = repository.get_inventory(revision_id)
 
93
        rev = branch.get_revision(revision_id)
 
94
        inventory = branch.get_inventory(revision_id)
97
95
        return cls(rev, inventory)
98
96
 
99
97
    def __init__(self, rev, inventory):
100
98
        """Create a new testament for rev using inventory."""
101
 
        self.revision_id = rev.revision_id
 
99
        self.revision_id = str(rev.revision_id)
102
100
        self.committer = rev.committer
103
101
        self.timezone = rev.timezone or 0
104
102
        self.timestamp = rev.timestamp
106
104
        self.parent_ids = rev.parent_ids[:]
107
105
        self.inventory = inventory
108
106
        self.revprops = copy(rev.properties)
109
 
        if contains_whitespace(self.revision_id):
110
 
            raise ValueError(self.revision_id)
111
 
        if contains_linebreaks(self.committer):
112
 
            raise ValueError(self.committer)
 
107
        assert not contains_whitespace(self.revision_id)
 
108
        assert not contains_linebreaks(self.committer)
113
109
 
114
110
    def as_text_lines(self):
115
111
        """Yield text form as a sequence of lines.
118
114
        hashed in that encoding.
119
115
        """
120
116
        r = []
121
 
        a = r.append
122
 
        a(self.long_header)
 
117
        def a(s):
 
118
            r.append(s)
 
119
        a('bazaar-ng testament version 1\n')
123
120
        a('revision-id: %s\n' % self.revision_id)
124
121
        a('committer: %s\n' % self.committer)
125
122
        a('timestamp: %d\n' % self.timestamp)
127
124
        # inventory length contains the root, which is not shown here
128
125
        a('parents:\n')
129
126
        for parent_id in sorted(self.parent_ids):
130
 
            if contains_whitespace(parent_id):
131
 
                raise ValueError(parent_id)
 
127
            assert not contains_whitespace(parent_id)
132
128
            a('  %s\n' % parent_id)
133
129
        a('message:\n')
134
130
        for l in self.message.splitlines():
135
131
            a('  %s\n' % l)
136
132
        a('inventory:\n')
137
 
        for path, ie in self._get_entries():
 
133
        for path, ie in self.inventory.iter_entries():
138
134
            a(self._entry_to_line(path, ie))
139
135
        r.extend(self._revprops_to_lines())
140
 
        return [line.encode('utf-8') for line in r]
141
 
 
142
 
    def _get_entries(self):
143
 
        entries = self.inventory.iter_entries()
144
 
        entries.next()
145
 
        return entries
 
136
        if __debug__:
 
137
            for l in r:
 
138
                assert isinstance(l, basestring), \
 
139
                    '%r of type %s is not a plain string' % (l, type(l))
 
140
        return r
146
141
 
147
142
    def _escape_path(self, path):
148
 
        if contains_linebreaks(path):
149
 
            raise ValueError(path)
150
 
        return unicode(path.replace('\\', '/').replace(' ', '\ '))
 
143
        assert not contains_linebreaks(path)
 
144
        return unicode(path.replace('\\', '/').replace(' ', '\ ')).encode('utf-8')
151
145
 
152
146
    def _entry_to_line(self, path, ie):
153
147
        """Turn an inventory entry into a testament line"""
154
 
        if contains_whitespace(ie.file_id):
155
 
            raise ValueError(ie.file_id)
156
 
        content = ''
157
 
        content_spacer=''
 
148
        l = '  ' + str(ie.kind)
 
149
        l += ' ' + self._escape_path(path)
 
150
        assert not contains_whitespace(ie.file_id)
 
151
        l += ' ' + unicode(ie.file_id).encode('utf-8')
158
152
        if ie.kind == 'file':
159
153
            # TODO: avoid switching on kind
160
 
            if not ie.text_sha1:
161
 
                raise AssertionError()
162
 
            content = ie.text_sha1
163
 
            content_spacer = ' '
 
154
            assert ie.text_sha1
 
155
            l += ' ' + ie.text_sha1
164
156
        elif ie.kind == 'symlink':
165
 
            if not ie.symlink_target:
166
 
                raise AssertionError()
167
 
            content = self._escape_path(ie.symlink_target)
168
 
            content_spacer = ' '
169
 
 
170
 
        l = u'  %s %s %s%s%s\n' % (ie.kind, self._escape_path(path),
171
 
                                   ie.file_id.decode('utf8'),
172
 
                                   content_spacer, content)
 
157
            assert ie.symlink_target
 
158
            l += ' ' + self._escape_path(ie.symlink_target)
 
159
        l += '\n'
173
160
        return l
174
161
 
175
162
    def as_text(self):
177
164
 
178
165
    def as_short_text(self):
179
166
        """Return short digest-based testament."""
180
 
        return (self.short_header + 
 
167
        s = sha()
 
168
        map(s.update, self.as_text_lines())
 
169
        return ('bazaar-ng testament short form 1\n'
181
170
                'revision-id: %s\n'
182
171
                'sha1: %s\n'
183
 
                % (self.revision_id, self.as_sha1()))
 
172
                % (self.revision_id, s.hexdigest()))
184
173
 
185
174
    def _revprops_to_lines(self):
186
175
        """Pack up revision properties."""
188
177
            return []
189
178
        r = ['properties:\n']
190
179
        for name, value in sorted(self.revprops.items()):
191
 
            if contains_whitespace(name):
192
 
                raise ValueError(name)
 
180
            assert isinstance(name, str)
 
181
            assert not contains_whitespace(name)
193
182
            r.append('  %s:\n' % name)
194
183
            for line in value.splitlines():
195
 
                r.append(u'    %s\n' % line)
 
184
                if not isinstance(line, str):
 
185
                    line = line.encode('utf-8')
 
186
                r.append('    %s\n' % line)
196
187
        return r
197
 
 
198
 
    def as_sha1(self):
199
 
        s = sha()
200
 
        map(s.update, self.as_text_lines())
201
 
        return s.hexdigest()
202
 
 
203
 
 
204
 
class StrictTestament(Testament):
205
 
    """This testament format is for use as a checksum in bundle format 0.8"""
206
 
 
207
 
    long_header = 'bazaar-ng testament version 2.1\n'
208
 
    short_header = 'bazaar-ng testament short form 2.1\n'
209
 
    def _entry_to_line(self, path, ie):
210
 
        l = Testament._entry_to_line(self, path, ie)[:-1]
211
 
        l += ' ' + ie.revision
212
 
        l += {True: ' yes\n', False: ' no\n'}[ie.executable]
213
 
        return l
214
 
 
215
 
 
216
 
class StrictTestament3(StrictTestament):
217
 
    """This testament format is for use as a checksum in bundle format 0.9+
218
 
    
219
 
    It differs from StrictTestament by including data about the tree root.
220
 
    """
221
 
 
222
 
    long_header = 'bazaar testament version 3 strict\n'
223
 
    short_header = 'bazaar testament short form 3 strict\n'
224
 
    def _get_entries(self):
225
 
        return self.inventory.iter_entries()
226
 
 
227
 
    def _escape_path(self, path):
228
 
        if contains_linebreaks(path):
229
 
            raise ValueError(path)
230
 
        if path == '':
231
 
            path = '.'
232
 
        return unicode(path.replace('\\', '/').replace(' ', '\ '))