Skip to content

Commit

Permalink
refactor: write line-wrapped code files into temp files
Browse files Browse the repository at this point in the history
We shouldn't modify student submissions, instead we use the
temp file when rendering them to PDF. Cleanup will be performed
after rendering is complete.
  • Loading branch information
ublefo committed Apr 18, 2024
1 parent c13c1f6 commit d454a14
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
17 changes: 6 additions & 11 deletions app/helpers/file_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,21 +530,16 @@ def task_submission_identifier_path_with_timestamp(type, task, timestamp)
"#{task_submission_identifier_path(type, task)}/#{timestamp.to_s}"
end

def text_line_length_limit(path, width: 200)
tempfile = Tempfile.new('processed-source')
def line_wrap(path, width: 200)
dir = File.dirname(path)
basename = File.basename(path)
output = File.join(dir, "WRAPPED-#{basename}")
begin
# run fold (from coreutils) on the source file, write the output to the temporary file
fold_cmd = "fold --width #{width} #{path.shellescape} > #{tempfile.path}"
fold_cmd = "fold --width #{width} #{path.shellescape} > #{output}"
logger.debug "Running fold on #{path} to limit line width to #{width}"
system_try_within 5, "Failed running fold on #{path} to limit line width to #{width}", fold_cmd
# copy the output file to the input file path
FileUtils.cp tempfile.path, path
rescue => e
logger.error "Failed to run fold on #{path} to limit line width to #{width}. Rescued with error:\n\t#{e.message}"
ensure
# ensure the temporary file is closed and deleted immediately
tempfile.close
tempfile.unlink
end
end
# Export functions as module functions
Expand Down Expand Up @@ -590,5 +585,5 @@ def text_line_length_limit(path, width: 200)
module_function :task_submission_identifier_path_with_timestamp
module_function :known_extension?
module_function :pages_in_pdf
module_function :text_line_length_limit
module_function :line_wrap
end
25 changes: 23 additions & 2 deletions app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -993,15 +993,36 @@ def init(task, is_retry)
end

def make_pdf
logger.debug "Running QPDF on all documents before rendering to repair any potential broken files."
logger.debug "Rendering PDF for a task, preprocessing attachments."
@files.each do |f|
if f[:type] == "document"
logger.debug "Running QPDF on #{f[:path]} before rendering to repair any potential broken files."
FileHelper.qpdf(f[:path])
elsif f[:type] == "code"
FileHelper.text_line_length_limit(f[:path])
extension = File.extname(f[:path])[1..-1]
case extension
when "ipynb"
logger.debug "File #{f[:path]} is a Jupyter Notebook, not performing line wrapping."
else
logger.debug "Performing line wrapping on #{f[:path]} before rendering to break up long lines."
FileHelper.line_wrap(f[:path])
end
end
end
logger.debug "Preprocessing complete, rendering file."
render_to_string(template: '/task/task_pdf', layout: true)
ensure
logger.debug "Cleaning up line-wrapped temporary files after rendering."
@files.each do |f|
next unless f[:type] == "code"
name = File.basename(f[:path])
dir = File.dirname(f[:path])
tempfile_path = File.join(dir, "WRAPPED-" + name)
if File.exist? tempfile_path
File.unlink(tempfile_path)
logger.debug "Deleted #{tempfile_path}"
end
end
end
end

Expand Down

0 comments on commit d454a14

Please sign in to comment.