Class: RunTeX::Job_chain

Inherits:
Object
  • Object
show all
Defined in:
lib/runtex_job_chain.rb

Overview

This class defines a process chain to translate a tex-File.

The process chain starts with a TeX-tool (LaTeX, TeX, PDFLaTeX…).

Postprocessors (dvi2ps, ps2pdf…) can be integrated here

Constant Summary collapse

MAX_TEX_RUN =

Stop for infinite TeX-runs

5
DEFAULT_OPTIONS =

Default options for TeX-Jobs. Used by Job_chain#new and Job_chain#help.

{
  :format             => :pdflatex,
  :delete_logfiles   => true,
  :delete_helpfiles => true,
  :zip_helpfiles      => true,
  :keep_summary   => true,
  :overfull => 100, #report overfull boxes > this value

  :underfull => 9000, #report underfull boxes > this badness

}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ Job_chain

Create a job and define the parameters. To execute it, use Job_chain#start()



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/runtex_job_chain.rb', line 56

def initialize( filename, options = {} )
  @options = DEFAULT_OPTIONS.merge(options)
     
  @logfiles   = []
  @helpfiles  = []
  
  @log = Log4r::Logger.new( File.basename(filename) )
  @log.level = Log4r::DEBUG
  @log.outputters = Log4r::StdoutOutputter.new('log_stdout', :level => Log4r::INFO )
  @logfiles << "#{filename}.runtex.log"
  @log.add(Log4r::FileOutputter.new('log_file', :filename => "#{@logfiles.last}", :level => Log4r::DEBUG ) )

  self.file= filename
  
  (options.keys - DEFAULT_OPTIONS.keys).each{|key|
    @log.warn( "#{self.inspect}: Unknown option-key #{key}" ) if @log.warn?
  }

end

Instance Attribute Details

#basenameObject (readonly)

Main document without extension.



78
79
80
# File 'lib/runtex_job_chain.rb', line 78

def basename
  @basename
end

#filenameObject (readonly)

Main document. This document is to be converted.



76
77
78
# File 'lib/runtex_job_chain.rb', line 76

def filename
  @filename
end

#helpfilesObject (readonly)

List with help-files. Used for Job_chain#delete_helpfiles



82
83
84
# File 'lib/runtex_job_chain.rb', line 82

def helpfiles
  @helpfiles
end

#logObject (readonly)

Access to the logger



271
272
273
# File 'lib/runtex_job_chain.rb', line 271

def log
  @log
end

#logfilesObject (readonly)

List with log-files. Used for Job_chain#delete_helpfiles



80
81
82
# File 'lib/runtex_job_chain.rb', line 80

def logfiles
  @logfiles
end

#texrunObject (readonly)

Counter for the texrun.



268
269
270
# File 'lib/runtex_job_chain.rb', line 268

def texrun
  @texrun
end

Class Method Details

.helpObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/runtex_job_chain.rb', line 34

def self.help()
  return "Help for RunTeX::Jobchain\nCodeexample>>>>>Start>>>>>>>\nrequire 'runtex'\n#~ puts RunTeX::Job_chain.help()\ntex = RunTeX::Job_chain.new('filename', {\n\#{DEFAULT_OPTIONS.map{|key, value| \"    \#{key.inspect} => \#{value.inspect}\" }.join(\",\\n\")}}\n})\ntex.start  \nrequire 'runtex'\n#~ tex.start( 'pdflatex' )\nCodeexample<<<<<<End<<<<<<<\n"
#~ Available formats and programms:

#~ #{Configuration.keys.map{|k| k =~ /tex/ ? k : nil }.compact.to_yaml}

end

Instance Method Details

#<<(step) ⇒ Object

Add a new step for the next step.



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/runtex_job_chain.rb', line 333

