def execute(parameters): modulepath = os.path.dirname(sys.modules["unoconvinvocator"].__file__) extraparams = [] #extraparams = ["--sleep=3"] #extraparams = extraparams+["-v"] cmd_output = "" if sys.platform == 'win32': oo_alternatives = [] oo_alternatives = glob.glob(os.environ['ProgramFiles']+'\\LibreOffice*\\program') + \ glob.glob(os.environ['ProgramFiles']+'\\LibreOffice*\\Basis\\program') + \ glob.glob(os.environ['ProgramFiles(x86)']+'\\LibreOffice*\\program') + \ glob.glob(os.environ['ProgramFiles(x86)']+'\\LibreOffice*\\Basis\\program') for alternative in oo_alternatives: oo_python_invocators = [os.path.join(alternative,'python.bat'),os.path.join(alternative,'python.exe')] for oo_python in oo_python_invocators: if os.path.isfile(oo_python): #print("unoconvinvocator:",oo_python) cmd_output, cmd_code = timeout.timeout_command([oo_python,os.path.join(modulepath,unoconv_executable)]+extraparams+parameters) break else: terminal = os.environ.get("DISPLAY","") if (terminal == ""): cmd_output, cmd_code = timeout.timeout_command(["ps","-ef"]) processes = cmd_output.splitlines() allX = ["/usr/bin/X","/usr/X11R6/bin/X"] found = False s = 0 while (s < len(allX)) and (not found): X = allX[s] i = 0 while (i < len(processes)) and (not found): if processes[i].find(X): arguments = processes[i].split(" "); j = 0 while j < len(arguments) and (not found): #print(arguments[j]) if (arguments[j] == X): terminal = arguments[j+1] found = True j=j+1 i=i+1 s=s+1 os.environ["DISPLAY"] = terminal #print("unoconvinvocator: DISPLAY="+os.environ.get("DISPLAY","")) #print("unoconvinvocator: ["+", ".join(parameters)+"]") cmd_output, cmd_code = timeout.timeout_command(["xhost","+localhost"]) #print(cmd_output) cmd_output, cmd_code = timeout.timeout_command([os.path.join(modulepath,unoconv_executable)]+extraparams+parameters) return cmd_output, cmd_code
def process_ooffice(self): file=os.path.join(self.final_path(),self.finalName+os.extsep+"xdv") try: self.log(file) cmd_line = [self.where("tex4ht"),"-.xdv",file,"-hVF","-cmozhtf","-utf8","-i"+os.path.join(self.texbasepath(),"tex4ht","ht-fonts","ooffice","!")] cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) brokenTemp = os.path.join(self.dir_input,self.finalName+os.extsep+"4od") finalTemp = os.path.join(self.final_path(),self.finalName+os.extsep+"4od") if (os.path.isfile(brokenTemp)): if (os.path.isfile(finalTemp)): os.remove(finalTemp) os.rename(brokenTemp,finalTemp) brokenTemp = os.path.join(self.dir_input,self.finalName+"-manifest"+os.extsep+"4of") finalTemp = os.path.join(self.final_path(),self.finalName+"-manifest"+os.extsep+"4of") if (os.path.isfile(brokenTemp)): if (os.path.isfile(finalTemp)): os.remove(finalTemp) os.rename(brokenTemp,finalTemp) brokenTemp = os.path.join(self.dir_input,self.finalName+"-meta"+os.extsep+"4ot") finalTemp = os.path.join(self.final_path(),self.finalName+"-meta"+os.extsep+"4ot") if (os.path.isfile(brokenTemp)): if (os.path.isfile(finalTemp)): os.remove(finalTemp) os.rename(brokenTemp,finalTemp) brokenTemp = os.path.join(self.dir_input,self.finalName+"-settings"+os.extsep+"4os") finalTemp = os.path.join(self.final_path(),self.finalName+"-settings"+os.extsep+"4os") if (os.path.isfile(brokenTemp)): if (os.path.isfile(finalTemp)): os.remove(finalTemp) os.rename(brokenTemp,finalTemp) brokenTemp = os.path.join(self.dir_input,self.finalName+"-styles"+os.extsep+"4oy") finalTemp = os.path.join(self.final_path(),self.finalName+"-styles"+os.extsep+"4oy") if (os.path.isfile(brokenTemp)): if (os.path.isfile(finalTemp)): os.remove(finalTemp) os.rename(brokenTemp,finalTemp) cmd_line = [self.where("t4ht"),"-.xdv",file,"-d"+self.final_path()+os.sep,"-cooxtpipes","-coo"] cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) except: self.errorCount = self.errorCount+1 self.error("processing "+file) self.error("".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))) return
def process_xelatex(self,extraoptions,override=""): reiterate = False fileName=os.path.join(self.dir_input,self.projectName+os.extsep+"tex") try: self.log(" ".join([fileName]+extraoptions+[override])) options = ["-enable-write18","-enable-pipes","-synctex=-1","-file-line-error","-interaction=nonstopmode","-recorder","-output-directory="+self.final_path(),"-jobname="+self.finalName] options = options+["-include-directory="+self.dir_input] for directory in self.process_path_recursive(self.dir_environment): options = options+["-include-directory="+directory] if (override==""): codeLatex = fileName else: codeLatex = override+" "+fileName.replace("\\","/") cmd_line = [self.where("xelatex")]+options+extraoptions+[codeLatex] cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) handler = codecs.open(os.path.join(self.final_path(), self.finalName+os.extsep+"log"), "r", encoding=sys.getdefaultencoding(), errors='replace') reiterate = self.process_tex_output(handler.read()) except: self.errorCount = self.errorCount+1 self.error("processing "+fileName) self.error("".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))) return reiterate
def call_comparepdf(self,generated,stored): cmd_line = [self.binary_comparepdf(),"--compare=a","--verbose=2",generated,stored] cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0) and (cmd_code != 10): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) return (cmd_code==0)
def call_qpdf(self,parameters): cmd_line = [self.binary_qpdf(),"--qdf"]+parameters cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) return (cmd_code==0)
def process_html(self): file=os.path.join(self.final_path(),self.finalName+os.extsep+"xdv") try: self.log(file) brokenTemp = os.path.join(self.dir_input,self.finalName+os.extsep+"css") if (os.path.isfile(brokenTemp)): os.remove(brokenTemp) brokenTemp = os.path.join(self.dir_input,self.finalName+os.extsep+"tmp") if (os.path.isfile(brokenTemp)): os.remove(brokenTemp) cmd_line = [self.where("tex4ht"),"-.xdv",file,"-hVF","-cunihtf","-utf8","-i"+os.path.join(self.texbasepath(),"tex4ht","ht-fonts","mozilla","!")] cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) brokenTemp = os.path.join(self.dir_input,self.finalName+os.extsep+"css") finalTemp = os.path.join(self.final_path(),self.finalName+os.extsep+"css") if (os.path.isfile(brokenTemp)): if (os.path.isfile(finalTemp)): os.remove(finalTemp) os.rename(brokenTemp,finalTemp) brokenTemp = os.path.join(self.dir_input,self.finalName+os.extsep+"tmp") finalTemp = os.path.join(self.final_path(),self.finalName+os.extsep+"tmp") if (os.path.isfile(brokenTemp)): if (os.path.isfile(finalTemp)): os.remove(finalTemp) os.rename(brokenTemp,finalTemp) cmd_line = [self.where("t4ht"),"-.xdv",file,"-d"+self.final_path()+os.sep] cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) except: self.errorCount = self.errorCount+1 self.error("processing "+file) self.error("".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))) return
def extract_fonts(file): cmd_output, cmd_code = timeout.timeout_command(["pdffonts",file]) properties = cmd_output.splitlines() properties.pop(0) properties.pop(0) result = [] for property in properties: tags = property.split() result.append(tags) return result
def read(parameters): if sys.platform == 'win32': paths = [] if "ProgramFiles" in os.environ: paths = paths + glob.glob(os.path.join(os.environ["ProgramFiles" ],'SumatraPDF' )) paths = paths + glob.glob(os.path.join(os.environ["ProgramFiles" ],'Adobe','Reader *','Reader')) if "ProgramW6432" in os.environ: paths = paths + glob.glob(os.path.join(os.environ["ProgramW6432" ],'SumatraPDF' )) paths = paths + glob.glob(os.path.join(os.environ["ProgramW6432" ],'Adobe','Reader *','Reader')) if "ProgramFiles(x86)" in os.environ: paths = paths + glob.glob(os.path.join(os.environ["ProgramFiles(x86)"],'SumatraPDF' )) paths = paths + glob.glob(os.path.join(os.environ["ProgramFiles(x86)"],'Adobe','Reader *','Reader')) executables = ["SumatraPDF.exe","AcroRd32.exe"] for path in paths: for executable in executables: fullpath = os.path.join(path,executable) if os.path.isfile(fullpath): if (executable=="SumatraPDF.exe"): args = ["WMIC.exe","path","win32_process","get","commandLine"] running = timeout.timeout_command(args)[0].splitlines() for process in running: if executable in process: process = process.strip() if process == "\""+fullpath+"\" "+" ".join(parameters): return return timeout.timeout_command(command=[fullpath]+parameters, nowait=True) else: return timeout.timeout_command([fullpath]+parameters, nowait=True) if sys.platform == 'linux': paths = ['/usr/bin'] for path in paths: executable = os.path.join(path,"acroread") if os.path.isfile(executable): return timeout.timeout_command([executable]+parameters) return
def process_makeindex(self): file = os.path.join(self.final_path(),self.finalName+os.extsep+"idx") try: if os.path.exists(file): self.log(file) options = ["-q"] cmd_line = [self.where("makeindex")]+options+[file] cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) except: self.errorCount = self.errorCount+1 self.error("processing "+file) self.error("".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))) return
def process_bibtex(self): file = self.finalName+os.extsep+"aux" try: if os.path.exists(os.path.join(self.final_path(),file)): self.log(file) options = ["-terse","-include-directory="+self.dir_references] cmd_line = [self.where("bibtex")]+options+[file] cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) except: self.errorCount = self.errorCount+1 self.error("processing "+file) self.error("".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))) return
def process_dvipdf(self,extraoptions): file=os.path.join(self.final_path(), self.finalName+os.extsep+"xdv") finalpath = os.path.join(self.final_path(), self.finalName+os.extsep+"pdf") try: self.log(" ".join([file]+extraoptions)) options = ["-V","7","-o",finalpath] cmd_line = [self.where("xdvipdfmx")]+options+extraoptions+[file] cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) if (self.stepMeta==True) and (os.path.isfile(finalpath)): self.clean_meta(finalpath) except: self.errorCount = self.errorCount+1 self.error("processing "+file) self.error("".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))) return
def process_count(self): file = self.finalName+os.extsep+"sum" try: if os.path.exists(os.path.join(self.dir_input,self.projectName+os.extsep+"tex")): self.log(file) options = ["-subcount=none","-merge",os.path.join(self.dir_input,self.projectName+os.extsep+"tex"),"-out="+os.path.join(self.final_path(),file),"-template={SUM} ={SUB? {SUM}?SUB}"] cmd_line = [self.where("perl")]+[os.path.join(os.path.dirname(os.path.abspath( __file__ )),"texcount.pl")]+options cmd_output, cmd_code = timeout.timeout_command(cmd_line) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) else: handler = open(os.path.join(self.final_path(),file), 'r') self.log(handler.readline().strip()+" words aprox.") handler.close() except: self.errorCount = self.errorCount+1 self.error("processing "+file) self.error("".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))) return
def process_format(self,original,format,force): self.log(format) rawDir = original if format == "pdf": conDir = self.folder_images_pdf() elif format == "png": conDir = self.folder_images_png() else: self.error("format not supported") return filesRawDir = self.generate_list(rawDir,"*") filesConDir = self.generate_list(conDir,"*") for fullentry in self.generate_files_ignore(rawDir): if fullentry in filesRawDir: filesRawDir.remove(fullentry) for fullentry in self.generate_files_ignore(conDir): if fullentry in filesConDir: filesConDir.remove(fullentry) filesRawDirSplit = {} for fullentry in filesRawDir: if os.path.isdir(fullentry): continue (strpath, strname) = os.path.split(fullentry) (strfile, strext ) = os.path.splitext(strname) filesRawDirSplit[strfile] = strext filesConDirSplit = {} for fullentry in filesConDir: if os.path.isdir(fullentry): continue (strpath, strname) = os.path.split(fullentry) (strfile, strext ) = os.path.splitext(strname) filesConDirSplit[strfile] = strext for i in filesConDirSplit.keys(): if not (i in filesRawDirSplit): try: self.log("removing outdated "+os.path.join(conDir,i+filesConDirSplit[i])) os.remove(os.path.join(conDir,i+filesConDirSplit[i])) except: self.errorCount = self.errorCount+1 self.error("error removing "+os.path.join(conDir,i+filesConDirSplit[i])) for rawFile in filesRawDir: try: if os.path.isdir(rawFile): continue #should not happen #if (rawFile in self.generate_files_ignore(rawDir)): # continue (strpath, strname) = os.path.split(rawFile) (strfile, strext ) = os.path.splitext(strname) conFile = os.path.join(conDir, strfile + os.extsep + format) if os.path.exists(conFile): if (os.path.getmtime(conFile) > os.path.getmtime(rawFile)) and (not force): continue else: try: os.remove(conFile) except: self.errorCount = self.errorCount+1 self.error("error removing "+conFile) self.log(rawFile+ " into "+conFile) cmd_line = [] cmd_output = "" cmd_code = 0 self.clean_meta(rawFile) if strext == os.extsep+format: shutil.copyfile(rawFile,conFile) elif strext == os.extsep+"svg": cmd_line = [self.where("inkscape"),"--without-gui","--file="+rawFile,"--export-"+format+"="+conFile] cmd_output, cmd_code = timeout.timeout_command(cmd_line) else: cmd_line = [self.where("convert"),rawFile,conFile] cmd_output, cmd_code = timeout.timeout_command(cmd_line) self.clean_meta(conFile) if (cmd_code != 0): self.errorCount = self.errorCount+1 self.log("command "+" ".join(cmd_line)+" failed with code "+str(cmd_code)) self.log(cmd_output) except: self.errorCount = self.errorCount+1 self.error("processing "+rawFile) self.error("".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))) continue return