Python调用subprocess.call的rsync [英] calling rsync from python subprocess.call

查看:717
本文介绍了Python调用subprocess.call的rsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过ssh从子在Python脚本从一台服务器将影像复制到另一个执行rsync的。我有一个函数定义为:

I'm trying to execute rsync over ssh from a subprocess in a python script to copy images from one server to another. I have a function defined as:

def rsyncBookContent(bookIds, serverEnv):
    bookPaths = ""
    if len(bookIds) > 1:
        bookPaths = "{" + ",".join(("book_"+str(x)) for x in bookIds) + "}"
    else:
        bookPaths = "book_" + str(bookIds[0])

    for host in serverEnv['content.hosts']:
        args = ["rsync", "-avz", "--include='*/'", "--include='*.jpg'", "--exclude='*'", "-e", "ssh", options.bookDestDir + "/" + bookPaths, "jill@" + host + ":/home/jill/web/public/static/"]
        print "executing " + ' '.join(args)
        subprocess.call(args)

什么我最终想要做的是有Python的执行这个(从一个bash shell中工作):

What I'm ultimately trying to do is have Python execute this (which works from a bash shell):

rsync -avz --include='*/' --include='*.jpg' --exclude='*' -e ssh /shared/books/{book_482,book_347} jill@10.12.27.20:/home/jill/web/public/static/

和的确是我的print语句输出:

And indeed my print statement outputs:

executing rsync -avz --include='*/' --include='*.jpg' --exclude='*' -e ssh /shared/books/{book_482,book_347} jill@10.12.27.20:/home/jill/web/public/static/

但是从这个python脚本内执行的时候,有两个问题:

But when executed from within this python script, there are two problems:


  1. 如果LEN(bookIds)> 1,下子目录/共享/书籍/名单是bash或rsync的PTED某种程度上misinter $ P $。错误消息是:

    • 的rsync:link_stat/共享/书籍/ {book_482,book_347}失败:没有这样的文件或目录(2))

看来,如果subprocess.call功能需要一些字符转义或什么的,不是吗?

Seems as if the subprocess.call function requires some characters to be escaped or something, no?

推荐答案

想通了我的问题。我的问题是我如何的subprocess.call函数执行和bash的花括号内列出的扩张误解的结果。

Figured out my issues. My problems were the result of my misunderstanding of how the subprocess.call function executes and bash's expansion of lists inside curly braces.

当我在用花括号子目录bash shell中发出rsync命令,bash的是真的扩大该到了正在传递给rsync的多个参数(/共享/书籍/ book_1共享/书籍/ book_2等) 。当传递用花括号相同的字符串/共享/书籍/ {book_1,book_2}到subprocess.call功能,扩张没有发生,因为它不是通过bash的打算,所以我的参数rsync的真/共享/书籍/ {book_1,book_2}。

When I was issuing the rsync command in a bash shell with subdirectories in curly braces, bash was really expanding that into multiple arguments which were being passed to rsync (/shared/books/book_1 shared/books/book_2, etc.). When passing the same string with curly braces "/shared/books/{book_1, book_2}" to the subprocess.call function, the expansion wasn't happening, since it wasn't going through bash, so my argument to rsync was really "/shared/books/{book_1, book_2}".

同样,围绕文件模式的单引号('*','* .JPG等)bash命令行(仅传递给rsync的的单引号内的值)上工作,但里面的子进程。呼,单引号传递给rsync的作为文件模式('* .JPG')。

Similarly, the single quotes around the file patterns ('*', '*.jpg', etc.) work on the bash command line (only the values inside the single quotes are passed to rsync), but inside subprocess.call, the single quotes are passed to rsync as the file pattern ("'*.jpg'").

新的(工作)code是这样的:

New (working) code looks like this:

def rsyncBookContent(bookIds, serverEnv):
    bookPaths = []
    for b in bookIds:
        bookPaths.append(options.bookDestDir + "/book_" + str(b))
    args = []
    for host in serverEnv['content.hosts']:
        # copy all *.jpg files via ssh
        args = ["rsync", "-avz", "--include", "*/", "--include", "*.jpg", "--exclude", "*", "-e", "ssh"]
        args.extend(bookPaths)
        args.append("jill@" + host + ":/home/jill/web/public/static/"])
        print "executing " + ' '.join(args)
        subprocess.call(args)

这篇关于Python调用subprocess.call的rsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