Top Level Namespace

Includes:
REXML

Defined Under Namespace

Modules: Seven Classes: ExecResult

Instance Method Summary collapse

Instance Method Details

#_device_args(device_serial = nil) ⇒ Object



235
236
237
238
239
240
241
242
243
# File 'lib/automate-standard-baseline/helpers.rb', line 235

def _device_args(device_serial = nil)
  if ENV["ADB_DEVICE_ARG"]
    "-s #{ENV["ADB_DEVICE_ARG"]}"
  elsif device_serial
    " -s #{device_serial} "
  else
    ""
  end
end

#adb_cmd(device_serial = nil) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/automate-standard-baseline/helpers.rb', line 227

def adb_cmd(device_serial = nil)
  if is_windows?
    %Q("#{ENV["ANDROID_HOME"]}\\platform-tools\\adb.exe" #{_device_args(device_serial)}) 
  else
    %Q("#{ENV["ANDROID_HOME"]}/platform-tools/adb" #{_device_args(device_serial)})
  end
end

#default_timeout_valObject



12
13
14
# File 'lib/automate-standard-baseline/helpers.rb', line 12

def default_timeout_val
    300
end

#download_file(url, saved_path = nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/automate-standard-baseline/helpers.rb', line 123

def download_file(url, saved_path = nil)
  uri = URI.parse(url)
  path = (!uri.query or uri.query.blank?) ? uri.path : uri.path+"?"+uri.query
  unless saved_path
    saved_path = 'oc_apks'
    Dir.mkdir(saved_path) unless Dir.exists?(saved_path)
    saved_path = "#{saved_path}/#{uri.path[uri.path.rindex("/") + 1, uri.path.length]}"
  end
  saved_path = File.expand_path(saved_path)
  puts "Download file by url #{url}; saved_path: #{saved_path}"
  f = File.new(saved_path, "wb")
  begin
  print "Downloading\n"
  i = 0
  Net::HTTP.start(uri.host, uri.port) do |http|
    http.request_get(path) do |resp|
      resp.read_body do |segment|
        f.write(segment)
        i = i + 1
        if (i % 80 == 0)
          print "\n"
        else 
          print "."
        end
      end
    end
  end
  ensure
    f.close()
  end
  print "\n"
  return saved_path
end

#exec_command(cmd, time_out = nil) ⇒ Object

Add timeout mechanism here to avoid external command executing the whole program.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/automate-standard-baseline/helpers.rb', line 184

def exec_command(cmd, time_out = nil)
  rd, wr = IO.pipe
  pid = spawn(cmd, [:out, :err] => wr)

  begin
      unless time_out
        Process.waitpid(pid)
        status_code = $?.exitstatus
      else 
        begin
          Timeout.timeout(time_out) do
            Process.waitpid(pid)
            status_code = $?.exitstatus
          end
        rescue Timeout::Error => e
          puts 'Timeout::Error'
          puts "kill"
          Process.kill("KILL", pid) rescue nil
          status_code = -99999

          backtrace = e.backtrace || []
          new_error = Timeout::Error.new("Failed to execute command #{cmd} due to it spent too much time than expected time: #{time_out}s")
          new_error.set_backtrace(backtrace + [e.message])

          raise new_error
        end
      end
  ensure
      wr.close
      output = rd.read
      rd.close
  end

  ExecResult.new(output, status_code == 0, status_code)
end

#get_available_devicesObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/automate-standard-baseline/helpers.rb', line 83

def get_available_devices
  # grep the connected devices
  cmd = "#{adb_cmd} devices | ruby -nae 'puts $F[0] if $F[1] =~ /device/'" 
    devices = exec_command(cmd, 10).output
  devices = devices.split /\s+/

  if devices.length > 0
    logger.info {
      msg = "\nAvailable devices\n"
      for i in 0...devices.length
        msg += "#{devices[i]}\n"
      end

      msg
    }
  end

  devices
end

#is_windows?Boolean

Returns:

  • (Boolean)


222
223
224
225
# File 'lib/automate-standard-baseline/helpers.rb', line 222

def is_windows?
  require 'rbconfig'
  (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
end

#log(message) ⇒ Object



25
26
27
# File 'lib/automate-standard-baseline/helpers.rb', line 25

def log(message)
    logger.info message
end

#loggerObject



16
17
18
# File 'lib/automate-standard-baseline/helpers.rb', line 16

def logger
	@logger ||= Logger.new(STDOUT)
end

#manifest(app) ⇒ Object



166
167
168
169
170
# File 'lib/automate-standard-baseline/helpers.rb', line 166

def manifest(app)
  cmd = %Q{java -jar "#{File.dirname(__FILE__)}/manifest_extractor.jar" "#{app}"}
  puts "exec #{cmd}"
  exec_command(cmd, default_timeout_val).output
end

#mkdirs(path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/automate-standard-baseline/helpers.rb', line 71

def mkdirs(path)
    if(!File.directory?(path))
        if(!mkdirs(File.dirname(path)))
            return false;
        end
        Dir.mkdir(path)
    end

    return true
end

#package_name(app) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/automate-standard-baseline/helpers.rb', line 158

def package_name(app)
  require 'rexml/document'
  require 'rexml/xpath'

  manifest = Document.new(manifest(app))
  manifest.root.attributes['package']
end

#pull_out_apk(device_serial, package_name, to_path = nil) ⇒ Object



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
# File 'lib/automate-standard-baseline/helpers.rb', line 45

def pull_out_apk(device_serial, package_name, to_path = nil)
    out_path = File.expand_path(to_path) if to_path
    out_path ||= File.expand_path("#{package_name}.apk")
    
    if File.exists?(out_path)
        new_name = "#{out_path}.#{Time.now.strftime('%Y%m%d%H%M%S%s%S')}"  
        puts %Q{rename #{out_path} to #{new_name} since it already exists.}
        File.rename(out_path, new_name)
    end

    for num in ['2', '1', '3']
        begin
            cmd = %Q{ #{adb_cmd(device_serial)} pull /data/app/#{package_name}-#{num}.apk #{out_path} }
            exec_command(cmd, 120)
        rescue
        end

        if File.exists?(out_path)
            break
        end
    end

    raise %Q{Failed to pull out the package "#{}"} unless File.exists?(out_path)
    out_path
end

#pull_out_tcpdump(devices) ⇒ Object

pull out all /sdcard/*.pcap to logs/



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/automate-standard-baseline/helpers.rb', line 104

def pull_out_tcpdump(devices)
    dest_dir = "/sdcard/"
    devices.each do |d|
        cmd = "#{adb_cmd(d)} shell su -c ls #{dest_dir} | grep pcap" 
        tcpdump_files = exec_command(cmd, 10).output.strip
        tcpdump_files.each_line do |f|
            Dir.mkdir('logs') unless Dir.exists?('logs')

            cmd = "#{adb_cmd(d)} pull #{File.join(dest_dir, f)} logs/#{d}_#{f}"
            logger.info(cmd)
            exec_command(cmd, 120)
            cmd = "#{adb_cmd(d)} shell su -c rm #{File.join(dest_dir, f)}"
            logger.info(cmd)
            exec_command(cmd, 10)
        end
    end
end

#set_env(device_id, id) ⇒ Object



20
21
22
23
# File 'lib/automate-standard-baseline/helpers.rb', line 20

def set_env(device_id,id)
   ENV["ADB_DEVICE_ARG"]="#{device_id}"
   ENV["ADB_DEVICE_INDEX"]="#{id}"
end

#stop_test(pid) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/automate-standard-baseline/helpers.rb', line 30

def stop_test(pid)
  to_kill=pid
  ProcTable.ps do |proc|
     to_kill << proc.pid if to_kill.include?(proc.ppid)
  end
  
  #delete the main process in case of kill itself 
  to_kill.delete_at(0)
  logger.info "Processes will be killed : #{to_kill}"
  to_kill.each do |pid|
     Process.kill("KILL", pid) rescue nil # in case of the process doesn't exist. 
  end
end