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_PPC64 =
:ppc64
CPU_PPC64LE =
:ppc64le
CPU_S390 =
:s390
CPU_LIST =
[CPU_X86_64, 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



62
63
64
# File 'lib/aspera/environment.rb', line 62

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

.cpuObject



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

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/
    # arm on mac has rosetta 2
    return CPU_X86_64 if os.eql?(OS_X)
  end
  raise "Unknown CPU: #{RbConfig::CONFIG['host_cpu']}"
end

.empty_bindingObject



79
80
81
# File 'lib/aspera/environment.rb', line 79

def empty_binding
  return Kernel.binding
end

.exe_extensionObject



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

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



73
74
75
76
77
# File 'lib/aspera/environment.rb', line 73

def fix_home
  return unless os.eql?(OS_WINDOWS) && ENV.key?('USERPROFILE') && Dir.exist?(ENV['USERPROFILE'])
  ENV['HOME'] = ENV['USERPROFILE']
  Log.log.debug{"Windows: set home to USERPROFILE: #{ENV['HOME']}"}
end

.osObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aspera/environment.rb', line 31

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



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

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



27
28
29
# File 'lib/aspera/environment.rb', line 27

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

.secure_eval(code) ⇒ Object

secure execution of Ruby code



84
85
86
# File 'lib/aspera/environment.rb', line 84

def secure_eval(code)
  Kernel.send('lave'.reverse, code, empty_binding, __FILE__, __LINE__)
end

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

value is provided in block



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

def write_file_restricted(path, force: false, mode: nil)
  raise 'coding error, missing content block' unless block_given?
  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