lib/scout/tmpfile.rb in scout-gear-2.0.0 vs lib/scout/tmpfile.rb in scout-gear-5.1.1
- old
+ new
@@ -1,9 +1,12 @@
-require 'fileutils'
require_relative 'misc'
+require_relative 'log'
+require 'fileutils'
module TmpFile
+ MAX_FILE_LENGTH = 150
+
def self.user_tmp(subdir = nil)
if subdir
File.join(ENV["HOME"],"/tmp/scout", subdir)
else
File.join(ENV["HOME"],"/tmp/scout")
@@ -86,7 +89,42 @@
with_dir(*args) do |dir|
Misc.in_dir dir do
yield dir
end
end
+ end
+
+ def self.tmp_for_file(file, tmp_options = {}, other_options = {})
+ tmp_for_file = IndiferentHash.process_options tmp_options, :file
+ return tmp_for_file unless tmp_for_file.nil?
+
+ prefix = IndiferentHash.process_options tmp_options, :prefix
+
+ if prefix.nil?
+ perfile = file.to_s.gsub(/\//, '>')
+ else
+ perfile = prefix.to_s + ":" + file.to_s.gsub(/\//, '>')
+ end
+
+ perfile.sub!(/\.b?gz$/,'')
+
+ if other_options.include? :filters
+ other_options[:filters].each do |match,value|
+ perfile = perfile + "&F[#{match}=#{Misc.digest(value)}]"
+ end
+ end
+
+ persistence_dir = IndiferentHash.process_options(tmp_options, :dir) || TmpFile.tmpdir
+ Path.setup(persistence_dir) unless Path === persistence_dir
+
+ filename = perfile.gsub(/\s/,'_').gsub(/\//,'>')
+ clean_options = other_options.dup
+ clean_options.delete :unnamed
+ clean_options.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(clean_options) unless clean_options.empty?
+
+ persistence_dir[filename]
end
end