~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: John Arbash Meinel
  • Date: 2005-07-19 22:13:01 UTC
  • mto: (1185.11.1)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050719221300-9a7b9bc9d866a9b9
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown.
Added readonly tests for Transports.
Added test for HttpTransport.

Show diffs side-by-side

added added

removed removed

Lines of Context:
287
287
        relpath = self._rel_controlfilename(file_or_path)
288
288
        #TODO: codecs.open() buffers linewise, so it was overloaded with
289
289
        # a much larger buffer, do we need to do the same for getreader/getwriter?
290
 
 
291
 
        # TODO: Try to use transport.put() rather than branch.controlfile(mode='w')
292
290
        if mode == 'rb': 
293
291
            return self._transport.get(relpath)
294
292
        elif mode == 'wb':
295
 
            raise BzrError("Branch.controlfile(mode='wb') is not supported, use put_controlfile")
 
293
            raise BzrError("Branch.controlfile(mode='wb') is not supported, use put_controlfiles")
296
294
        elif mode == 'r':
297
 
            return self._transport.get(relpath, decode=True)
 
295
            return codecs.getreader('utf-8')(self._transport.get(relpath), errors='replace')
298
296
        elif mode == 'w':
299
 
            raise BzrError("Branch.controlfile(mode='w') is not supported, use put_controlfile")
300
 
            #return codecs.getwriter('utf-8')(
301
 
            #        self._transport.open(relpath), errors='replace')
 
297
            raise BzrError("Branch.controlfile(mode='w') is not supported, use put_controlfiles")
302
298
        else:
303
299
            raise BzrError("invalid controlfile mode %r" % mode)
304
300
 
305
 
    def put_controlfile(self, file_or_path, f, encode=True):
 
301
    def put_controlfile(self, path, f, encode=True):
306
302
        """Write an entry as a controlfile.
307
303
 
308
 
        :param file_or_path: This is the sub-path underneath the bzr control directory
309
 
        :param f: A file-like or string object whose contents should be placed
310
 
                  in the appropriate location.
311
 
        :param encode:  If true, encode the contents as utf-8
312
 
        """
313
 
        self._transport.put(self._rel_controlfilename(file_or_path), f, encode=encode)
 
304
        :param path: The path to put the file, relative to the .bzr control
 
305
                     directory
 
306
        :param f: A file-like or string object whose contents should be copied.
 
307
        :param encode:  If true, encode the contents as utf-8
 
308
        """
 
309
        self.put_controlfiles([(path, f)], encode=encode)
 
310
 
 
311
    def put_controlfiles(self, files, encode=True):
 
312
        """Write several entries as controlfiles.
 
313
 
 
314
        :param files: A list of [(path, file)] pairs, where the path is the directory
 
315
                      underneath the bzr control directory
 
316
        :param encode:  If true, encode the contents as utf-8
 
317
        """
 
318
        import codecs
 
319
        ctrl_files = []
 
320
        for path, f in files:
 
321
            if encode:
 
322
                if isinstance(f, basestring):
 
323
                    f = f.encode('utf-8', 'replace')
 
324
                else:
 
325
                    f = codecs.getwriter('utf-8')(f, errors='replace')
 
326
            path = self._rel_controlfilename(path)
 
327
            ctrl_files.append((path, f))
 
328
        self._transport.put_multi(ctrl_files)
314
329
 
315
330
    def _make_control(self):
316
331
        from bzrlib.inventory import Inventory
335
350
            ('inventory', sio.getvalue())
336
351
        ]
337
352
        self._transport.mkdir_multi([self._rel_controlfilename(d) for d in dirs])
338
 
        self._transport.put_multi([(self._rel_controlfilename(f[0]),f[1]) for f in files])
 
353
        self.put_controlfiles(files)
339
354
        mutter('created control directory in ' + self._transport.base)
340
355
 
341
356
    def _check_format(self):
417
432
            sio = StringIO()
418
433
            pack_xml(inv, sio)
419
434
            sio.seek(0)
420
 
            self._transport.put(self._rel_controlfilename('inventory'), sio)
 
435
            self.put_controlfile('inventory', sio)
421
436
        finally:
422
437
            self.unlock()
423
438
        
605
620
 
606
621
        self.lock_write()
607
622
        try:
608
 
            self._transport.put(self._rel_controlfilename('revision-history'),
609
 
                    '\n'.join(rev_history))
 
623
            self.put_controlfile('revision-history', '\n'.join(rev_history))
610
624
        finally:
611
625
            self.unlock()
612
626
 
1315
1329
    def set_pending_merges(self, rev_list):
1316
1330
        self.lock_write()
1317
1331
        try:
1318
 
            self._transport.put(self._rel_controlfilename('pending-merges'),
1319
 
                    '\n'.join(rev_list))
 
1332
            self.put_controlfile('pending-merges', '\n'.join(rev_list))
1320
1333
        finally:
1321
1334
            self.unlock()
1322
1335