os_platform 0.0.1.pre.beta

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3dc644eba6b7ebcf6c387a181a8730268152def
4
+ data.tar.gz: de7d94b9dd3c2c0b81c95e3bd1b7f6beee86d231
5
+ SHA512:
6
+ metadata.gz: 940597d6a1507ea4fa19626a5cd9bf833fc83107caf45716bf0471153c4668f9ae4defcba59065243068dc564a246043d21700efa184e6e39c7c307dd53a2062
7
+ data.tar.gz: 2b0b5a071e1289d88385d43beed72e6db9647f06a332b79a1c8d7b89d7e29c269a558e475d9df57c21c76d0e92cd8e28dc25cdf21bba7e1932c1cbe7b592165e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in os_platform.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Newell Zhu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # OsPlatform
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/os_platform`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ Inspire by Ohai, extract the platform part.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'os_platform'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install os_platform
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ OSPlatform.local # => #<OSPlatform::Platform::Darwin:0x007fe2d421d950 @platform="mac_os_x", @platform_version="10.10.4", @platform_family="mac_os_x">
27
+ ```
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/[my-github-username]/os_platform/fork )
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "os_platform"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ module OSPlatform
2
+ module Platform
3
+ class Darwin
4
+ include OSPlatform::Platformable
5
+
6
+ def collect_data
7
+ results = `/usr/bin/sw_vers`
8
+ results.to_s.split("\n").each do |line|
9
+ case line
10
+ when /^ProductName:\s+(.+)$/
11
+ macname = $1
12
+ macname.downcase!
13
+ macname.gsub!(" ", "_")
14
+ @platform = macname
15
+ when /^ProductVersion:\s+(.+)$/
16
+ @platform_version = $1
17
+ end
18
+ end
19
+
20
+ @platform_family = "mac_os_x"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,175 @@
1
+ module OSPlatform
2
+ module Platform
3
+ class Linux
4
+ include OSPlatform::Platformable
5
+
6
+ def collect_data
7
+ if File.exist?("/etc/oracle-release")
8
+ contents = File.read("/etc/oracle-release").chomp
9
+ @platform = "oracle"
10
+ @platform_version = get_redhatish_version(contents)
11
+ elsif File.exist?("/etc/enterprise-release")
12
+ contents = File.read("/etc/enterprise-release").chomp
13
+ @platform = "oracle"
14
+ @platform_version = get_redhatish_version(contents)
15
+ elsif File.exist?("/etc/debian_version")
16
+ # Ubuntu and Debian both have /etc/debian_version
17
+ # Ubuntu should always have a working lsb, debian does not by default
18
+ if lsb[:id] =~ /Ubuntu/i
19
+ @platform = "ubuntu"
20
+ @platform_version = lsb[:release]
21
+ elsif lsb[:id] =~ /LinuxMint/i
22
+ @platform = "linuxmint"
23
+ @platform_version = lsb[:release]
24
+ else
25
+ if File.exist?("/usr/bin/raspi-config")
26
+ @platform = "raspbian"
27
+ else
28
+ @platform = "debian"
29
+ end
30
+ @platform_version = File.read("/etc/debian_version").chomp
31
+ end
32
+ elsif File.exist?("/etc/parallels-release")
33
+ contents = File.read("/etc/parallels-release").chomp
34
+ @platform = get_redhatish_platform(contents)
35
+ @platform_version = contents.match(/(\d\.\d\.\d)/)[0]
36
+ elsif File.exist?("/etc/redhat-release")
37
+ if File.exist?('/etc/os-release') && (os_release_info = os_release_file_is_cisco? ) # check if Cisco
38
+ @platform = os_release_info['ID']
39
+ @platform_family = os_release_info['ID_LIKE']
40
+ @platform_version = os_release_info['VERSION'] || ""
41
+ else
42
+ contents = File.read("/etc/redhat-release").chomp
43
+ @platform = get_redhatish_platform(contents)
44
+ @platform_version = get_redhatish_version(contents)
45
+ end
46
+ elsif File.exist?("/etc/system-release")
47
+ contents = File.read("/etc/system-release").chomp
48
+ @platform = get_redhatish_platform(contents)
49
+ @platform_version = get_redhatish_version(contents)
50
+ elsif File.exist?('/etc/gentoo-release')
51
+ @platform = "gentoo"
52
+ @platform_version = File.read('/etc/gentoo-release').scan(/(\d+|\.+)/).join
53
+ elsif File.exist?('/etc/SuSE-release')
54
+ suse_release = File.read("/etc/SuSE-release")
55
+ suse_version = suse_release.scan(/VERSION = (\d+)\nPATCHLEVEL = (\d+)/).flatten.join(".")
56
+ suse_version = suse_release[/VERSION = ([\d\.]{2,})/, 1] if suse_version == ""
57
+ @platform_version = suse_version
58
+ if suse_release =~ /^openSUSE/
59
+ @platform = "opensuse"
60
+ else
61
+ @platform = "suse"
62
+ end
63
+ elsif File.exist?('/etc/slackware-version')
64
+ @platform = "slackware"
65
+ @platform_version = File.read("/etc/slackware-version").scan(/(\d+|\.+)/).join
66
+ elsif File.exist?('/etc/arch-release')
67
+ @platform = "arch"
68
+ # no way to determine @platform_version in a rolling release distribution
69
+ # kernel release will be used - ex. 2.6.32-ARCH
70
+ @platform_version = `uname -r`.strip
71
+ elsif File.exist?('/etc/exherbo-release')
72
+ @platform = "exherbo"
73
+ # no way to determine @platform_version in a rolling release distribution
74
+ # kernel release will be used - ex. 3.13
75
+ @platform_version = `uname -r`.strip
76
+ elsif lsb[:id] =~ /RedHat/i
77
+ @platform = "redhat"
78
+ @platform_version = lsb[:release]
79
+ elsif lsb[:id] =~ /Amazon/i
80
+ @platform = "amazon"
81
+ @platform_version = lsb[:release]
82
+ elsif lsb[:id] =~ /ScientificSL/i
83
+ @platform = "scientific"
84
+ @platform_version = lsb[:release]
85
+ elsif lsb[:id] =~ /XenServer/i
86
+ @platform = "xenserver"
87
+ @platform_version = lsb[:release]
88
+ elsif lsb[:id] # LSB can provide odd data that changes between releases, so we currently fall back on it rather than dealing with its subtleties
89
+ @platform = lsb[:id].downcase
90
+ @platform_version = lsb[:release]
91
+ end
92
+
93
+ case @platform
94
+ when /debian/, /ubuntu/, /linuxmint/, /raspbian/
95
+ @platform_family = "debian"
96
+ when /fedora/, /pidora/
97
+ self.pinstancelatform_family = "fedora"
98
+ when /oracle/, /centos/, /redhat/, /scientific/, /enterpriseenterprise/, /amazon/, /xenserver/, /cloudlinux/, /ibm_powerkvm/, /parallels/ # Note that 'enterpriseenterprise' is oracle's LSB "distributor ID"
99
+ @platform_family = "rhel"
100
+ when /suse/
101
+ @platform_family = "suse"
102
+ when /gentoo/
103
+ @platform_family = "gentoo"
104
+ when /slackware/
105
+ @platform_family = "slackware"
106
+ when /arch/
107
+ @platform_family = "arch"
108
+ when /exherbo/
109
+ @platform_family = "exherbo"
110
+ end
111
+ end
112
+
113
+ private
114
+
115
+ def get_redhatish_platform(contents)
116
+ contents[/^Red Hat/i] ? "redhat" : contents[/(\w+)/i, 1].downcase
117
+ end
118
+
119
+ def get_redhatish_version(contents)
120
+ contents[/Rawhide/i] ? contents[/((\d+) \(Rawhide\))/i, 1].downcase : contents[/release ([\d\.]+)/, 1]
121
+ end
122
+
123
+ def os_release_file_is_cisco?
124
+ return false unless File.exist?('/etc/os-release')
125
+ os_release_info = File.read('/etc/os-release').split.inject({}) do |map, key_value_line|
126
+ key, _separator, value = key_value_line.partition('=')
127
+ map[key] = value
128
+ map
129
+ end
130
+ if os_release_info['CISCO_RELEASE_INFO'] && File.exist?(os_release_info['CISCO_RELEASE_INFO'])
131
+ os_release_info
132
+ else
133
+ false
134
+ end
135
+ end
136
+
137
+ def lsb
138
+ return @lsb if @lsb
139
+ @lsb = {}
140
+ if File.exists?("/etc/lsb-release")
141
+ File.open("/etc/lsb-release").each do |line|
142
+ case line
143
+ when /^DISTRIB_ID=["']?(.+?)["']?$/
144
+ @lsb[:id] = $1
145
+ when /^DISTRIB_RELEASE=["']?(.+?)["']?$/
146
+ @lsb[:release] = $1
147
+ when /^DISTRIB_CODENAME=["']?(.+?)["']?$/
148
+ @lsb[:codename] = $1
149
+ when /^DISTRIB_DESCRIPTION=["']?(.+?)["']?$/
150
+ @lsb[:description] = $1
151
+ end
152
+ end
153
+ elsif File.exists?("/usr/bin/lsb_release")
154
+ # Fedora/Redhat, requires redhat-lsb package
155
+ results = `lsb_release -a`
156
+ results.to_s.split("\n").each do |line|
157
+ case line
158
+ when /^Distributor ID:\s+(.+)$/
159
+ @lsb[:id] = $1
160
+ when /^Description:\s+(.+)$/
161
+ @lsb[:description] = $1
162
+ when /^Release:\s+(.+)$/
163
+ @lsb[:release] = $1
164
+ when /^Codename:\s+(.+)$/
165
+ @lsb[:codename] = $1
166
+ else
167
+ @lsb[:id] = line
168
+ end
169
+ end
170
+ end
171
+ @lsb
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,3 @@
1
+ require 'os_platform/platformable'
2
+ require 'os_platform/platform/linux'
3
+ require 'os_platform/platform/darwin'
@@ -0,0 +1,11 @@
1
+ require 'singleton'
2
+
3
+ module OSPlatform
4
+ module Platformable
5
+ attr_reader :platform, :platform_version, :platform_family
6
+
7
+ def collect_data
8
+ fail NotImplementedError, "Should implement collect_data in subclass"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module OSPlatform
2
+ VERSION = "0.0.1-beta"
3
+ end
@@ -0,0 +1,18 @@
1
+ require "os_platform/version"
2
+ require "rubygems/platform"
3
+ require "os_platform/platform"
4
+
5
+ module OSPlatform
6
+ class << self
7
+ def local
8
+ @os = Gem::Platform.local.os
9
+ if @os.respond_to?(:capitalize) && OSPlatform.const_defined?("Platform::#{@os.capitalize}")
10
+ klass = OSPlatform.const_get("Platform::#{@os.capitalize}").send(:new)
11
+ klass.collect_data
12
+ klass
13
+ else
14
+ fail "Not Support Platform For #{@os} Now, please submit bug on https://github.com/zlx/os_platform/issues."
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'os_platform/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "os_platform"
8
+ spec.version = OSPlatform::VERSION
9
+ spec.authors = ["Newell Zhu"]
10
+ spec.email = ["zlx.star@gmail.com"]
11
+
12
+ spec.summary = %q{Detect OS Platform}
13
+ spec.description = %q{Detect OS Platform, include platform, platform version}
14
+ spec.homepage = "https://github.com/zlx/os_platform"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: os_platform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre.beta
5
+ platform: ruby
6
+ authors:
7
+ - Newell Zhu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Detect OS Platform, include platform, platform version
42
+ email:
43
+ - zlx.star@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/os_platform.rb
58
+ - lib/os_platform/platform.rb
59
+ - lib/os_platform/platform/darwin.rb
60
+ - lib/os_platform/platform/linux.rb
61
+ - lib/os_platform/platformable.rb
62
+ - lib/os_platform/version.rb
63
+ - os_platform.gemspec
64
+ homepage: https://github.com/zlx/os_platform
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">"
80
+ - !ruby/object:Gem::Version
81
+ version: 1.3.1
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.2.0
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Detect OS Platform
88
+ test_files: []