Run wine commands from Python 3.6 on Mac OSX

孤街浪徒 提交于 2019-12-12 19:38:21

问题


I am trying to write a script in Python which opens wine and then sends code to the wine terminal to open a .exe program. The .exe program is also command driven.

I can open wine, but I can't get any further:

import shlex, subprocess

line = "/usr/bin/open -n -a /Applications/Wine\ Stable.app"
line2 = '''cd /Applications/application/ && wine application.exe /h1 
/k0 Z:/Users/myname/Desktop/File.TXT''' 
line = shlex.split(line)
p1 = subprocess.Popen(line)
p1.stdin.write(line2.encode())

The above doesn't work, wine doesn't seem to receive line2, although

/usr/bin/open -n -a /Applications/Wine\ Stable.app

by itself is fine (it opens Wine but nothing else.)

I'm pretty confused about what the next step should be. I would like to avoid additional dependencies if possible, because it seems simple.


回答1:


The following has worked for me in many cases (on Linux):

import subprocess

command = 'echo "echo foo && echo bar" | wine cmd > std_out.txt 2> std_error.txt &'
subprocess.Popen(command, shell = True)

(I believe wine is also available as a command on MacOS, just like that. Please correct me if I am wrong.)

The command fires up a Windows/DOS-like shell (wine cmd). You can actually type wine cmd into your Linux shell and hit enter - you'll find yourself in a DOS shell. The next step is to get commands into the DOS shell. I do this by piping them into it as a string. In my example, I run two commands: echo foo and echo bar. The initial echo writes the command string to stdout, the following | opens a pipe and forwards the string into the DOS shell.

Besides, once you send commands to the DOS shell, keep in mind that it expects Windows paths (when you change directories etc). I.e. you must translate your Unix paths into Windows paths before you send them into the DOS shell. You can automatically convert your path on a command line like this ...

winepath -w /home/ 2> /dev/null

... resulting in Z:\home\ (for example). Alternatively, the following Python snipped will do the same for you:

def convert_unix_path_to_windows_path(in_path):
    cmd = ['winepath', '-w', in_path]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (out, err) = proc.communicate()
    return out.decode('utf-8').strip()


来源:https://stackoverflow.com/questions/46965024/run-wine-commands-from-python-3-6-on-mac-osx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!