Class: Aspera::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/environment.rb

Overview

detect OS, architecture, and specific stuff

Constant Summary collapse

OS_WINDOWS =
:windows
OS_X =
:osx
OS_LINUX =
:linux
OS_AIX =
:aix
OS_LIST =
[OS_WINDOWS, OS_X, 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

Class Method Summary collapse

Class Method Details

.architectureObject



65
66
67
# File 'lib/aspera/environment.rb', line 65

def architecture
  return "#{os}-#{cpu}"
end

.cpuObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aspera/environment.rb', line 50

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

.empty_bindingObject



82
83
84
# File 'lib/aspera/environment.rb', line 82

def empty_binding
  return Kernel.binding
end

.exe_extensionObject



69
70
71
72
# File 'lib/aspera/environment.rb', line 69

def exe_extension
  return '.exe' if os.eql?(OS_WINDOWS)
  return ''
end

.fix_homeObject

on Windows, the env var %USERPROFILE% provides the path to user’s home more reliably than %HOMEDRIVE%%HOMEPATH% so, tell Ruby the right way



76
77
78
79
80
# File 'lib/aspera/environment.rb', line 76

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

.osObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aspera/environment.rb', line 35

def os
  case RbConfig::CONFIG['host_os']
  when /mswin/, /msys/, /mingw/, /cygwin/, /bccwin/, /wince/, /emc/
    return OS_WINDOWS
  when /darwin/, /mac os/
    return OS_X
  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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aspera/environment.rb', line 104

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.message)
end

.ruby_versionObject



31
32
33
# File 'lib/aspera/environment.rb', line 31

def ruby_version
  return RbConfig::CONFIG['RUBY_PROGRAM_VERSION']
end

.secure_eval(code, file, line) ⇒ Object

secure execution of Ruby code



87
88
89
# File 'lib/aspera/environment.rb', line 87

def secure_eval(code, file, line)
  Kernel.send('lave'.reverse, code, empty_binding, file, line)
end

.terminal?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/aspera/environment.rb', line 120

def terminal?
  $stdout.tty?
end

.terminal_supports_unicode?Boolean

Returns true if we can display Unicode characters.

Returns:

  • (Boolean)

    true if we can display Unicode characters



125
126
127
128
# File 'lib/aspera/environment.rb', line 125

def terminal_supports_unicode?
  @terminal_supports_unicode = terminal? && ENV.values_at('LC_ALL', 'LC_CTYPE', 'LANG').compact.first.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



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/aspera/environment.rb', line 92

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