stf-client 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b6357ade2135b93ec5ee58f22c0036931abca91
4
- data.tar.gz: 16efc1453a6c53162183ee47f9fbb931cc5ab632
3
+ metadata.gz: 3f4e33126f8648b92df84f8f064226d2cda27f06
4
+ data.tar.gz: ec864ead54872cd022c637b16e8860298a7438bd
5
5
  SHA512:
6
- metadata.gz: 6f4c092325848af5a33890fcef68323b875eabf41be9cde7f939e3f0fd85c1910a1ea69533ea4cf2360bc075d03d724611f8586ae4927961afa1788efdcf2db8
7
- data.tar.gz: 57f3073eaab9ab2a3b23980b912f42208041913b7e69c50bb70cc85ed4568818cb849f4cf239811661a138c2444ae7a1df6838d2f13d672bf84fff9ea3d7b6ea
6
+ metadata.gz: 6fd1cf716d57494d7c3e244b995ee947f11182f9dff13b192927e5db81d2c93f384f375caa91462656d1832069299972b279ce7b67bd34eba4d206e8b7372ca7
7
+ data.tar.gz: eb8c0476effa4f868403464d7bd7a6d35b56a12cbd00e236504c8526abfb5aeba4b2ebb4a9551f7ce53ed2caca636f4d241e94c8bbd204c041f1eecf543d155c
data/.gitignore CHANGED
@@ -33,6 +33,12 @@
33
33
  # Mongo Explorer plugin:
34
34
  .idea/mongoSettings.xml
35
35
 
36
+ # RubyMine
37
+ .idea/.rakeTasks
38
+ .idea/misc.xml
39
+ .idea/modules.xml
40
+ .idea/stf-client.iml
41
+
36
42
  ## File-based project format:
37
43
  *.iws
38
44
 
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.3
data/README.md CHANGED
@@ -35,8 +35,8 @@ SYNOPSIS
35
35
 
36
36
  GLOBAL OPTIONS
37
37
  --help - Show this message
38
- -t, --token=arg - Authorization token (default: none)
39
- -u, --url=arg - URL to STF (default: none)
38
+ -t, --token=arg - Authorization token, can also be set by environment variable STF_TOKEN (default: none)
39
+ -u, --url=arg - URL to STF, can also be set by environment variable STF_URL (default: none)
40
40
  -v, --[no-]verbose - Be verbose
41
41
 
42
42
  COMMANDS
@@ -44,6 +44,12 @@ COMMANDS
44
44
  connect - Search for a device available in STF and attach it to local adb server
45
45
  disconnect - Disconnect device(s) from local adb server and remove device(s) from user devices in STF
46
46
  help - Shows a list of commands or help for one command
