def upload_file(self, remote_file_path, local_file_path='', file_string=''): """ Upload `local_file_path` or the contents of `file_string` to `remote_file_path`. Either `file_string` or `local_file_path` must be given. """ if local_file_path and not os.path.isfile(local_file_path): raise InputError('Cannot upload a non-existing file.' ' Check why file in path {0} is missing.'.format( local_file_path)) sftp, ssh = self.connect() times_tried = 0 max_times_to_try = 10 success = False while not success and times_tried < max_times_to_try: times_tried += 1 try: write_file(sftp, remote_file_path, local_file_path, file_string) except IOError: pass else: success = True if times_tried == max_times_to_try: raise ServerError('Could not write file {0} on {1}'.format( remote_file_path, self.server)) sftp.close() ssh.close()
def connect(self): """A helper function for calling self.try_connecting until successful""" times_tried = 0 max_times_to_try = 1440 # continue trying for 24 hrs... interval = 60 # wait 60 sec between trials while times_tried < max_times_to_try: times_tried += 1 try: sftp, ssh = self.try_connecting() except: pass else: logging.debug( 'Successfully connected to {0} at the {1} trial.'.format( self.server, times_tried)) return sftp, ssh if not times_tried % 10: logging.info( 'Tried connecting to {0} {1} times with no success....'. format(self.server, times_tried)) else: print('Tried connecting to {0} {1} times with no success....'. format(self.server, times_tried)) time.sleep(interval) raise ServerError( 'Could not connect to server {0} even after {1} trials.'.format( self.server, times_tried))
def download_file(self, remote_file_path, local_file_path): """ Download a file from `remote_file_path` to `local_file_path`. """ i, max_times_to_try = 1, 30 success = False sleep_time = 10 # seconds while i < 30: self._download_file(remote_file_path, local_file_path) if os.path.isfile(local_file_path): success = True i = 1000 else: logger.error('Could not download file {0} from {1}!'.format( remote_file_path, self.server)) logger.error( 'ARC is sleeping for {0} seconds before re-trying,' ' please check your connectivity.'.format(sleep_time * i)) logger.info('ZZZZZ..... ZZZZZ.....') time.sleep(sleep_time * i) # in seconds i += 1 if not success: raise ServerError( 'Could not download file {0} from {1}. Tried {2} times.'. format(remote_file_path, self.server, max_times_to_try))
def upload_file(self, remote_file_path, local_file_path='', file_string=''): """ Upload `local_file_path` or the contents of `file_string` to `remote_file_path`. Either `file_string` or `local_file_path` must be given. """ if local_file_path and not os.path.isfile(local_file_path): raise InputError('Cannot upload a non-existing file.' ' Check why file in path {0} is missing.'.format( local_file_path)) sftp, ssh = self.connect() i, max_times_to_try = 1, 30 success = False sleep_time = 10 # seconds while i < 30: try: write_file(sftp, remote_file_path, local_file_path, file_string) except IOError: logger.error('Could not upload file {0} to {1}!'.format( local_file_path, self.server)) logger.error( 'ARC is sleeping for {0} seconds before re-trying,' ' please check your connectivity.'.format(sleep_time * i)) logger.info('ZZZZZ..... ZZZZZ.....') time.sleep(sleep_time * i) # in seconds else: success = True i = 1000 i += 1 if not success: raise ServerError( 'Could not write file {0} on {1}. Tried {2} times.'.format( remote_file_path, self.server, max_times_to_try)) sftp.close() ssh.close()