Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0c73068

Browse files
committedOct 7, 2019
Use Dir.mktmpdir for the workdir, add a :preservenothing config option to not leave files behind"
1 parent 637ed0f commit 0c73068

File tree

2 files changed

+64
-47
lines changed

2 files changed

+64
-47
lines changed
 

‎lib/rails-latex/latex_to_pdf.rb

+58-47
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,69 @@ 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+
Dir.mktmpdir(["rails-latex-", "-#{config[:workdir].call}"], File.join(Rails.root, 'tmp')) do |dir|
52+
input = File.join(dir, 'input.tex')
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)}
5858

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
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
74+
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
7482
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
8283
end
84+
)
85+
end
86+
87+
pdf_file = input.sub(/\.tex$/,'.pdf')
88+
success = $?&.exitstatus&.zero? && File.exist?(pdf_file)
89+
90+
# Preserve files if requested
91+
unless config[:preservenothing]
92+
FileUtils.mkdir_p(config[:basedir]) unless File.directory?(config[:basedir])
93+
FileUtils.cp(input, File.join(config[:basedir], 'input.tex'))
94+
FileUtils.cp(input.sub(/\.tex$/,'.log'), File.join(config[:basedir], 'input.log'))
95+
96+
if config[:preservework] || !success
97+
preservation_dir = File.join(config[:basedir], config[:workdir].call)
98+
FileUtils.cp_r(dir, preservation_dir)
8399
end
84-
)
85-
end
100+
end
101+
102+
# Finish
103+
unless success
104+
error_log_location = config[:preservenothing] ? "exception.log" : input.sub(/\.tex$/,'.log')
105+
raise RailsLatex::ProcessingError.new(
106+
"rails-latex failed: See #{error_log_location} for details",
107+
File.open(input).read,
108+
File.open(input.sub(/\.tex$/,'.log')).read
109+
)
110+
end
86111

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-
)
112+
return File.read(pdf_file)
101113
end
102-
result
103114
end
104115

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

‎test/test_latex_to_pdf.rb

+6
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)
Please sign in to comment.