def << (step)
  @log.debug( "#{@latex.step} Add new step #{step.inspect}" % @texrun ) if @log.debug?
  
  case step
    when Splitindex
      @log.debug( "#{@latex.step} Replace Makeindex by Splitindex" ) if @log.debug?
      @nextsteps.pop if @nextsteps.last.is_a?(Makeindex)
    when Makeindex
      if step.options[:name] =~ /Index2/ and
          @nextsteps.last.is_a?(Makeindex) and
          @nextsteps.last.options[:name] == 'Index'
        @log.debug( "#{@latex.step} Replace Makeindex by #{step.options[:name]}" ) if @log.debug?
        @nextsteps.pop if @nextsteps.last.is_a?(Makeindex)
      end
    when Rail
      #Execute Rail only once

      @nextsteps.each{|lstep| return false if lstep.is_a?(Rail) }
  end
  @nextsteps << step
end

#add2zip(filename) ⇒ Object

Add the given file to the log-zip



355
356
357
358
359
360
361
362
363
364
365
# File 'lib/runtex_job_chain.rb', line 355

def add2zip(filename)
  zipname = "%02i/%s" % [@texrun, filename]
  
  count = 1
  while @zip.find_entry(zipname)
    count += 1
    zipname = "%02i/%s.%i" % [@texrun, filename, count]
  end
  @zip.add( zipname, filename )
  
end

#build_summary(texresult, jobreport, format) ⇒ Object

Build the summary and store it in file jobreport



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/runtex_job_chain.rb', line 218

def  build_summary( texresult, jobreport, format )
  #Get main data (filename, number of pages, size...)

  summary = texresult[:fileinfo].to_yaml
  #Get more data

  [ [:error, 'Errors'], 
    [:warning,'Warnings'],
    [:overfull, "Overfull boxes > #{@options[:overfull]}"],
    [:underfull, "Underfull boxes with badness > #{@options[:underfull]}"],
    ].each{|key, text|
    next unless texresult[key]
    next if texresult[key].empty?
    summary << "#{text}:\n"
    summary << texresult[key].to_yaml.sub(/--- \n/, '')
  }
  
  #Save the summary to a file

  File.open(jobreport, 'w'){| file |
  
    file << "==================================================\n|  Report for TeX-Job \#{filename}\"\n|   \#{Time.now.strftime('%Y-%m-%d %H:%M')}, TeX-run \#{@texrun}\n==================================================\n\n"
    file << "#{'='*50}\n"
    file << "LaTeX (#{format})\n"
    file << "#{'='*50}\n"
    file << summary
    @nextsteps.each{|step|
      file << "\n#{'='*50}\n"
      file << "#{step.inspect}\n"
      file << "#{'='*50}\n"
      file  << step.summary
      summary  << "#{step.inspect}: #{step.summary(:count)}\n"
    }#@nextsteps

    @stop_rerun.each{|tool, reasons|
      summary << "!! Rerun-Stopp for #{tool}!"
      file << "\n#{'='*50}\n"
      file << "!! Rerun-Stopp for #{tool}"
      file << "\n#{'='*50}\n"
      reasons.each{|reason|
        file << "- #{reason}\n"
      }
    }#@stop_rerun

  } #open file

  return summary
end

#file=(filename) ⇒ Object

Define the TeX-file



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/runtex_job_chain.rb', line 85

def file=( filename )
  @dirname = File.dirname( filename)
  @filename = File.basename( filename ) 
  #~ @basename = File.basename( filename, ".tex" ) 

  @basename = @filename.sub(/\.(tex|dtx)$/, '') 
  Dir.chdir(@dirname){
    if ! File.exist?( @filename )
      @log.fatal("File #{@filename } not found (looking in #{Dir.pwd()})") if @log.fatal?
    end
  }
end

#inspectObject

add2zip



367
368
369
# File 'lib/runtex_job_chain.rb', line 367

def inspect()
  return "<RunTeX::Job_chain #{@filename}>"
end

#please_rerun(reason) ⇒ Object

Add a reason, why an additional TeX-run is necessary.



318
319
320
321
# File 'lib/runtex_job_chain.rb', line 318

