Module: DATA
Overview
这个类是数据相关服务的顶层接口,提供本机/远程数据相关服务e.g. compare file contents, get file match pattern’s times…
- Author
-
chenjie
- Date
-
2009-4-30
Instance Method Summary collapse
-
#_init_file_(filepath) ⇒ Object
功能: 模块测试的时候的helper文件的load策略.
-
#file_exist?(host, file_path, user = USERNAME, password = PASSWORD) ⇒ Boolean
功能 指定机器、文件路径的文件是否存在 Return true if the named file exists.
-
#file_match?(filename, pattern) ⇒ Boolean
功能 whether the file content match the specified pattern ===参数 - filename, pattern return: true if the file content match the pattern, false if not ===example file_match? file1, “time”.
-
#file_match_times(filename, pattern) ⇒ Object
功能 文件内容match正则串的次数 return: int, the match times ===exmaple times = file_match_times file1, “time”.
-
#file_size?(host, file_path, user = USERNAME, password = PASSWORD) ⇒ Boolean
功能 获取文件大小 Returns nil if file_name doesn’t exist or has zero size, the size of the file otherwise.
-
#get_all_pack(data_path, suffix = ".send") ⇒ Object
功能 get all data prefix(packno) === 参数 - data_path return packno array ===example datas = get_all_pack data_path.
-
#get_mcpack_value(filename, key, index = 1) ⇒ Object
功能 根据给定的mcpack key值获取value ===参数 - filename: 文件名 - key: mcpack key - index: 匹配的key的index Return the value according to the key, nil if nothing found ===example value = get_mcpack_value file1, “name”.
-
#include_file_str(p1, p2) ⇒ Object
include_file_str(file1, file2).
-
#same_content_value(p1, p2) ⇒ Object
Example: same_content_value(file1, file2).
-
#same_file_contents(p1, p2) ⇒ Object
功能 compare two files contents ===参数 - p1 filename 1 - p2 filename 2.
-
#same_file_md5(p1, p2) ⇒ Object
功能 compare two files md5 ===参数 - p1 filename 1 - p2 filename 2.
-
#same_file_str(p1, p2) ⇒ Object
same_file_str(file1, file2).
Instance Method Details
#_init_file_(filepath) ⇒ Object
功能:
模块测试的时候的helper文件的load策略
参数解释:
-
filepath: 运行的case文件的路径,从__FILE__获取
Example:
内部使用
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/common/socket/data.rb', line 23 def _init_file_(filepath) =begin 以下的内容取消,由init_dir.rb实现 module_dir = `dirname #{filepath}` module_base = "module" module_helper_name = module_dir.chop!+"/"+module_base+"_helper.rb" filepath.gsub(/.rb/, "") case_helper_name = filepath+"_helper.rb" if(File.exist?(module_helper_name)) require "#{module_helper_name}" end if(File.exist?(case_helper_name)) require "#{case_helper_name}" end =end case_pwd = `pwd` case_pwd_case = `basename #{case_pwd}` case_pwd.gsub!(/#{case_pwd_case}/, "") case_pwd_module = `basename #{case_pwd}` Context.set(:MODULE_NAME, case_pwd_module.chop!) Context.set(:CASE_NAME, case_pwd_case.chop!) end |
#file_exist?(host, file_path, user = USERNAME, password = PASSWORD) ⇒ Boolean
功能
指定机器、文件路径的文件是否存在Return true if the named file exists.
example
file_exist? host, path
330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/common/socket/data.rb', line 330 def file_exist? host, file_path, user=USERNAME, password=PASSWORD if Util.is_localhost? host then return DataHelper.file_exist? file_path else #lib_path = Util.get_lib_path host #res = Util.exe_ssh_cmd! host, "cd #{lib_path} && ruby data_helper.rb file_exist? #{file_path}" #return res.strip.chomp res = Util.exe_ssh_cmd! host, "if [ -f #{file_path} ];then echo true; else echo false; fi", user, password return res.strip.chomp end end |
#file_match?(filename, pattern) ⇒ Boolean
功能
whether the file content match the specified pattern
参数
-
filename, pattern
return: true if the file content match the pattern, false if not
example
file_match? file1, “time”
231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/common/socket/data.rb', line 231 def file_match?(filename, pattern) return false if !File.exists?(filename) open(filename, 'r:gbk').each{|line| mdata = line.scan(pattern) if mdata.size > 0 then return true end } return false end |
#file_match_times(filename, pattern) ⇒ Object
功能
文件内容match正则串的次数return: int, the match times
exmaple
times = file_match_times file1, “time”
248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/common/socket/data.rb', line 248 def file_match_times(filename, pattern) return 0 if !File.exists?(filename) times = 0 open(filename, 'r:gbk').each{|line| mdata = line.scan(pattern) if mdata.size > 0 then times += mdata.size end } return times end |
#file_size?(host, file_path, user = USERNAME, password = PASSWORD) ⇒ Boolean
功能
获取文件大小Returns nil if file_name doesn’t exist or has zero size, the size of the file otherwise.
example
file_size? host, filepath
347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/common/socket/data.rb', line 347 def file_size? host, file_path, user=USERNAME, password=PASSWORD if Util.is_localhost? host then return DataHelper.file_size? file_path else #lib_path = Util.get_lib_path host #res = Util.exe_ssh_cmd! host, "cd #{lib_path} && ruby data_helper.rb file_size? #{file_path}" #return res.strip.chomp.to_i res = Util.exe_ssh_cmd! host, "ls -l #{file_path} |awk -F\" \" '{print $5}'", user, password return res.strip.chomp.to_i end end |
#get_all_pack(data_path, suffix = ".send") ⇒ Object
功能
get all data prefix(packno)
参数
-
data_path
return packno array
example
datas = get_all_pack data_path
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/common/socket/data.rb', line 52 def get_all_pack(data_path, suffix=".send") #param check if !File.directory?(data_path) || !File.exist?(data_path) then $log.error "bad data_path=" + data_path return end datas = Array.new Dir.foreach(data_path) do |entry| if entry.match("#{suffix}$") then packno = entry[0, entry.size-suffix.length] datas.push(packno) end end return datas.sort end |
#get_mcpack_value(filename, key, index = 1) ⇒ Object
功能
根据给定的mcpack key值获取value
参数
-
filename: 文件名
-
key: mcpack key
-
index: 匹配的key的index
Return the value according to the key, nil if nothing found
example
value = get_mcpack_value file1, “name”
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/common/socket/data.rb', line 271 def get_mcpack_value filename, key, index=1 return nil if !File.exists? filename #build the mcpack string mcstr = "" open(filename, 'r:gbk').each{|line| mcstr += line.strip.chomp } #pattern = /("#{key}":[^,\}]*)/ pattern = [/"#{key}":([^{][^,\}]*)/, /#{key}:([^{][^,\}]*)/] #pattern = /#{key}:([^,\}]*)| "#{key}":([^,\}]*)|#{key}:({[^,]*})|"#{key}":({[^,]*})/ #mdata = mcstr.scan pattern mdata = Array.new pattern.each{|line| mdata+=mcstr.scan line } if index > mdata.size return nil else return mdata[index-1][0] end end |
#include_file_str(p1, p2) ⇒ Object
include_file_str(file1, file2)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/common/socket/data.rb', line 161 def include_file_str p1, p2 return false if File.exists?(p1) != File.exists?(p2) return true if !File.exists?(p1) return true if File.(p1) == File.(p2) return false if File.ftype(p1) != File.ftype(p2) str1 = "" open(p1, 'r:gbk').each{|line| str1 += line.strip.chomp } str2 = "" open(p2, 'r:gbk').each{|line| str2 += line.strip.chomp } minclude = str2.scan(str1) if minclude.size > 0 then return true end return false end |
#same_content_value(p1, p2) ⇒ Object
Example:
same_content_value(file1, file2)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/common/socket/data.rb', line 110 def same_content_value(p1, p2) return false if File.exists?(p1) != File.exists?(p2) return true if !File.exists?(p1) return true if File.(p1) == File.(p2) return false if File.ftype(p1) != File.ftype(p2) || File.size(p1) != File.size(p2) str1 = "" open(p1, 'r:gbk').each{|line| str1 += line.strip.chomp } str2 = "" open(p2, 'r:gbk').each{|line| str2 += line.strip.chomp } pattern = /"content":(.*)}/ content1 = str1.scan pattern content2 = str2.scan pattern return content1 == content2 end |
#same_file_contents(p1, p2) ⇒ Object
功能
compare two files contents
参数
-
p1 filename 1
-
p2 filename 2
Logic
1. if p1 exists and p2 not exists, return false
2. if both not exist, return true
3. same file ,return true
4. type or size different, return false
5. then compare content
example
same_file_contents file1, file2
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/common/socket/data.rb', line 86 def same_file_contents(p1, p2) return false if File.exists?(p1) != File.exists?(p2) return true if !File.exists?(p1) return true if File.(p1) == File.(p2) return false if File.ftype(p1) != File.ftype(p2) || File.size(p1) != File.size(p2) #compare content open(p1) do |f1| open(p2) do |f2| blocksize = f1.lstat.blksize same = true while same && !f1.eof && !f2.eof? same = f1.read(blocksize) == f2.read(blocksize) end return same end end end |
#same_file_md5(p1, p2) ⇒ Object
功能
compare two files md5
参数
-
p1 filename 1
-
p2 filename 2
Logic
1. if p1 exists and p2 not exists, return false
2. if both not exist, return true
3. same file ,return true
4. type or size different, return false
5. then compare md5
example
same_file_md5 file1, file2
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/common/socket/data.rb', line 198 def same_file_md5(p1, p2) return false if File.exists?(p1) != File.exists?(p2) return true if !File.exists?(p1) return true if File.(p1) == File.(p2) puts p1 puts p2 puts File.ftype(p1) puts File.ftype(p2) puts File.size(p1) puts File.size(p2) return false if File.ftype(p1) != File.ftype(p2) || File.size(p1) != File.size(p2) #compare md5 md5 = `md5sum #{p1}`.split(" ")[0] md6 = `md5sum #{p2}`.split(" ")[0] puts md5 puts md6 if md5 == md6 then return true else return false end end |
#same_file_str(p1, p2) ⇒ Object
same_file_str(file1, file2)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/common/socket/data.rb', line 138 def same_file_str p1, p2 return false if File.exists?(p1) != File.exists?(p2) return true if !File.exists?(p1) return true if File.(p1) == File.(p2) return false if File.ftype(p1) != File.ftype(p2) str1 = "" open(p1, 'r:gbk').each{|line| str1 += line.strip.chomp } str2 = "" open(p2, 'r:gbk').each{|line| str2 += line.strip.chomp } return str1 == str2 end |