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



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

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

.cpuObject



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

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



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

def empty_binding
  return Kernel.binding
end

.exe_extensionObject



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

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



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

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



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

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



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

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



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

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

.secure_eval(code, file, line) ⇒ Object

secure execution of Ruby code



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

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

.terminal?Boolean

Returns:

  • (Boolean)


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

def terminal?
  $stdout.tty?
end

.use_unicode?Boolean

Returns true if we can display Unicode characters.

Returns:

  • (Boolean)

    true if we can display Unicode characters



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

def use_unicode?
  @use_unicode = terminal? && ENV.values_at('LC_ALL', 'LC_CTYPE', 'LANG').compact.first.include?('UTF-8') if @use_unicode.nil?
  return @use_unicode
end

.write_file_restricted(path, force: false, mode: nil) ⇒ Object

value is provided in block



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

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