def please_rerun( reason )
  @log.debug( "%02i    Rerun necessary (#{reason})" % @texrun) if @log.debug?
  @rerun << reason    
end

#prepare_result(result) ⇒ Object

Delete result hash form empty arrays and zero-counters, build an ordered string. Sort messages by importance



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/runtex_job_chain.rb', line 301

def prepare_result( result )
  result.delete_if{|key,val| ( val.is_a?(Array) and val.empty? ) or val == 0  }
  string = []
  string << "File: #{result[:fileinfo].join(', ')}" if result[:fileinfo]    #List with filename, size , number of errors...

  string << "Errors: #{result[:error].join(', ')}" if result[:error]
  string << "Warnings: #{result[:warning].join(', ')}" if result[:warning]
  string << "Info: #{result[:info].join(', ')}" if result[:info]
  (result.keys - [:fileinfo, :file, :size, :pages, :error, :warnings, :info ]).each{|key|
    if result[key].respond_to?(:join)
      string << "#{result[key]}: #{result[key].join(', ')}"
    else
      string << "#{result[key]}: #{result[key]}"
    end
  }
  string.join('; ')
end

#run(latex) ⇒ Object

Start a run with LaTeX. Required additional tools are added with << during the TeX-run.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/runtex_job_chain.rb', line 275

def run( latex )
  
  if ! latex.is_a?(LaTeX)
    @log.fatal( "Run with a non-LaTeX-tool") if @log.fatal?
  end
  
  @texrun += 1
  @nextsteps  = []
  
  #Run LaTeX

  @latex = latex  #store it for log in << 

  @log.info( "%02i    Start TeX-run #{@texrun}" % @texrun ) if @log.info?
  texresult = latex.execute(1)
  @log.debug( "#{latex.step} -> #{prepare_result(texresult)}") if @log.debug?
  
  #Run the necessary tools

  @nextsteps.each_with_index{|step, index|
    result = step.execute( index + 2 ) #TeX is step 1

    @log.debug( "#{step.step} -> #{prepare_result(result)}") if @log.debug? and !result.empty?
  }
  #Print the end statistic

  return texresult
  
end

#start(format = ) ⇒ Object

Start the job chain.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/runtex_job_chain.rb', line 98

