1 | #! /usr/bin/env python |
---|
2 | # encoding: utf-8 |
---|
3 | # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file |
---|
4 | |
---|
5 | import os,re |
---|
6 | from waflib import Utils,Task,Errors,Logs |
---|
7 | from waflib.TaskGen import feature,before_method |
---|
8 | re_bibunit=re.compile(r'\\(?P<type>putbib)\[(?P<file>[^\[\]]*)\]',re.M) |
---|
9 | def bibunitscan(self): |
---|
10 | node=self.inputs[0] |
---|
11 | nodes=[] |
---|
12 | if not node:return nodes |
---|
13 | code=node.read() |
---|
14 | for match in re_bibunit.finditer(code): |
---|
15 | path=match.group('file') |
---|
16 | if path: |
---|
17 | for k in['','.bib']: |
---|
18 | Logs.debug('tex: trying %s%s'%(path,k)) |
---|
19 | fi=node.parent.find_resource(path+k) |
---|
20 | if fi: |
---|
21 | nodes.append(fi) |
---|
22 | else: |
---|
23 | Logs.debug('tex: could not find %s'%path) |
---|
24 | Logs.debug("tex: found the following bibunit files: %s"%nodes) |
---|
25 | return nodes |
---|
26 | exts_deps_tex=['','.ltx','.tex','.bib','.pdf','.png','.eps','.ps'] |
---|
27 | exts_tex=['.ltx','.tex'] |
---|
28 | re_tex=re.compile(r'\\(?P<type>include|bibliography|putbib|includegraphics|input|import|bringin|lstinputlisting)(\[[^\[\]]*\])?{(?P<file>[^{}]*)}',re.M) |
---|
29 | g_bibtex_re=re.compile('bibdata',re.M) |
---|
30 | class tex(Task.Task): |
---|
31 | bibtex_fun,_=Task.compile_fun('${BIBTEX} ${BIBTEXFLAGS} ${SRCFILE}',shell=False) |
---|
32 | bibtex_fun.__doc__=""" |
---|
33 | Execute the program **bibtex** |
---|
34 | """ |
---|
35 | makeindex_fun,_=Task.compile_fun('${MAKEINDEX} ${MAKEINDEXFLAGS} ${SRCFILE}',shell=False) |
---|
36 | makeindex_fun.__doc__=""" |
---|
37 | Execute the program **makeindex** |
---|
38 | """ |
---|
39 | def exec_command(self,cmd,**kw): |
---|
40 | bld=self.generator.bld |
---|
41 | try: |
---|
42 | if not kw.get('cwd',None): |
---|
43 | kw['cwd']=bld.cwd |
---|
44 | except AttributeError: |
---|
45 | bld.cwd=kw['cwd']=bld.variant_dir |
---|
46 | return Utils.subprocess.Popen(cmd,**kw).wait() |
---|
47 | def scan_aux(self,node): |
---|
48 | nodes=[node] |
---|
49 | re_aux=re.compile(r'\\@input{(?P<file>[^{}]*)}',re.M) |
---|
50 | def parse_node(node): |
---|
51 | code=node.read() |
---|
52 | for match in re_aux.finditer(code): |
---|
53 | path=match.group('file') |
---|
54 | found=node.parent.find_or_declare(path) |
---|
55 | if found and found not in nodes: |
---|
56 | Logs.debug('tex: found aux node '+found.abspath()) |
---|
57 | nodes.append(found) |
---|
58 | parse_node(found) |
---|
59 | parse_node(node) |
---|
60 | return nodes |
---|
61 | def scan(self): |
---|
62 | node=self.inputs[0] |
---|
63 | nodes=[] |
---|
64 | names=[] |
---|
65 | seen=[] |
---|
66 | if not node:return(nodes,names) |
---|
67 | def parse_node(node): |
---|
68 | if node in seen: |
---|
69 | return |
---|
70 | seen.append(node) |
---|
71 | code=node.read() |
---|
72 | global re_tex |
---|
73 | for match in re_tex.finditer(code): |
---|
74 | for path in match.group('file').split(','): |
---|
75 | if path: |
---|
76 | add_name=True |
---|
77 | found=None |
---|
78 | for k in exts_deps_tex: |
---|
79 | Logs.debug('tex: trying %s%s'%(path,k)) |
---|
80 | found=node.parent.find_resource(path+k) |
---|
81 | if found and not found in self.outputs: |
---|
82 | nodes.append(found) |
---|
83 | add_name=False |
---|
84 | for ext in exts_tex: |
---|
85 | if found.name.endswith(ext): |
---|
86 | parse_node(found) |
---|
87 | break |
---|
88 | if add_name: |
---|
89 | names.append(path) |
---|
90 | parse_node(node) |
---|
91 | for x in nodes: |
---|
92 | x.parent.get_bld().mkdir() |
---|
93 | Logs.debug("tex: found the following : %s and names %s"%(nodes,names)) |
---|
94 | return(nodes,names) |
---|
95 | def check_status(self,msg,retcode): |
---|
96 | if retcode!=0: |
---|
97 | raise Errors.WafError("%r command exit status %r"%(msg,retcode)) |
---|
98 | def bibfile(self): |
---|
99 | need_bibtex=False |
---|
100 | try: |
---|
101 | for aux_node in self.aux_nodes: |
---|
102 | ct=aux_node.read() |
---|
103 | if g_bibtex_re.findall(ct): |
---|
104 | need_bibtex=True |
---|
105 | break |
---|
106 | except(OSError,IOError): |
---|
107 | Logs.error('error bibtex scan') |
---|
108 | else: |
---|
109 | if need_bibtex: |
---|
110 | Logs.warn('calling bibtex') |
---|
111 | self.env.env={} |
---|
112 | self.env.env.update(os.environ) |
---|
113 | self.env.env.update({'BIBINPUTS':self.TEXINPUTS,'BSTINPUTS':self.TEXINPUTS}) |
---|
114 | self.env.SRCFILE=self.aux_nodes[0].name[:-4] |
---|
115 | self.check_status('error when calling bibtex',self.bibtex_fun()) |
---|
116 | def bibunits(self): |
---|
117 | try: |
---|
118 | bibunits=bibunitscan(self) |
---|
119 | except OSError: |
---|
120 | Logs.error('error bibunitscan') |
---|
121 | else: |
---|
122 | if bibunits: |
---|
123 | fn=['bu'+str(i)for i in xrange(1,len(bibunits)+1)] |
---|
124 | if fn: |
---|
125 | Logs.warn('calling bibtex on bibunits') |
---|
126 | for f in fn: |
---|
127 | self.env.env={'BIBINPUTS':self.TEXINPUTS,'BSTINPUTS':self.TEXINPUTS} |
---|
128 | self.env.SRCFILE=f |
---|
129 | self.check_status('error when calling bibtex',self.bibtex_fun()) |
---|
130 | def makeindex(self): |
---|
131 | try: |
---|
132 | idx_path=self.idx_node.abspath() |
---|
133 | os.stat(idx_path) |
---|
134 | except OSError: |
---|
135 | Logs.warn('index file %s absent, not calling makeindex'%idx_path) |
---|
136 | else: |
---|
137 | Logs.warn('calling makeindex') |
---|
138 | self.env.SRCFILE=self.idx_node.name |
---|
139 | self.env.env={} |
---|
140 | self.check_status('error when calling makeindex %s'%idx_path,self.makeindex_fun()) |
---|
141 | def run(self): |
---|
142 | env=self.env |
---|
143 | if not env['PROMPT_LATEX']: |
---|
144 | env.append_value('LATEXFLAGS','-interaction=batchmode') |
---|
145 | env.append_value('PDFLATEXFLAGS','-interaction=batchmode') |
---|
146 | env.append_value('XELATEXFLAGS','-interaction=batchmode') |
---|
147 | fun=self.texfun |
---|
148 | node=self.inputs[0] |
---|
149 | srcfile=node.abspath() |
---|
150 | texinputs=self.env.TEXINPUTS or'' |
---|
151 | self.TEXINPUTS=node.parent.get_bld().abspath()+os.pathsep+node.parent.get_src().abspath()+os.pathsep+texinputs+os.pathsep |
---|
152 | self.cwd=self.inputs[0].parent.get_bld().abspath() |
---|
153 | Logs.warn('first pass on %s'%self.__class__.__name__) |
---|
154 | self.env.env={} |
---|
155 | self.env.env.update(os.environ) |
---|
156 | self.env.env.update({'TEXINPUTS':self.TEXINPUTS}) |
---|
157 | self.env.SRCFILE=srcfile |
---|
158 | self.check_status('error when calling latex',fun()) |
---|
159 | self.aux_nodes=self.scan_aux(node.change_ext('.aux')) |
---|
160 | self.idx_node=node.change_ext('.idx') |
---|
161 | self.bibfile() |
---|
162 | self.bibunits() |
---|
163 | self.makeindex() |
---|
164 | hash='' |
---|
165 | for i in range(10): |
---|
166 | prev_hash=hash |
---|
167 | try: |
---|
168 | hashes=[Utils.h_file(x.abspath())for x in self.aux_nodes] |
---|
169 | hash=Utils.h_list(hashes) |
---|
170 | except(OSError,IOError): |
---|
171 | Logs.error('could not read aux.h') |
---|
172 | pass |
---|
173 | if hash and hash==prev_hash: |
---|
174 | break |
---|
175 | Logs.warn('calling %s'%self.__class__.__name__) |
---|
176 | self.env.env={} |
---|
177 | self.env.env.update(os.environ) |
---|
178 | self.env.env.update({'TEXINPUTS':self.TEXINPUTS}) |
---|
179 | self.env.SRCFILE=srcfile |
---|
180 | self.check_status('error when calling %s'%self.__class__.__name__,fun()) |
---|
181 | class latex(tex): |
---|
182 | texfun,vars=Task.compile_fun('${LATEX} ${LATEXFLAGS} ${SRCFILE}',shell=False) |
---|
183 | class pdflatex(tex): |
---|
184 | texfun,vars=Task.compile_fun('${PDFLATEX} ${PDFLATEXFLAGS} ${SRCFILE}',shell=False) |
---|
185 | class xelatex(tex): |
---|
186 | texfun,vars=Task.compile_fun('${XELATEX} ${XELATEXFLAGS} ${SRCFILE}',shell=False) |
---|
187 | class dvips(Task.Task): |
---|
188 | run_str='${DVIPS} ${DVIPSFLAGS} ${SRC} -o ${TGT}' |
---|
189 | color='BLUE' |
---|
190 | after=['latex','pdflatex','xelatex'] |
---|
191 | class dvipdf(Task.Task): |
---|
192 | run_str='${DVIPDF} ${DVIPDFFLAGS} ${SRC} ${TGT}' |
---|
193 | color='BLUE' |
---|
194 | after=['latex','pdflatex','xelatex'] |
---|
195 | class pdf2ps(Task.Task): |
---|
196 | run_str='${PDF2PS} ${PDF2PSFLAGS} ${SRC} ${TGT}' |
---|
197 | color='BLUE' |
---|
198 | after=['latex','pdflatex','xelatex'] |
---|
199 | @feature('tex') |
---|
200 | @before_method('process_source') |
---|
201 | def apply_tex(self): |
---|
202 | if not getattr(self,'type',None)in['latex','pdflatex','xelatex']: |
---|
203 | self.type='pdflatex' |
---|
204 | tree=self.bld |
---|
205 | outs=Utils.to_list(getattr(self,'outs',[])) |
---|
206 | self.env['PROMPT_LATEX']=getattr(self,'prompt',1) |
---|
207 | deps_lst=[] |
---|
208 | if getattr(self,'deps',None): |
---|
209 | deps=self.to_list(self.deps) |
---|
210 | for filename in deps: |
---|
211 | n=self.path.find_resource(filename) |
---|
212 | if not n: |
---|
213 | self.bld.fatal('Could not find %r for %r'%(filename,self)) |
---|
214 | if not n in deps_lst: |
---|
215 | deps_lst.append(n) |
---|
216 | for node in self.to_nodes(self.source): |
---|
217 | if self.type=='latex': |
---|
218 | task=self.create_task('latex',node,node.change_ext('.dvi')) |
---|
219 | elif self.type=='pdflatex': |
---|
220 | task=self.create_task('pdflatex',node,node.change_ext('.pdf')) |
---|
221 | elif self.type=='xelatex': |
---|
222 | task=self.create_task('xelatex',node,node.change_ext('.pdf')) |
---|
223 | task.env=self.env |
---|
224 | if deps_lst: |
---|
225 | try: |
---|
226 | lst=tree.node_deps[task.uid()] |
---|
227 | for n in deps_lst: |
---|
228 | if not n in lst: |
---|
229 | lst.append(n) |
---|
230 | except KeyError: |
---|
231 | tree.node_deps[task.uid()]=deps_lst |
---|
232 | if self.type=='latex': |
---|
233 | if'ps'in outs: |
---|
234 | tsk=self.create_task('dvips',task.outputs,node.change_ext('.ps')) |
---|
235 | tsk.env.env={'TEXINPUTS':node.parent.abspath()+os.pathsep+self.path.abspath()+os.pathsep+self.path.get_bld().abspath()} |
---|
236 | if'pdf'in outs: |
---|
237 | tsk=self.create_task('dvipdf',task.outputs,node.change_ext('.pdf')) |
---|
238 | tsk.env.env={'TEXINPUTS':node.parent.abspath()+os.pathsep+self.path.abspath()+os.pathsep+self.path.get_bld().abspath()} |
---|
239 | elif self.type=='pdflatex': |
---|
240 | if'ps'in outs: |
---|
241 | self.create_task('pdf2ps',task.outputs,node.change_ext('.ps')) |
---|
242 | self.source=[] |
---|
243 | def configure(self): |
---|
244 | v=self.env |
---|
245 | for p in'tex latex pdflatex xelatex bibtex dvips dvipdf ps2pdf makeindex pdf2ps'.split(): |
---|
246 | try: |
---|
247 | self.find_program(p,var=p.upper()) |
---|
248 | except self.errors.ConfigurationError: |
---|
249 | pass |
---|
250 | v['DVIPSFLAGS']='-Ppdf' |
---|