0

我正在尝试在远程系统的目录中运行 find 命令。Fabric 有时会更改目录,但有时会失败,具体取决于路径是否包含括号或空格以及我是否使用 shlex.quote() 。处理这个问题的正确方法是什么?

我的代码基本上是这样的:

from shlex import quote
from fabric import Connection

with Connection(remote_login) as c:
    with c.cd(quote(node.src)):    # Condition 1
    # with c.cd(node.src):         # Condition 2
        result = c.run(r"nice find -maxdepth 1 -type f -printf '%f\n'", echo=True)

如果我使用条件 1,则当路径包含括号时它会成功。在这种情况下,Fabric 会生成这一行:

# Fabric output success with parens in path
cd '/data/PixelSizeTestRuby105mm(Zoom248.5mm)' && nice find -maxdepth 1 -type f -printf '%f\n'

但是当路径包含空格时它会失败,因为空格被转义但路径也被引用,而不仅仅是一个或另一个。

# Fabric output failure for spaces in path
cd '/data/Crystal\ Bending\ Test/Bending0' && nice find -maxdepth 1 -type f -printf '%f\n'
sh: line 0: cd: /data/Crystal\ Bending\ Test/Bending0: No such file or directory

如果我改为使用条件 2,则第一个路径失败,第二个路径成功。

# Fabric output failure for parens in path
cd /data/PixelSizeTestRuby105mm(Zoom248.5mm) && nice find -maxdepth 1 -type f -printf '%f\n'
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `cd /data/PixelSizeTestRuby105mm(Zoom248.5mm) && nice find -maxdepth 1 -type f -printf '%f\n''
4

1 回答 1

2

这是Invoke 实现中的一个错误。它根本不为cd.

作为快速修复,您可以通过在前面添加反斜杠来手动转义路径中的括号。shlex.quote正如您自己注意到的那样,使用将不起作用。理想情况下,应该将 Invoke 实现固定为shlex.quote内部使用,而不是它当前执行的临时、错误的手动转义。

于 2020-08-03T08:51:21.483 回答