-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.rb
More file actions
230 lines (197 loc) · 8.12 KB
/
system.rb
File metadata and controls
230 lines (197 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
52
53
54
55
56
57
58
59
60
61
62
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
126
127
128
129
130
131
132
133
134
135
136
137
138
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/ruby -w
# Ruby-based build system.
# Author: ram (Munagala V. Ramanath)
#
# Classes to extract platform, OS and hardware details
#
c = File.expand_path( File.dirname __FILE__ ) # this dir
$LOAD_PATH.unshift c if ! $LOAD_PATH.include? c
# keep requirements here to a minimum since Build.init_system is low-level code whose
# results are used in many other places
#
%w{ set singleton common.rb }.each{ |f| require f }
# code to extract system information such as OS, architecture, no. of cores, etc.
class Build
def self.system
raise "@@system not yet initialized" if !defined?( @@system )
return @@system
end # system
def self.init_system
return if defined? @@system
# create and store appropriate singleton object in class variable
# use uname to get basic info about machine
uname = Util.find_in_path 'uname'
raise "Unable to find uname executable" if uname.nil?
name = Util.run_cmd( "#{uname} -s" ).last
case name
when 'Linux' then @@system = Linux.instance
when 'Darwin' then @@system = Darwin.instance
else raise "Unknown sytem type: #{name}"
end # case
end # init_system
class Nix # generic Linux/Unix stuff
# Some sample values:
#
# Ubuntu 10.4 RedHat 5 OSX 10.6
# =========== ======== ========
# os_name Linux Linux Darwin
# os_release 2.6.32-41-generic 2.6.18-238.12.1.el5 10.8.0
# os_version (see below) (see below) (see below)
# cpu_type x86_64 x86_64 x86_64
# cpu_width 64 64 64
#
# os_version:
# Ubuntu 10.4 -- #88-Ubuntu SMP Thu Mar 29 13:10:32 UTC 2012
# Redhat 5 -- #1 SMP Sat May 7 20:18:50 EDT 2011
# OSX 10.6 -- Darwin Kernel Version 10.8.0: Tue Jun 7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64
#
attr :hostname, :host, :os_name, :os_release, :os_verson, :cpu_type, :cpu_width
def dump # debugging
printf( "host = %s\nos_name = %s\nos_release = %s\nos_version = %s\n" +
"cpu_type = %s\ncpu_width = %d\nhostname = %s\n",
@host, @os_name, @os_release, @os_version,
@cpu_type, @cpu_width, @hostname )
end # dump
def initialize
# use uname to get basic info about machine
uname = Util.find_in_path 'uname'
hostname = Util.find_in_path 'hostname'
raise "Unable to find hostname executable" if hostname.nil?
@hostname = Util.run_cmd( "#{hostname} -s" ).last # omit domain name
@host = Util.run_cmd( "#{uname} -n" ).last
@os_name = Util.run_cmd( "#{uname} -s" ).last
@os_release = Util.run_cmd( "#{uname} -r" ).last
@os_version = Util.run_cmd( "#{uname} -v" ).last
@cpu_type = Util.run_cmd( "#{uname} -m" ).last
@cpu_width = (/_64$/o =~ @cpu_type) ? 64 : 32
end # initialize
def darwin? # return true iff OS is Darwin
'Darwin' == @os_name
end # darwin?
def pre_lion? # return true iff OS is pre-Lion Darwin (10.6 or earlier)
darwin? && @prod_version_major <= 10 && @prod_version_minor <= 6
end # pre_lion?
end # Nix
class Linux < Nix # Linux specific stuff
include Singleton
# Some sample values:
#
# Ubuntu 10.4 Ubuntu 12.04 RedHat 5
# =========== ============ ========
# dist_id Ubuntu Ubuntu RedHatEnterpriseClient
# dist_release 10.04 12.04 5.6
# dist_codename lucid precise Tikanga
#
# dist_description:
# Ubuntu 10.04 -- Ubuntu 10.04.4 LTS
# Ubuntu 12.04 -- Ubuntu 12.04.1 LTS
# Redhat 5 -- Red Hat Enterprise Linux Client release 5.6 (Tikanga)
#
attr :dist_id, :dist_release, :dist_codename, :dist_description, :ncores, :mem
def dump # debugging
super
printf( "dist_id = %s\ndist_release = %s\ndist_codename = %s\n" +
"dist_description = %s\nncores = %d\nmem = %d MB\n",
@dist_id, @dist_release, @dist_codename, @dist_description, @ncores, @mem )
end # dump
def initialize
super
# use lsb_release if installed
lsb_rel = Util.find_in_path 'lsb_release'
if lsb_rel
@dist_id = Util.run_cmd( "#{lsb_rel} -si" ).last
@dist_release = Util.run_cmd( "#{lsb_rel} -sr" ).last
@dist_codename = Util.run_cmd( "#{lsb_rel} -sc" ).last
@dist_description = Util.run_cmd( "#{lsb_rel} -sd" ).last
else # check common files
['/etc/lsb_release', '/etc/centos_release', '/etc/redhat_release'].each { |f|
next if !File.exist? f
IO.foreach( f ) { |line|
fields = line.split '='
raise "Bad line: #{line}" if 2 != fields.size
fields.map( &:strip! )
raise "Empty key" if fields[ 0 ].empty?
key, val = fields
case key
when 'DISTRIB_ID' then @dist_id = val
when 'DISTRIB_RELEASE' then @dist_release = val
when 'DISTRIB_CODENAME' then @dist_codename = val
when 'DISTRIB_DESCRIPTION' then @dist_description = val
else raise "Bad key: #{key}"
end # case
} # IO.foreach
} # files block
end # lsb_rel
# no. of cores
@ncores = Util.run_cmd( "grep -c processor /proc/cpuinfo" ).last.to_i
# memory
free = Util.find_in_path 'free'
raise "Command 'free' not found" if !free
# in MBs
@mem = Util.run_cmd( "#{free} -m" ).last.split( $/ )[ 1 ].split[ 1 ].to_i
end # initialize
end # Linux
class Darwin < Nix # Darwin/OSX specific stuff
include Singleton
attr :ncores, :mem, :prod_name, :prod_version_major, :prod_version_minor,
:prod_version_patch, :build_version
def dump # debugging
super
printf( "ncores = %d\nmem = %d MB\nProduct Name = %s\n" +
"Product Version = %d.%d.%d\nBuild Version = %s\npre_lion? = %s\n",
@ncores, @mem, @prod_name, @prod_version_major, @prod_version_minor,
@prod_version_patch, @build_version, pre_lion? )
end # dump
# regex to extract major, minor, patch components of product version
R_PROD_VERSION = /^ProductVersion:\s+(\d+)\.(\d+)\.(\d+)/o
def initialize
super
# Output of sw_vers looks something like this:
# ProductName: Mac OS X Server
# ProductVersion: 10.6.8
# BuildVersion: 10K540
#
sys = Util.run_cmd( 'sw_vers' ).last
sys.each_line { |line|
case line
when /^ProductName:/ then
@prod_name = line.split[ 1..-1 ].join ' '
when R_PROD_VERSION then
@prod_version_major = $1.to_i
@prod_version_minor = $2.to_i
@prod_version_patch = $3.to_i
when /^BuildVersion:/ then
@build_version = line.split[ 1 ]
# ignore any other output
end
}
# this shows different values
# @ncores = Util.run_cmd( "sysctl hw.ncpu" ).last.split[ 1 ].to_i
sys = Util.run_cmd( "/usr/sbin/system_profiler SPHardwareDataType" ).last
sys.each_line { |line|
line.strip!
case line # ignore case since we see 'Of' on 10.6, 'of' on 10.7
when /Total Number Of Cores:/io then @ncores = line.split[ 4 ].to_i
when /Memory:/io
arr = line.split; unit = arr[ 2 ]
@mem = case unit
when 'GB' then arr[ 1 ].to_i * 1024
when 'MB' then arr[ 1 ].to_i
else raise "Unknown units: #{unit}"
end
end
} # line loop
raise "Unable to determine number of cores" if !defined? @ncores
raise "Unable to determine size of installed RAM" if !defined? @mem
end # initialize
end # OSX
def self.dump_system # debugging
raise "System not yet initialized" if !defined? @@system
@@system.dump
end # dump_system
init_system # do this right away so results are available elsewhere
end # Build
if $0 == __FILE__
#Build.init_system
Build.dump_system
end