2016-06-29 4 views
0

私は、Googleドキュメントの(ラテックスソース)コンテンツをとり、pdfを作成するPythonスクリプトを持っています。 Pythonを使ってLatexコンパイラを実行すると、ラテックスにエラーがあるとハングするのはなぜですか?

この

は、私はPDFファイルに使用する機能です:ラテックスにエラーがない場合

# -*- coding: utf-8 -*- 
#!/usr/bin/python 
"""One of the main activiating files of IMPS - this downloads all the files in a directory, creates the input.tex file and archives them a tar file 

TODO 

Before we send to stackoverflow we should make sure that everthing is in a function and that the If __main__ trigger is working 
I'd also like to have doc strings for all of the methods 


""" 

import os 
import glob 
import tarfile 
import time 
import datetime 
from pydrive.auth import GoogleAuth 
from pydrive.drive import GoogleDrive 
import urlparse 
import argparse 
import re 

def generate_pdf(filename,destination=""): 
    """ 
    Genertates the pdf from string 
    from http://stackoverflow.com/q/19683123/170243 
    """ 
    import subprocess 
    import shutil 
    current = os.getcwd() 
    this_dir=os.path.dirname(os.path.realpath(__file__)) 
    os.chdir(this_dir+"/latex") 
    proc=subprocess.Popen(['pdflatex',filename+'.tex'],stdout=subprocess.PIPE) 
# subprocess.Popen(['pdflatex',tex]) 
    temp=proc.communicate() 
    #Have to do it twice so that the contents pages are right 
    proc=subprocess.Popen(['pdflatex',filename+'.tex'],stdout=subprocess.PIPE) 
    temp=proc.communicate() 
    shutil.copy(filename+'.pdf',"../../live/"+destination+filename+ str(datetime.datetime.now()).replace(".", "-").replace(":", "_") + ".pdf") 
    trace_file = open("../../live/"+destination+"trace.txt", "w") 
    print >>trace_file, temp[0] 
    print >>trace_file, temp[1] 
    trace_file.close() 
    os.chdir(current) 

すべてがうまく動作しますが、問題がある場合、関数手と何も行われます。私が望むのは、問題が記録され、トレースにエクスポートされるということです。何が間違っているのでしょうか?

答えて

1

pdflatexを進めるので、あなたのスクリプトが「ハング」する方法についてユーザーに尋ねます。 pdflatex -interaction=nonstopmode -halt-on-errorを使用してください。 this TeX StackExchange questionを参照してください。

+0

これは私に距離のいくつかを与えます - しかし、コードはそれがリリースする前に私がenterkeyを数回打つことを望んでいます... – Joe

+0

スクラッチ - 私がそれに戻ったときに機能しました。 :) – Joe

0

あなたが欠けているものは、STDERRのパイプをセットアップする必要があるということです。これにより、pdflatexのエラーメッセージが表示されます。 Popenを呼び出すときに、バッファサイズをゼロに明示的に設定することもできます。それがエラーを検出すると、それが入力を期待しているので、

self.child = subprocess.Popen(command 
           ,bufsize=0 
           ,stdout=subprocess.PIPE 
           ,stderr=subprocess.PIPE) 
関連する問題