Class: Aspera::Environment
- Inherits:
-
Object
- Object
- Aspera::Environment
- Includes:
- Singleton
- Defined in:
- lib/aspera/environment.rb
Overview
detect OS, architecture, and specific stuff
Constant Summary collapse
- USER_INTERFACES =
%i[text graphical].freeze
- OS_WINDOWS =
:windows
- OS_MACOS =
:osx
- OS_LINUX =
:linux
- OS_AIX =
:aix
- OS_LIST =
[OS_WINDOWS, OS_MACOS, OS_LINUX, OS_AIX].freeze
- CPU_X86_64 =
:x86_64
- CPU_ARM64 =
:arm64
- CPU_PPC64 =
:ppc64
- CPU_PPC64LE =
:ppc64le
- CPU_S390 =
:s390
- CPU_LIST =
[CPU_X86_64, CPU_ARM64, CPU_PPC64, CPU_PPC64LE, CPU_S390].freeze
- BITS_PER_BYTE =
8
- MEBI =
1024 * 1024
- BYTES_PER_MEBIBIT =
MEBI / BITS_PER_BYTE
Instance Attribute Summary collapse
-
#url_method ⇒ Object
Returns the value of attribute url_method.
Class Method Summary collapse
- .architecture ⇒ Object
- .cpu ⇒ Object
- .default_gui_mode ⇒ Object
- .empty_binding ⇒ Object
- .exe_extension ⇒ Object
-
.fix_home ⇒ Object
on Windows, the env var %USERPROFILE% provides the path to user’s home more reliably than %HOMEDRIVE%%HOMEPATH% so, tell Ruby the right way.
- .open_editor(file_path) ⇒ Object
-
.open_uri_graphical(uri) ⇒ Object
command must be non blocking.
- .os ⇒ Object
- .restrict_file_access(path, mode: nil) ⇒ Object
- .ruby_version ⇒ Object
-
.secure_eval(code, file, line) ⇒ Object
secure execution of Ruby code.
- .terminal? ⇒ Boolean
- .terminal_supports_unicode? ⇒ Boolean
-
.write_file_restricted(path, force: false, mode: nil) ⇒ Object
value is provided in block.
Instance Method Summary collapse
-
#initialize ⇒ Environment
constructor
A new instance of Environment.
-
#open_uri(the_url) ⇒ Object
Allows a user to open a Url if method is “text”, then URL is displayed on terminal if method is “graphical”, then the URL will be opened with the default browser.
Constructor Details
#initialize ⇒ Environment
Returns a new instance of Environment.
167 168 169 |
# File 'lib/aspera/environment.rb', line 167 def initialize @url_method = self.class.default_gui_mode end |
Instance Attribute Details
#url_method ⇒ Object
Returns the value of attribute url_method.
165 166 167 |
# File 'lib/aspera/environment.rb', line 165 def url_method @url_method end |
Class Method Details
.architecture ⇒ Object
69 70 71 |
# File 'lib/aspera/environment.rb', line 69 def architecture return "#{os}-#{cpu}" end |
.cpu ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/aspera/environment.rb', line 54 def cpu case RbConfig::CONFIG['host_cpu'] when /x86_64/, /x64/ return CPU_X86_64 when /powerpc/, /ppc64/ return CPU_PPC64LE if os.eql?(OS_LINUX) return CPU_PPC64 when /s390/ return CPU_S390 when /arm/, /aarch64/ return CPU_ARM64 end raise "Unknown CPU: #{RbConfig::CONFIG['host_cpu']}" end |
.default_gui_mode ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/aspera/environment.rb', line 136 def default_gui_mode # assume not remotely connected on macos and windows return :graphical if [Environment::OS_WINDOWS, Environment::OS_MACOS].include?(Environment.os) # unix family return :graphical if ENV.key?('DISPLAY') && !ENV['DISPLAY'].empty? return :text end |
.empty_binding ⇒ Object
86 87 88 |
# File 'lib/aspera/environment.rb', line 86 def empty_binding return Kernel.binding end |
.exe_extension ⇒ Object
73 74 75 76 |
# File 'lib/aspera/environment.rb', line 73 def exe_extension return '.exe' if os.eql?(OS_WINDOWS) return '' end |
.fix_home ⇒ Object
on Windows, the env var %USERPROFILE% provides the path to user’s home more reliably than %HOMEDRIVE%%HOMEPATH% so, tell Ruby the right way
80 81 82 83 84 |
# File 'lib/aspera/environment.rb', line 80 def fix_home return unless os.eql?(OS_WINDOWS) && ENV.key?('USERPROFILE') && Dir.exist?(ENV.fetch('USERPROFILE', nil)) ENV['HOME'] = ENV.fetch('USERPROFILE', nil) Log.log.debug{"Windows: set HOME to USERPROFILE: #{Dir.home}"} end |
.open_editor(file_path) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/aspera/environment.rb', line 155 def open_editor(file_path) if ENV.key?('EDITOR') system(ENV['EDITOR'], file_path.to_s) elsif Environment.os.eql?(Environment::OS_WINDOWS) system('notepad.exe', %Q{"#{file_path}"}) else open_uri_graphical(file_path.to_s) end end |
.open_uri_graphical(uri) ⇒ Object
command must be non blocking
145 146 147 148 149 150 151 152 153 |
# File 'lib/aspera/environment.rb', line 145 def open_uri_graphical(uri) case Environment.os when Environment::OS_MACOS then return system('open', uri.to_s) when Environment::OS_WINDOWS then return system('start', 'explorer', %Q{"#{uri}"}) when Environment::OS_LINUX then return system('xdg-open', uri.to_s) else raise "no graphical open method for #{Environment.os}" end end |
.os ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/aspera/environment.rb', line 39 def os case RbConfig::CONFIG['host_os'] when /mswin/, /msys/, /mingw/, /cygwin/, /bccwin/, /wince/, /emc/ return OS_WINDOWS when /darwin/, /mac os/ return OS_MACOS when /linux/ return OS_LINUX when /aix/ return OS_AIX else raise "Unknown OS: #{RbConfig::CONFIG['host_os']}" end end |
.restrict_file_access(path, mode: nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/aspera/environment.rb', line 108 def restrict_file_access(path, mode: nil) if mode.nil? # or FileUtils ? if File.file?(path) mode = 0o600 elsif File.directory?(path) mode = 0o700 else Log.log.debug{"No restriction can be set for #{path}"} end end File.chmod(mode, path) unless mode.nil? rescue => e Log.log.warn(e.) end |
.ruby_version ⇒ Object
35 36 37 |
# File 'lib/aspera/environment.rb', line 35 def ruby_version return RbConfig::CONFIG['RUBY_PROGRAM_VERSION'] end |
.secure_eval(code, file, line) ⇒ Object
secure execution of Ruby code
91 92 93 |
# File 'lib/aspera/environment.rb', line 91 def secure_eval(code, file, line) Kernel.send('lave'.reverse, code, empty_binding, file, line) end |
.terminal? ⇒ Boolean
124 125 126 |
# File 'lib/aspera/environment.rb', line 124 def terminal? $stdout.tty? end |
.terminal_supports_unicode? ⇒ Boolean
131 132 133 134 |
# File 'lib/aspera/environment.rb', line 131 def terminal_supports_unicode? @terminal_supports_unicode = terminal? && %w(LC_ALL LC_CTYPE LANG).any?{|var|ENV[var]&.include?('UTF-8')} if @terminal_supports_unicode.nil? return @terminal_supports_unicode end |
.write_file_restricted(path, force: false, mode: nil) ⇒ Object
value is provided in block
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/aspera/environment.rb', line 96 def write_file_restricted(path, force: false, mode: nil) Aspera.assert(block_given?, exception_class: Aspera::InternalError) if force || !File.exist?(path) # Windows may give error File.unlink(path) rescue nil # content provided by block File.write(path, yield) restrict_file_access(path, mode: mode) end return path end |
Instance Method Details
#open_uri(the_url) ⇒ Object
Allows a user to open a Url if method is “text”, then URL is displayed on terminal if method is “graphical”, then the URL will be opened with the default browser. this is non blocking
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/aspera/environment.rb', line 175 def open_uri(the_url) case @url_method when :graphical self.class.open_uri_graphical(the_url) when :text case the_url.to_s when /^http/ puts "USER ACTION: please enter this url in a browser:\n#{the_url.to_s.red}\n" else puts "USER ACTION: open this:\n#{the_url.to_s.red}\n" end else raise StandardError, "unsupported url open method: #{@url_method}" end end |