Module: Utils
- Defined in:
- lib/utils.rb
Class Method Summary collapse
- .home_dir ⇒ Object
- .md5_key(file) ⇒ Object
- .md5_key_exists(key_file, key) ⇒ Object
- .md5_line(file) ⇒ Object
- .md5sum(file_array) ⇒ Object
- .md5sum_file(file_array, key_file_name) ⇒ Object
- .merge_files(file_array) ⇒ Object
- .read_properties(file) ⇒ Object
- .rm_tmp_dir ⇒ Object
- .tmp_dir ⇒ Object
- .tmp_file ⇒ Object
-
.unique_stamp ⇒ Object
not thread-safe.
Class Method Details
.home_dir ⇒ Object
49 50 51 |
# File 'lib/utils.rb', line 49 def Utils.home_dir ENV['HOME'] end |
.md5_key(file) ⇒ Object
86 87 88 |
# File 'lib/utils.rb', line 86 def Utils.md5_key file MD5.new(File.open(file, 'rb').read).hexdigest end |
.md5_key_exists(key_file, key) ⇒ Object
98 99 100 |
# File 'lib/utils.rb', line 98 def Utils.md5_key_exists(key_file, key) IO.read(key_file).grep(/#{key}/).size > 0 end |
.md5_line(file) ⇒ Object
90 91 92 |
# File 'lib/utils.rb', line 90 def Utils.md5_line file "#{Utils.md5_key file} #{file}" end |
.md5sum(file_array) ⇒ Object
94 95 96 |
# File 'lib/utils.rb', line 94 def Utils.md5sum file_array file_array.collect { |file| "#{Utils.md5_key file} #{file}" } end |
.md5sum_file(file_array, key_file_name) ⇒ Object
102 103 104 105 106 |
# File 'lib/utils.rb', line 102 def Utils.md5sum_file(file_array, key_file_name) File.open(key_file_name, 'w') do |file| file.write Utils.md5sum(file_array).collect {|line| line+= "\n"} end end |
.merge_files(file_array) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/utils.rb', line 67 def Utils.merge_files file_array merged_file = File.new("#{Utils.tmp_dir}/#{Utils.unique_stamp}_merge", "w") file_array.each do |file| if (! File.exist? file) puts "input file does not exist!" next end merged_file.puts File.new(file).to_a merged_file.puts "\n" merged_file.flush end merged_file.close merged_file.path end |
.read_properties(file) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/utils.rb', line 2 def Utils.read_properties file path = file if file.kind_of? File path = file.path end properties = {} IO.foreach(path) do |line| next if line =~ /^\s*#/ if line =~ /([^=]*)=(.*)\/\/(.*)/ || line =~ /([^=]*)=(.*)/ key = $1.strip value = $2.strip if (value[0,1] == "[" && value[-1, 1]=="]") value = value[1..-2] value = value.split(",").collect {|v| v.strip} end if (value[0,1] == "{" && value[-1, 1]=="}") hash = value[1..-2] value = {} hash_array= hash.split(",").collect {|v| v.strip} hash_array.each do |item| (k, v) = item.split("=>").collect {|item| item.strip} value[k] = v end end properties[key] = value end end return properties end |
.rm_tmp_dir ⇒ Object
59 60 61 |
# File 'lib/utils.rb', line 59 def Utils.rm_tmp_dir FileUtils.rm_r Utils.tmp_dir, :force => true end |
.tmp_dir ⇒ Object
53 54 55 56 57 |
# File 'lib/utils.rb', line 53 def Utils.tmp_dir tmp_dir = "#{Utils.home_dir}/tmp_dir" FileUtils.mkdir_p tmp_dir if (! File.exist? tmp_dir) tmp_dir end |
.tmp_file ⇒ Object
63 64 65 |
# File 'lib/utils.rb', line 63 def Utils.tmp_file "#{Utils.tmp_dir}/#{Utils.unique_stamp}.tmp" end |
.unique_stamp ⇒ Object
not thread-safe
39 40 41 42 43 44 45 46 47 |
# File 'lib/utils.rb', line 39 def Utils.unique_stamp new_stamp = Time.new.strftime("%Y%m%d-%h%m%s") if $stamp == new_stamp $stamp = "#{$stamp}_" else $stamp = new_stamp end $stamp end |