Class: OS

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

Overview

a set of friendly files for determining your Ruby runtime treats cygwin as linux also treats IronRuby on mono as…linux

Defined Under Namespace

Classes: Underlying

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.


8
9
10
# File 'lib/os.rb', line 8

def config
  @config
end

Class Method Details

.app_config_path(name) ⇒ Object


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/os.rb', line 267

def self.app_config_path(name)
  if OS.doze?
    if ENV['LOCALAPPDATA']
      return File.join(ENV['LOCALAPPDATA'], name)
    end

    File.join ENV['USERPROFILE'], 'Local Settings', 'Application Data', name
  elsif OS.mac?
    File.join ENV['HOME'], 'Library', 'Application Support', name
  else
    if ENV['XDG_CONFIG_HOME']
      return File.join(ENV['XDG_CONFIG_HOME'], name)
    end

    File.join ENV['HOME'], '.config', name
  end
end

.bitsObject


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/os.rb', line 80

def self.bits
  @bits ||= begin
    if host_cpu =~ /_64$/ || RUBY_PLATFORM =~ /x86_64/
      64
    elsif RUBY_PLATFORM == 'java' && ENV_JAVA['sun.arch.data.model'] # "32" or "64":http://www.ruby-forum.com/topic/202173#880613
      ENV_JAVA['sun.arch.data.model'].to_i
    elsif host_cpu == 'i386'
      32
    elsif host_os =~ /32$/ # mingw32, mswin32
      32
    else # cygwin only...I think
      if 1.size == 8
        64
      else
        32
      end
    end
  end
end

.configObject


10
11
12
# File 'lib/os.rb', line 10

def self.config
  @config ||= RbConfig::CONFIG
end

.cpu_countObject


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/os.rb', line 228