def start( format = @options[:format] )
  puts "Start job chain for #{@filename }"
  @log.info( "Start job chain for #{@dirname}/#{@filename }") if @log.info?
  
  @texrun = 0
  @rerun = [ nil ]   #Flag, if an additional texrun is necessary (nil, so compact can delete it again)

  @stop_rerun = {}  #Set a Rerun-Stop in case of serious problems.

  zipfilename = "#{@basename}.log.zip"
  summary = nil
  #Don't use "#{filename}.runtex.log", Thats already the log.

  jobreport = "#{filename}.runtex_summary.txt"
  
  Dir.chdir(@dirname){
    if ! File.exist?( @filename )
      @log.error( "File #{@filename} missing (dir #{@dirname})" ) if @log.error?
      return false
    end
    
    if File.exist?( zipfilename )
      #Extract previous help files

      Zip::ZipFile.open( zipfilename, Zip::ZipFile::CREATE ){|zip|
        zip.each{|f| f.extract if File.dirname("#{f}") == '.' and ! File.exist?("#{f}")}
      }
      File.delete( zipfilename)#Delete zip file. Easier then rebuild the zip.

      @log.debug( "Re-Create log/help-zip #{zipfilename}") if @log.debug?
    else
      @log.debug( "Create log/help-zip #{zipfilename}") if @log.debug?
    end
    @zip = Zip::ZipFile.new( zipfilename, Zip::ZipFile::CREATE )

    configuration = Configuration["#{format}"]
    @log.fatal("No configuration for #{format}") if ! configuration and @log.fatal
    
    while ! @rerun.empty? and @texrun < MAX_TEX_RUN
      #Write the message only, if it is the 2nd run (-> test with compact).

      @log.info( "%02i--- Rerun necessary (#{@rerun.join(', ')})" % @texrun ) if @log.info? and ! @rerun.compact.empty?
      @rerun = []
      texresult = run( LaTeX.new( self, { 
                                                    :format => configuration['call'],
                                                    :overfull => @options[:overfull],
                                                    :underfull => @options[:underfull],
                                                    }
                          ) )
      texresult = {} unless texresult
      #jobreport/summary for each step

      summary = build_summary( texresult, jobreport, format )
      @zip.add("%02i/#{jobreport}" % @texrun, jobreport )
      #The deletion is done with the end-summary


    end
    if @texrun >= MAX_TEX_RUN
      @log.warn( "Stop after #{MAX_TEX_RUN} tex runs") if @log.warn?
    end
    
    ##################

    #fixme - postprocesses

    ##################

    configuration['post'].each{|post|
      @log.error( "Post-process #{post} undefined") if @log.error?
    } if configuration['post']

    @helpfiles.uniq!
    #Save all helpfiles to the zip file.

    #In a later run, this helpfiles are extracted again.

    if @options[:zip_helpfiles]
      @log.debug( "Zip help files for later use (#{@helpfiles.join(', ')})") if @log.debug?
      @helpfiles.each{|filename|
        #~ @log.debug( "Zip #{filename} for later use") if @log.debug?

        @zip.add( filename, filename ) if File.exist?(filename)
      }
    end
    
    #fixme - I would like to add the log - does not work

    #~ filename = @log.outputters[1].filename

    #~ @log.outputters[1].close

    #~ @log.remove[1]

    #~ @zip.add( filename, filename )


    #build the summary and save it

    summary = build_summary( texresult, jobreport, format )      
    @zip.add(jobreport, jobreport)
    @logfiles << jobreport unless @options[:keep_summary]
    
    #Close must be done before the files are deleted.

    @zip.close

    #Delete logfiles if requested

    if @options[:delete_logfiles]
      @logfiles.delete_if{|filename| ! File.exist?(filename) }  #delete for log

      @log.debug( "Delete log files #{@logfiles.join(', ')}") if @log.debug?
      @logfiles.each{|filename|
        #~ @log.debug( "Delete #{filename}") if @log.debug?

        begin; File.delete(filename) if File.exist?(filename)
        rescue Errno::EACCES => err
          @log.error( "Delete #{filename}: #{err}") if @log.error?
        end
      }
    end
    #Delete helpfiles if requested

    if @options[:delete_helpfiles]
      @helpfiles.delete_if{|filename| ! File.exist?(filename) }  #delete for log

      @log.debug( "Delete helpfiles #{@helpfiles.join(', ')}") if @log.debug?
      @helpfiles.each{|filename|
        #~ @log.debug( "Delete #{filename}") if @log.debug?

        begin; File.delete(filename) if File.exist?(filename)
        rescue Errno::EACCES => err
          #Happens with rail.exe at xxx.rai-file

          @log.error( "Delete #{filename}: #{err}") if @log.error?
        end
      }
    end

  } #Dir.chdir(@dirname)

  puts summary
  puts "More Details see #{jobreport} or #{zipfilename}"
  return summary  #for other usages (e.g.runtex_gui.rb..)

end

#stop_rerun(tool, reason) ⇒ Object

Set a Rerun-Stop in case of serious problems. Contains Symbols with name of the tool which should be blocked.



324
325
326
327
# File 'lib/runtex_job_chain.rb', line 324

def stop_rerun( tool, reason )
  @stop_rerun[tool] = [] unless @stop_rerun[tool]
  @stop_rerun[tool] << reason
end

#stop_rerun?(tool) ⇒ Boolean

Is there a rerun-stop for a special tool?

Returns:

  • (Boolean)


329
330
331
# File 'lib/runtex_job_chain.rb', line 329

def stop_rerun?( tool )
  return @stop_rerun.keys.include?(tool)
end