You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

987 lines
39 KiB

6 years ago
  1. """SCons.Tool.tex
  2. Tool-specific initialization for TeX.
  3. Generates .dvi files from .tex files
  4. There normally shouldn't be any need to import this module directly.
  5. It will usually be imported through the generic SCons.Tool.Tool()
  6. selection method.
  7. """
  8. #
  9. # Copyright (c) 2001 - 2017 The SCons Foundation
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining
  12. # a copy of this software and associated documentation files (the
  13. # "Software"), to deal in the Software without restriction, including
  14. # without limitation the rights to use, copy, modify, merge, publish,
  15. # distribute, sublicense, and/or sell copies of the Software, and to
  16. # permit persons to whom the Software is furnished to do so, subject to
  17. # the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included
  20. # in all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  23. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  24. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. #
  30. from __future__ import print_function
  31. __revision__ = "src/engine/SCons/Tool/tex.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  32. import os.path
  33. import re
  34. import shutil
  35. import sys
  36. import platform
  37. import glob
  38. import SCons.Action
  39. import SCons.Node
  40. import SCons.Node.FS
  41. import SCons.Util
  42. import SCons.Scanner.LaTeX
  43. Verbose = False
  44. must_rerun_latex = True
  45. # these are files that just need to be checked for changes and then rerun latex
  46. check_suffixes = ['.toc', '.lof', '.lot', '.out', '.nav', '.snm']
  47. # these are files that require bibtex or makeindex to be run when they change
  48. all_suffixes = check_suffixes + ['.bbl', '.idx', '.nlo', '.glo', '.acn', '.bcf']
  49. #
  50. # regular expressions used to search for Latex features
  51. # or outputs that require rerunning latex
  52. #
  53. # search for all .aux files opened by latex (recorded in the .fls file)
  54. openout_aux_re = re.compile(r"OUTPUT *(.*\.aux)")
  55. # search for all .bcf files opened by latex (recorded in the .fls file)
  56. # for use by biber
  57. openout_bcf_re = re.compile(r"OUTPUT *(.*\.bcf)")
  58. #printindex_re = re.compile(r"^[^%]*\\printindex", re.MULTILINE)
  59. #printnomenclature_re = re.compile(r"^[^%]*\\printnomenclature", re.MULTILINE)
  60. #printglossary_re = re.compile(r"^[^%]*\\printglossary", re.MULTILINE)
  61. # search to find rerun warnings
  62. warning_rerun_str = '(^LaTeX Warning:.*Rerun)|(^Package \w+ Warning:.*Rerun)'
  63. warning_rerun_re = re.compile(warning_rerun_str, re.MULTILINE)
  64. # search to find citation rerun warnings
  65. rerun_citations_str = "^LaTeX Warning:.*\n.*Rerun to get citations correct"
  66. rerun_citations_re = re.compile(rerun_citations_str, re.MULTILINE)
  67. # search to find undefined references or citations warnings
  68. undefined_references_str = '(^LaTeX Warning:.*undefined references)|(^Package \w+ Warning:.*undefined citations)'
  69. undefined_references_re = re.compile(undefined_references_str, re.MULTILINE)
  70. # used by the emitter
  71. auxfile_re = re.compile(r".", re.MULTILINE)
  72. tableofcontents_re = re.compile(r"^[^%\n]*\\tableofcontents", re.MULTILINE)
  73. makeindex_re = re.compile(r"^[^%\n]*\\makeindex", re.MULTILINE)
  74. bibliography_re = re.compile(r"^[^%\n]*\\bibliography", re.MULTILINE)
  75. bibunit_re = re.compile(r"^[^%\n]*\\begin\{bibunit\}", re.MULTILINE)
  76. multibib_re = re.compile(r"^[^%\n]*\\newcites\{([^\}]*)\}", re.MULTILINE)
  77. addbibresource_re = re.compile(r"^[^%\n]*\\(addbibresource|addglobalbib|addsectionbib)", re.MULTILINE)
  78. listoffigures_re = re.compile(r"^[^%\n]*\\listoffigures", re.MULTILINE)
  79. listoftables_re = re.compile(r"^[^%\n]*\\listoftables", re.MULTILINE)
  80. hyperref_re = re.compile(r"^[^%\n]*\\usepackage.*\{hyperref\}", re.MULTILINE)
  81. makenomenclature_re = re.compile(r"^[^%\n]*\\makenomenclature", re.MULTILINE)
  82. makeglossary_re = re.compile(r"^[^%\n]*\\makeglossary", re.MULTILINE)
  83. makeglossaries_re = re.compile(r"^[^%\n]*\\makeglossaries", re.MULTILINE)
  84. makeacronyms_re = re.compile(r"^[^%\n]*\\makeglossaries", re.MULTILINE)
  85. beamer_re = re.compile(r"^[^%\n]*\\documentclass\{beamer\}", re.MULTILINE)
  86. regex = r'^[^%\n]*\\newglossary\s*\[([^\]]+)\]?\s*\{([^}]*)\}\s*\{([^}]*)\}\s*\{([^}]*)\}\s*\{([^}]*)\}'
  87. newglossary_re = re.compile(regex, re.MULTILINE)
  88. biblatex_re = re.compile(r"^[^%\n]*\\usepackage.*\{biblatex\}", re.MULTILINE)
  89. newglossary_suffix = []
  90. # search to find all files included by Latex
  91. include_re = re.compile(r'^[^%\n]*\\(?:include|input){([^}]*)}', re.MULTILINE)
  92. includeOnly_re = re.compile(r'^[^%\n]*\\(?:include){([^}]*)}', re.MULTILINE)
  93. # search to find all graphics files included by Latex
  94. includegraphics_re = re.compile(r'^[^%\n]*\\(?:includegraphics(?:\[[^\]]+\])?){([^}]*)}', re.MULTILINE)
  95. # search to find all files opened by Latex (recorded in .log file)
  96. openout_re = re.compile(r"OUTPUT *(.*)")
  97. # list of graphics file extensions for TeX and LaTeX
  98. TexGraphics = SCons.Scanner.LaTeX.TexGraphics
  99. LatexGraphics = SCons.Scanner.LaTeX.LatexGraphics
  100. # An Action sufficient to build any generic tex file.
  101. TeXAction = None
  102. # An action to build a latex file. This action might be needed more
  103. # than once if we are dealing with labels and bibtex.
  104. LaTeXAction = None
  105. # An action to run BibTeX on a file.
  106. BibTeXAction = None
  107. # An action to run Biber on a file.
  108. BiberAction = None
  109. # An action to run MakeIndex on a file.
  110. MakeIndexAction = None
  111. # An action to run MakeIndex (for nomencl) on a file.
  112. MakeNclAction = None
  113. # An action to run MakeIndex (for glossary) on a file.
  114. MakeGlossaryAction = None
  115. # An action to run MakeIndex (for acronyms) on a file.
  116. MakeAcronymsAction = None
  117. # An action to run MakeIndex (for newglossary commands) on a file.
  118. MakeNewGlossaryAction = None
  119. # Used as a return value of modify_env_var if the variable is not set.
  120. _null = SCons.Scanner.LaTeX._null
  121. modify_env_var = SCons.Scanner.LaTeX.modify_env_var
  122. def check_file_error_message(utility, filename='log'):
  123. msg = '%s returned an error, check the %s file\n' % (utility, filename)
  124. sys.stdout.write(msg)
  125. def FindFile(name,suffixes,paths,env,requireExt=False):
  126. if requireExt:
  127. name,ext = SCons.Util.splitext(name)
  128. # if the user gave an extension use it.
  129. if ext:
  130. name = name + ext
  131. if Verbose:
  132. print(" searching for '%s' with extensions: " % name,suffixes)
  133. for path in paths:
  134. testName = os.path.join(path,name)
  135. if Verbose:
  136. print(" look for '%s'" % testName)
  137. if os.path.isfile(testName):
  138. if Verbose:
  139. print(" found '%s'" % testName)
  140. return env.fs.File(testName)
  141. else:
  142. name_ext = SCons.Util.splitext(testName)[1]
  143. if name_ext:
  144. continue
  145. # if no suffix try adding those passed in
  146. for suffix in suffixes:
  147. testNameExt = testName + suffix
  148. if Verbose:
  149. print(" look for '%s'" % testNameExt)
  150. if os.path.isfile(testNameExt):
  151. if Verbose:
  152. print(" found '%s'" % testNameExt)
  153. return env.fs.File(testNameExt)
  154. if Verbose:
  155. print(" did not find '%s'" % name)
  156. return None
  157. def InternalLaTeXAuxAction(XXXLaTeXAction, target = None, source= None, env=None):
  158. """A builder for LaTeX files that checks the output in the aux file
  159. and decides how many times to use LaTeXAction, and BibTeXAction."""
  160. global must_rerun_latex
  161. # This routine is called with two actions. In this file for DVI builds
  162. # with LaTeXAction and from the pdflatex.py with PDFLaTeXAction
  163. # set this up now for the case where the user requests a different extension
  164. # for the target filename
  165. if (XXXLaTeXAction == LaTeXAction):
  166. callerSuffix = ".dvi"
  167. else:
  168. callerSuffix = env['PDFSUFFIX']
  169. basename = SCons.Util.splitext(str(source[0]))[0]
  170. basedir = os.path.split(str(source[0]))[0]
  171. basefile = os.path.split(str(basename))[1]
  172. abspath = os.path.abspath(basedir)
  173. targetext = os.path.splitext(str(target[0]))[1]
  174. targetdir = os.path.split(str(target[0]))[0]
  175. saved_env = {}
  176. for var in SCons.Scanner.LaTeX.LaTeX.env_variables:
  177. saved_env[var] = modify_env_var(env, var, abspath)
  178. # Create base file names with the target directory since the auxiliary files
  179. # will be made there. That's because the *COM variables have the cd
  180. # command in the prolog. We check
  181. # for the existence of files before opening them--even ones like the
  182. # aux file that TeX always creates--to make it possible to write tests
  183. # with stubs that don't necessarily generate all of the same files.
  184. targetbase = os.path.join(targetdir, basefile)
  185. # if there is a \makeindex there will be a .idx and thus
  186. # we have to run makeindex at least once to keep the build
  187. # happy even if there is no index.
  188. # Same for glossaries, nomenclature, and acronyms
  189. src_content = source[0].get_text_contents()
  190. run_makeindex = makeindex_re.search(src_content) and not os.path.isfile(targetbase + '.idx')
  191. run_nomenclature = makenomenclature_re.search(src_content) and not os.path.isfile(targetbase + '.nlo')
  192. run_glossary = makeglossary_re.search(src_content) and not os.path.isfile(targetbase + '.glo')
  193. run_glossaries = makeglossaries_re.search(src_content) and not os.path.isfile(targetbase + '.glo')
  194. run_acronyms = makeacronyms_re.search(src_content) and not os.path.isfile(targetbase + '.acn')
  195. saved_hashes = {}
  196. suffix_nodes = {}
  197. for suffix in all_suffixes+sum(newglossary_suffix, []):
  198. theNode = env.fs.File(targetbase + suffix)
  199. suffix_nodes[suffix] = theNode
  200. saved_hashes[suffix] = theNode.get_csig()
  201. if Verbose:
  202. print("hashes: ",saved_hashes)
  203. must_rerun_latex = True
  204. # .aux files already processed by BibTex
  205. already_bibtexed = []
  206. #
  207. # routine to update MD5 hash and compare
  208. #
  209. def check_MD5(filenode, suffix):
  210. global must_rerun_latex
  211. # two calls to clear old csig
  212. filenode.clear_memoized_values()
  213. filenode.ninfo = filenode.new_ninfo()
  214. new_md5 = filenode.get_csig()
  215. if saved_hashes[suffix] == new_md5:
  216. if Verbose:
  217. print("file %s not changed" % (targetbase+suffix))
  218. return False # unchanged
  219. saved_hashes[suffix] = new_md5
  220. must_rerun_latex = True
  221. if Verbose:
  222. print("file %s changed, rerunning Latex, new hash = " % (targetbase+suffix), new_md5)
  223. return True # changed
  224. # generate the file name that latex will generate
  225. resultfilename = targetbase + callerSuffix
  226. count = 0
  227. while (must_rerun_latex and count < int(env.subst('$LATEXRETRIES'))) :
  228. result = XXXLaTeXAction(target, source, env)
  229. if result != 0:
  230. return result
  231. count = count + 1
  232. must_rerun_latex = False
  233. # Decide if various things need to be run, or run again.
  234. # Read the log file to find warnings/errors
  235. logfilename = targetbase + '.log'
  236. logContent = ''
  237. if os.path.isfile(logfilename):
  238. logContent = open(logfilename, "r").read()
  239. # Read the fls file to find all .aux files
  240. flsfilename = targetbase + '.fls'
  241. flsContent = ''
  242. auxfiles = []
  243. if os.path.isfile(flsfilename):
  244. flsContent = open(flsfilename, "r").read()
  245. auxfiles = openout_aux_re.findall(flsContent)
  246. # remove duplicates
  247. dups = {}
  248. for x in auxfiles:
  249. dups[x] = 1
  250. auxfiles = list(dups.keys())
  251. bcffiles = []
  252. if os.path.isfile(flsfilename):
  253. flsContent = open(flsfilename, "r").read()
  254. bcffiles = openout_bcf_re.findall(flsContent)
  255. # remove duplicates
  256. dups = {}
  257. for x in bcffiles:
  258. dups[x] = 1
  259. bcffiles = list(dups.keys())
  260. if Verbose:
  261. print("auxfiles ",auxfiles)
  262. print("bcffiles ",bcffiles)
  263. # Now decide if bibtex will need to be run.
  264. # The information that bibtex reads from the .aux file is
  265. # pass-independent. If we find (below) that the .bbl file is unchanged,
  266. # then the last latex saw a correct bibliography.
  267. # Therefore only do this once
  268. # Go through all .aux files and remember the files already done.
  269. for auxfilename in auxfiles:
  270. if auxfilename not in already_bibtexed:
  271. already_bibtexed.append(auxfilename)
  272. target_aux = os.path.join(targetdir, auxfilename)
  273. if os.path.isfile(target_aux):
  274. content = open(target_aux, "r").read()
  275. if content.find("bibdata") != -1:
  276. if Verbose:
  277. print("Need to run bibtex on ",auxfilename)
  278. bibfile = env.fs.File(SCons.Util.splitext(target_aux)[0])
  279. result = BibTeXAction(bibfile, bibfile, env)
  280. if result != 0:
  281. check_file_error_message(env['BIBTEX'], 'blg')
  282. must_rerun_latex = True
  283. # Now decide if biber will need to be run.
  284. # When the backend for biblatex is biber (by choice or default) the
  285. # citation information is put in the .bcf file.
  286. # The information that biber reads from the .bcf file is
  287. # pass-independent. If we find (below) that the .bbl file is unchanged,
  288. # then the last latex saw a correct bibliography.
  289. # Therefore only do this once
  290. # Go through all .bcf files and remember the files already done.
  291. for bcffilename in bcffiles:
  292. if bcffilename not in already_bibtexed:
  293. already_bibtexed.append(bcffilename)
  294. target_bcf = os.path.join(targetdir, bcffilename)
  295. if os.path.isfile(target_bcf):
  296. content = open(target_bcf, "r").read()
  297. if content.find("bibdata") != -1:
  298. if Verbose:
  299. print("Need to run biber on ",bcffilename)
  300. bibfile = env.fs.File(SCons.Util.splitext(target_bcf)[0])
  301. result = BiberAction(bibfile, bibfile, env)
  302. if result != 0:
  303. check_file_error_message(env['BIBER'], 'blg')
  304. must_rerun_latex = True
  305. # Now decide if latex will need to be run again due to index.
  306. if check_MD5(suffix_nodes['.idx'],'.idx') or (count == 1 and run_makeindex):
  307. # We must run makeindex
  308. if Verbose:
  309. print("Need to run makeindex")
  310. idxfile = suffix_nodes['.idx']
  311. result = MakeIndexAction(idxfile, idxfile, env)
  312. if result != 0:
  313. check_file_error_message(env['MAKEINDEX'], 'ilg')
  314. return result
  315. # TO-DO: need to add a way for the user to extend this list for whatever
  316. # auxiliary files they create in other (or their own) packages
  317. # Harder is case is where an action needs to be called -- that should be rare (I hope?)
  318. for index in check_suffixes:
  319. check_MD5(suffix_nodes[index],index)
  320. # Now decide if latex will need to be run again due to nomenclature.
  321. if check_MD5(suffix_nodes['.nlo'],'.nlo') or (count == 1 and run_nomenclature):
  322. # We must run makeindex
  323. if Verbose:
  324. print("Need to run makeindex for nomenclature")
  325. nclfile = suffix_nodes['.nlo']
  326. result = MakeNclAction(nclfile, nclfile, env)
  327. if result != 0:
  328. check_file_error_message('%s (nomenclature)' % env['MAKENCL'],
  329. 'nlg')
  330. #return result
  331. # Now decide if latex will need to be run again due to glossary.
  332. if check_MD5(suffix_nodes['.glo'],'.glo') or (count == 1 and run_glossaries) or (count == 1 and run_glossary):
  333. # We must run makeindex
  334. if Verbose:
  335. print("Need to run makeindex for glossary")
  336. glofile = suffix_nodes['.glo']
  337. result = MakeGlossaryAction(glofile, glofile, env)
  338. if result != 0:
  339. check_file_error_message('%s (glossary)' % env['MAKEGLOSSARY'],
  340. 'glg')
  341. #return result
  342. # Now decide if latex will need to be run again due to acronyms.
  343. if check_MD5(suffix_nodes['.acn'],'.acn') or (count == 1 and run_acronyms):
  344. # We must run makeindex
  345. if Verbose:
  346. print("Need to run makeindex for acronyms")
  347. acrfile = suffix_nodes['.acn']
  348. result = MakeAcronymsAction(acrfile, acrfile, env)
  349. if result != 0:
  350. check_file_error_message('%s (acronyms)' % env['MAKEACRONYMS'],
  351. 'alg')
  352. return result
  353. # Now decide if latex will need to be run again due to newglossary command.
  354. for ig in range(len(newglossary_suffix)):
  355. if check_MD5(suffix_nodes[newglossary_suffix[ig][2]],newglossary_suffix[ig][2]) or (count == 1):
  356. # We must run makeindex
  357. if Verbose:
  358. print("Need to run makeindex for newglossary")
  359. newglfile = suffix_nodes[newglossary_suffix[ig][2]]
  360. MakeNewGlossaryAction = SCons.Action.Action("$MAKENEWGLOSSARYCOM ${SOURCE.filebase}%s -s ${SOURCE.filebase}.ist -t ${SOURCE.filebase}%s -o ${SOURCE.filebase}%s" % (newglossary_suffix[ig][2],newglossary_suffix[ig][0],newglossary_suffix[ig][1]), "$MAKENEWGLOSSARYCOMSTR")
  361. result = MakeNewGlossaryAction(newglfile, newglfile, env)
  362. if result != 0:
  363. check_file_error_message('%s (newglossary)' % env['MAKENEWGLOSSARY'],
  364. newglossary_suffix[ig][0])
  365. return result
  366. # Now decide if latex needs to be run yet again to resolve warnings.
  367. if warning_rerun_re.search(logContent):
  368. must_rerun_latex = True
  369. if Verbose:
  370. print("rerun Latex due to latex or package rerun warning")
  371. if rerun_citations_re.search(logContent):
  372. must_rerun_latex = True
  373. if Verbose:
  374. print("rerun Latex due to 'Rerun to get citations correct' warning")
  375. if undefined_references_re.search(logContent):
  376. must_rerun_latex = True
  377. if Verbose:
  378. print("rerun Latex due to undefined references or citations")
  379. if (count >= int(env.subst('$LATEXRETRIES')) and must_rerun_latex):
  380. print("reached max number of retries on Latex ,",int(env.subst('$LATEXRETRIES')))
  381. # end of while loop
  382. # rename Latex's output to what the target name is
  383. if not (str(target[0]) == resultfilename and os.path.isfile(resultfilename)):
  384. if os.path.isfile(resultfilename):
  385. print("move %s to %s" % (resultfilename, str(target[0]), ))
  386. shutil.move(resultfilename,str(target[0]))
  387. # Original comment (when TEXPICTS was not restored):
  388. # The TEXPICTS enviroment variable is needed by a dvi -> pdf step
  389. # later on Mac OSX so leave it
  390. #
  391. # It is also used when searching for pictures (implicit dependencies).
  392. # Why not set the variable again in the respective builder instead
  393. # of leaving local modifications in the environment? What if multiple
  394. # latex builds in different directories need different TEXPICTS?
  395. for var in SCons.Scanner.LaTeX.LaTeX.env_variables:
  396. if var == 'TEXPICTS':
  397. continue
  398. if saved_env[var] is _null:
  399. try:
  400. del env['ENV'][var]
  401. except KeyError:
  402. pass # was never set
  403. else:
  404. env['ENV'][var] = saved_env[var]
  405. return result
  406. def LaTeXAuxAction(target = None, source= None, env=None):
  407. result = InternalLaTeXAuxAction( LaTeXAction, target, source, env )
  408. return result
  409. LaTeX_re = re.compile("\\\\document(style|class)")
  410. def is_LaTeX(flist,env,abspath):
  411. """Scan a file list to decide if it's TeX- or LaTeX-flavored."""
  412. # We need to scan files that are included in case the
  413. # \documentclass command is in them.
  414. # get path list from both env['TEXINPUTS'] and env['ENV']['TEXINPUTS']
  415. savedpath = modify_env_var(env, 'TEXINPUTS', abspath)
  416. paths = env['ENV']['TEXINPUTS']
  417. if SCons.Util.is_List(paths):
  418. pass
  419. else:
  420. # Split at os.pathsep to convert into absolute path
  421. paths = paths.split(os.pathsep)
  422. # now that we have the path list restore the env
  423. if savedpath is _null:
  424. try:
  425. del env['ENV']['TEXINPUTS']
  426. except KeyError:
  427. pass # was never set
  428. else:
  429. env['ENV']['TEXINPUTS'] = savedpath
  430. if Verbose:
  431. print("is_LaTeX search path ",paths)
  432. print("files to search :",flist)
  433. # Now that we have the search path and file list, check each one
  434. for f in flist:
  435. if Verbose:
  436. print(" checking for Latex source ",str(f))
  437. content = f.get_text_contents()
  438. if LaTeX_re.search(content):
  439. if Verbose:
  440. print("file %s is a LaTeX file" % str(f))
  441. return 1
  442. if Verbose:
  443. print("file %s is not a LaTeX file" % str(f))
  444. # now find included files
  445. inc_files = [ ]
  446. inc_files.extend( include_re.findall(content) )
  447. if Verbose:
  448. print("files included by '%s': "%str(f),inc_files)
  449. # inc_files is list of file names as given. need to find them
  450. # using TEXINPUTS paths.
  451. # search the included files
  452. for src in inc_files:
  453. srcNode = FindFile(src,['.tex','.ltx','.latex'],paths,env,requireExt=False)
  454. # make this a list since is_LaTeX takes a list.
  455. fileList = [srcNode,]
  456. if Verbose:
  457. print("FindFile found ",srcNode)
  458. if srcNode is not None:
  459. file_test = is_LaTeX(fileList, env, abspath)
  460. # return on first file that finds latex is needed.
  461. if file_test:
  462. return file_test
  463. if Verbose:
  464. print(" done scanning ",str(f))
  465. return 0
  466. def TeXLaTeXFunction(target = None, source= None, env=None):
  467. """A builder for TeX and LaTeX that scans the source file to
  468. decide the "flavor" of the source and then executes the appropriate
  469. program."""
  470. # find these paths for use in is_LaTeX to search for included files
  471. basedir = os.path.split(str(source[0]))[0]
  472. abspath = os.path.abspath(basedir)
  473. if is_LaTeX(source,env,abspath):
  474. result = LaTeXAuxAction(target,source,env)
  475. if result != 0:
  476. check_file_error_message(env['LATEX'])
  477. else:
  478. result = TeXAction(target,source,env)
  479. if result != 0:
  480. check_file_error_message(env['TEX'])
  481. return result
  482. def TeXLaTeXStrFunction(target = None, source= None, env=None):
  483. """A strfunction for TeX and LaTeX that scans the source file to
  484. decide the "flavor" of the source and then returns the appropriate
  485. command string."""
  486. if env.GetOption("no_exec"):
  487. # find these paths for use in is_LaTeX to search for included files
  488. basedir = os.path.split(str(source[0]))[0]
  489. abspath = os.path.abspath(basedir)
  490. if is_LaTeX(source,env,abspath):
  491. result = env.subst('$LATEXCOM',0,target,source)+" ..."
  492. else:
  493. result = env.subst("$TEXCOM",0,target,source)+" ..."
  494. else:
  495. result = ''
  496. return result
  497. def tex_eps_emitter(target, source, env):
  498. """An emitter for TeX and LaTeX sources when
  499. executing tex or latex. It will accept .ps and .eps
  500. graphics files
  501. """
  502. (target, source) = tex_emitter_core(target, source, env, TexGraphics)
  503. return (target, source)
  504. def tex_pdf_emitter(target, source, env):
  505. """An emitter for TeX and LaTeX sources when
  506. executing pdftex or pdflatex. It will accept graphics
  507. files of types .pdf, .jpg, .png, .gif, and .tif
  508. """
  509. (target, source) = tex_emitter_core(target, source, env, LatexGraphics)
  510. return (target, source)
  511. def ScanFiles(theFile, target, paths, file_tests, file_tests_search, env, graphics_extensions, targetdir, aux_files):
  512. """ For theFile (a Node) update any file_tests and search for graphics files
  513. then find all included files and call ScanFiles recursively for each of them"""
  514. content = theFile.get_text_contents()
  515. if Verbose:
  516. print(" scanning ",str(theFile))
  517. for i in range(len(file_tests_search)):
  518. if file_tests[i][0] is None:
  519. if Verbose:
  520. print("scan i ",i," files_tests[i] ",file_tests[i], file_tests[i][1])
  521. file_tests[i][0] = file_tests_search[i].search(content)
  522. if Verbose and file_tests[i][0]:
  523. print(" found match for ",file_tests[i][1][-1])
  524. # for newglossary insert the suffixes in file_tests[i]
  525. if file_tests[i][0] and file_tests[i][1][-1] == 'newglossary':
  526. findresult = file_tests_search[i].findall(content)
  527. for l in range(len(findresult)) :
  528. (file_tests[i][1]).insert(0,'.'+findresult[l][3])
  529. (file_tests[i][1]).insert(0,'.'+findresult[l][2])
  530. (file_tests[i][1]).insert(0,'.'+findresult[l][0])
  531. suffix_list = ['.'+findresult[l][0],'.'+findresult[l][2],'.'+findresult[l][3] ]
  532. newglossary_suffix.append(suffix_list)
  533. if Verbose:
  534. print(" new suffixes for newglossary ",newglossary_suffix)
  535. incResult = includeOnly_re.search(content)
  536. if incResult:
  537. aux_files.append(os.path.join(targetdir, incResult.group(1)))
  538. if Verbose:
  539. print("\include file names : ", aux_files)
  540. # recursively call this on each of the included files
  541. inc_files = [ ]
  542. inc_files.extend( include_re.findall(content) )
  543. if Verbose:
  544. print("files included by '%s': "%str(theFile),inc_files)
  545. # inc_files is list of file names as given. need to find them
  546. # using TEXINPUTS paths.
  547. for src in inc_files:
  548. srcNode = FindFile(src,['.tex','.ltx','.latex'],paths,env,requireExt=False)
  549. if srcNode is not None:
  550. file_tests = ScanFiles(srcNode, target, paths, file_tests, file_tests_search, env, graphics_extensions, targetdir, aux_files)
  551. if Verbose:
  552. print(" done scanning ",str(theFile))
  553. return file_tests
  554. def tex_emitter_core(target, source, env, graphics_extensions):
  555. """An emitter for TeX and LaTeX sources.
  556. For LaTeX sources we try and find the common created files that
  557. are needed on subsequent runs of latex to finish tables of contents,
  558. bibliographies, indices, lists of figures, and hyperlink references.
  559. """
  560. basename = SCons.Util.splitext(str(source[0]))[0]
  561. basefile = os.path.split(str(basename))[1]
  562. targetdir = os.path.split(str(target[0]))[0]
  563. targetbase = os.path.join(targetdir, basefile)
  564. basedir = os.path.split(str(source[0]))[0]
  565. abspath = os.path.abspath(basedir)
  566. target[0].attributes.path = abspath
  567. #
  568. # file names we will make use of in searching the sources and log file
  569. #
  570. emit_suffixes = ['.aux', '.log', '.ilg', '.blg', '.nls', '.nlg', '.gls', '.glg', '.alg'] + all_suffixes
  571. auxfilename = targetbase + '.aux'
  572. logfilename = targetbase + '.log'
  573. flsfilename = targetbase + '.fls'
  574. syncfilename = targetbase + '.synctex.gz'
  575. env.SideEffect(auxfilename,target[0])
  576. env.SideEffect(logfilename,target[0])
  577. env.SideEffect(flsfilename,target[0])
  578. env.SideEffect(syncfilename,target[0])
  579. if Verbose:
  580. print("side effect :",auxfilename,logfilename,flsfilename,syncfilename)
  581. env.Clean(target[0],auxfilename)
  582. env.Clean(target[0],logfilename)
  583. env.Clean(target[0],flsfilename)
  584. env.Clean(target[0],syncfilename)
  585. content = source[0].get_text_contents()
  586. # set up list with the regular expressions
  587. # we use to find features used
  588. file_tests_search = [auxfile_re,
  589. makeindex_re,
  590. bibliography_re,
  591. bibunit_re,
  592. multibib_re,
  593. addbibresource_re,
  594. tableofcontents_re,
  595. listoffigures_re,
  596. listoftables_re,
  597. hyperref_re,
  598. makenomenclature_re,
  599. makeglossary_re,
  600. makeglossaries_re,
  601. makeacronyms_re,
  602. beamer_re,
  603. newglossary_re,
  604. biblatex_re ]
  605. # set up list with the file suffixes that need emitting
  606. # when a feature is found
  607. file_tests_suff = [['.aux','aux_file'],
  608. ['.idx', '.ind', '.ilg','makeindex'],
  609. ['.bbl', '.blg','bibliography'],
  610. ['.bbl', '.blg','bibunit'],
  611. ['.bbl', '.blg','multibib'],
  612. ['.bbl', '.blg','.bcf','addbibresource'],
  613. ['.toc','contents'],
  614. ['.lof','figures'],
  615. ['.lot','tables'],
  616. ['.out','hyperref'],
  617. ['.nlo', '.nls', '.nlg','nomenclature'],
  618. ['.glo', '.gls', '.glg','glossary'],
  619. ['.glo', '.gls', '.glg','glossaries'],
  620. ['.acn', '.acr', '.alg','acronyms'],
  621. ['.nav', '.snm', '.out', '.toc','beamer'],
  622. ['newglossary',],
  623. ['.bcf', '.blg','biblatex'] ]
  624. # for newglossary the suffixes are added as we find the command
  625. # build the list of lists
  626. file_tests = []
  627. for i in range(len(file_tests_search)):
  628. file_tests.append( [None, file_tests_suff[i]] )
  629. # TO-DO: need to add a way for the user to extend this list for whatever
  630. # auxiliary files they create in other (or their own) packages
  631. # get path list from both env['TEXINPUTS'] and env['ENV']['TEXINPUTS']
  632. savedpath = modify_env_var(env, 'TEXINPUTS', abspath)
  633. paths = env['ENV']['TEXINPUTS']
  634. if SCons.Util.is_List(paths):
  635. pass
  636. else:
  637. # Split at os.pathsep to convert into absolute path
  638. paths = paths.split(os.pathsep)
  639. # now that we have the path list restore the env
  640. if savedpath is _null:
  641. try:
  642. del env['ENV']['TEXINPUTS']
  643. except KeyError:
  644. pass # was never set
  645. else:
  646. env['ENV']['TEXINPUTS'] = savedpath
  647. if Verbose:
  648. print("search path ",paths)
  649. # scan all sources for side effect files
  650. aux_files = []
  651. file_tests = ScanFiles(source[0], target, paths, file_tests, file_tests_search, env, graphics_extensions, targetdir, aux_files)
  652. for (theSearch,suffix_list) in file_tests:
  653. # add side effects if feature is present.If file is to be generated,add all side effects
  654. if Verbose and theSearch:
  655. print("check side effects for ",suffix_list[-1])
  656. if (theSearch != None) or (not source[0].exists() ):
  657. file_list = [targetbase,]
  658. # for bibunit we need a list of files
  659. if suffix_list[-1] == 'bibunit':
  660. file_basename = os.path.join(targetdir, 'bu*.aux')
  661. file_list = glob.glob(file_basename)
  662. # remove the suffix '.aux'
  663. for i in range(len(file_list)):
  664. file_list.append(SCons.Util.splitext(file_list[i])[0])
  665. # for multibib we need a list of files
  666. if suffix_list[-1] == 'multibib':
  667. for multibibmatch in multibib_re.finditer(content):
  668. if Verbose:
  669. print("multibib match ",multibibmatch.group(1))
  670. if multibibmatch != None:
  671. baselist = multibibmatch.group(1).split(',')
  672. if Verbose:
  673. print("multibib list ", baselist)
  674. for i in range(len(baselist)):
  675. file_list.append(os.path.join(targetdir, baselist[i]))
  676. # now define the side effects
  677. for file_name in file_list:
  678. for suffix in suffix_list[:-1]:
  679. env.SideEffect(file_name + suffix,target[0])
  680. if Verbose:
  681. print("side effect tst :",file_name + suffix, " target is ",str(target[0]))
  682. env.Clean(target[0],file_name + suffix)
  683. for aFile in aux_files:
  684. aFile_base = SCons.Util.splitext(aFile)[0]
  685. env.SideEffect(aFile_base + '.aux',target[0])
  686. if Verbose:
  687. print("side effect aux :",aFile_base + '.aux')
  688. env.Clean(target[0],aFile_base + '.aux')
  689. # read fls file to get all other files that latex creates and will read on the next pass
  690. # remove files from list that we explicitly dealt with above
  691. if os.path.isfile(flsfilename):
  692. content = open(flsfilename, "r").read()
  693. out_files = openout_re.findall(content)
  694. myfiles = [auxfilename, logfilename, flsfilename, targetbase+'.dvi',targetbase+'.pdf']
  695. for filename in out_files[:]:
  696. if filename in myfiles:
  697. out_files.remove(filename)
  698. env.SideEffect(out_files,target[0])
  699. if Verbose:
  700. print("side effect fls :",out_files)
  701. env.Clean(target[0],out_files)
  702. return (target, source)
  703. TeXLaTeXAction = None
  704. def generate(env):
  705. """Add Builders and construction variables for TeX to an Environment."""
  706. global TeXLaTeXAction
  707. if TeXLaTeXAction is None:
  708. TeXLaTeXAction = SCons.Action.Action(TeXLaTeXFunction,
  709. strfunction=TeXLaTeXStrFunction)
  710. env.AppendUnique(LATEXSUFFIXES=SCons.Tool.LaTeXSuffixes)
  711. generate_common(env)
  712. from . import dvi
  713. dvi.generate(env)
  714. bld = env['BUILDERS']['DVI']
  715. bld.add_action('.tex', TeXLaTeXAction)
  716. bld.add_emitter('.tex', tex_eps_emitter)
  717. def generate_darwin(env):
  718. try:
  719. environ = env['ENV']
  720. except KeyError:
  721. environ = {}
  722. env['ENV'] = environ
  723. if (platform.system() == 'Darwin'):
  724. try:
  725. ospath = env['ENV']['PATHOSX']
  726. except:
  727. ospath = None
  728. if ospath:
  729. env.AppendENVPath('PATH', ospath)
  730. def generate_common(env):
  731. """Add internal Builders and construction variables for LaTeX to an Environment."""
  732. # Add OSX system paths so TeX tools can be found
  733. # when a list of tools is given the exists() method is not called
  734. generate_darwin(env)
  735. # A generic tex file Action, sufficient for all tex files.
  736. global TeXAction
  737. if TeXAction is None:
  738. TeXAction = SCons.Action.Action("$TEXCOM", "$TEXCOMSTR")
  739. # An Action to build a latex file. This might be needed more
  740. # than once if we are dealing with labels and bibtex.
  741. global LaTeXAction
  742. if LaTeXAction is None:
  743. LaTeXAction = SCons.Action.Action("$LATEXCOM", "$LATEXCOMSTR")
  744. # Define an action to run BibTeX on a file.
  745. global BibTeXAction
  746. if BibTeXAction is None:
  747. BibTeXAction = SCons.Action.Action("$BIBTEXCOM", "$BIBTEXCOMSTR")
  748. # Define an action to run Biber on a file.
  749. global BiberAction
  750. if BiberAction is None:
  751. BiberAction = SCons.Action.Action("$BIBERCOM", "$BIBERCOMSTR")
  752. # Define an action to run MakeIndex on a file.
  753. global MakeIndexAction
  754. if MakeIndexAction is None:
  755. MakeIndexAction = SCons.Action.Action("$MAKEINDEXCOM", "$MAKEINDEXCOMSTR")
  756. # Define an action to run MakeIndex on a file for nomenclatures.
  757. global MakeNclAction
  758. if MakeNclAction is None:
  759. MakeNclAction = SCons.Action.Action("$MAKENCLCOM", "$MAKENCLCOMSTR")
  760. # Define an action to run MakeIndex on a file for glossaries.
  761. global MakeGlossaryAction
  762. if MakeGlossaryAction is None:
  763. MakeGlossaryAction = SCons.Action.Action("$MAKEGLOSSARYCOM", "$MAKEGLOSSARYCOMSTR")
  764. # Define an action to run MakeIndex on a file for acronyms.
  765. global MakeAcronymsAction
  766. if MakeAcronymsAction is None:
  767. MakeAcronymsAction = SCons.Action.Action("$MAKEACRONYMSCOM", "$MAKEACRONYMSCOMSTR")
  768. try:
  769. environ = env['ENV']
  770. except KeyError:
  771. environ = {}
  772. env['ENV'] = environ
  773. # Some Linux platforms have pdflatex set up in a way
  774. # that requires that the HOME environment variable be set.
  775. # Add it here if defined.
  776. v = os.environ.get('HOME')
  777. if v:
  778. environ['HOME'] = v
  779. CDCOM = 'cd '
  780. if platform.system() == 'Windows':
  781. # allow cd command to change drives on Windows
  782. CDCOM = 'cd /D '
  783. env['TEX'] = 'tex'
  784. env['TEXFLAGS'] = SCons.Util.CLVar('-interaction=nonstopmode -recorder')
  785. env['TEXCOM'] = CDCOM + '${TARGET.dir} && $TEX $TEXFLAGS ${SOURCE.file}'
  786. env['PDFTEX'] = 'pdftex'
  787. env['PDFTEXFLAGS'] = SCons.Util.CLVar('-interaction=nonstopmode -recorder')
  788. env['PDFTEXCOM'] = CDCOM + '${TARGET.dir} && $PDFTEX $PDFTEXFLAGS ${SOURCE.file}'
  789. env['LATEX'] = 'latex'
  790. env['LATEXFLAGS'] = SCons.Util.CLVar('-interaction=nonstopmode -recorder')
  791. env['LATEXCOM'] = CDCOM + '${TARGET.dir} && $LATEX $LATEXFLAGS ${SOURCE.file}'
  792. env['LATEXRETRIES'] = 4
  793. env['PDFLATEX'] = 'pdflatex'
  794. env['PDFLATEXFLAGS'] = SCons.Util.CLVar('-interaction=nonstopmode -recorder')
  795. env['PDFLATEXCOM'] = CDCOM + '${TARGET.dir} && $PDFLATEX $PDFLATEXFLAGS ${SOURCE.file}'
  796. env['BIBTEX'] = 'bibtex'
  797. env['BIBTEXFLAGS'] = SCons.Util.CLVar('')
  798. env['BIBTEXCOM'] = CDCOM + '${TARGET.dir} && $BIBTEX $BIBTEXFLAGS ${SOURCE.filebase}'
  799. env['BIBER'] = 'biber'
  800. env['BIBERFLAGS'] = SCons.Util.CLVar('')
  801. env['BIBERCOM'] = CDCOM + '${TARGET.dir} && $BIBER $BIBERFLAGS ${SOURCE.filebase}'
  802. env['MAKEINDEX'] = 'makeindex'
  803. env['MAKEINDEXFLAGS'] = SCons.Util.CLVar('')
  804. env['MAKEINDEXCOM'] = CDCOM + '${TARGET.dir} && $MAKEINDEX $MAKEINDEXFLAGS ${SOURCE.file}'
  805. env['MAKEGLOSSARY'] = 'makeindex'
  806. env['MAKEGLOSSARYSTYLE'] = '${SOURCE.filebase}.ist'
  807. env['MAKEGLOSSARYFLAGS'] = SCons.Util.CLVar('-s ${MAKEGLOSSARYSTYLE} -t ${SOURCE.filebase}.glg')
  808. env['MAKEGLOSSARYCOM'] = CDCOM + '${TARGET.dir} && $MAKEGLOSSARY ${SOURCE.filebase}.glo $MAKEGLOSSARYFLAGS -o ${SOURCE.filebase}.gls'
  809. env['MAKEACRONYMS'] = 'makeindex'
  810. env['MAKEACRONYMSSTYLE'] = '${SOURCE.filebase}.ist'
  811. env['MAKEACRONYMSFLAGS'] = SCons.Util.CLVar('-s ${MAKEACRONYMSSTYLE} -t ${SOURCE.filebase}.alg')
  812. env['MAKEACRONYMSCOM'] = CDCOM + '${TARGET.dir} && $MAKEACRONYMS ${SOURCE.filebase}.acn $MAKEACRONYMSFLAGS -o ${SOURCE.filebase}.acr'
  813. env['MAKENCL'] = 'makeindex'
  814. env['MAKENCLSTYLE'] = 'nomencl.ist'
  815. env['MAKENCLFLAGS'] = '-s ${MAKENCLSTYLE} -t ${SOURCE.filebase}.nlg'
  816. env['MAKENCLCOM'] = CDCOM + '${TARGET.dir} && $MAKENCL ${SOURCE.filebase}.nlo $MAKENCLFLAGS -o ${SOURCE.filebase}.nls'
  817. env['MAKENEWGLOSSARY'] = 'makeindex'
  818. env['MAKENEWGLOSSARYCOM'] = CDCOM + '${TARGET.dir} && $MAKENEWGLOSSARY '
  819. def exists(env):
  820. generate_darwin(env)
  821. return env.Detect('tex')
  822. # Local Variables:
  823. # tab-width:4
  824. # indent-tabs-mode:nil
  825. # End:
  826. # vim: set expandtab tabstop=4 shiftwidth=4: