os 0.9.5 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ChangeLog +11 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +15 -10
- data/LICENSE +8 -0
- data/README.md +104 -0
- data/Rakefile +6 -3
- data/VERSION +1 -1
- data/lib/os.rb +316 -271
- data/os.gemspec +63 -51
- data/spec/os_spec.rb +30 -6
- metadata +52 -18
- data/README.rdoc +0 -71
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 267756c34a9e72581c6861c1d50a2b670f579fba25e6e156bf9cd2b3974f60a4
|
4
|
+
data.tar.gz: 2bfef965fa2e0da6edad906075fbf82e89d26a33af36c12840da6f28b8e84d3f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 399f7df8d9e900a3d72e4702cbe2f1a73c62f107a2733074447e22d638b85434bf1368a2a1bb8894357d69fbc1ee692179439ed869d425add596869abbca0a37
|
7
|
+
data.tar.gz: c96bf1ba08052c88f1729a326d50a5672cc8c854e9d6149aa5e8428b30136f53392a3d6675813d9c3aeb7be14b076e597e836d2a004e6346cbe6b5c882afc31b
|
data/ChangeLog
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,29 +1,34 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
|
+
specs:
|
4
|
+
os (1.0.1)
|
3
5
|
|
4
6
|
GEM
|
5
7
|
remote: http://rubygems.org/
|
6
8
|
specs:
|
7
|
-
diff-lcs (1.1.
|
8
|
-
|
9
|
-
|
10
|
-
bundler (~> 1.0.0)
|
11
|
-
git (>= 1.2.5)
|
12
|
-
rake
|
13
|
-
rake (0.8.7)
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
power_assert (1.0.2)
|
11
|
+
rake (0.9.6)
|
14
12
|
rspec (2.5.0)
|
15
13
|
rspec-core (~> 2.5.0)
|
16
14
|
rspec-expectations (~> 2.5.0)
|
17
15
|
rspec-mocks (~> 2.5.0)
|
18
|
-
rspec-core (2.5.
|
16
|
+
rspec-core (2.5.2)
|
19
17
|
rspec-expectations (2.5.0)
|
20
18
|
diff-lcs (~> 1.1.2)
|
21
19
|
rspec-mocks (2.5.0)
|
20
|
+
test-unit (3.2.3)
|
21
|
+
power_assert
|
22
22
|
|
23
23
|
PLATFORMS
|
24
24
|
ruby
|
25
25
|
x86-mingw32
|
26
26
|
|
27
27
|
DEPENDENCIES
|
28
|
-
|
29
|
-
|
28
|
+
os!
|
29
|
+
rake (~> 0.8)
|
30
|
+
rspec (~> 2.5.0)
|
31
|
+
test-unit (~> 3)
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
2.1.4
|
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
Copyright (c) 2012 Roger Pack
|
3
|
+
|
4
|
+
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:
|
5
|
+
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
7
|
+
|
8
|
+
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.
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
The OS gem allows for some easy telling if you're on windows or not.
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
require 'os'
|
5
|
+
|
6
|
+
>> OS.windows?
|
7
|
+
=> true # or OS.doze?
|
8
|
+
|
9
|
+
>> OS.bits
|
10
|
+
=> 32
|
11
|
+
|
12
|
+
>> OS.java?
|
13
|
+
=> true # if you're running in jruby. Also OS.jruby?
|
14
|
+
|
15
|
+
>> OS.ruby_bin
|
16
|
+
=> "c:\ruby18\bin\ruby.exe" # or "/usr/local/bin/ruby" or what not
|
17
|
+
|
18
|
+
>> OS.posix?
|
19
|
+
=> false # true for linux, os x, cygwin
|
20
|
+
|
21
|
+
>> OS.mac? # or OS.osx? or OS.x?
|
22
|
+
=> false
|
23
|
+
|
24
|
+
>> OS.dev_null
|
25
|
+
=> "NUL" # or "/dev/null" depending on which platform
|
26
|
+
|
27
|
+
>> OS.rss_bytes
|
28
|
+
=> 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)
|
29
|
+
|
30
|
+
>> puts OS.report
|
31
|
+
==> # a yaml report of helpful values
|
32
|
+
---
|
33
|
+
arch: x86_64-darwin10.6.0
|
34
|
+
target_os: darwin10.6.0
|
35
|
+
target_vendor: apple
|
36
|
+
target_cpu: x86_64
|
37
|
+
target: x86_64-apple-darwin10.6.0
|
38
|
+
host_os: darwin10.6.0
|
39
|
+
host_vendor: apple
|
40
|
+
host_cpu: i386
|
41
|
+
host: i386-apple-darwin10.6.0
|
42
|
+
RUBY_PLATFORM: x86_64-darwin10.6.0
|
43
|
+
|
44
|
+
>> OS.cpu_count
|
45
|
+
=> 2 # number of cores, doesn't include hyper-threaded cores.
|
46
|
+
|
47
|
+
>> OS.open_file_command
|
48
|
+
=> "start" # or open on mac, or xdg-open on linux (all designed to open a file)
|
49
|
+
|
50
|
+
>> OS::Underlying.windows?
|
51
|
+
=> true # true for cygwin or MRI, whereas OS.windows? is false for cygwin
|
52
|
+
|
53
|
+
>> OS::Underlying.bsd?
|
54
|
+
=> true # true for OS X
|
55
|
+
|
56
|
+
>> OS::Underlying.docker?
|
57
|
+
=> false # true if running inside a Docker container
|
58
|
+
|
59
|
+
>> pp OS.parse_os_release
|
60
|
+
==> # A hash of details on the current Linux distro (or an exception if not Linux)
|
61
|
+
{:NAME=>"Ubuntu",
|
62
|
+
:VERSION=>"18.04.4 LTS (Bionic Beaver)",
|
63
|
+
:ID=>"ubuntu",
|
64
|
+
:ID_LIKE=>"debian",
|
65
|
+
:PRETTY_NAME=>"Ubuntu 18.04.4 LTS",
|
66
|
+
:VERSION_ID=>"18.04",
|
67
|
+
:HOME_URL=>"https://www.ubuntu.com/",
|
68
|
+
:SUPPORT_URL=>"https://help.ubuntu.com/",
|
69
|
+
:BUG_REPORT_URL=>"https://bugs.launchpad.net/ubuntu/",
|
70
|
+
:PRIVACY_POLICY_URL=>
|
71
|
+
"https://www.ubuntu.com/legal/terms-and-policies/privacy-policy",
|
72
|
+
:VERSION_CODENAME=>"bionic",
|
73
|
+
:UBUNTU_CODENAME=>"bionic"}
|
74
|
+
```
|
75
|
+
|
76
|
+
If there are any other features you'd like, let me know, I'll do what I can to add them :)
|
77
|
+
|
78
|
+
http://github.com/rdp/os for feedback et al
|
79
|
+
|
80
|
+
Related projects:
|
81
|
+
|
82
|
+
rubygems:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
Gem::Platform.local
|
86
|
+
Gem.ruby
|
87
|
+
```
|
88
|
+
|
89
|
+
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 gem.
|
90
|
+
|
91
|
+
the facets gem (has a class similar to rubygems, above)
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
require 'facets/platform'
|
95
|
+
Platform.local
|
96
|
+
```
|
97
|
+
|
98
|
+
the ["platform" gem](http://rubydoc.info/github/ffi/ffi/master/FFI/Platform), itself (a different gem)
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
FFI::Platform::OS
|
102
|
+
```
|
103
|
+
|
104
|
+
License: MIT (see LICENSE file)
|
data/Rakefile
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'rubygems' if RUBY_VERSION < '1.9.0'
|
2
|
-
require 'rake'
|
3
2
|
|
4
3
|
# Don't forget to run rake gemspec with each release! ... I think...
|
4
|
+
# then manually edit os.gemspec remove duplicatee rspecs, circular dependency? HUH?
|
5
|
+
# # then gem build os.gemspec
|
6
|
+
# rake build doesn't work???
|
5
7
|
|
6
8
|
begin
|
7
9
|
require 'jeweler'
|
@@ -12,12 +14,13 @@ begin
|
|
12
14
|
gem.email = "rogerpack2005@gmail.com"
|
13
15
|
gem.homepage = "http://github.com/rdp/os"
|
14
16
|
gem.authors = ["rdp", "David McCullars"]
|
17
|
+
gem.license = "MIT"
|
15
18
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
19
|
# gem.add_development_dependency "fast_require"
|
17
20
|
gem.add_development_dependency "rspec", ">= 2.0"
|
18
21
|
end
|
19
|
-
rescue LoadError
|
20
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
+
rescue LoadError => e
|
23
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler #{e}"
|
21
24
|
end
|
22
25
|
|
23
26
|
require 'rspec/core/rake_task'
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
1.1.1
|
data/lib/os.rb
CHANGED
@@ -1,272 +1,317 @@
|
|
1
|
-
require 'rbconfig'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
# a set of friendly files for determining your Ruby runtime
|
5
|
-
# treats cygwin as linux
|
6
|
-
# also treats IronRuby on mono as...linux
|
7
|
-
class OS
|
8
|
-
attr_reader :config
|
9
|
-
|
10
|
-
def self.config
|
11
|
-
@config ||= RbConfig::CONFIG
|
12
|
-
end
|
13
|
-
|
14
|
-
# true if on windows [and/or jruby]
|
15
|
-
# false if on linux or cygwin on windows
|
16
|
-
|
17
|
-
def self.windows?
|
18
|
-
@windows ||= begin
|
19
|
-
if RUBY_PLATFORM =~ /cygwin/ # i386-cygwin
|
20
|
-
false
|
21
|
-
elsif ENV['OS'] == 'Windows_NT'
|
22
|
-
true
|
23
|
-
else
|
24
|
-
false
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
# true for linux, os x, cygwin
|
31
|
-
def self.posix?
|
32
|
-
@posix ||=
|
33
|
-
begin
|
34
|
-
if OS.windows?
|
35
|
-
begin
|
36
|
-
begin
|
37
|
-
# what if we're on interix...
|
38
|
-
# untested, of course
|
39
|
-
Process.wait fork{}
|
40
|
-
true
|
41
|
-
rescue NotImplementedError, NoMethodError
|
42
|
-
false
|
43
|
-
end
|
44
|
-
end
|
45
|
-
else
|
46
|
-
# assume non windows is posix
|
47
|
-
true
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
# true for linux, false for windows, os x, cygwin
|
54
|
-
def self.linux?
|
55
|
-
if (host_os =~ /linux/)
|
56
|
-
true
|
57
|
-
else
|
58
|
-
false
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
def self.
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
raise if memory_used
|
153
|
-
memory_used = process.WorkingSetSize.to_i
|
154
|
-
end
|
155
|
-
memory_used
|
156
|
-
end
|
157
|
-
elsif OS.posix? # linux [though I've heard it works in OS X]
|
158
|
-
kb = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
|
159
|
-
else
|
160
|
-
raise 'unknown rss for this platform'
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
class Underlying
|
165
|
-
|
166
|
-
def self.bsd?
|
167
|
-
OS.osx?
|
168
|
-
end
|
169
|
-
|
170
|
-
def self.windows?
|
171
|
-
ENV['OS'] == 'Windows_NT'
|
172
|
-
end
|
173
|
-
|
174
|
-
def self.linux?
|
175
|
-
OS.host_os =~ /linux/ ? true : false
|
176
|
-
end
|
177
|
-
|
178
|
-
end
|
179
|
-
|
180
|
-
def self.cygwin?
|
181
|
-
@cygwin = begin
|
182
|
-
if RUBY_PLATFORM =~ /-cygwin/
|
183
|
-
true
|
184
|
-
else
|
185
|
-
false
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
def self.dev_null
|
191
|
-
@dev_null ||= begin
|
192
|
-
if OS.windows?
|
193
|
-
"NUL"
|
194
|
-
else
|
195
|
-
"/dev/null"
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
# provides easy way to see the relevant config entries
|
201
|
-
def self.report
|
202
|
-
relevant_keys = [
|
203
|
-
'arch',
|
204
|
-
'host',
|
205
|
-
'host_cpu',
|
206
|
-
'host_os',
|
207
|
-
'host_vendor',
|
208
|
-
'target',
|
209
|
-
'target_cpu',
|
210
|
-
'target_os',
|
211
|
-
'target_vendor',
|
212
|
-
]
|
213
|
-
RbConfig::CONFIG.reject {|key, val| !relevant_keys.include? key }.merge({'RUBY_PLATFORM' => RUBY_PLATFORM}).to_yaml
|
214
|
-
end
|
215
|
-
|
216
|
-
def self.cpu_count
|
217
|
-
@cpu_count ||=
|
218
|
-
case RUBY_PLATFORM
|
219
|
-
when /darwin9/
|
220
|
-
`hwprefs cpu_count`.to_i
|
221
|
-
when /darwin10/
|
222
|
-
(hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
|
223
|
-
when /linux/
|
224
|
-
`cat /proc/cpuinfo | grep processor | wc -l`.to_i
|
225
|
-
when /freebsd/
|
226
|
-
`sysctl -n hw.ncpu`.to_i
|
227
|
-
else
|
228
|
-
if RbConfig::CONFIG['host_os'] =~ /darwin/
|
229
|
-
(hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
|
230
|
-
elsif self.windows?
|
231
|
-
# counts hyper threaded...not good.
|
232
|
-
# out = ENV['NUMBER_OF_PROCESSORS'].to_i
|
233
|
-
require 'win32ole'
|
1
|
+
require 'rbconfig'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
# a set of friendly files for determining your Ruby runtime
|
5
|
+
# treats cygwin as linux
|
6
|
+
# also treats IronRuby on mono as...linux
|
7
|
+
class OS
|
8
|
+
attr_reader :config
|
9
|
+
|
10
|
+
def self.config
|
11
|
+
@config ||= RbConfig::CONFIG
|
12
|
+
end
|
13
|
+
|
14
|
+
# true if on windows [and/or jruby]
|
15
|
+
# false if on linux or cygwin on windows
|
16
|
+
|
17
|
+
def self.windows?
|
18
|
+
@windows ||= begin
|
19
|
+
if RUBY_PLATFORM =~ /cygwin/ # i386-cygwin
|
20
|
+
false
|
21
|
+
elsif ENV['OS'] == 'Windows_NT'
|
22
|
+
true
|
23
|
+
else
|
24
|
+
false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
# true for linux, os x, cygwin
|
31
|
+
def self.posix?
|
32
|
+
@posix ||=
|
33
|
+
begin
|
34
|
+
if OS.windows?
|
35
|
+
begin
|
36
|
+
begin
|
37
|
+
# what if we're on interix...
|
38
|
+
# untested, of course
|
39
|
+
Process.wait fork{}
|
40
|
+
true
|
41
|
+
rescue NotImplementedError, NoMethodError
|
42
|
+
false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
else
|
46
|
+
# assume non windows is posix
|
47
|
+
true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
# true for linux, false for windows, os x, cygwin
|
54
|
+
def self.linux?
|
55
|
+
if (host_os =~ /linux/)
|
56
|
+
true
|
57
|
+
else
|
58
|
+
false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.freebsd?
|
63
|
+
if (host_os =~ /freebsd/)
|
64
|
+
true
|
65
|
+
else
|
66
|
+
false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.iron_ruby?
|
71
|
+
@iron_ruby ||= begin
|
72
|
+
if defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'ironruby')
|
73
|
+
true
|
74
|
+
else
|
75
|
+
false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.bits
|
81
|
+
@bits ||= begin
|
82
|
+
if host_cpu =~ /_64$/ || RUBY_PLATFORM =~ /x86_64/
|
83
|
+
64
|
84
|
+
elsif RUBY_PLATFORM == 'java' && ENV_JAVA['sun.arch.data.model'] # "32" or "64":http://www.ruby-forum.com/topic/202173#880613
|
85
|
+
ENV_JAVA['sun.arch.data.model'].to_i
|
86
|
+
elsif host_cpu == 'i386'
|
87
|
+
32
|
88
|
+
elsif host_os =~ /32$/ # mingw32, mswin32
|
89
|
+
32
|
90
|
+
else # cygwin only...I think
|
91
|
+
if 1.size == 8
|
92
|
+
64
|
93
|
+
else
|
94
|
+
32
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
def self.java?
|
102
|
+
@java ||= begin
|
103
|
+
if RUBY_PLATFORM =~ /java/
|
104
|
+
true
|
105
|
+
else
|
106
|
+
false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.ruby_bin
|
112
|
+
@ruby_exe ||= begin
|
113
|
+
File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.mac?
|
118
|
+
@mac = begin
|
119
|
+
if host_os =~ /darwin/
|
120
|
+
true
|
121
|
+
else
|
122
|
+
false
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.osx?
|
128
|
+
mac?
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.x?
|
132
|
+
mac?
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
# amount of memory the current process "is using", in RAM
|
137
|
+
# (doesn't include any swap memory that it may be using, just that in actual RAM)
|
138
|
+
# raises 'unknown' on jruby currently
|
139
|
+
def self.rss_bytes
|
140
|
+
# attempt to do this in a jruby friendly way
|
141
|
+
if OS::Underlying.windows?
|
142
|
+
# MRI, Java, IronRuby, Cygwin
|
143
|
+
if OS.java?
|
144
|
+
# no win32ole on 1.5.x, so leave here for compatibility...maybe for awhile :P
|
145
|
+
require 'java'
|
146
|
+
mem_bean = java.lang.management.ManagementFactory.getMemoryMXBean
|
147
|
+
mem_bean.heap_memory_usage.used + mem_bean.non_heap_memory_usage.used
|
148
|
+
else
|
149
|
+
wmi = nil
|
150
|
+
begin
|
151
|
+
require 'win32ole'
|
234
152
|
wmi = WIN32OLE.connect("winmgmts://")
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
end
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
end
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
end
|
153
|
+
rescue LoadError, NoMethodError => e # NoMethod for IronRuby currently [sigh]
|
154
|
+
raise 'rss unknown for this platform ' + e.to_s
|
155
|
+
end
|
156
|
+
processes = wmi.ExecQuery("select * from win32_process where ProcessId = #{Process.pid}")
|
157
|
+
memory_used = nil
|
158
|
+
# only allow for one...
|
159
|
+
for process in processes
|
160
|
+
raise if memory_used
|
161
|
+
memory_used = process.WorkingSetSize.to_i
|
162
|
+
end
|
163
|
+
memory_used
|
164
|
+
end
|
165
|
+
elsif OS.posix? # linux [though I've heard it works in OS X]
|
166
|
+
`ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
|
167
|
+
else
|
168
|
+
raise 'unknown rss for this platform'
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
class Underlying
|
173
|
+
|
174
|
+
def self.bsd?
|
175
|
+
OS.osx?
|
176
|
+
end
|
177
|
+
|
178
|
+
def self.windows?
|
179
|
+
ENV['OS'] == 'Windows_NT'
|
180
|
+
end
|
181
|
+
|
182
|
+
def self.linux?
|
183
|
+
OS.host_os =~ /linux/ ? true : false
|
184
|
+
end
|
185
|
+
|
186
|
+
def self.docker?
|
187
|
+
system('grep -q docker /proc/self/cgroup') if OS.linux?
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
|
192
|
+
def self.cygwin?
|
193
|
+
@cygwin = begin
|
194
|
+
if RUBY_PLATFORM =~ /-cygwin/
|
195
|
+
true
|
196
|
+
else
|
197
|
+
false
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def self.dev_null # File::NULL in 1.9.3+
|
203
|
+
@dev_null ||= begin
|
204
|
+
if OS.windows?
|
205
|
+
"NUL"
|
206
|
+
else
|
207
|
+
"/dev/null"
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
# provides easy way to see the relevant config entries
|
213
|
+
def self.report
|
214
|
+
relevant_keys = [
|
215
|
+
'arch',
|
216
|
+
'host',
|
217
|
+
'host_cpu',
|
218
|
+
'host_os',
|
219
|
+
'host_vendor',
|
220
|
+
'target',
|
221
|
+
'target_cpu',
|
222
|
+
'target_os',
|
223
|
+
'target_vendor',
|
224
|
+
]
|
225
|
+
RbConfig::CONFIG.reject {|key, val| !relevant_keys.include? key }.merge({'RUBY_PLATFORM' => RUBY_PLATFORM}).to_yaml
|
226
|
+
end
|
227
|
+
|
228
|
+
def self.cpu_count
|
229
|
+
@cpu_count ||=
|
230
|
+
case RUBY_PLATFORM
|
231
|
+
when /darwin9/
|
232
|
+
`hwprefs cpu_count`.to_i
|
233
|
+
when /darwin10/
|
234
|
+
(hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
|
235
|
+
when /linux/
|
236
|
+
`cat /proc/cpuinfo | grep processor | wc -l`.to_i
|
237
|
+
when /freebsd/
|
238
|
+
`sysctl -n hw.ncpu`.to_i
|
239
|
+
else
|
240
|
+
if RbConfig::CONFIG['host_os'] =~ /darwin/
|
241
|
+
(hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
|
242
|
+
elsif self.windows?
|
243
|
+
# ENV counts hyper threaded...not good.
|
244
|
+
# out = ENV['NUMBER_OF_PROCESSORS'].to_i
|
245
|
+
require 'win32ole'
|
246
|
+
wmi = WIN32OLE.connect("winmgmts://")
|
247
|
+
cpu = wmi.ExecQuery("select NumberOfCores from Win32_Processor") # don't count hyper-threaded in this
|
248
|
+
cpu.to_enum.first.NumberOfCores
|
249
|
+
else
|
250
|
+
raise 'unknown platform processor_count'
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
def self.open_file_command
|
256
|
+
if OS.doze? || OS.cygwin?
|
257
|
+
"start"
|
258
|
+
elsif OS.mac?
|
259
|
+
"open"
|
260
|
+
else
|
261
|
+
# linux...what about cygwin?
|
262
|
+
"xdg-open"
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
def self.app_config_path(name)
|
268
|
+
if OS.doze?
|
269
|
+
if ENV['LOCALAPPDATA']
|
270
|
+
return File.join(ENV['LOCALAPPDATA'], name)
|
271
|
+
end
|
272
|
+
|
273
|
+
File.join ENV['USERPROFILE'], 'Local Settings', 'Application Data', name
|
274
|
+
elsif OS.mac?
|
275
|
+
File.join ENV['HOME'], 'Library', 'Application Support', name
|
276
|
+
else
|
277
|
+
if ENV['XDG_CONFIG_HOME']
|
278
|
+
return File.join(ENV['XDG_CONFIG_HOME'], name)
|
279
|
+
end
|
280
|
+
|
281
|
+
File.join ENV['HOME'], '.config', name
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
def self.parse_os_release
|
286
|
+
if OS.linux? && File.exist?('/etc/os-release')
|
287
|
+
output = {}
|
288
|
+
|
289
|
+
File.read('/etc/os-release').each_line do |line|
|
290
|
+
parsed_line = line.chomp.tr('"', '').split('=')
|
291
|
+
next if parsed_line.empty?
|
292
|
+
output[parsed_line[0].to_sym] = parsed_line[1]
|
293
|
+
end
|
294
|
+
output
|
295
|
+
else
|
296
|
+
raise "File /etc/os-release doesn't exists or not Linux"
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
class << self
|
301
|
+
alias :doze? :windows? # a joke name but I use it and like it :P
|
302
|
+
alias :jruby? :java?
|
303
|
+
|
304
|
+
# delegators for relevant config values
|
305
|
+
%w(host host_cpu host_os).each do |method_name|
|
306
|
+
define_method(method_name) { config[method_name] }
|
307
|
+
end
|
308
|
+
|
309
|
+
end
|
310
|
+
|
311
|
+
private
|
312
|
+
|
313
|
+
def self.hwprefs_available?
|
314
|
+
`which hwprefs` != ''
|
315
|
+
end
|
316
|
+
|
317
|
+
end
|
data/os.gemspec
CHANGED
@@ -1,51 +1,63 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
s.
|
9
|
-
|
10
|
-
|
11
|
-
s.
|
12
|
-
s.
|
13
|
-
s.
|
14
|
-
s.
|
15
|
-
s.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
"
|
20
|
-
".
|
21
|
-
|
22
|
-
|
23
|
-
"
|
24
|
-
"
|
25
|
-
"
|
26
|
-
"
|
27
|
-
"
|
28
|
-
"
|
29
|
-
"
|
30
|
-
"
|
31
|
-
"
|
32
|
-
"
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: os 1.1.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "os".freeze
|
9
|
+
s.version = "1.1.1"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["rdp".freeze, "David McCullars".freeze]
|
14
|
+
s.date = "2020-03-31"
|
15
|
+
s.description = "The OS gem allows for some useful and easy functions, like OS.windows? (=> true or false) OS.bits ( => 32 or 64) etc\"".freeze
|
16
|
+
s.email = "rogerpack2005@gmail.com".freeze
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"ChangeLog",
|
19
|
+
"LICENSE",
|
20
|
+
"README.md"
|
21
|
+
]
|
22
|
+
s.files = [
|
23
|
+
".autotest",
|
24
|
+
".document",
|
25
|
+
"ChangeLog",
|
26
|
+
"Gemfile",
|
27
|
+
"Gemfile.lock",
|
28
|
+
"LICENSE",
|
29
|
+
"README.md",
|
30
|
+
"Rakefile",
|
31
|
+
"VERSION",
|
32
|
+
"autotest/discover.rb",
|
33
|
+
"lib/os.rb",
|
34
|
+
"os.gemspec",
|
35
|
+
"spec/linux_spec.rb",
|
36
|
+
"spec/os_spec.rb",
|
37
|
+
"spec/osx_spec.rb",
|
38
|
+
"spec/spec_helper.rb"
|
39
|
+
]
|
40
|
+
s.homepage = "http://github.com/rdp/os".freeze
|
41
|
+
s.licenses = ["MIT".freeze]
|
42
|
+
s.rubygems_version = "2.7.6".freeze
|
43
|
+
s.summary = "Simple and easy way to know if you're on windows or not (reliably), as well as how many bits the OS is, etc.".freeze
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
s.specification_version = 4
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<rake>.freeze, ["~> 0.8"])
|
50
|
+
s.add_development_dependency(%q<test-unit>.freeze, ["~> 3"])
|
51
|
+
s.add_development_dependency(%q<rspec>.freeze, [">= 2.0"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rake>.freeze, ["~> 0.8"])
|
54
|
+
s.add_dependency(%q<test-unit>.freeze, ["~> 3"])
|
55
|
+
s.add_dependency(%q<rspec>.freeze, [">= 2.0"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<rake>.freeze, ["~> 0.8"])
|
59
|
+
s.add_dependency(%q<test-unit>.freeze, ["~> 3"])
|
60
|
+
s.add_dependency(%q<rspec>.freeze, [">= 2.0"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
data/spec/os_spec.rb
CHANGED
@@ -119,12 +119,11 @@ describe "OS" do
|
|
119
119
|
end
|
120
120
|
|
121
121
|
it "has working cpu count method" do
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
end
|
122
|
+
cpu_count = OS.cpu_count
|
123
|
+
assert cpu_count >= 1
|
124
|
+
assert (cpu_count & (cpu_count - 1)) == 0 # CPU count is normally a power of 2
|
126
125
|
end
|
127
|
-
|
126
|
+
|
128
127
|
it "has working cpu count method with no env. variable" do
|
129
128
|
OS.instance_variable_set(:@cpu_count, nil) # reset it
|
130
129
|
if OS.windows?
|
@@ -132,7 +131,7 @@ describe "OS" do
|
|
132
131
|
assert OS.cpu_count >= 1
|
133
132
|
end
|
134
133
|
end
|
135
|
-
|
134
|
+
|
136
135
|
it "should have a start/open command helper" do
|
137
136
|
if OS.doze?
|
138
137
|
assert OS.open_file_command == "start"
|
@@ -143,6 +142,31 @@ describe "OS" do
|
|
143
142
|
end
|
144
143
|
end
|
145
144
|
|
145
|
+
it "should provide a path to directory for application config" do
|
146
|
+
ENV.stub(:[])
|
147
|
+
|
148
|
+
if OS.mac?
|
149
|
+
ENV.stub(:[]).with('HOME').and_return('/home/user')
|
150
|
+
assert OS.app_config_path('appname') == '/home/xdg/Library/Application Support/appname'
|
151
|
+
elsif OS.doze?
|
152
|
+
# TODO
|
153
|
+
else
|
154
|
+
ENV.stub(:[]).with('HOME').and_return('/home/user')
|
155
|
+
assert OS.app_config_path('appname') == '/home/user/.config/appname'
|
156
|
+
|
157
|
+
ENV.stub(:[]).with('XDG_CONFIG_HOME').and_return('/home/xdg/.config')
|
158
|
+
assert OS.app_config_path('appname') == '/home/xdg/.config/appname'
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should have a freebsd? method" do
|
163
|
+
if OS.host_os =~ /freebsd/
|
164
|
+
assert OS.freebsd? == true
|
165
|
+
else
|
166
|
+
assert OS.freebsd? == false
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
146
170
|
end
|
147
171
|
|
148
172
|
describe OS, "provides access to to underlying config values" do
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: os
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- rdp
|
@@ -10,32 +9,67 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2020-03-31 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0.8'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0.8'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: test-unit
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '3'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '3'
|
15
42
|
- !ruby/object:Gem::Dependency
|
16
43
|
name: rspec
|
17
|
-
requirement:
|
18
|
-
none: false
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
19
45
|
requirements:
|
20
|
-
- -
|
46
|
+
- - ">="
|
21
47
|
- !ruby/object:Gem::Version
|
22
48
|
version: '2.0'
|
23
49
|
type: :development
|
24
50
|
prerelease: false
|
25
|
-
version_requirements:
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '2.0'
|
26
56
|
description: The OS gem allows for some useful and easy functions, like OS.windows?
|
27
57
|
(=> true or false) OS.bits ( => 32 or 64) etc"
|
28
58
|
email: rogerpack2005@gmail.com
|
29
59
|
executables: []
|
30
60
|
extensions: []
|
31
61
|
extra_rdoc_files:
|
32
|
-
-
|
62
|
+
- ChangeLog
|
63
|
+
- LICENSE
|
64
|
+
- README.md
|
33
65
|
files:
|
34
|
-
- .autotest
|
35
|
-
- .document
|
66
|
+
- ".autotest"
|
67
|
+
- ".document"
|
68
|
+
- ChangeLog
|
36
69
|
- Gemfile
|
37
70
|
- Gemfile.lock
|
38
|
-
-
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
39
73
|
- Rakefile
|
40
74
|
- VERSION
|
41
75
|
- autotest/discover.rb
|
@@ -46,28 +80,28 @@ files:
|
|
46
80
|
- spec/osx_spec.rb
|
47
81
|
- spec/spec_helper.rb
|
48
82
|
homepage: http://github.com/rdp/os
|
49
|
-
licenses:
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
50
86
|
post_install_message:
|
51
87
|
rdoc_options: []
|
52
88
|
require_paths:
|
53
89
|
- lib
|
54
90
|
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
-
none: false
|
56
91
|
requirements:
|
57
|
-
- -
|
92
|
+
- - ">="
|
58
93
|
- !ruby/object:Gem::Version
|
59
94
|
version: '0'
|
60
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
-
none: false
|
62
96
|
requirements:
|
63
|
-
- -
|
97
|
+
- - ">="
|
64
98
|
- !ruby/object:Gem::Version
|
65
99
|
version: '0'
|
66
100
|
requirements: []
|
67
101
|
rubyforge_project:
|
68
|
-
rubygems_version:
|
102
|
+
rubygems_version: 2.7.6.2
|
69
103
|
signing_key:
|
70
|
-
specification_version:
|
104
|
+
specification_version: 4
|
71
105
|
summary: Simple and easy way to know if you're on windows or not (reliably), as well
|
72
106
|
as how many bits the OS is, etc.
|
73
107
|
test_files: []
|
data/README.rdoc
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
The OS gem allows for some easy telling if you're on windows or not.
|
2
|
-
|
3
|
-
require 'os'
|
4
|
-
|
5
|
-
>> OS.windows?
|
6
|
-
=> true # or OS.doze?
|
7
|
-
|
8
|
-
>> OS.bits
|
9
|
-
=> 32
|
10
|
-
|
11
|
-
>> OS.java?
|
12
|
-
=> true # if you're running in jruby. Also OS.jruby?
|
13
|
-
|
14
|
-
>> OS.ruby_bin
|
15
|
-
=> "c:\ruby18\bin\ruby.exe" # or "/usr/local/bin/ruby" or what not
|
16
|
-
|
17
|
-
>> OS.posix?
|
18
|
-
=> false # true for linux, os x, cygwin
|
19
|
-
|
20
|
-
>> OS.mac? # or OS.osx? or OS.x?
|
21
|
-
=> false
|
22
|
-
|
23
|
-
>> OS.dev_null
|
24
|
-
=> "NUL" # or "/dev/null" depending on which platform
|
25
|
-
|
26
|
-
>> OS.rss_bytes
|
27
|
-
=> 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)
|
28
|
-
|
29
|
-
>> puts OS.report
|
30
|
-
==> # a yaml report of helpful values
|
31
|
-
---
|
32
|
-
arch: x86_64-darwin10.6.0
|
33
|
-
target_os: darwin10.6.0
|
34
|
-
target_vendor: apple
|
35
|
-
target_cpu: x86_64
|
36
|
-
target: x86_64-apple-darwin10.6.0
|
37
|
-
host_os: darwin10.6.0
|
38
|
-
host_vendor: apple
|
39
|
-
host_cpu: i386
|
40
|
-
host: i386-apple-darwin10.6.0
|
41
|
-
RUBY_PLATFORM: x86_64-darwin10.6.0
|
42
|
-
|
43
|
-
>> OS.cpu_count
|
44
|
-
=> 2 # number of cores, doesn't include hyper-threaded cores.
|
45
|
-
|
46
|
-
>> OS.open_file_command
|
47
|
-
=> "start" # or open on mac, or xdg-open on linux (all designed to open a file)
|
48
|
-
|
49
|
-
>> OS::Underlying.windows?
|
50
|
-
=> true # true for cygwin or MRI, whereas OS.windows? is false for cygwin
|
51
|
-
|
52
|
-
>> OS::Underlying.bsd?
|
53
|
-
=> true # true for OS X
|
54
|
-
|
55
|
-
If there are any other features you'd like, let me know, I'll do what I can to add them :)
|
56
|
-
|
57
|
-
http://github.com/rdp/os for feedback et al
|
58
|
-
|
59
|
-
Related projects:
|
60
|
-
|
61
|
-
rubygems:
|
62
|
-
Gem::Platform.local
|
63
|
-
Gem.ruby
|
64
|
-
|
65
|
-
the facets gem (has a class similar to rubygems, above)
|
66
|
-
require 'facets/platform'
|
67
|
-
Platform.local
|
68
|
-
|
69
|
-
the "platform" gem, itself (a different gem)
|
70
|
-
|
71
|
-
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.
|