os-0.9.6/0000755000175000017500000000000013043624163011175 5ustar pravipravios-0.9.6/Gemfile.lock0000644000175000017500000000074113043624163013421 0ustar pravipraviPATH remote: . GEM remote: http://rubygems.org/ specs: diff-lcs (1.1.2) git (1.2.5) jeweler (1.6.0) bundler (~> 1.0.0) git (>= 1.2.5) rake rake (0.8.7) rspec (2.5.0) rspec-core (~> 2.5.0) rspec-expectations (~> 2.5.0) rspec-mocks (~> 2.5.0) rspec-core (2.5.1) rspec-expectations (2.5.0) diff-lcs (~> 1.1.2) rspec-mocks (2.5.0) PLATFORMS ruby x86-mingw32 DEPENDENCIES jeweler rspec (>= 2.0) os-0.9.6/autotest/0000755000175000017500000000000013043624163013045 5ustar pravipravios-0.9.6/autotest/discover.rb0000644000175000017500000000004413043624163015206 0ustar pravipraviAutotest.add_discovery { "rspec2" } os-0.9.6/.document0000644000175000017500000000007413043624163013015 0ustar pravipraviREADME.rdoc lib/**/*.rb bin/* features/**/*.feature LICENSE os-0.9.6/lib/0000755000175000017500000000000013043624163011743 5ustar pravipravios-0.9.6/lib/os.rb0000644000175000017500000001362613043624163012721 0ustar pravipravirequire 'rbconfig' require 'yaml' # a set of friendly files for determining your Ruby runtime # treats cygwin as linux # also treats IronRuby on mono as...linux class OS attr_reader :config def self.config @config ||= RbConfig::CONFIG end # true if on windows [and/or jruby] # false if on linux or cygwin on windows def self.windows? @windows ||= begin if RUBY_PLATFORM =~ /cygwin/ # i386-cygwin false elsif ENV['OS'] == 'Windows_NT' true else false end end end # true for linux, os x, cygwin 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 # true for linux, false for windows, os x, cygwin def self.linux? if (host_os =~ /linux/) true else false end end def self.iron_ruby? @iron_ruby ||= begin if defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'ironruby') true else false end end end 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 def self.java? @java ||= begin if RUBY_PLATFORM =~ /java/ true else false end end end def self.ruby_bin @ruby_exe ||= begin File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT'] end end def self.mac? @mac = begin if host_os =~ /darwin/ true else false end end end def self.osx? mac? end 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 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... for process in processes raise 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] kb = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes else raise 'unknown rss for this platform' end end class Underlying def self.bsd? OS.osx? end def self.windows? ENV['OS'] == 'Windows_NT' end def self.linux? OS.host_os =~ /linux/ ? true : false end end def self.cygwin? @cygwin = begin if RUBY_PLATFORM =~ /-cygwin/ true else false end end end def self.dev_null @dev_null ||= begin if OS.windows? "NUL" else "/dev/null" end end end # provides easy way to see the relevant config entries 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 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 def self.open_file_command if OS.doze? || OS.cygwin? "start" elsif OS.mac? "open" else # linux...what about cygwin? "xdg-open" end end class << self alias :doze? :windows? # a joke name but I use it and like it :P alias :jruby? :java? # delegators for relevant config values %w(host host_cpu host_os).each do |method_name| define_method(method_name) { config[method_name] } end end private def self.hwprefs_available? `which hwprefs` != '' end end os-0.9.6/ChangeLog0000644000175000017500000000014513043624163012747 0ustar pravipravi0.9.6 add changelog, license 0.9.5 don't count hyper thread by default windows [TODO check linux ]os-0.9.6/README.rdoc0000644000175000017500000000373613043624163013014 0ustar pravipraviThe OS gem allows for some easy telling if you're on windows or not. require 'os' >> OS.windows? => true # or OS.doze? >> OS.bits => 32 >> OS.java? => true # if you're running in jruby. Also OS.jruby? >> OS.ruby_bin => "c:\ruby18\bin\ruby.exe" # or "/usr/local/bin/ruby" or what not >> OS.posix? => false # true for linux, os x, cygwin >> OS.mac? # or OS.osx? or OS.x? => false >> OS.dev_null => "NUL" # or "/dev/null" depending on which platform >> 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 target_cpu: x86_64 target: x86_64-apple-darwin10.6.0 host_os: darwin10.6.0 host_vendor: apple host_cpu: i386 host: i386-apple-darwin10.6.0 RUBY_PLATFORM: x86_64-darwin10.6.0 >> OS.cpu_count => 2 # number of cores, doesn't include hyper-threaded cores. >> OS.open_file_command => "start" # or open on mac, or xdg-open on linux (all designed to open a file) >> OS::Underlying.windows? => true # true for cygwin or MRI, whereas OS.windows? is false for cygwin >> OS::Underlying.bsd? => true # true for OS X 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 Related projects: rubygems: Gem::Platform.local Gem.ruby the facets gem (has a class similar to rubygems, above) require 'facets/platform' Platform.local the "platform" gem, itself (a different gem) The reason Gem::Platform.local felt wrong to me is that it treated cygwin as windows--which for most build environments, is wrong. Hence the creation of this. License: MIT (see LICENSE file)os-0.9.6/VERSION0000644000175000017500000000000513043624163012240 0ustar pravipravi0.9.6os-0.9.6/.autotest0000644000175000017500000000076513043624163013056 0ustar pravipravi# autotest config for rspec # see: https://github.com/rspec/rspec/wiki/autotest Autotest.add_hook(:initialize) {|at| at.add_exception %r{^\.git} # ignore Version Control System at.add_exception %r{^pkg} # ignore gem pkg dir # at.add_exception %r{^./tmp} # ignore temp files, lest autotest will run again, and again... # at.clear_mappings # take out the default (test/test*rb) ## include specs at.add_mapping(%r{^lib/.*\.rb$}) {|f, _| Dir['spec/**/*_spec.rb'] } nil } os-0.9.6/LICENSE0000644000175000017500000000207213043624163012203 0ustar pravipraviThe MIT License (MIT) Copyright (c) 2012 Roger Pack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.os-0.9.6/Rakefile0000644000175000017500000000200413043624163012636 0ustar pravipravirequire 'rubygems' if RUBY_VERSION < '1.9.0' require 'rake' # Don't forget to run rake gemspec with each release! ... I think... begin require 'jeweler' Jeweler::Tasks.new do |gem| gem.name = "os" gem.summary = %Q{Simple and easy way to know if you're on windows or not (reliably), as well as how many bits the OS is, etc.} gem.description = %Q{The OS gem allows for some useful and easy functions, like OS.windows? (=> true or false) OS.bits ( => 32 or 64) etc"} gem.email = "rogerpack2005@gmail.com" gem.homepage = "http://github.com/rdp/os" gem.authors = ["rdp", "David McCullars"] # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings # gem.add_development_dependency "fast_require" gem.add_development_dependency "rspec", ">= 2.0" end rescue LoadError puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler" end require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) task :default => :spec os-0.9.6/os.gemspec0000644000175000017500000000300313043624163013157 0ustar pravipravi# Generated by jeweler # DO NOT EDIT THIS FILE DIRECTLY # Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = %q{os} s.version = "0.9.4" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["rdp", "David McCullars"] s.date = %q{2011-08-11} s.description = %q{The OS gem allows for some useful and easy functions, like OS.windows? (=> true or false) OS.bits ( => 32 or 64) etc"} s.email = %q{rogerpack2005@gmail.com} s.extra_rdoc_files = [ "README.rdoc" ] s.files = [ ".autotest", ".document", "Gemfile", "Gemfile.lock", "README.rdoc", "Rakefile", "VERSION", "autotest/discover.rb", "lib/os.rb", "os.gemspec", "spec/linux_spec.rb", "spec/os_spec.rb", "spec/osx_spec.rb", "spec/spec_helper.rb" ] s.homepage = %q{http://github.com/rdp/os} s.require_paths = ["lib"] s.rubygems_version = %q{1.7.2} s.summary = %q{Simple and easy way to know if you're on windows or not (reliably), as well as how many bits the OS is, etc.} if s.respond_to? :specification_version then s.specification_version = 3 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_development_dependency(%q, [">= 2.0"]) else s.add_dependency(%q, [">= 2.0"]) end else s.add_dependency(%q, [">= 2.0"]) end end os-0.9.6/Gemfile0000644000175000017500000000006313043624163012467 0ustar pravipravisource "http://rubygems.org" #gem 'rspec', ">= 2" os-0.9.6/spec/0000755000175000017500000000000013043624163012127 5ustar pravipravios-0.9.6/spec/spec_helper.rb0000644000175000017500000000040513043624163014744 0ustar pravipravirequire 'rubygems' if RUBY_VERSION < '1.9.0' require File.expand_path('../lib/os.rb', File.dirname(__FILE__)) require 'rspec' # rspec2 require 'rspec/autorun' RSpec.configure do |config| config.expect_with :rspec, :stdlib # enable `should` OR `assert` end os-0.9.6/spec/os_spec.rb0000644000175000017500000001104413043624163014107 0ustar pravipravirequire File.expand_path('spec_helper.rb', File.dirname(__FILE__)) describe "OS" do it "identifies whether windows? or posix?" do if ENV['OS'] == 'Windows_NT' unless RUBY_PLATFORM =~ /cygwin/ assert OS.windows? == true assert OS.doze? == true assert OS.posix? == false # can fail in error at times...I guess because some other spec has reset ENV on us... else assert OS::Underlying.windows? assert OS.windows? == false assert OS.posix? == true end assert OS::Underlying.windows? elsif [/linux/, /darwin/].any? {|posix_pattern| (RbConfig::CONFIG["host_os"] =~ posix_pattern) || RUBY_PLATFORM =~ posix_pattern } assert OS.windows? == false assert OS.posix? == true assert !OS::Underlying.windows? else pending "create test" end end it "has a bits method" do if RUBY_PLATFORM =~ /mingw32/ assert OS.bits == 32 elsif RUBY_PLATFORM =~ /64/ # linux... assert OS.bits == 64 elsif RUBY_PLATFORM =~ /i686/ assert OS.bits == 32 elsif RUBY_PLATFORM =~ /java/ && RbConfig::CONFIG['host_os'] =~ /32$/ assert OS.bits == 32 elsif RUBY_PLATFORM =~ /java/ && RbConfig::CONFIG['host_cpu'] =~ /i386/ assert OS.bits == 32 elsif RUBY_PLATFORM =~ /i386/ assert OS.bits == 32 else pending "os bits not tested!" + RUBY_PLATFORM + ' ' + RbConfig::CONFIG['host_os'] end end it "should have an iron_ruby method" do if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ironruby' assert OS.iron_ruby? == true else assert OS.iron_ruby? == false end end it "should know if you're on java" do if RUBY_PLATFORM == 'java' assert OS.java? == true # must be [true | false] else assert OS.java? == false end end it "should have a ruby_bin method" do if OS.windows? assert OS.ruby_bin.include?('.exe') if OS.iron_ruby? assert OS.ruby_bin.include?('ir.exe') else assert OS.ruby_bin.include?('ruby.exe') end else assert OS.ruby_bin.include?('ruby') && OS.ruby_bin.include?('/') end if OS.java? assert OS.ruby_bin.include?('jruby') # I think end end it "should have a cygwin? method" do if RUBY_PLATFORM =~ /cygwin/ assert OS.cygwin? == true else assert OS.cygwin? == false end end it "should have a functional mac? method" do if RUBY_PLATFORM =~ /darwin/ assert OS.mac? == true else if OS.host_os == 'darwin' assert OS.mac? == true else assert OS.mac? == false end end end it "should have a way to get rss_bytes on each platform" do bytes = OS.rss_bytes assert bytes > 0 # should always be true assert bytes.is_a?(Numeric) # don't want strings from any platform... end it "should tell you what the right /dev/null is" do if OS.windows? OS.dev_null.should == "NUL" else OS.dev_null.should == "/dev/null" end end it "should have a jruby method" do if defined?(RUBY_DESCRIPTION) && RUBY_DESCRIPTION =~ /^(jruby|java)/ assert OS.jruby? else assert !OS.jruby? end end it "has working cpu count method" do assert OS.cpu_count >= 1 if OS.mac? assert OS.cpu_count == 2 # my own developer box :P end end it "has working cpu count method with no env. variable" do OS.instance_variable_set(:@cpu_count, nil) # reset it if OS.windows? ENV.delete('NUMBER_OF_PROCESSORS') assert OS.cpu_count >= 1 end end it "should have a start/open command helper" do if OS.doze? assert OS.open_file_command == "start" elsif OS.mac? assert OS.open_file_command == "open" else assert OS.open_file_command == "xdg-open" end end end describe OS, "provides access to to underlying config values" do describe "#config, supplys the CONFIG hash" do subject { OS.config } specify { subject.should be_a(Hash) } it "should supply 'host_cpu'" do subject['host_cpu'].should eq(RbConfig::CONFIG['host_cpu']) end it "should supply 'host_os'" do subject['host_os'].should eq(RbConfig::CONFIG['host_os']) end end describe "by providing a delegate method for relevant keys in RbConfig::CONFIG" do %w(host host_cpu host_os).sort.each do |config_key| it "should delegate '#{config_key}'" do expected = "TEST #{config_key}" RbConfig::CONFIG.should_receive(:[]).with(config_key).and_return(expected) OS.send(config_key).should == expected end end end end os-0.9.6/spec/osx_spec.rb0000644000175000017500000000144613043624163014304 0ustar pravipravirequire File.expand_path('spec_helper.rb', File.dirname(__FILE__)) describe 'For OSX (Snow Leopard, 10.6),' do before(:each) do ENV.stub!(:[]).with('OS').and_return(nil) # Issues stubbing RUBY_PLATFORM, using RbConfig instead. # Kernel.stub!(:RUBY_PLATFORM => "x86_64-darwin10.6") RbConfig::CONFIG.stub!(:[]).with('host_os').and_return("darwin10.6.0") RbConfig::CONFIG.stub!(:[]).with('host_cpu').and_return('i386') end describe OS do subject { OS } # class, not instance it { should be_mac } it { should be_x } # OS.x? it { should be_osx } it { should be_posix } it { should_not be_windows } end describe OS::Underlying do subject { OS::Underlying } # class, not instance it { should be_bsd } it { should_not be_windows } end end os-0.9.6/spec/linux_spec.rb0000644000175000017500000000226213043624163014627 0ustar pravipravirequire 'rubygems' if RUBY_VERSION < '1.9.0' require File.dirname(__FILE__) + '/../lib/os.rb' # load before sane to avoid sane being able to requir the gemified version... require 'rspec' # rspec2 describe 'For Linux, (Ubuntu, Ubuntu 10.04 LTS) ' do before(:each) do ENV.should_receive(:[]).with('OS').any_number_of_times.and_return() ## Having difficulties finding a stub for RUBY_PLATFORM # Looking into something like: http://stackoverflow.com/questions/1698335/can-i-use-rspec-mocks-to-stub-out-version-constants # For now, simply using RbConfig::CONFIG # Kernel.stub!(:const_get).with('RUBY_PLATFORM').and_return("i686-linux") RbConfig::CONFIG.stub!(:[]).with('host_os').and_return('linux_gnu') RbConfig::CONFIG.stub!(:[]).with('host_cpu').and_return('i686') end describe OS do subject { OS } # class, not instance it { should be_linux } it { should be_posix } it { should_not be_mac } it { should_not be_osx } it { should_not be_windows } end describe OS::Underlying do subject { OS::Underlying } # class, not instance it { should be_linux } it { should_not be_bsd } it { should_not be_windows } end end