Changeset 199

Show
Ignore:
Timestamp:
2002-10-22 20:58:17 (6 years ago)
Author:
schwa
Message:
FTPHost.upload_if_newer/download_if_newer: Return a flag telling whether
    an upload or download, respectively, was necessary. If yes, return
    `True` else return `False`.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/_test_ftputil.py

    r184 r199  
    3030# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    3131 
    32 # $Id: _test_ftputil.py,v 1.60 2002/04/03 21:50:24 schwa Exp $ 
     32# $Id: _test_ftputil.py,v 1.61 2002/10/22 20:58:17 schwa Exp $ 
    3333 
    3434import unittest 
     
    157157        except ftputil.RootDirError, exc_obj: 
    158158            self.failIf( isinstance(exc_obj, ftputil.FTPOSError) ) 
    159          
     159 
    160160    def test_lstat_one_file(self): 
    161161        """Test lstat for a file.""" 
     
    179179        # The comparison with the value 937785600.0 may fail in 
    180180        #  some Python environments. It seems that this depends on 
    181         #  how time.mktime interprets the dst flag. 
    182         self.assertEqual(stat_result.st_mtime, 937785600.0) 
     181        #  how `time.mktime` interprets the dst flag. 
     182        self.failUnless(stat_result.st_mtime == 937785600.0 or 
     183                        stat_result.st_mtime == 937778400.0) 
    183184        self.assertEqual(stat_result.st_ctime, None) 
    184         self.assertEqual( stat_result, (17901, None, None, 6, '45854', '200', 
    185                                         512, None, 937785600.0, None) ) 
     185        # same here (or similarly) 
     186        self.failUnless( stat_result == (17901, None, None, 6, '45854', '200', 
     187                                         512, None, 937785600.0, None) or 
     188                         stat_result == (17901, None, None, 6, '45854', '200', 
     189                                         512, None, 937778400.0, None) ) 
    186190 
    187191    def test_lstat_via_stat_module(self): 
     
    479483        host = ftp_host_factory( 
    480484               ftp_host_class=FailingUploadAndDownloadFTPHost) 
    481         host.upload_if_newer(local_source, '/home/newer') 
     485        flag = host.upload_if_newer(local_source, '/home/newer') 
     486        self.assertEqual(flag, False) 
    482487        # target is older, so upload 
    483488        host = ftp_host_factory() 
    484         host.upload_if_newer(local_source, '/home/older') 
     489        flag = host.upload_if_newer(local_source, '/home/older') 
     490        self.assertEqual(flag, True) 
    485491        # check uploaded content 
    486492        # the data which was uploaded has its line endings converted 
     
    491497        # target doesn't exist, so upload 
    492498        host = ftp_host_factory() 
    493         host.upload_if_newer(local_source, '/home/notthere') 
     499        flag = host.upload_if_newer(local_source, '/home/notthere') 
     500        self.assertEqual(flag, True) 
    494501        remote_file_content = _mock_ftplib.content_of('/home/notthere') 
    495502        self.assertEqual(data, remote_file_content) 
     
    511518        # target does not exist, so download 
    512519        host = ftp_host_factory(session_factory=BinaryDownloadMockSession) 
    513         host.download_if_newer('/home/newer', local_target, 'b') 
     520        flag = host.download_if_newer('/home/newer', local_target, 'b') 
     521        self.assertEqual(flag, True) 
    514522        self.compare_and_delete_downloaded_data(local_target) 
    515523 
     
    521529        # source is newer, so download 
    522530        host = ftp_host_factory(session_factory=BinaryDownloadMockSession) 
    523         host.download_if_newer('/home/newer', local_target, 'b') 
     531        flag = host.download_if_newer('/home/newer', local_target, 'b') 
     532        self.assertEqual(flag, True) 
    524533        self.compare_and_delete_downloaded_data(local_target) 
    525534 
     
    534543               ftp_host_class=FailingUploadAndDownloadFTPHost, 
    535544               session_factory=BinaryDownloadMockSession) 
    536         host.download_if_newer('/home/older', local_target, 'b') 
     545        flag = host.download_if_newer('/home/older', local_target, 'b') 
     546        self.assertEqual(flag, False) 
    537547        # remove target file 
    538548        os.unlink(local_target) 
  • trunk/ftputil.py

    r198 r199  
    3030# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    3131 
    32 # $Id: ftputil.py,v 1.94 2002/05/29 11:50:17 schwa Exp $ 
     32# $Id: ftputil.py,v 1.95 2002/10/22 20:58:17 schwa Exp $ 
    3333 
    3434""" 
     
    102102           'PermanentError', 'ParserError', 'FTPIOError', 
    103103           'RootDirError', 'FTPHost'] 
    104 __version__ = '1.1.1' 
     104__version__ = '1.1.4' 
     105 
     106 
     107# define `True` and `False` if necessary 
     108try: 
     109    True, False 
     110except NameError: 
     111    True, False = 1 == 1, 1 == 0 
    105112 
    106113 
     
    508515        Upload a file only if it's newer than the target on the 
    509516        remote host or if the target file does not exist. 
     517 
     518        If an upload was necessary, return `True`, else return 
     519        `False`. 
    510520        """ 
    511521        source_timestamp = os.path.getmtime(source) 
     
    517527        if source_timestamp > target_timestamp: 
    518528            self.upload(source, target, mode) 
     529            return True 
     530        else: 
     531            return False 
    519532 
    520533    def download_if_newer(self, source, target, mode=''): 
     
    522535        Download a file only if it's newer than the target on the 
    523536        local host or if the target file does not exist. 
     537 
     538        If a download was necessary, return `True`, else return 
     539        `False`. 
    524540        """ 
    525541        source_timestamp = self.path.getmtime(source) 
     
    531547        if source_timestamp > target_timestamp: 
    532548            self.download(source, target, mode) 
     549            return True 
     550        else: 
     551            return False 
    533552 
    534553    def close(self):