Class: RTeX::Tempdir

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

Overview

Utility class to manage the lifetime of a temporary directory in which LaTeX can do its processing

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Tempdir

Manually control tempdir creation and removal. Accepts the same arguments as RTeX::Tempdir.open, but the user must call #create! and #remove! themselves:

tempdir = RTeX::Tempdir.new
tempdir.create!
# Use the temporary directory
# ...
tempdir.remove!


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rtex/tempdir.rb', line 56

def initialize(*args)
  options = args.last.is_a?(Hash) ? options.pop : {}
  specific_path = args.first
  
  if specific_path
    @path = specific_path
  else
    @parent_path = options[:parent_path] || Dir.tmpdir
    @basename = options[:basename] || 'rtex'
  end
end

Class Method Details

.open(*args) ⇒ Object

Create a new temporary directory, yield for processing, then remove it.

RTeX::Tempdir.open do |tempdir|
  # Use newly created temporary directory
  # ...
end

When the block completes, the directory is removed.

Arguments can be either an specific directory name,

RTeX::Tempdir.open("/tmp/somewhere") do ...

or options to assist the generation of a randomly named unique directory,

RTeX::Tempdir.open(:parent_path => PARENT, :basename => BASENAME) do ...

which would result in a new directory “[PARENT]/rtex/-[random hash]/” being created.

The default :parent_path is +Dir.tmpdir“, and :basename ‘rtex’.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rtex/tempdir.rb', line 33

def self.open(*args)
  tempdir = new(*args)
  tempdir.create!
  
  # Yield the path and wait for the block to finish
  result = yield tempdir
  
  # We don't remove the temporary directory when exceptions occur,
  # so that the source of the exception can be dubbed (logfile kept)
  tempdir.remove!
  result
end

.uuidObject

Try using uuidgen, but if that doesn’t work drop down to a poor-man’s UUID; timestamp, thread & object hashes Note: I don’t want to add any dependencies (so no UUID library)



98
99
100
101
102
103
104
# File 'lib/rtex/tempdir.rb', line 98

def self.uuid
  if (result = `uuidgen`.strip rescue nil).empty?
    "#{Time.now.to_i}-#{Thread.current.hash}-#{hash}"
  else
    result
  end
end

Instance Method Details

#create!Object



68
69
70
71
72
# File 'lib/rtex/tempdir.rb', line 68

def create!
  FileUtils.mkdir_p path
  @removed = false
  path
end

#exists?Boolean

Is the temporary directory present on the filesystem

Returns:

  • (Boolean)


75
76
77
# File 'lib/rtex/tempdir.rb', line 75

def exists?
  File.exists?(path)
end

#pathObject Also known as: to_s

The filesystem location of the temporary directory



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

def path
  @path ||= File.expand_path(File.join(@parent_path, 'rtex', "#{@basename}-#{self.class.uuid}"))
end

#remove!Object

Forcibly remove this temporary directory



88
89
90
91
92
# File 'lib/rtex/tempdir.rb', line 88

def remove!
  return false if @removed
  FileUtils.rm_rf path
  @removed = true
end