def transfer_stage_to_lucky(): # Make a tarfile of everything in stage directory tarname = 'stage_{0:d}.tar'.format(int(DateTime().secs)) logger.info('Making tarfile {0}'.format(tarname)) tar = tarfile.open(name=tarname, mode='w') tar.add('stage') tar.close() # Put the tarfile on lucky. First put it at the top level, then when # complete move it into a subdir eng_archive. This lets the OCC side # just watch for fully-uploaded files in that directory. logger.info('ftp to lucky') ftp = Ska.ftp.SFTP('lucky', logger=logger) ftp.cd('/home/taldcroft') files = ftp.ls() if opt.ftp_dir not in files: logger.info('mkdir {}'.format(opt.ftp_dir)) ftp.mkdir(opt.ftp_dir) logger.info('put {0}'.format(tarname)) ftp.put(tarname) logger.info('rename {0} {1}/{0}'.format(tarname, opt.ftp_dir)) ftp.rename(tarname, '{}/{}'.format(opt.ftp_dir, tarname)) ftp.close() logger.info('unlink {0}'.format(tarname)) os.unlink(tarname) logger.info('rmtree stage') shutil.rmtree('stage')
def ftp_put_to_lucky(ftp_dirname, local_files, user=None, logger=None): """Put the ``local_files`` onto lucky in /``user``/``ftp_dirname``. First put it at the top level, then when complete move it into a subdir eng_archive. This lets the OCC side just watch for fully-uploaded files in that directory. The directory paths of ``local_files`` are stripped off so they all wind up in a flat structure within ``ftp_dirname``. """ import Ska.File import Ska.ftp import uuid ftp = Ska.ftp.SFTP('lucky', logger=logger, user=user) if user is None: user = ftp.ftp.get_channel().transport.get_username() ftp.cd('/home/{}'.format(user)) files = ftp.ls() if ftp_dirname not in files: ftp.mkdir(ftp_dirname) dir_files = ftp.ls(ftp_dirname) for local_file in local_files: file_dir, file_base = os.path.split(os.path.abspath(local_file)) ftp_file = str(uuid.uuid4()) # random unique identifier with Ska.File.chdir(file_dir): ftp.put(file_base, ftp_file) destination_file = "{}/{}".format(ftp_dirname, file_base) # If final destination of file already exists, delete that file. if file_base in dir_files: ftp.delete(destination_file) # Rename the temp/uniq-id file to the final destination ftp.rename(ftp_file, destination_file) ftp.close()