Advertisement
Guest User

texliveonfly.py

a guest
Sep 24th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.61 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 24, 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(0)
  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) )
  64.         else:
  65.             process = subprocess.Popen ( ['x-terminal-emulator', '-e',  'sh -c "{0}"'.format(bashCommand) ]  )
  66.             process.wait()
  67.     except OSError:
  68.         print("\n{0}: Unable to update because we can't spawn new terminal:\n  {1}".format(scriptName,
  69.             "osascript does not seem to be working!" if os.name == "mac" else "there is no x-terminal-emulator!" ) )
  70.         print("We've already compiled the .tex document, so there's nothing else to do.\n  Exiting..")
  71.         os.remove(lockfilePath)
  72.         sys.exit(returnCode)
  73.  
  74.     #doesn't let us proceed until the lock file has been removed by the bash command
  75.     while os.path.exists(lockfilePath):
  76.         time.sleep(0.1)
  77.  
  78. def updateTLMGR():
  79.     global checkedForUpdates
  80.     if not checkedForUpdates:
  81.         spawnInNewTerminal('''echo \\"Updating tlmgr prior to installing packages\n(this is necessary to avoid complaints from itself).\\n\\" ; sudo tlmgr update --self''')
  82.         checkedForUpdates = True
  83.  
  84. #strictmatch requires an entire /file match in the search results
  85. def getSearchResults(preamble, term, strictMatch):
  86.     print( "{0}: Searching repositories for missing {1} {2}".format(scriptName, "font" if "font" in preamble else "file",  term) )
  87.     output = subprocess.getoutput("tlmgr search --global --file " + term)
  88.     outList = output.split("\n")
  89.  
  90.     results = ["latex"]    #latex 'result' for removal later
  91.  
  92.     for line in outList:
  93.         line = line.strip()
  94.         if line.startswith(preamble) and (not strictMatch or line.endswith("/" + term)):
  95.             #filters out the package in:
  96.             #   texmf-dist/.../package/file
  97.             #and adds it to packages
  98.             results.append(line.split("/")[-2].strip())
  99.             results.append(line.split("/")[-3].strip()) #occasionally the package is one more slash before
  100.  
  101.     results = list(set(results))    #removes duplicates
  102.     results.remove("latex")     #removes most common fake result
  103.  
  104.     if len(results) == 0:
  105.         print("No results found for " + term)
  106.  
  107.     return results
  108.  
  109. def getFilePackage(file):
  110.     return " ".join( getSearchResults("texmf-dist/", file, True) )
  111.  
  112. def getFontPackage(font):
  113.     font = re.sub(r"\((.*)\)", "", font)    #gets rid of parentheses
  114.     results = getSearchResults("texmf-dist/fonts/", font , False)
  115.  
  116.     #allow for possibility of lowercase
  117.     if len(results) == 0:
  118.         return "" if font.islower() else getFontPackage(font.lower())
  119.     else:
  120.         return " ".join(results)
  121.  
  122. #string can contain more than one package
  123. def installPackages(packagesString):
  124.     if packagesString.strip() == "":
  125.         return
  126.  
  127.     #New terminal is required: we're not guaranteed user can input sudo password into editor
  128.     print("Attempting to install LaTex package(s): " + packagesString )
  129.     print("A new terminal will open and you may be prompted for your sudo password.")
  130.  
  131.     updateTLMGR()  #avoids complaints about tlmgr not being updated
  132.  
  133.     #bash command to download
  134.     bashCommand='''echo \\"Attempting to install LaTeX package(s): {0} \\"
  135. echo \\"(Some of them might not be real.)\\n\\"
  136. sudo tlmgr install {0}'''.format(packagesString)
  137.  
  138.     spawnInNewTerminal(bashCommand)
  139.  
  140. def readFromProcess(process):
  141.     output = ""
  142.     for line in process.stdout:
  143.         line = line.decode("UTF-8")
  144.         output += line
  145.         print(line, end="")
  146.  
  147.     returnCode = None
  148.     while returnCode == None:
  149.         returnCode = process.poll()
  150.  
  151.     return (output, returnCode)
  152.  
  153. ### MAIN PROGRAM ###
  154. licenseinfo = """texliveonfly.py Copyright (C) 2011 Saitulaa Naranong
  155. This program comes with ABSOLUTELY NO WARRANTY;
  156. See the GNU General Public License v3 for more info."""
  157.  
  158. defaultArgs = "-synctex=1 -interaction=nonstopmode"
  159.  
  160. if __name__ == '__main__':
  161.     # Parse command line
  162.     parser = optparse.OptionParser(
  163.         usage="\n\n\t%prog [options] file.tex\n\nUse option --help for more info.\n\n" + licenseinfo ,
  164.         version='2011.24.9',
  165.         conflict_handler='resolve'
  166.     )
  167.  
  168.     parser.add_option('-h', '--help', action='help', help='print this help text and exit')
  169.     parser.add_option('-e', '--engine', dest='engine', metavar='ENGINE',
  170.         help='your LaTeX compiler; defaults to lualatex', default="lualatex")
  171.     parser.add_option('-a', '--arguments', dest='arguments', metavar='ARGS',
  172.         help='arguments to send to engine; default is: "{0}"'.format(defaultArgs) , default=defaultArgs)
  173.     parser.add_option('-f', '--fail_silently', action = "store_true" , dest='fail_silently',
  174.         help="If tlmgr cannot be found, compile document anyway.", default=False)
  175.  
  176.     (options, args) = parser.parse_args()
  177.  
  178.     if len(args) == 0:
  179.         parser.error( "{0}: You must specify a .tex file to compile.".format(scriptName) )
  180.  
  181.     texDoc = args[0]
  182.  
  183.     if "not found" in subprocess.getoutput("tlmgr"):
  184.         if options.fail_silently:
  185.             subprocess.getoutput( options.engine + ' ' + options.arguments + ' "' + texDoc + '"')
  186.             sys.exit(0)
  187.         else:
  188.             parser.error( "{0}: It appears tlmgr is not installed.  Are you sure you have TeX Live 2010 or later?".format(scriptName) )
  189.  
  190.     #loop constraints
  191.     done = False
  192.     previousFile = ""
  193.     previousFontFile = ""
  194.     previousFont =""
  195.  
  196.     #keeps running until all missing font/file errors are gone, or the same ones persist in all categories
  197.     while not done:
  198.         try:
  199.             process = subprocess.Popen([options.engine] + shlex.split(options.arguments) + [texDoc], stdin=sys.stdin, stdout = subprocess.PIPE )
  200.             (output, returnCode) = readFromProcess(process)
  201.         except OSError:
  202.             print( "{0} Unable to start {1}; are you sure it is installed?".format(scriptName, options.engine) )
  203.             sys.exit(1)
  204.  
  205.         #most reliable: searches for missing file
  206.         filesSearch = re.findall(r"! LaTeX Error: File `([^`']*)' not found" , output) + re.findall(r"! I can't find file `([^`']*)'." , output)
  207.         filesSearch = [ name for name in filesSearch if name != texDoc ]  #strips our .tex doc from list of files
  208.         #next most reliable: infers filename from font error
  209.         fontsFileSearch = [ name + ".tfm" for name in re.findall(r"! Font \\[^=]*=([^\s]*)\s", output) ]
  210.         #brute force search for font name in files
  211.         fontsSearch =  re.findall(r"! Font [^\n]*file\:([^\:\n]*)\:", output) + re.findall(r"! Font \\[^/]*/([^/]*)/", output)
  212.  
  213.         if len(filesSearch) > 0 and filesSearch[0] != previousFile:
  214.             installPackages(getFilePackage(filesSearch[0]))
  215.             previousFile = filesSearch[0]
  216.         elif len(fontsFileSearch) > 0 and fontsFileSearch[0] != previousFontFile:
  217.             installPackages(getFilePackage(fontsFileSearch[0]))
  218.             previousFontFile = fontsFileSearch[0]
  219.         elif len(fontsSearch) > 0 and fontsSearch[0] != previousFont:
  220.             installPackages(getFontPackage(fontsSearch[0]))
  221.             previousFont = fontsSearch[0]
  222.         else:
  223.             done = True
  224.  
  225.     sys.exit(returnCode)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement