Advertisement
Guest User

texliveonfly.py

a guest
Sep 24th, 2011
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.10 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # texliveonfly.py (formerly lualatexonfly.py) - "Downloading on the fly"
  4. #     (similar to miktex) for texlive.
  5. #
  6. # Given a .tex file, runs lualatex (by default) repeatedly, using error messages
  7. #     to install missing packages.
  8. #
  9. #
  10. # September 26, 2011 Release
  11. #
  12. # Written on Ubuntu 10.04 with TexLive 2011
  13. # Other systems may have not been tested.
  14. #
  15. # Copyright (C) 2011 Saitulaa Naranong
  16. #
  17. # This program is free software; you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation; either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. # GNU General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program; if not, see <http://www.gnu.org/copyleft/gpl.html>.
  29.  
  30. import re, subprocess, os, time,  optparse, sys, shlex
  31.  
  32. scriptName = os.path.basename(__file__)
  33.  
  34. #sets up temp directory and paths
  35. tempDirectory =  os.path.join(os.getenv("HOME"), ".texliveonfly")
  36. lockfilePath = os.path.join(tempDirectory,  "newterminal_lock")
  37.  
  38. #makes sure the temp directory exists
  39. try:
  40.     os.mkdir(tempDirectory)
  41. except OSError:
  42.     print(scriptName + ": Our temp directory " + tempDirectory +  " already exists; good.")
  43.  
  44. checkedForUpdates = False   #have we checked for updates yet?
  45.  
  46. #NOTE: double-escaping \\ is neccessary for a slash to appear in the bash command
  47. # in particular, double quotations in the command need to be written \\"
  48. #NOTE: This function assumes it's only called after doc is compiled; otherwise remove sys.exit
  49. def spawnInNewTerminal(bashCommand):
  50.     #creates lock file
  51.     lockfile = open(lockfilePath, 'w')
  52.     lockfile.write( scriptName + " currently performing task in separate terminal.")
  53.     lockfile.close()
  54.  
  55.     #adds intro and line to remove lock
  56.     bashCommand = '''echo \\"This is {0}'s 'install packages on the fly' feature.\\n{1}\\n\\";{2}; rm \\"{3}\\"'''.format(scriptName, "-"*18,  bashCommand, lockfilePath)
  57.  
  58.     #runs the bash command in a new terminal
  59.     try:
  60.         if os.name == "mac":
  61.             #possible OS X bash implementation (needs testing)
  62.             process = subprocess.Popen(['osascript'], stdin=subprocess.PIPE, stderr=subprocess.PIPE)
  63.             process.communicate( '''tell application "Terminal"\nactivate\ndo script with command "{0} $EXIT"\nend tell'''.format(bashCommand).encode() )
  64.             process.wait()
  65.         else:
  66.             process = subprocess.Popen ( ['x-terminal-emulator', '-e',  'sh -c "{0}"'.format(bashCommand) ]  )
  67.             process.wait()
  68.     except OSError:
  69.         print("\n{0}: Unable to update because we can't spawn new terminal:\n  {1}".format(scriptName,
  70.             "osascript does not seem to be working!" if os.name == "mac" else "there is no x-terminal-emulator!" ) )
  71.         print("We've already compiled the .tex document, so there's nothing else to do.\n  Exiting..")
  72.         os.remove(lockfilePath)
  73.         sys.exit(returnCode)
  74.  
  75.     #doesn't let us proceed until the lock file has been removed by the bash command
  76.     while os.path.exists(lockfilePath):
  77.         time.sleep(0.1)
  78.  
  79. def updateTLMGR():
  80.     global checkedForUpdates
  81.     if not checkedForUpdates:
  82.         spawnInNewTerminal('''echo \\"Updating tlmgr prior to installing packages\n(this is necessary to avoid complaints from itself).\\n\\" ; sudo tlmgr update --self''')
  83.         checkedForUpdates = True
  84.  
  85. #strictmatch requires an entire /file match in the search results
  86. def getSearchResults(preamble, term, strictMatch):
  87.     print( "{0}: Searching repositories for missing {1} {2}".format(scriptName, "font" if "font" in preamble else "file",  term) )
  88.     output = subprocess.getoutput("tlmgr search --global --file " + term)
  89.     outList = output.split("\n")
  90.  
  91.     results = ["latex"]    #latex 'result' for removal later
  92.  
  93.     for line in outList:
  94.         line = line.strip()
  95.         if line.startswith(preamble) and (not strictMatch or line.endswith("/" + term)):
  96.             #filters out the package in:
  97.             #   texmf-dist/.../package/file
  98.             #and adds it to packages
  99.             results.append(line.split("/")[-2].strip())
  100.             results.append(line.split("/")[-3].strip()) #occasionally the package is one more slash before
  101.  
  102.     results = list(set(results))    #removes duplicates
  103.     results.remove("latex")     #removes most common fake result
  104.  
  105.     if len(results) == 0:
  106.         print("No results found for " + term)
  107.  
  108.     return results
  109.  
  110. def getFilePackage(file):
  111.     return " ".join( getSearchResults("texmf-dist/", file, True) )
  112.  
  113. def getFontPackage(font):
  114.     font = re.sub(r"\((.*)\)", "", font)    #gets rid of parentheses
  115.     results = getSearchResults("texmf-dist/fonts/", font , False)
  116.  
  117.     #allow for possibility of lowercase
  118.     if len(results) == 0:
  119.         return "" if font.islower() else getFontPackage(font.lower())
  120.     else:
  121.         return " ".join(results)
  122.  
  123. #string can contain more than one package
  124. def installPackages(packagesString):
  125.     if packagesString.strip() == "":
  126.         return
  127.  
  128.     #New terminal is required: we're not guaranteed user can input sudo password into editor
  129.     print("Attempting to install LaTex package(s): " + packagesString )
  130.     print("A new terminal will open and you may be prompted for your sudo password.")
  131.  
  132.     updateTLMGR()  #avoids complaints about tlmgr not being updated
  133.  
  134.     #bash command to download
  135.     bashCommand='''echo \\"Attempting to install LaTeX package(s): {0} \\"
  136. echo \\"(Some of them might not be real.)\\n\\"
  137. sudo tlmgr install {0}'''.format(packagesString)
  138.  
  139.     spawnInNewTerminal(bashCommand)
  140.  
  141. def readFromProcess(process):
  142.     output = ""
  143.     for line in process.stdout:
  144.         line = line.decode("UTF-8")
  145.         output += line
  146.         print(line, end="")
  147.  
  148.     returnCode = None
  149.     while returnCode == None:
  150.         returnCode = process.poll()
  151.  
  152.     return (output, returnCode)
  153.  
  154. def compileTex(compiler, arguments, texDoc):
  155.     try:
  156.         process = subprocess.Popen( [compiler] + shlex.split(arguments) + [texDoc], stdin=sys.stdin, stdout = subprocess.PIPE )
  157.         return readFromProcess(process)
  158.     except OSError:
  159.         print( "{0}: Unable to start {1}; are you sure it is installed?{2}".format(scriptName, compiler,
  160.             "  \n\n(Or run " + scriptName + " --help for info on how to choose a different compiler.)" if compiler == defaultCompiler else "" )
  161.             )
  162.         sys.exit(1)
  163.  
  164. ### MAIN PROGRAM ###
  165. licenseinfo = """texliveonfly.py Copyright (C) 2011 Saitulaa Naranong
  166. This program comes with ABSOLUTELY NO WARRANTY;
  167. See the GNU General Public License v3 for more info."""
  168.  
  169. defaultArgs = "-synctex=1 -interaction=nonstopmode"
  170. defaultCompiler = "lualatex"
  171.  
  172. if __name__ == '__main__':
  173.     # Parse command line
  174.     parser = optparse.OptionParser(
  175.         usage="\n\n\t%prog [options] file.tex\n\nUse option --help for more info.\n\n" + licenseinfo ,
  176.         version='2011.26.9',
  177.         conflict_handler='resolve'
  178.     )
  179.  
  180.     parser.add_option('-h', '--help', action='help', help='print this help text and exit')
  181.     parser.add_option('-c', '--compiler', dest='compiler', metavar='COMPILER',
  182.         help='your LaTeX compiler; defaults to {0}'.format(defaultCompiler), default=defaultCompiler)
  183.     parser.add_option('-a', '--arguments', dest='arguments', metavar='ARGS',
  184.         help='arguments to pass to compiler; default is: "{0}"'.format(defaultArgs) , default=defaultArgs)
  185.     parser.add_option('-f', '--fail_silently', action = "store_true" , dest='fail_silently',
  186.         help="If tlmgr cannot be found, compile document anyway.", default=False)
  187.  
  188.     (options, args) = parser.parse_args()
  189.  
  190.     if len(args) == 0:
  191.         parser.error( "{0}: You must specify a .tex file to compile.".format(scriptName) )
  192.  
  193.     texDoc = args[0]
  194.  
  195.     #checks that tlmgr is installed, responds based on --fail_silently flag
  196.     try:
  197.         process = subprocess.Popen( ["tlmgr"] , stdout = subprocess.PIPE,  stderr=subprocess.PIPE )
  198.     except OSError:
  199.         if options.fail_silently:
  200.             (output, returnCode)  = compileTex(options.compiler, options.arguments, texDoc)
  201.             sys.exit(returnCode)
  202.         else:
  203.             parser.error( "{0}: It appears tlmgr is not installed.  Are you sure you have TeX Live 2010 or later?".format(scriptName) )
  204.  
  205.     #loop constraints
  206.     done = False
  207.     previousFile = ""
  208.     previousFontFile = ""
  209.     previousFont =""
  210.  
  211.     #keeps running until all missing font/file errors are gone, or the same ones persist in all categories
  212.     while not done:
  213.         (output, returnCode)  = compileTex(options.compiler, options.arguments, texDoc)
  214.  
  215.         #most reliable: searches for missing file
  216.         filesSearch = re.findall(r"! LaTeX Error: File `([^`']*)' not found" , output) + re.findall(r"! I can't find file `([^`']*)'." , output)
  217.         filesSearch = [ name for name in filesSearch if name != texDoc ]  #strips our .tex doc from list of files
  218.         #next most reliable: infers filename from font error
  219.         fontsFileSearch = [ name + ".tfm" for name in re.findall(r"! Font \\[^=]*=([^\s]*)\s", output) ]
  220.         #brute force search for font name in files
  221.         fontsSearch =  re.findall(r"! Font [^\n]*file\:([^\:\n]*)\:", output) + re.findall(r"! Font \\[^/]*/([^/]*)/", output)
  222.  
  223.         if len(filesSearch) > 0 and filesSearch[0] != previousFile:
  224.             installPackages(getFilePackage(filesSearch[0]))
  225.             previousFile = filesSearch[0]
  226.         elif len(fontsFileSearch) > 0 and fontsFileSearch[0] != previousFontFile:
  227.             installPackages(getFilePackage(fontsFileSearch[0]))
  228.             previousFontFile = fontsFileSearch[0]
  229.         elif len(fontsSearch) > 0 and fontsSearch[0] != previousFont:
  230.             installPackages(getFontPackage(fontsSearch[0]))
  231.             previousFont = fontsSearch[0]
  232.         else:
  233.             done = True
  234.  
  235.     sys.exit(returnCode)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement