예제 #1
0
def pendingFile(filepath, filesize=0):
    logger.info('pendingFile........')
    from os.path import getsize

    newsize = getsize(filepath)

    logger.info('{filepath} size is {size}'.format(
        filepath=filepath, size=newsize))
    if newsize == filesize and newsize > 0:
        try:
            logger.info('This is is watch file?')
            f = FileRoute(filepath)
            transtype, ftpname, dest_path, filename = f.routeInfo()
            return True
        except TypeError:
            logger.info('{filepath} is not watch file'.format(
                filepath=filepath))
            return False
        except:
            logger.exception('pendingFile')
            return False
    logger.info('File is not Ok,pending {pendingtime} seconds'.format(
        pendingtime=conf.PENDING_TIME))
    time.sleep(conf.PENDING_TIME)
    return pendingFile(filepath, newsize)
예제 #2
0
def UploadFile(filepath):
    # import shutil
    from tools.ftptools import (
        FTPInfo,
        StandardFTPFactory
    )

    q = DogQueue()
    q.doing(filepath)

    pendingFile(filepath)
    f = FileRoute(filepath)
    try:
        logger.info('Send file to route')

        transtype, ftpname, dest_path, filename = f.routeInfo()
        logger.info('Copy file {filepath} Begin'.format(filepath=filepath))

        info = FTPInfo().get_info(ftpname)

        host = info.get('host', None)
        port = info.get('port', 22)
        username = info.get('username', None)
        password = info.get('password', None)
        key = info.get('key', None)

        f = StandardFTPFactory.get_factory(transtype)
        c = f.get_client(host, port, username, password, key)

        local = filepath
        if dest_path is None:
            dest_path = ''
        remote = os.path.join(dest_path, filename)
        temp = os.path.join(dest_path, filename + '.transfering')

        c.put(local, temp)
        c.rename(temp, remote)
        c.close()

        os.remove(filepath)

        # shutil.copyfile(filepath, os.path.join(dest_path, filename))
        q.done(filepath)
    except TypeError:
        q.done(filepath)
        logger.info('{filepath} is not our file'.format(filepath=filepath))
        return False
    except:
        logger.exception('UploadFile')
        return False
    else:
        logger.info('Copy file {filepath} Success'.format(filepath=filepath))
        return True