Class: Webdrivers::System Private

Inherits:
Object
  • Object
show all
Defined in:
lib/webdrivers/system.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.bitsizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



143
144
145
# File 'lib/webdrivers/system.rb', line 143

def bitsize
  Selenium::WebDriver::Platform.bitsize
end

.cache_version(file_name, version) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
34
35
36
37
38
39
# File 'lib/webdrivers/system.rb', line 33

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

.cached_version(file_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
# File 'lib/webdrivers/system.rb', line 41

def cached_version(file_name)
  File.open("#{install_dir}/#{file_name.gsub('.exe', '')}.version", 'r', &:read)
end

.call(process, arg = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/webdrivers/system.rb', line 147

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

.decompress_file(tempfile, file_name, target) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/webdrivers/system.rb', line 85

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

.delete(file) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/webdrivers/system.rb', line 13

def delete(file)
  max_attempts = 3
  attempts_made = 0
  delay = 0.5
  Webdrivers.logger.debug "Deleting #{file}"

  begin
    attempts_made += 1
    File.delete file if File.exist? file
  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

.download(url, target) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
55
56
57
58
59
60
# File 'lib/webdrivers/system.rb', line 52

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

.download_file(url, target) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/webdrivers/system.rb', line 62

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

.exists?(file) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/webdrivers/system.rb', line 79

def exists?(file)
  result = File.exist? file
  Webdrivers.logger.debug "#{file} is#{' not' unless result} already downloaded"
  result
end

.install_dirObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
# File 'lib/webdrivers/system.rb', line 29

def install_dir
  Webdrivers.install_dir
end

.platformObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/webdrivers/system.rb', line 131

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

.untarbz2_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



101
102
103
104
105
# File 'lib/webdrivers/system.rb', line 101

def untarbz2_file(filename)
  Webdrivers.logger.debug "Decompressing #{filename}"

  call("tar xjf #{filename}").gsub('.tar.bz2', '')
end

.untargz_file(source, target) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



107
108
109
110
111
112
113
114
115
116
# File 'lib/webdrivers/system.rb', line 107

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

.unzip_file(filename, driver_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/webdrivers/system.rb', line 118

def unzip_file(filename, driver_name)
  Webdrivers.logger.debug "Decompressing #{filename}"

  Zip::File.open(filename) do |zip_file|
    driver = zip_file.get_entry(driver_name)
    f_path = File.join(Dir.pwd, 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

.valid_cache?(file_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/webdrivers/system.rb', line 45

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