自動アップロードコマンドを追加

date:2013-04-15
status:close
type:request

結論

make upload コマンドを追加しました。 _build_/dirhtml/ をアップロードするようにしています。 make upload でhtmlの生成までは行いません。 事前に make dirhtml をしておく必要があります。

#! /usr/bin/env python
#-*- coding: utf-8 -*-
import sys
import os
cur = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(cur, '../_etc_'))

import re
import time
import socket

import ftputil

import ftp_conf

exts = [
    'html',
    'htm',
    'hdm',
    'wml',
    'gif',
    'png',
    'jpg',
    'jpeg',
    'css',
    'swf',
    'ico',
    'js',
    'class',
    'mel',
    'jar',
    'au',
    'dor',
    'txt',
    'htaccess',
    'htpasswd',
    'dat',
    'rss',
    'ref',
    'xml',
    'pmd',
    'ppt',
    'pptx',
    'pdf',
    'doc',
    'docx',
    'gra',
    'mny',
    'mdb',
    'pwi',
    'xls',
    'xlsx',
    'mid',
    ]

def main():
    host = ftp_conf.host
    user = ftp_conf.user
    pswd = ftp_conf.pswd
    root = ftp_conf.local
    
    print host
    print user
    print pswd
    print root

    regext = re.compile('\.({0})$'.format('|'.join(exts)), re.I)
    regpic = re.compile('\.(png|bmp|jpg)$', re.I)

    cache_name = '_upload_cache_'
    upload_cache = []
    if os.path.isfile(cache_name):
        with open(cache_name, 'rb') as fp:
            upload_cache = fp.readlines()
            
    os.chdir(root)
    cur = os.path.relpath(os.getcwd())
    filepaths = []
    for root, dirs, files in os.walk(cur):
        for filename in files:
            if regext.search(filename) and not regpic.search(filename):
                pp = os.path.join(root, filename)
                filepaths.append(pp)

    upload_cache = map(os.path.abspath, upload_cache)
    cache_file = open(cache_name, 'a+b')
    while filepaths:
        try:
            with ftputil.FTPHost(host, user, pswd) as host:
                while filepaths:
                    path = filepaths.pop()
                    path = path.replace('\\', '/')
                    dst = path.replace('./', '/', 1)
                    print path, '-->',
                    
                    if os.path.abspath(path) in upload_cache:
                        print 'skip'
                        continue
                
                    dirpath = os.path.dirname(dst)
                    if dirpath and not host.path.exists(dirpath):
                        try:
                            host.makedirs(dirpath)
                        except ftputil.ftp_error.PermanentError as err:
                            print err
                        except ftputil.ftp_error.FTPOSError as err:
                            print err

                    try:
                        if host.upload_if_newer(path, dst) is not False:
                            print 'ok'
                            cache_file.write('{0}\n'.format(path))
                        else:
                            print 'do not uploaded'
                    except ftputil.ftp_error.FTPIOError as err:
                        print err
                    except ftputil.ftp_error.FTPOSError as err:
                        print err
                        print dirpath
        except socket.error as err:
            print err
            time.sleep(5)

if __name__ == '__main__':
    main()

目次

前のトピックへ

make archive 時に無駄な引数チェックが入る

次のトピックへ

todo にステータスをつけたい

このページ

inserted by FC2 system