Skip to content

Commit 66bee00

Browse files
committed
Use Dir.mktmpdir for the workdir, add a :preservenothing config option to not leave files behind"
1 parent 637ed0f commit 66bee00

File tree

2 files changed

+66
-47
lines changed

2 files changed

+66
-47
lines changed

lib/rails-latex/latex_to_pdf.rb

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def self.config
1515
:default_arguments => ['-shell-escape', '-interaction=batchmode'],
1616
workdir: ->() { "#{Process.pid}-#{Thread.current.hash}" },
1717
preservework: false,
18+
preservenothing: false,
1819
basedir: File.join(Rails.root, 'tmp', 'rails-latex'),
1920
:parse_runs => 1
2021
}
@@ -47,59 +48,71 @@ def self.generate_pdf(code, config)
4748
end
4849

4950
# Create directory, prepare additional supporting files (.cls, .sty, ...)
50-
dir = File.join(config[:basedir], config[:workdir].call)
51-
input = File.join(dir, 'input.tex')
52-
FileUtils.mkdir_p(dir)
53-
supporting = config[:supporting]
54-
if supporting.kind_of?(String) or supporting.kind_of?(Pathname) or (supporting.kind_of?(Array) and supporting.length > 0)
55-
FileUtils.cp_r(supporting, dir)
56-
end
57-
File.open(input,'wb') {|io| io.write(code)}
51+
parent_tmpdir = File.join(Rails.root, 'tmp')
52+
FileUtils.mkdir_p(parent_tmpdir) unless File.directory?(parent_tmpdir)
53+
Dir.mktmpdir(["rails-latex-", "-#{config[:workdir].call}"], parent_tmpdir) do |dir|
54+
input = File.join(dir, 'input.tex')
55+
supporting = config[:supporting]
56+
if supporting.kind_of?(String) or supporting.kind_of?(Pathname) or (supporting.kind_of?(Array) and supporting.length > 0)
57+
FileUtils.cp_r(supporting, dir)
58+
end
59+
File.open(input,'wb') {|io| io.write(code)}
5860

59-
# Process recipe
60-
recipe.each do |item|
61-
command = item[:command] || config[:command]
62-
runs = item[:runs] || config[:parse_runs]
63-
args = item[:arguments] || config[:arguments] + config[:default_arguments]
64-
args += item[:extra_arguments].to_a + ['input']
65-
kwargs = {:out => ["input.log", "a"]}
66-
Rails.logger.info "Running '#{command} #{args.join(' ')}' in #{dir} #{runs} times..."
67-
Process.waitpid(
68-
fork do
69-
begin
70-
Dir.chdir dir
71-
(runs - 1).times do
72-
clean_exit = system command, *args, **kwargs
73-
Process.exit! 1 unless clean_exit
61+
# Process recipe
62+
recipe.each do |item|
63+
command = item[:command] || config[:command]
64+
runs = item[:runs] || config[:parse_runs]
65+
args = item[:arguments] || config[:arguments] + config[:default_arguments]
66+
args += item[:extra_arguments].to_a + ['input']
67+
kwargs = {:out => ["input.log", "a"]}
68+
Rails.logger.info "Running '#{command} #{args.join(' ')}' in #{dir} #{runs} times..."
69+
Process.waitpid(
70+
fork do
71+
begin
72+
Dir.chdir dir
73+
(runs - 1).times do
74+
clean_exit = system command, *args, **kwargs
75+
Process.exit! 1 unless clean_exit
76+
end
77+
exec command, *args, **kwargs
78+
rescue
79+
File.open("input.log", 'a'){|io|
80+
io.write("#{$!.message}:\n#{$!.backtrace.join("\n")}\n")
81+
}
82+
ensure
83+
Process.exit! 1
7484
end
75-
exec command, *args, **kwargs
76-
rescue
77-
File.open("input.log", 'a'){|io|
78-
io.write("#{$!.message}:\n#{$!.backtrace.join("\n")}\n")
79-
}
80-
ensure
81-
Process.exit! 1
8285
end
86+
)
87+
end
88+
89+
pdf_file = input.sub(/\.tex$/,'.pdf')
90+
success = $?&.exitstatus&.zero? && File.exist?(pdf_file)
91+
92+
# Preserve files if requested
93+
unless config[:preservenothing]
94+
FileUtils.mkdir_p(config[:basedir]) unless File.directory?(config[:basedir])
95+
FileUtils.cp(input, File.join(config[:basedir], 'input.tex'))
96+
FileUtils.cp(input.sub(/\.tex$/,'.log'), File.join(config[:basedir], 'input.log'))
97+
98+
if config[:preservework] || !success
99+
preservation_dir = File.join(config[:basedir], config[:workdir].call)
100+
FileUtils.cp_r(dir, preservation_dir)
83101
end
84-
)
85-
end
102+
end
103+
104+
# Finish
105+
unless success
106+
error_log_location = config[:preservenothing] ? "exception.log" : input.sub(/\.tex$/,'.log')
107+
raise RailsLatex::ProcessingError.new(
108+
"rails-latex failed: See #{error_log_location} for details",
109+
File.open(input).read,
110+
File.open(input.sub(/\.tex$/,'.log')).read
111+
)
112+
end
86113

87-
# Finish
88-
if $?.exitstatus.zero? && File.exist?(pdf_file=input.sub(/\.tex$/,'.pdf'))
89-
cmd = config[:preservework] ? :cp : :mv
90-
FileUtils.send(cmd, input, File.join(config[:basedir], 'input.tex'))
91-
FileUtils.send(cmd, input.sub(/\.tex$/,'.log'),
92-
File.join(config[:basedir], 'input.log'))
93-
result = File.read(pdf_file)
94-
FileUtils.rm_rf(dir) unless config[:preservework]
95-
else
96-
raise RailsLatex::ProcessingError.new(
97-
"rails-latex failed: See #{input.sub(/\.tex$/,'.log')} for details",
98-
File.open(input).read,
99-
File.open(input.sub(/\.tex$/,'.log')).read
100-
)
114+
return File.read(pdf_file)
101115
end
102-
result
103116
end
104117

105118
# Escapes LaTex special characters in text so that they wont be interpreted as LaTex commands.

test/test_latex_to_pdf.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,10 @@ def test_custom_recipe
9292
end
9393
end
9494

95+
def test_preserve_nothing
96+
begin
97+
LatexToPdf.generate_pdf(IO.read(File.expand_path('../test_doc.tex',__FILE__)), :preservenothing => true)
98+
assert(!File.exist?("#{TMP_DIR}/tmp/rails-latex"))
99+
end
100+
end
95101
end

0 commit comments

Comments
 (0)