0

Windowsで動作する必要のあるPythonのバックアップアプリケーションがあります。UTF互換性が必要です(イタリア語のアクセントなどのUTF文字を含むディレクトリをバックアップできるようにするため)。問題は、外部プログラム(plink、cygwin、ssh、rsync)を使用していて、それらを機能させることができないことです。プロトタイプの長さは32行です。ご覧ください。

# -*- coding: utf-8 -*-
import subprocess

def safestr(obj, encoding='utf-8'):
    r"""Converts any given object to utf-8 encoded string.

        >>> safestr('hello')
        'hello'
        >>> safestr(u'\u1234')
        '\xe1\x88\xb4'
        >>> safestr(2)
        '2'
    """
    if isinstance(obj, unicode):
        return obj.encode("utf-8")
    elif isinstance(obj, str):
        return obj.encode
    else:
        return str(obj)

def execute(command):
    pipe = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    out, errs = pipe.communicate()
    retcode = pipe.poll()

    print "OUT: " + repr(out)
    print "ERRS: " + repr(errs)
    print "RET: " + str(retcode)


command = u'rsync --stats -az --numeric-ids --delete --blocking-io --modify-window=2 --no-group --chmod=u=rwX,g=,o=  -e \'cygnative plink -ssh -2 -batch  -pw test \' "/cygdrive/c/κόσμε" vaidab@192.168.1.86:/volatile/backup/vaidab/2010-03-03.15_41_56/ --link-dest=../2010-03-03.15_00_57'.encode('utf-8')
execute(command)

それでもnoskloのバージョンでは機能しません。結果を確認してください:

python protocol_unicode_new.py'rsync.exe --stats -az --numeric-ids --delete --blocking-io --modify-window = 2 --no-group --chmod = u = rwX、g =、o = -e "cygnative plink -ssh -2 -batch -pw test" / cygdr ive / c / \ xce \ xba \ xcf \ x8c \ xcf \ x83 \ xce \ xbc \ xce \ xb5 vaidab@192.168.1.86:/ volatile / bac kup / vaidab / 2010-03-03.15_41_56 / '

OUT:'\ nファイル数:0 \ n転送ファイル数:0 \ n合計ファイルサイズ:0バイト\ n合計転送ファイルサイズ:0バイト\ nリテラルデータ:0バイト\ n一致データ:0バイト\ nファイルリストサイズ:9 \ nファイルリストの生成時間:0.001秒\ nファイルリストの転送時間:0.000秒\ n送信された合計バイト数:22 \ n受信された合計バイト数:12 \ n\ns送信された22バイト12バイト68.00バイト/秒\n合計サイズは0ですスピードアップは0.00\ n'ERRS:' rsync:link_stat "/ cygdrive / c / \ xc3 \ x8e \ xc2 \ xba \ xc3 \ x8f \ xc5 \ x92 \ xc3 \ x8f \ xc 6 \ x92 \ xc3 \ x8e \ xc2 \ xbc \ xc3 \ x8e \ xc2 \ xb5 "失敗:そのようなファイルまたはディレクトリはありません(2)\ nrs yncエラー:/home/lapo/packaging/rsync-3.0.6で一部のファイル/属性が転送されませんでした(前のエラーを参照)(コード23) -1 / src / rsync-3.0.6 / main.c(1039)[sender=3.0。6] \ n'RET:23

4

2 に答える 2

1
  • 使用しないでくださいshell=TrueEVER。それは、プログラムを呼び出すためにシェルを呼び出します。
  • パラメータを文字列ではなくリストとして渡します。

この例は、パラメーターが正しく、rsync.exeが現在のフォルダー(またはPATH)にある場合に機能するはずです。

# -*- coding: utf-8 -*-
import subprocess

def execute(command):
    pipe = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, errs = pipe.communicate()
    retcode = pipe.poll()

    print "OUT: " + repr(out)
    print "ERRS: " + repr(errs)
    print "RET: " + str(retcode)
    return out


command = ['rsync.exe', '--stats', '-az', '--numeric-ids', '--delete', 
           '--blocking-io', '--modify-window=2', '--no-group', 
           '--chmod=u=rwX,g=,o=', '-e', 
           'cygnative plink -ssh -2 -batch -pw test', 
           u'/cygdrive/c/κόσμε'.encode('utf-8'), 
           'vaidab@192.168.1.86:/volatile/backup/vaidab/2010-03-03.15_41_56/', 
           '--link-dest=../2010-03-03.15_00_57']

execute(command)
于 2010-03-09T12:16:14.127 に答える
0

すべての理解に合格するコードの一部:

if isinstance(obj, unicode):
    return obj.encode("utf-8")
elif isinstance(obj, str):
    return obj.encode
    # the above is returning a METHOD ***************************
else:
    return str(obj)

実行しない場合のdoctestのポイントは何ですか?

于 2010-03-09T13:35:56.797 に答える