/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to commands.py

  • Committer: Ian Clatworthy
  • Date: 2009-02-16 14:50:34 UTC
  • mto: (0.64.116 trunk)
  • mto: This revision was merged to the branch mainline in revision 6631.
  • Revision ID: ian.clatworthy@canonical.com-20090216145034-yw1htkfblxp1uh67
code & tests for formatting Commands as file-import stream strings

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
            self.id = ':' + mark
77
77
        self._binary = ['data']
78
78
 
 
79
    def __repr__(self):
 
80
        if self.mark is None:
 
81
            mark_line = ""
 
82
        else:
 
83
            mark_line = "\nmark :%s" % self.mark
 
84
        return "blob%s\ndata %d\n%s" % (mark_line, len(self.data), self.data)
 
85
 
79
86
 
80
87
class CheckpointCommand(ImportCommand):
81
88
 
82
89
    def __init__(self):
83
90
        ImportCommand.__init__(self, 'checkpoint')
84
91
 
 
92
    def __repr__(self):
 
93
        return "checkpoint"
 
94
 
85
95
 
86
96
class CommitCommand(ImportCommand):
87
97
 
104
114
        else:
105
115
            self.id = ':' + mark
106
116
 
 
117
    def __repr__(self):
 
118
        if self.mark is None:
 
119
            mark_line = ""
 
120
        else:
 
121
            mark_line = "\nmark :%s" % self.mark
 
122
        if self.author is None:
 
123
            author_line = ""
 
124
        else:
 
125
            author_line = "\nauthor %s" % _format_who_when(self.author)
 
126
        committer = "committer %s" % _format_who_when(self.committer)
 
127
        if self.message is None:
 
128
            msg = ""
 
129
        else:
 
130
            msg = "\ndata %d\n%s" % (len(self.message), self.message)
 
131
        if self.from_ is None:
 
132
            from_line = ""
 
133
        else:
 
134
            from_line = "\nfrom :%s" % self.from_
 
135
        if self.merges is None:
 
136
            merge_lines = ""
 
137
        else:
 
138
            merge_lines = "\n" + "\n".join(["merge :%s" % m
 
139
                for m in self.merges])
 
140
        if self.file_iter is None:
 
141
            filecommands = ""
 
142
        else:
 
143
            filecommands = "\n" + "\n".join([repr(c)
 
144
                for c in self.file_iter])
 
145
        return "commit %s%s%s\n%s%s%s%s%s" % (self.ref, mark_line, author_line,
 
146
            committer, msg, from_line, merge_lines, filecommands)
 
147
 
107
148
    def dump_str(self, names=None, child_lists=None, verbose=False):
108
149
        result = [ImportCommand.dump_str(self, names, verbose=verbose)]
109
150
        for f in self.file_iter():
123
164
        ImportCommand.__init__(self, 'progress')
124
165
        self.message = message
125
166
 
 
167
    def __repr__(self):
 
168
        return "progress %s" % (self.message,)
 
169
 
126
170
 
127
171
class ResetCommand(ImportCommand):
128
172
 
131
175
        self.ref = ref
132
176
        self.from_ = from_
133
177
 
 
178
    def __repr__(self):
 
179
        if self.from_ is None:
 
180
            from_line = ""
 
181
        else:
 
182
            from_line = "\nfrom :%s" % self.from_
 
183
        return "reset %s%s" % (self.ref, from_line)
 
184
 
134
185
 
135
186
class TagCommand(ImportCommand):
136
187
 
141
192
        self.tagger = tagger
142
193
        self.message = message
143
194
 
 
195
    def __repr__(self):
 
196
        if self.from_ is None:
 
197
            from_line = ""
 
198
        else:
 
199
            from_line = "\nfrom :%s" % self.from_
 
200
        if self.tagger is None:
 
201
            tagger_line = ""
 
202
        else:
 
203
            tagger_line = "\ntagger %s" % _format_who_when(self.tagger)
 
204
        if self.message is None:
 
205
            msg = ""
 
206
        else:
 
207
            msg = "\ndata %d\n%s" % (len(self.message), self.message)
 
208
        return "tag %s%s%s%s" % (self.id, from_line, tagger_line, msg)
 
209
 
144
210
 
145
211
class FileCommand(ImportCommand):
146
212
    """Base class for file commands."""
159
225
        self.data = data
160
226
        self._binary = ['data']
161
227
 
 
228
    def __repr__(self):
 
229
        if self.kind == 'symlink':
 
230
            mode = "120000"
 
231
        elif self.is_executable:
 
232
            mode = "755"
 
233
        else:
 
234
            mode = "644"
 
235
        if self.dataref is None:
 
236
            dataref = "inline"
 
237
            datastr = "\ndata %d\n%s" % (len(self.data), self.data)
 
238
        else:
 
239
            dataref = ":%s" % (self.dataref,)
 
240
            datastr = ""
 
241
        return "M %s %s %s%s" % (mode, dataref, self.path, datastr)
 
242
 
162
243
 
163
244
class FileDeleteCommand(FileCommand):
164
245
 
166
247
        FileCommand.__init__(self, 'filedelete')
167
248
        self.path = path
168
249
 
 
250
    def __repr__(self):
 
251
        return "D %s" % (self.path,)
 
252
 
169
253
 
170
254
class FileCopyCommand(FileCommand):
171
255
 
174
258
        self.src_path = src_path
175
259
        self.dest_path = dest_path
176
260
 
 
261
    def __repr__(self):
 
262
        return "C %s %s" % (_quote_path(self.src_path), self.dest_path)
 
263
 
177
264
 
178
265
class FileRenameCommand(FileCommand):
179
266
 
182
269
        self.old_path = old_path
183
270
        self.new_path = new_path
184
271
 
 
272
    def __repr__(self):
 
273
        return "R %s %s" % (_quote_path(self.old_path), self.new_path)
 
274
 
185
275
 
186
276
class FileDeleteAllCommand(FileCommand):
187
277
 
188
278
    def __init__(self):
189
279
        FileCommand.__init__(self, 'filedeleteall')
 
280
 
 
281
    def __repr__(self):
 
282
        return "deleteall"
 
283
 
 
284
 
 
285
def _quote_path(s):
 
286
    """Surround a path with quotes if it contains spaces."""
 
287
    if ' ' in s:
 
288
        return '"%s"' % (s,)
 
289
    else:
 
290
        return s
 
291
 
 
292
 
 
293
def _format_who_when(fields):
 
294
    """Format a tuple of name,email,secs-since-epoch,utc-offset-secs as a string."""
 
295
    offset = fields[3]
 
296
    if offset < 0:
 
297
        offset_sign = '-'
 
298
        offset = abs(offset)
 
299
    else:
 
300
        offset_sign = '+'
 
301
    offset_hours = offset / 3600
 
302
    offset_minutes = offset / 60 - offset_hours * 60
 
303
    offset_str = "%s%02d%02d" % (offset_sign, offset_hours, offset_minutes)
 
304
    return "%s <%s> %d %s" % (fields[0], fields[1], fields[2], offset_str)