1

編集:私の最終的なコードは次のようになります:

#WARNING: all " in command need to be escaped: \\"
def spawnInNewTerminal(command):
    #creates lock file
    lock = open(lockPath, 'w')
    lock.write("Currently performing task in separate terminal.")
    lock.close()

    #adds line to command to remove lock file
    command += ";rm " + lockPath

    #executes the command in a new terminal
    process = subprocess.Popen (
        ['x-terminal-emulator', '-e',  'sh -c "{0}"'.format(command) ]
        , stdout=subprocess.PIPE )
    process.wait()

    #doesn't let us proceed until the lock file has been removed by the bash command
    while os.path.exists(lockPath):
        time.sleep(0.1)

元の質問:

最終的にLuaLaTeXを実行する前に、不足しているパッケージを「オンザフライ」でインストールする単純なラッパーを作成しています。ほとんどの場合は機能しますが、最後にコマンドを実行する必要があります

sudo tlmgr install [string of packages]

さらに、LaTeX エディターがユーザー入力を許可する保証はないため、ユーザーが sudo パスワードを入力できるように、これを行うために新しい端末を呼び出す必要があります。

私はほとんどこれを理解しました:どちらか

process = subprocess.Popen(
    shlex.split('''x-terminal-emulator -t \'Installing new packages\' -e \'sudo tlmgr install ''' + packagesString + '''\''''), stdout=subprocess.PIPE)
retcode = process.wait()

また

os.system('''x-terminal-emulator -t \'Installing new packages\' -e \'sudo tlmgr install ''' + packagesString + '''\'''')

唯一の問題は、生成された端末プロセスが完了するまでこの行が待機しないことです。実際、ユーザーがパスワードを入力したり、パッケージをダウンロードしたりする前に、すぐに次の行 (実際の LuaLaTeX の実行) に進みます!

私が理解していることから、これは sudo 子プロセスがすぐに終了するためです。続行する前に tlmgr プロセスが終了していることを確認する方法はありますか?

4

1 に答える 1

3

その理由は、x-terminal-emulator が新しいプロセスを生成して終了するため、実行されたコマンドが実際にいつ終了するかを知ることができないためです。これを回避するには、コマンドを変更して通知する別のコマンドを追加することです。どうやら x-terminal-emulator は 1 つのコマンドしか実行しないため、シェルを使用してそれらを連鎖させることができます。おそらく最善の方法ではありませんが、次の方法があります。

os.system('x-terminal-emulator -t "Installing new packages" -e "sh -c \\"sudo tlmgr install %s; touch /tmp/install_completed\\""' % packagesString)
while not os.path.exists("/tmp/install_completed"):
    time.sleep(0.1)
os.remove("/tmp/install_completed")
于 2011-09-17T22:59:30.367 に答える