Module: TmpFile
- Defined in:
- lib/scout/tmpfile.rb,
lib/scout/path/tmpfile.rb
Constant Summary collapse
- MAX_FILE_LENGTH =
150- SLASH_REPLACE =
'·'
Class Method Summary collapse
- .in_dir(*args) ⇒ Object
-
.random_name(prefix = 'tmp-', max = 1_000_000_000) ⇒ Object
Creates a random file name, with the given suffix and a random number up to
max. -
.tmp_file(prefix = 'tmp-', max = 1_000_000_000, dir = nil) ⇒ Object
Creates a random filename in the temporary directory.
- .tmp_for_file(file, tmp_options = {}, other_options = {}) ⇒ Object
- .tmpdir ⇒ Object
- .tmpdir=(tmpdir) ⇒ Object
- .user_tmp(subdir = nil) ⇒ Object
- .with_dir(erase = true, options = {}) ⇒ Object
- .with_file(content = nil, erase = true, options = {}) ⇒ Object
- .with_path(*args, &block) ⇒ Object
Class Method Details
.in_dir(*args) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/scout/tmpfile.rb', line 89 def self.in_dir(*args) with_dir(*args) do |dir| Misc.in_dir dir do yield dir end end end |
.random_name(prefix = 'tmp-', max = 1_000_000_000) ⇒ Object
Creates a random file name, with the given suffix and a random number up to max
26 27 28 29 |
# File 'lib/scout/tmpfile.rb', line 26 def self.random_name(prefix = 'tmp-', max = 1_000_000_000) n = rand(max) prefix + n.to_s end |
.tmp_file(prefix = 'tmp-', max = 1_000_000_000, dir = nil) ⇒ Object
Creates a random filename in the temporary directory
32 33 34 35 36 |
# File 'lib/scout/tmpfile.rb', line 32 def self.tmp_file(prefix = 'tmp-', max = 1_000_000_000, dir = nil) dir ||= TmpFile.tmpdir dir = dir.find if Path === dir File.(File.join(dir, random_name(prefix, max))) end |
.tmp_for_file(file, tmp_options = {}, other_options = {}) ⇒ Object
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 |
# File 'lib/scout/tmpfile.rb', line 98 def self.tmp_for_file(file, = {}, = {}) tmp_for_file, prefix, key, persistence_dir = IndiferentHash. , :file, :prefix, :key, :dir return tmp_for_file unless tmp_for_file.nil? if prefix.nil? perfile = file.to_s.sub(/\.b?gz$/,'') else perfile = prefix.to_s + ":" + file.to_s.sub(/\.b?gz$/,'') end perfile += "[#{ key }]" if key if .include? :filters [:filters].each do |match,value| perfile = perfile + "&F[#{match}=#{Misc.digest(value)}]" end end persistence_dir = TmpFile.tmpdir if persistence_dir.nil? Path.setup(persistence_dir) unless Path === persistence_dir filename = perfile.gsub(/\s/,'_').gsub('/', SLASH_REPLACE) = .dup .delete :unnamed .delete "unnamed" filename = filename[0..MAX_FILE_LENGTH] << Misc.digest(filename[MAX_FILE_LENGTH+1..-1]) if filename.length > MAX_FILE_LENGTH + 10 filename += ":" << Misc.digest() unless .empty? persistence_dir[filename] end |
.tmpdir ⇒ Object
20 21 22 |
# File 'lib/scout/tmpfile.rb', line 20 def self.tmpdir @tmpdir ||= self.user_tmp('tmpfiles') end |
.tmpdir=(tmpdir) ⇒ Object
16 17 18 |
# File 'lib/scout/tmpfile.rb', line 16 def self.tmpdir=(tmpdir) @tmpdir = tmpdir end |
.user_tmp(subdir = nil) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/scout/tmpfile.rb', line 8 def self.user_tmp(subdir = nil) if subdir File.join(ENV["HOME"],"/tmp/scout", subdir) else File.join(ENV["HOME"],"/tmp/scout") end end |
.with_dir(erase = true, options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/scout/tmpfile.rb', line 76 def self.with_dir(erase = true, = {}) prefix = [:prefix] || 'tmpdir-' tmpdir = tmp_file prefix FileUtils.mkdir_p tmpdir result = yield(tmpdir) FileUtils.rm_rf tmpdir if File.exist?(tmpdir) && erase result end |
.with_file(content = nil, erase = true, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/scout/tmpfile.rb', line 38 def self.with_file(content = nil, erase = true, = {}) if content.is_a?(Hash) = content content = nil erase = true end if erase.is_a?(Hash) = erase erase = true end prefix = [:prefix] || 'tmp-' tmpdir = [:tmpdir] || TmpFile.tmpdir max = [:max] || 1_000_000_000 tmpfile = tmp_file prefix, max, tmpdir tmpfile += ".#{options[:extension]}" if [:extension] FileUtils.mkdir_p tmpdir if IO === content File.open(tmpfile, 'wb') do |f| begin while c = content.readpartial(1024) f << c end rescue EOFError end end elsif !content.nil? File.open(tmpfile, 'w') { |f| f.write content } end result = yield(tmpfile) FileUtils.rm_rf tmpfile if File.exist?(tmpfile) && erase result end |
.with_path(*args, &block) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/scout/path/tmpfile.rb', line 2 def self.with_path(*args, &block) TmpFile.with_file(*args) do |file| Path.setup(file) yield file end end |