2017-09-02 1 views
0
def monkey_patch_string(file_name, old_string, new_string): 
    # Read in the file 
    with open(file_name, 'r') as f : 
     filedata = f.read() 

    # Replace the target string 
    filedata = filedata.replace(old_string, new_string) 

    # Write the file out again 
    with open(file_name, 'w') as f: 
     f.write(filedata) 

if __name__ == '__main__': 

    file_name = sys.argv[1] 
    old_string = sys.argv[2] 
    new_string = sys.argv[3] 
    monkey_patch_string(file_name, old_string, new_string) 

私はこのファイルを展開中に文字列を置き換えるプロセスとして使用します。その実行は、ファブリックスクリプトを介してリモートで実行されます。ubuntu pythonで文字列が置換されない

old_string = '"{}"'.format('$CONFIG_DIR') 
new_string = '"{}"'.format('$CONFIG_DIR --logfile /var/www/' + domain_name + '/logs/transmission.log') 
run('python deployment/monkey_patch.py /etc/default/transmission-daemon '+ old_string + ' ' + new_string) 

古い文字列を新しい文字列に置き換えるのではなく、ファイル全体を新しい文字列のガベージ値に置き換えます。
ファイルを私のwindows envにコピーしても、まったく同じ手順を繰り返してもうまく動作しますが、ubuntuではうまく動作します。 私はrootユーザーですが、とにかくそのファイルに何かを書いているので、権限の問題もありません。

他の文字列とファイルに対してこのファイルを試してみましたが、動作しています。

old_string = 'debian-transmission' 
new_string = 'www-data' 
run('python deployment/monkey_patch.py /etc/init.d/transmission-daemon '+ old_string + ' ' + new_string) 

ドル記号とファブリックモジュールとは関係がありますが、正確に把握できません。誰でも手掛かりがありますか?

答えて

0

そのファブリックはコマンドを実行します /bin/bash -l -c "Your commands goes here"

コマンドスペース内で文字列を引用すると問題が発生していました。本質的にその文字列引用問題。 パイプで解決しました。

import pipes 
old_string = pipes.quote('$CONFIG_DIR') 
new_string = pipes.quote('$CONFIG_DIR --logfile /var/www/' + domain_name + '/logs/transmission.log')