Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
os (1.0.1)
os (1.1.1)

GEM
remote: http://rubygems.org/
Expand All @@ -27,7 +27,7 @@ PLATFORMS
DEPENDENCIES
os!
rake (~> 0.8)
rspec (~> 2.5.0)
rspec (>= 2.0)
test-unit (~> 3)

BUNDLED WITH
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The OS gem allows for some easy telling if you're on windows or not.
The OS gem allows for some easy telling if you're on windows or not.

```ruby
require 'os'
Expand All @@ -24,12 +24,15 @@ require 'os'
>> OS.dev_null
=> "NUL" # or "/dev/null" depending on which platform

>> OS.ipv4_private
=> "192.168.0.106"

>> OS.rss_bytes
=> 12300033 # number of rss bytes this process is using currently. Basically "total in memory footprint" (doesn't include RAM used by the process that's in swap/page file)

>> puts OS.report
==> # a yaml report of helpful values
---
---
arch: x86_64-darwin10.6.0
target_os: darwin10.6.0
target_vendor: apple
Expand All @@ -41,7 +44,7 @@ host_cpu: i386
host: i386-apple-darwin10.6.0
RUBY_PLATFORM: x86_64-darwin10.6.0

>> OS.cpu_count
>> OS.cpu_count
=> 2 # number of cores, doesn't include hyper-threaded cores.

>> OS.open_file_command
Expand Down Expand Up @@ -72,7 +75,7 @@ RUBY_PLATFORM: x86_64-darwin10.6.0
:VERSION_CODENAME=>"bionic",
:UBUNTU_CODENAME=>"bionic"}
```

If there are any other features you'd like, let me know, I'll do what I can to add them :)

http://github.com/rdp/os for feedback et al
Expand Down
58 changes: 34 additions & 24 deletions lib/os.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def self.posix?
true
end
end

end

# true for linux, false for windows, os x, cygwin
Expand All @@ -69,11 +68,7 @@ def self.freebsd?

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

Expand Down Expand Up @@ -132,7 +127,6 @@ def self.x?
mac?
end


# 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
Expand Down Expand Up @@ -204,7 +198,7 @@ def self.dev_null # File::NULL in 1.9.3+
if OS.windows?
"NUL"
else
"/dev/null"
File::NULL
end
end
end
Expand Down Expand Up @@ -233,25 +227,39 @@ def self.cpu_count
when /darwin10/
(hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
when /linux/
`cat /proc/cpuinfo | grep processor | wc -l`.to_i
IO.readlines('/proc/cpuinfo').count { |x| x[/^processor/] }
when /freebsd/
`sysctl -n hw.ncpu`.to_i
else
if RbConfig::CONFIG['host_os'] =~ /darwin/
if File.readable?('/proc/cpuinfo')
# JRuby workaround ; should also work on the Android if cpuinfo is available to user
IO.readlines('/proc/cpuinfo').count { |x| x[/^processor/] }
elsif 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
# 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'
begin
require 'etc'
Etc.nprocessors
rescue Exception
raise 'unknown platform processor_count'
end
end
end
end

def self.ipv4_private
require 'socket'
ip = Socket.ip_address_list.detect(&:ipv4_private?)
ip ? ip.ip? ? ip.ip_unpack[0] : '' : ''
end

def self.open_file_command
if OS.doze? || OS.cygwin?
"start"
Expand All @@ -261,7 +269,6 @@ def self.open_file_command
# linux...what about cygwin?
"xdg-open"
end

end

def self.app_config_path(name)
Expand All @@ -283,18 +290,11 @@ def self.app_config_path(name)
end

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

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
def self.parse_lsb_release
parse_release('/etc/lsb-release')
end

class << self
Expand All @@ -314,4 +314,14 @@ def self.hwprefs_available?
`which hwprefs` != ''
end

def self.parse_release(file)
if OS.linux? && File.readable?(file)
File.readlines(file).reduce({}) do |output, line|
parsed_line = line.strip.delete(?").split('=')
parsed_line.empty? ? output : output.merge(parsed_line[0].to_sym => parsed_line[1])
end
else
raise "File #{file} doesn't exist or not Linux"
end
end
end