def self.cpu_count
  @cpu_count ||=
  case RUBY_PLATFORM
  when /darwin9/
    `hwprefs cpu_count`.to_i
  when /darwin10/
    (hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
  when /linux/
    `cat /proc/cpuinfo | grep processor | wc -l`.to_i
  when /freebsd/
    `sysctl -n hw.ncpu`.to_i
  else
    if RbConfig::CONFIG['host_os'] =~ /darwin/
       (hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
    elsif self.windows?
      # ENV counts hyper threaded...not good.
    	      # out = ENV['NUMBER_OF_PROCESSORS'].to_i
      require 'win32ole'
      wmi = WIN32OLE.connect("winmgmts://")
      cpu = wmi.ExecQuery("select NumberOfCores from Win32_Processor") # don't count hyper-threaded in this
      cpu.to_enum.first.NumberOfCores
    else
      raise 'unknown platform processor_count'
    end
  end
end

.cygwin?Boolean

Returns:

  • (Boolean)

192
193
194
195
196
197
198
199
200
# File 'lib/os.rb', line 192

def self.cygwin?
  @cygwin = begin
    if RUBY_PLATFORM =~ /-cygwin/
      true
    else
      false
    end
  end
end

.dev_nullObject

File::NULL in 1.9.3+


202
203
204
205
206
207
208
209
210
# File 'lib/os.rb', line 202

def self.dev_null # File::NULL in 1.9.3+
  @dev_null ||= begin
    if OS.windows?
      "NUL"
    else
      "/dev/null"
    end
  end
end

.freebsd?Boolean

Returns:

  • (Boolean)

62
63
64
65
66
67
68
# File 'lib/os.rb', line 62

def self.freebsd?
  if (host_os =~ /freebsd/)
    true
  else
    false
  end
end

.iron_ruby?Boolean

Returns:

  • (Boolean)

70
71
72
73
74
75
76
77
78
# File 'lib/os.rb', line 70

def self.iron_ruby?
 @iron_ruby ||= begin
   if defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'ironruby')
     true
   else
     false
   end
 end
end

.java?Boolean Also known as: jruby?

Returns:

  • (Boolean)

101
102
103
104
105
106
107
108
109
# File 'lib/os.rb', line 101

def self.java?
  @java ||= begin
    if RUBY_PLATFORM =~ /java/
      true
    else
      false
    end
  end
end

.linux?Boolean

true for linux, false for windows, os x, cygwin

Returns:

  • (Boolean)

54
55
56
57
58
59
60
# File 'lib/os.rb', line 54

def self.linux?
  if (host_os =~ /linux/)
    true
  else
    false
  end
end

.mac?Boolean

Returns:

  • (Boolean)

117
118
119
120
121
122
123
124
125
# File 'lib/os.rb', line 117

def self.mac?
  @mac = begin
    if host_os =~ /darwin/
      true
    else
      false
    end
  end
end

.open_file_commandObject


255
256
257
258
259
260
261
262
263
264
265
# File 'lib/os.rb', line 255

def self.open_file_command
  if OS.doze? || OS.cygwin?
    "start"
  elsif OS.mac?
    "open"
  else
    # linux...what about cygwin?
    "xdg-open"
  end

end

.osx?Boolean

Returns:

  • (Boolean)

127
128
129
# File 'lib/os.rb', line 127

def self.osx?
  mac?
end

.parse_os_releaseObject


285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/os.rb', line 285

def self.parse_os_release
  if OS.linux? && File.exist?('/etc/os-release')
    output = {}

    File.read('/etc/os-release').each_line do |line|
      parsed_line = line.chomp.tr('"', '').split('=')
      next if parsed_line.empty?
      output[parsed_line[0].to_sym] = parsed_line[1]
    end
    output
  else
    raise "File /etc/os-release doesn't exists or not Linux"
  end
end

.posix?Boolean

true for linux, os x, cygwin

Returns:

  • (Boolean)

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/os.rb', line 31

def self.posix?
  @posix ||=
  begin
    if OS.windows?
      begin
        begin
          # what if we're on interix...
          # untested, of course
          Process.wait fork{}
          true
        rescue NotImplementedError, NoMethodError
          false
        end
      end
    else
      # assume non windows is posix
      true
    end
  end

end

.reportObject

provides easy way to see the relevant config entries


213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/os.rb', line 213

def self.report
  relevant_keys = [
    'arch',
    'host',
    'host_cpu',
    'host_os',
    'host_vendor',
    'target',
    'target_cpu',
    'target_os',
    'target_vendor',
  ]
  RbConfig::CONFIG.reject {|key, val| !relevant_keys.include? key }.merge({'RUBY_PLATFORM' => RUBY_PLATFORM}).to_yaml
end

.rss_bytesObject

amount of memory the current process “is using”, in RAM (doesn't include any swap memory that it may be using, just that in actual RAM) raises 'unknown' on jruby currently


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/os.rb', line 139

def self.rss_bytes
  # attempt to do this in a jruby friendly way
  if OS::Underlying.windows?
    # MRI, Java, IronRuby, Cygwin
    if OS.java?
      # no win32ole on 1.5.x, so leave here for compatibility...maybe for awhile :P
      require 'java'
      mem_bean = java.lang.management.ManagementFactory.getMemoryMXBean
      mem_bean.heap_memory_usage.used + mem_bean.non_heap_memory_usage.used
    else
      wmi = nil
      begin
        require 'win32ole'
        wmi = WIN32OLE.connect("winmgmts://")
      rescue LoadError, NoMethodError => e # NoMethod for IronRuby currently [sigh]
        raise 'rss unknown for this platform ' + e.to_s
      end
      processes = wmi.ExecQuery("select * from win32_process where ProcessId = #{Process.pid}")
      memory_used = nil
      # only allow for one process...
      for process in processes
        raise "multiple processes same pid?" if memory_used
        memory_used = process.WorkingSetSize.to_i
      end
      memory_used
    end
  elsif OS.posix? # linux [though I've heard it works in OS X]
    `ps -o rss= -p #{Process.pid}`.to_i * 1024 # in kiloBytes
  else
    raise 'unknown rss for this platform'
  end
end

.ruby_binObject


111
112
113
114
115
# File 'lib/os.rb', line 111

def self.ruby_bin
  @ruby_exe ||= begin
    File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
  end
end

.windows?Boolean Also known as: doze?

true if on windows [and/or jruby] false if on linux or cygwin on windows

Returns:

  • (Boolean)

17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/os.rb', line 17

def self.windows?
  @windows ||= begin
    if RUBY_PLATFORM =~ /cygwin/ # i386-cygwin
      false
    elsif ENV['OS'] == 'Windows_NT'
      true
    else
      false
    end
  end

end

.x?Boolean

Returns:

  • (Boolean)

131
132
133
# File 'lib/os.rb', line 131

def self.x?
  mac?
end