-
Notifications
You must be signed in to change notification settings - Fork 115
/
system.rb
208 lines (169 loc) · 5.96 KB
/
system.rb
1
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
37
38
39
40
41
42
43
44
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
# frozen_string_literal: true
require 'rubygems/package'
require 'zip'
require 'English'
# validate zip entry sizes to avoid zip bombs
# see https://github.com/rubyzip/rubyzip#size-validation
# and https://github.com/rubyzip/rubyzip/pull/403 for further details
# this will be the default in rubyzip 2.0+
Zip.validate_entry_sizes = true
module Webdrivers
#
# @api private
#
class System
class << self
def delete(file)
max_attempts = 3
attempts_made = 0
delay = 0.5
begin
attempts_made += 1
if File.exist? file
Webdrivers.logger.debug "Deleting #{file}"
File.delete file
end
rescue Errno::EACCES # Solves an intermittent file locking issue on Windows
sleep(delay)
retry if File.exist?(file) && attempts_made <= max_attempts
raise
end
end
def install_dir
Webdrivers.install_dir
end
def cache_version(file_name, version)
FileUtils.mkdir_p(install_dir) unless File.exist?(install_dir)
File.open("#{install_dir}/#{file_name.gsub('.exe', '')}.version", 'w+') do |file|
file.print(version)
end
end
def cached_version(file_name)
File.open("#{install_dir}/#{file_name.gsub('.exe', '')}.version", 'r', &:read)
end
def valid_cache?(file_name)
file = "#{install_dir}/#{file_name.gsub('.exe', '')}.version"
return false unless File.exist?(file)
Time.now - File.mtime(file) < Webdrivers.cache_time
end
def download(url, target)
FileUtils.mkdir_p(install_dir) unless File.exist?(install_dir)
download_file(url, target)
FileUtils.chmod 'ugo+rx', target
Webdrivers.logger.debug "Completed download and processing of #{target}"
target
end
def download_file(url, target)
file_name = File.basename(url)
Dir.chdir(install_dir) do
tempfile = Tempfile.open(['', file_name], binmode: true) do |file|
file.print Network.get(url)
file
end
raise "Could not download #{url}" unless File.exist?(tempfile.to_path)
Webdrivers.logger.debug "Successfully downloaded #{tempfile.to_path}"
decompress_file(tempfile, file_name, target)
tempfile.close!
end
end
def exists?(file)
result = File.exist? file
Webdrivers.logger.debug "#{file} is#{' not' unless result} already downloaded"
result
end
def decompress_file(tempfile, file_name, target)
tempfile = tempfile.to_path
case tempfile
when /tar\.gz$/
untargz_file(tempfile, File.basename(target))
when /tar\.bz2$/
untarbz2_file(tempfile)
when /\.zip$/
unzip_file(tempfile, File.basename(target))
else
Webdrivers.logger.debug 'No Decompression needed'
FileUtils.cp(tempfile, File.join(Dir.pwd, file_name))
end
raise "Could not decompress #{file_name} to get #{target}" unless File.exist?(File.basename(target))
end
def untarbz2_file(filename)
Webdrivers.logger.debug "Decompressing #{filename}"
call("tar xjf #{filename}").gsub('.tar.bz2', '')
end
def untargz_file(source, target)
Webdrivers.logger.debug "Decompressing #{source}"
tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(source))
File.open(target, 'w+b') do |ucf|
tar_extract.each { |entry| ucf << entry.read }
File.basename ucf
end
end
def unzip_file(filename, driver_name)
Webdrivers.logger.debug "Decompressing #{filename}"
Zip::File.open(filename) do |zip_file|
driver, f_path = driver_and_path(zip_file, driver_name)
delete(f_path)
FileUtils.mkdir_p(File.dirname(f_path)) unless File.exist?(File.dirname(f_path))
zip_file.extract(driver, f_path)
end
driver_name
end
def driver_and_path(zip_file, driver_name)
driver = zip_file.get_entry(driver_name)
f_path = File.join(Dir.pwd, driver.name)
[driver, f_path]
rescue Errno::ENOENT
driver = zip_file.entries.find { |e| File.basename(e.name) == driver_name }
f_path = File.join(Dir.pwd, File.basename(driver.name))
[driver, f_path]
end
def platform
if Selenium::WebDriver::Platform.linux?
'linux'
elsif Selenium::WebDriver::Platform.mac?
'mac'
elsif Selenium::WebDriver::Platform.windows?
'win'
else
raise NotImplementedError, 'Your OS is not supported by webdrivers gem.'
end
end
def apple_m1_architecture?
if platform == 'mac' && RUBY_PLATFORM.include?('arm64-darwin')
Webdrivers.logger.debug 'Apple architecture: M1 (arm64-darwin)'
return true
end
Webdrivers.logger.debug 'Apple architecture: Intel (mac64)'
false
end
# @return [TrueClass, FalseClass]
def wsl_v1?
platform == 'linux' && File.open('/proc/version').read.include?('Microsoft')
end
# @param [String] path
# @return [String]
def to_win32_path(path)
return path if /[a-z]:\\/iu.match?(path)
call("wslpath -w '#{path}'").chomp
end
# @param [String] path
# @return [String]
def to_wsl_path(path)
call("wslpath -u '#{path}'").chomp
end
def bitsize
Selenium::WebDriver::Platform.bitsize
end
def call(process, arg = nil)
cmd = arg ? [process, arg] : process # Windows provides powershell command (process) only, no args.
Webdrivers.logger.debug "making System call: #{cmd}"
p = IO.popen(cmd)
out = p.read
p.close
raise "Failed to make system call: #{cmd}" unless $CHILD_STATUS.success?
Webdrivers.logger.debug "System call returned: #{out}"
out
end
end
end
end