47
+ keys - Show avaliable keys for filtering
48
+ values - Show known values for the filtering key
49
+
50
+ ENVIRONMENT VARIABLES
51
+ STF_TOKEN - Authorization token
52
+ STF_URL - URL to STF
47
53
  ```
48
54
 
49
55
  ## Contributing
@@ -0,0 +1,33 @@
1
+ require 'ADB'
2
+
3
+ require 'stf/client'
4
+ require 'stf/log/log'
5
+ require 'stf/errors'
6
+ require 'stf/model/session'
7
+ require 'stf/model/device'
8
+
9
+ class GetKeysInteractor
10
+
11
+ include Log
12
+ include ADB
13
+
14
+ def initialize(stf)
15
+ @stf = stf
16
+ end
17
+
18
+ def execute
19
+ devices = @stf.get_devices
20
+
21
+ if devices.nil? || (devices.is_a?(Array) && devices.empty?)
22
+ logger.info 'No devices connected to STF'
23
+ return []
24
+ end
25
+
26
+ return devices
27
+ .map {|d| Device.new(d)}
28
+ .flat_map {|d| d.getKeys }
29
+ .uniq
30
+ .sort
31
+ end
32
+
33
+ end
@@ -0,0 +1,32 @@
1
+ require 'ADB'
2
+
3
+ require 'stf/client'
4
+ require 'stf/log/log'
5
+ require 'stf/errors'
6
+ require 'stf/model/session'
7
+ require 'stf/model/device'
8
+
9
+ class GetValuesInteractor
10
+
11
+ include Log
12
+ include ADB
13
+
14
+ def initialize(stf)
15
+ @stf = stf
16
+ end
17
+
18
+ def execute(key)
19
+ devices = @stf.get_devices
20
+
21
+ if devices.nil? || (devices.is_a?(Array) && devices.empty?)
22
+ logger.info r 'No devices connected to STF'
23
+ return []
24
+ end
25
+
26
+ return devices
27
+ .map {|d| Device.new(d)}
28
+ .map {|d| d.getValue(key)}
29
+ .uniq
30
+ end
31
+
32
+ end
@@ -14,52 +14,83 @@ class StartDebugSessionInteractor
14
14
  @stf = stf
15
15
  end
16
16
 
17
- def execute
18
- randomized_device = nil
17
+ def execute(wanted, all_flag, filter)
18
+ wanted = 1 if wanted.nil?
19
+ wanted = wanted.to_i
19
20
 
20
21
  1..10.times do
21
- begin
22
- devices = @stf.get_devices
23
- if devices.nil? || (devices.is_a?(Array) && devices.empty?)
24
- logger.info 'No devices connected to STF. Retrying'
25
- sleep 5
26
- next
27
- end
28
- usable_devices = devices.select{ |d| d.ready == true && d.present == true && d.using == false }
29
- if usable_devices.empty?
30
- logger.error 'All devices are being used. Retrying'
31
- sleep 5
32
- next
33
- end
34
-
35
- randomized_device = usable_devices.sample
36
- raise new DeviceNotAvailableError if randomized_device.nil?
37
-
38
- serial = randomized_device.serial
39
- success = @stf.add_device serial
40
- if success
41
- logger.info "Device #{serial} added"
42
- elsif logger.error "Can't add device #{serial}. Retrying"
43
- next
44
- end
45
-
46
- result = @stf.start_debug serial
47
- if !result.success
48
- logger.error "Can't start debugging session for device #{serial}. Retrying"
49
- @stf.remove_device serial
50
- next
51
- end
52
-
53
- execute_adb_with 30, "connect #{result.remoteConnectUrl}"
54
-
55
- return Session.new(serial, result.remoteConnectUrl)
56
- break
57
-
58
- raise new DeviceNotAvailableError
59
- rescue DeviceNotAvailableError, Net::HTTPFatalError
60
- logger.error 'Failed to start debug session. Retrying...'
61
- next
22
+ wanted -= connect(wanted, all_flag, filter)
23
+ return if all_flag || wanted <= 0
24
+ logger.info 'We are still waiting for ' + wanted.to_s + ' device(s). Retrying'
25
+ sleep 5
26
+ end
27
+ end
28
+
29
+ def connect(wanted, all_flag, filter)
30
+ devices = @stf.get_devices
31
+ if devices.nil? || (devices.is_a?(Array) && devices.empty?)
32
+ logger.info 'No devices connected to STF'
33
+ return 0
34
+ end
35
+
36
+ usable_devices = devices
37
+ .map {|d| Device.new(d)}
38
+ .select do |d|
39
+ d.ready == true && d.present == true && d.using == false
40
+ end
41
+
42
+ if usable_devices.empty?
43
+ logger.error 'All devices are being used'
44
+ return 0
45
+ end
46
+
47
+ unless filter.nil?
48
+ key, value = filter.split(':', 2)
49
+
50
+ usable_devices = usable_devices.select do |d|
51
+ d.getValue(key) == value
52
+ end
53
+ end
54
+
55
+ if usable_devices.empty?
56
+ logger.error 'There is no device with criteria ' + filter
57
+ return 0
58
+ end
59
+
60
+ n = 0
61
+ usable_devices.shuffle.each do |d|
62
+ n += 1 if connect_device(d)
63
+ break if !all_flag && n >= wanted
64
+ end
65
+
66
+ n
67
+ end
68
+
69
+ def connect_device(device)
70
+ begin
71
+ return false if device.nil?
72
+
73
+ serial = device.serial
74
+ success = @stf.add_device serial
75
+ if success
76
+ logger.info "Device #{serial} added"
77
+ elsif logger.error "Can't add device #{serial}"
78
+ return false
79
+ end
80
+
81
+ result = @stf.start_debug serial
82
+ unless result.success
83
+ logger.error "Can't start debugging session for device #{serial}"
84
+ @stf.remove_device serial
85
+ return false
62
86
  end
87
+
88
+ execute_adb_with 30, "connect #{result.remoteConnectUrl}"
89
+ return true
90
+
91
+ rescue Net::HTTPFatalError
92
+ logger.error 'Failed to start debug session'
93
+ return false
63
94
  end
64
95
  end
65
96
  end
@@ -0,0 +1,38 @@
1
+ class Device < OpenStruct
2
+ def getValue(key)
3
+ getValueFromObject(self, key)
4
+ end
5
+
6
+ def getKeys
7
+ getKeysNextLevel('', self)
8
+ end
9
+
10
+ def getKeysNextLevel(prefix, o)
11
+ return [] if o.nil?
12
+
13
+ o.each_pair.flat_map do |k, v|
14
+ if v.is_a? OpenStruct
15
+ getKeysNextLevel(concat(prefix, k.to_s), v)
16
+ else
17
+ [concat(prefix, k.to_s)]
18
+ end
19
+ end
20
+ end
21
+
22
+ def concat(prefix, key)
23
+ prefix.to_s.empty? ? key : prefix + '.' + key
24
+ end
25
+
26
+
27
+ def getValueFromObject(obj, key)
28
+ keys = key.split('.', 2)
29
+ if keys[1].nil?
30
+ obj[key]
31
+ else
32
+ getValueFromObject(obj[keys[0]], keys[1])
33
+ end
34
+ end
35
+
36
+ private :getValueFromObject,:concat, :getKeysNextLevel
37
+
38
+ end
data/lib/stf/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Stf
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.6'
3
3
  end
data/lib/stf/view/cli.rb CHANGED
@@ -7,6 +7,8 @@ module Stf
7
7
  require 'stf/interactor/stop_debug_session_interactor'
8
8
  require 'stf/interactor/stop_all_debug_sessions_interactor'
9
9
  require 'stf/interactor/remove_all_user_devices_interactor'
10
+ require 'stf/interactor/get_keys_interactor'
11
+ require 'stf/interactor/get_values_interactor'
10
12
 
11
13
  include GLI::App
12
14
  extend self
@@ -16,13 +18,17 @@ module Stf
16
18
  desc 'Be verbose'
17
19
  switch [:v, :verbose]
18
20
 
19
- desc 'Authorization token'
21
+ desc 'Authorization token, can also be set by environment variable STF_TOKEN'
20
22
  flag [:t, :token]
21
23
 
22
- desc 'URL to STF'
24
+ desc 'URL to STF, can also be set by environment variable STF_URL'
23
25
  flag [:u, :url]
24
26
 
25
27
  pre do |global_options, command, options, args|
28
+
29
+ global_options[:url] = ENV['STF_URL'] if global_options[:url].nil?
30
+ global_options[:token] = ENV['STF_TOKEN'] if global_options[:token].nil?
31
+
26
32
  help_now!('STF url is required') if global_options[:url].nil?
27
33
  help_now!('Authorization token is required') if global_options[:token].nil?
28
34
 
@@ -33,8 +39,32 @@ module Stf
33
39
 
34
40
  desc 'Search for a device available in STF and attach it to local adb server'
35
41
  command :connect do |c|
42
+ c.switch [:all]
43
+ c.flag [:n, :number]
44
+ c.flag [:f, :filter]
45
+
46
+ c.action do |global_options, options, args|
47
+ StartDebugSessionInteractor.new($stf).execute(options[:number], options[:all], options[:filter])
48
+ end
49
+ end
50
+
51
+ desc 'Show avaliable keys for filtering'
52
+ command :keys do |c|
36
53
  c.action do |global_options, options, args|
37
- StartDebugSessionInteractor.new($stf).execute
54
+ puts GetKeysInteractor.new($stf).execute
55
+ end
56
+ end
57
+
58
+ desc 'Show known values for the filtering key'
59
+ command :values do |c|
60
+ c.flag [:k, :key]
61
+
62
+ c.action do |global_options, options, args|
63
+ if options[:key].nil?
64
+ help_now!('Please specify the key (--key)')
65
+ else
66
+ puts GetValuesInteractor.new($stf).execute(options[:key])
67
+ end
38
68
  end
39
69
  end
40
70
 
@@ -49,7 +79,7 @@ module Stf
49
79
  StopAllDebugSessionsInteractor.new($stf).execute
50
80
  elsif !options[:device].nil? && options[:all] == false
51
81
  StopDebugSessionInteractor.new($stf).execute(options[:device])
52
- elsif help_now!('Please specify disconnect mode (-all or -device)')
82
+ elsif help_now!('Please specify disconnect mode (--all or --device)')
53
83
  end
54
84
  end
55
85
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stf-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Malinskiy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-22 00:00:00.000000000 Z
11
+ date: 2017-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gli
@@ -118,6 +118,7 @@ extra_rdoc_files: []
118
118
  files:
119
119
  - ".gitignore"
120
120
  - ".rspec"
121
+ - ".travis.yml"
121
122
  - Gemfile
122
123
  - LICENSE.txt
123
124
  - README.md
@@ -127,11 +128,14 @@ files:
127
128
  - lib/stf.rb
128
129
  - lib/stf/client.rb
129
130
  - lib/stf/errors.rb
131
+ - lib/stf/interactor/get_keys_interactor.rb
132
+ - lib/stf/interactor/get_values_interactor.rb
130
133
  - lib/stf/interactor/remove_all_user_devices_interactor.rb
131
134
  - lib/stf/interactor/start_debug_session_interactor.rb
132
135
  - lib/stf/interactor/stop_all_debug_sessions_interactor.rb
133
136
  - lib/stf/interactor/stop_debug_session_interactor.rb
134
137
  - lib/stf/log/log.rb
138
+ - lib/stf/model/device.rb
135
139
  - lib/stf/model/session.rb
136
140
  - lib/stf/version.rb
137
141
  - lib/stf/view/cli.rb
@@ -156,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
160
  version: '0'
157
161
  requirements: []
158
162
  rubyforge_project:
159
- rubygems_version: 2.6.12
163
+ rubygems_version: 2.6.13
160
164
  signing_key:
161
165
  specification_version: 4
162
166
  summary: Request devices from Smartphone Test Farm for adb connection