~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/ftp.py

Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
130
130
            connection = ftplib.FTP()
131
131
            connection.connect(host=self._host, port=self._port)
132
132
            if self._user and self._user != 'anonymous' and \
133
 
                    password is not None: # '' is a valid password
 
133
                    password is None: # '' is a valid password
134
134
                get_password = bzrlib.ui.ui_factory.get_password
135
135
                password = get_password(prompt='FTP %(user)s@%(host)s password',
136
136
                                        user=self._user, host=self._host)
271
271
        abspath = self._remote_path(relpath)
272
272
        tmp_abspath = '%s.tmp.%.9f.%d.%d' % (abspath, time.time(),
273
273
                        os.getpid(), random.randint(0,0x7FFFFFFF))
 
274
        bytes = None
274
275
        if getattr(fp, 'read', None) is None:
275
 
            fp = StringIO(fp)
 
276
            # hand in a string IO
 
277
            bytes = fp
 
278
            fp = StringIO(bytes)
 
279
        else:
 
280
            # capture the byte count; .read() may be read only so
 
281
            # decorate it.
 
282
            class byte_counter(object):
 
283
                def __init__(self, fp):
 
284
                    self.fp = fp
 
285
                    self.counted_bytes = 0
 
286
                def read(self, count):
 
287
                    result = self.fp.read(count)
 
288
                    self.counted_bytes += len(result)
 
289
                    return result
 
290
            fp = byte_counter(fp)
276
291
        try:
277
292
            mutter("FTP put: %s", abspath)
278
293
            f = self._get_FTP()
279
294
            try:
280
295
                f.storbinary('STOR '+tmp_abspath, fp)
281
296
                self._rename_and_overwrite(tmp_abspath, abspath, f)
 
297
                if bytes is not None:
 
298
                    return len(bytes)
 
299
                else:
 
300
                    return fp.counted_bytes
282
301
            except (ftplib.error_temp,EOFError), e:
283
302
                warning("Failure during ftp PUT. Deleting temporary file.")
284
303
                try:
586
605
 
587
606
    def tearDown(self):
588
607
        """See bzrlib.transport.Server.tearDown."""
589
 
        # have asyncore release the channel
590
 
        self._ftp_server.del_channel()
 
608
        self._ftp_server.close()
591
609
        asyncore.close_all()
592
610
        self._async_thread.join()
593
611
 
635
653
 
636
654
        def __init__(self, root):
637
655
            self.root = root
 
656
            # If secured_user is set secured_password will be checked
 
657
            self.secured_user = None
 
658
            self.secured_password = None
638
659
 
639
660
        def authorize(self, channel, username, password):
640
661
            """Return (success, reply_string, filesystem)"""
647
668
            else:
648
669
                channel.read_only = 0
649
670
 
650
 
            return 1, 'OK.', medusa.filesys.os_filesystem(self.root)
 
671
            # Check secured_user if set
 
672
            if (self.secured_user is not None
 
673
                and username == self.secured_user
 
674
                and password != self.secured_password):
 
675
                return 0, 'Password invalid.', None
 
676
            else:
 
677
                return 1, 'OK.', medusa.filesys.os_filesystem(self.root)
651
678
 
652
679
 
653
680
    class ftp_channel(medusa.ftp_server.ftp_channel):