使用 Python 编写 Unison 脚本时出现问题

发布于 2024-08-08 10:22:16 字数 957 浏览 6 评论 0原文

我正在尝试制作一个简单的脚本来通过 Unison 自动执行和记录同步。我还使用 subprocess.Popen 而不是通常的 os.system 调用,因为它已被弃用。在过去的两天里,我一直在查看文档,试图找出我做错了什么,但出于某种原因,如果我从终端调用一致,它就没有问题,但是当我从 Python 进行相同的调用时,它尝试进行用户交互,此外我没有捕获大约一半的输出,另一个仍在打印到终端。

这是我尝试使用的代码:

sync = Popen(["unison", "sync"], shell = True, stdout = PIPE)

for line in sync.stdout
    logFile.write(line)

sync.wait()

if sync.returncode == 0 or sync.returncode == None:
    logFile.write("Sync Completed Successfully\n")
else
    logFile.write("!! Sync Failed with a returncode of: " + str(sync.returncode) + "\n")

这是我的 Unison 配置文件:

root = /home/zephyrxero/temp/
root = /home/zephyrxero/test/

auto = true
batch = true
prefer = newer
times = true
owner = true
group = true
retry = 2

我做错了什么?为什么 Unison 的所有输出都没有保存到我的日志文件中,为什么它在脚本运行时要求我确认,而不是在我直接从终端运行它时要求我确认?

更新: 好的,多亏了 Emil,我现在捕获了所有输出,但我仍然无法弄清楚为什么在终端中输入“unisonsync”会得到与从脚本调用它时不同的结果。

I am trying to make a simple script to automate and log synchronization via Unison. I am also using subprocess.Popen rather than the usual os.system call as it's deprecated. I've spent the past 2 days looking at docs and such trying to figure out what I'm doing wrong, but for some reason if I call unison from a terminal it works no problem, yet when I make the same call from Python it tries to do user interaction and in addition I'm not capturing but about half of the output, the other is still printing to the terminal.

Here is my code I am trying to use:

sync = Popen(["unison", "sync"], shell = True, stdout = PIPE)

for line in sync.stdout
    logFile.write(line)

sync.wait()

if sync.returncode == 0 or sync.returncode == None:
    logFile.write("Sync Completed Successfully\n")
else
    logFile.write("!! Sync Failed with a returncode of: " + str(sync.returncode) + "\n")

Here is my Unison config file:

root = /home/zephyrxero/temp/
root = /home/zephyrxero/test/

auto = true
batch = true
prefer = newer
times = true
owner = true
group = true
retry = 2

What am I doing wrong? Why isn't all output from Unison getting saved to my logfile, and why is it asking me for confirmation when the script runs, but not when I run it plainly from a terminal?

UPDATE:
Ok, thanks to Emil I'm now capturing all the output, but I still can't figure out why typing "unison sync" into a terminal is getting different results than when calling it from my script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤舞 2024-08-15 10:22:16

最可能的罪魁祸首是一致将一些输出发送到 stderr 而不仅仅是 stdout。 Popen 需要一个额外的 stderr 参数,因此您可以尝试捕获该参数,而不是(或除了 stdout)之外。

有关标准流的快速参考,请参阅维基百科

The most likely culprit is that unison is sending some output to stderr instead of just stdout. Popen takes an additional stderr argument so you can try capturing that instead of (or in addition to) stdout).

For a quick reference on standard streams see Wikipedia.

柳若烟 2024-08-15 10:22:16

将 [“unison”,“sync”] 更改为简单的 [“unisonsync”]...现在似乎无需用户交互即可工作,不确定为什么会有所不同...但对我有用。

Changed ["unison", "sync"] to simply ["unison sync"]...appears to be working without need for user interaction now, not sure why that would be any different...but works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文