macaddr-1.7.1/0000755000076400007640000000000012421236115012144 5ustar pravipravimacaddr-1.7.1/LICENSE0000644000076400007640000000001712421236115013147 0ustar pravipravisame as ruby's macaddr-1.7.1/macaddr.gemspec0000644000076400007640000000151512421236115015106 0ustar pravipravi## macaddr.gemspec # Gem::Specification::new do |spec| spec.name = "macaddr" spec.version = "1.7.1" spec.platform = Gem::Platform::RUBY spec.summary = "macaddr" spec.description = "cross platform mac address determination for ruby" spec.license = "Ruby" spec.files = ["Gemfile", "LICENSE", "README", "Rakefile", "lib", "lib/macaddr.rb", "macaddr.gemspec", "rvmrc.example", "test", "test/data", "test/data/noifconfig", "test/data/osx", "test/mac_test.rb", "test/testing.rb"] spec.executables = [] spec.require_path = "lib" spec.test_files = nil spec.add_dependency(*["systemu", "~> 2.6.2"]) spec.extensions.push(*[]) spec.rubyforge_project = "codeforpeople" spec.author = "Ara T. Howard" spec.email = "ara.t.howard@gmail.com" spec.homepage = "https://github.com/ahoward/macaddr" end macaddr-1.7.1/rvmrc.example0000644000076400007640000000004412421236115014650 0ustar pravipravirvm use --create ruby-1.9.3@macaddr macaddr-1.7.1/README0000644000076400007640000000145212421236115013026 0ustar pravipraviNAME macaddr DESCRIPTION cross platform mac address determination for ruby URI http://github.com/ahoward/macaddr http://rubyforg.org/projects/codeforpeople INSTALL gem install macaddr HISTORY New: - added a Gemfile for easier testing/dev with Bundler - added an example .rvmrc file that will setup for ruby 1.9.3 - updated systemu gem to ~>2.4.0 to work with ruby 1.9.3 1.1.0: - added dependancy on systemu to work around butt-licky windoze io capture: http://redmine.ruby-lang.org/issues/show/3215 1.0.0: - rdoc added - eric hodel kicks ass. to find why, see http://drawohara.com/post/44678286/eric-hodel-kicks-ass SYNOPSIS require 'macaddr' Mac.addr #=> first mac addr on your system Mac.addr.list #=> all mac addrs on your system macaddr-1.7.1/test/0000755000076400007640000000000012421236115013123 5ustar pravipravimacaddr-1.7.1/test/testing.rb0000644000076400007640000000671212421236115015133 0ustar pravipravirequire 'test/unit' testdir = File.expand_path(File.dirname(__FILE__)) rootdir = File.dirname(testdir) libdir = File.join(rootdir, 'lib') STDOUT.sync = true $:.unshift(testdir) unless $:.include?(testdir) $:.unshift(libdir) unless $:.include?(libdir) $:.unshift(rootdir) unless $:.include?(rootdir) def Testing(*args, &block) Class.new(Test::Unit::TestCase) do ## class methods # class << self def context(*args, &block) @context = args unless args.empty? block.call(*@context) if block @context end def slug_for(*args) string = [@context, args].flatten.compact.join('-') words = string.to_s.scan(%r/\w+/) words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''} words.delete_if{|word| word.nil? or word.strip.empty?} words.join('-').downcase end def name() const_get(:Name) end def testno() '%05d' % (@testno ||= 0) ensure @testno += 1 end def testing(*args, &block) method = ["test", testno, slug_for(*args)].delete_if{|part| part.empty?}.join('_') define_method("test_#{ testno }_#{ slug_for(*args) }", &block) end def setup(&block) define_method(:setup, &block) if block end def teardown(&block) define_method(:teardown, &block) if block end def prepare(&block) @prepare ||= [] @prepare.push(block) if block @prepare end def cleanup(&block) @cleanup ||= [] @cleanup.push(block) if block @cleanup end end ## configure the subclass! # const_set(:Testno, '0') slug = slug_for(*args).gsub(%r/-/,'_') name = ['TESTING', '%03d' % const_get(:Testno), slug].delete_if{|part| part.empty?}.join('_') name = name.upcase! const_set(:Name, name) const_set(:Missing, Object.new.freeze) ## instance methods # alias_method('__assert__', 'assert') def assert(*args, &block) if args.size == 1 and args.first.is_a?(Hash) options = args.first expected = getopt(:expected, options){ missing } actual = getopt(:actual, options){ missing } if expected == missing and actual == missing actual, expected, *ignored = options.to_a.flatten end expected = expected.call() if expected.respond_to?(:call) actual = actual.call() if actual.respond_to?(:call) assert_equal(expected, actual) end if block label = "assert(#{ args.join(' ') })" result = nil assert_nothing_raised{ result = block.call } __assert__(result, label) result else result = args.shift label = "assert(#{ args.join(' ') })" __assert__(result, label) result end end def missing self.class.const_get(:Missing) end def getopt(opt, hash, options = nil, &block) [opt.to_s, opt.to_s.to_sym].each do |key| return hash[key] if hash.has_key?(key) end default = if block block.call else options.is_a?(Hash) ? options[:default] : nil end return default end def subclass_of exception class << exception def ==(other) super or self > other end end exception end ## # module_eval(&block) self.setup() self.prepare.each{|b| b.call()} at_exit{ self.teardown() self.cleanup.each{|b| b.call()} } self end end macaddr-1.7.1/test/data/0000755000076400007640000000000012421236115014034 5ustar pravipravimacaddr-1.7.1/test/data/noifconfig0000644000076400007640000000006612421236115016102 0ustar pravipravid0:4e:f1:68:29:79 00:00:00:00:00:00 a2:28:b1:98:31:30 macaddr-1.7.1/test/data/osx0000644000076400007640000000326512421236115014576 0ustar pravipravilo0: flags=8049 mtu 16384 inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 inet 127.0.0.1 netmask 0xff000000 inet6 fdb1:49bc:790c:b10:cabc:c8ff:fe9b:28b1 prefixlen 128 gif0: flags=8010 mtu 1280 stf0: flags=0<> mtu 1280 en0: flags=8863 mtu 1500 ether c8:bc:c8:9b:28:b1 media: autoselect (none) status: inactive en1: flags=8863 mtu 1500 ether c8:bc:c8:dc:c9:44 inet 10.0.1.7 netmask 0xffffff00 broadcast 10.0.1.255 media: autoselect status: active en2: flags=8863 mtu 1500 ether 0b:0b:0b:0b:0b:0b inet6 fe80::cabc:c8ff:fe9b:28b1%en2 prefixlen 64 scopeid 0x6 inet 169.254.67.37 netmask 0xffff0000 broadcast 169.254.255.255 media: autoselect (10baseT/UTP ) status: active fw0: flags=8863 mtu 4078 lladdr 78:ca:39:ff:fe:37:4f:44 media: autoselect status: inactive vmnet1: flags=8863 mtu 1500 ether 00:50:56:c0:00:01 inet 172.16.139.1 netmask 0xffffff00 broadcast 172.16.139.255 vmnet8: flags=8863 mtu 1500 ether 00:50:56:c0:00:08 inet 192.168.33.1 netmask 0xffffff00 broadcast 192.168.33.255 en4: flags=8863 mtu 1500 ether c8:bc:c8:dc:ba:8a media: autoselect status: inactive utun0: flags=8051 mtu 1500 inet6 fe80::cabc:c8ff:fe9b:28b1%utun0 prefixlen 64 scopeid 0x9 inet6 fd00:6587:52d7:f8f2:cabc:c8ff:fe9b:28b1 prefixlen 64 macaddr-1.7.1/test/mac_test.rb0000644000076400007640000000156312421236115015254 0ustar pravipraviTesting Mac do $data.each do |basename, output| testing "#{ basename } output" do expected = basename macaddr = assert{ Mac.parse(output) } assert{ macaddr.is_a?(String) } assert{ macaddr =~ Mac::RE } assert{ macaddr.list.is_a?(Array) } assert{ macaddr == expected } end end testing ".addr" do assert{ Mac.addr } end end ### c8\:bc\:c8\:9b\:28\:b1 BEGIN { $testdir = File.dirname(File.expand_path(__FILE__)) $rootdir = File.dirname($testdir) $libdir = File.join($rootdir, 'lib') require File.join($libdir, 'macaddr') require File.join($testdir, 'testing') $datadir = File.join($testdir, 'data') $data = {} { 'c8:bc:c8:9b:28:b1' => 'osx', 'd0:4e:f1:68:29:79' => 'noifconfig' }.each do |expected, basename| entry = File.join($datadir, basename) $data[expected] = IO.read(entry) end } macaddr-1.7.1/Gemfile0000644000076400007640000000004512421236115013436 0ustar pravipravisource "http://rubygems.org" gemspec macaddr-1.7.1/metadata.yml0000644000076400007640000000266412421236115014457 0ustar pravipravi--- !ruby/object:Gem::Specification name: macaddr version: !ruby/object:Gem::Version version: 1.7.1 platform: ruby authors: - Ara T. Howard autorequire: bindir: bin cert_chain: [] date: 2014-04-11 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: systemu requirement: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: 2.6.2 type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: 2.6.2 description: cross platform mac address determination for ruby email: ara.t.howard@gmail.com executables: [] extensions: [] extra_rdoc_files: [] files: - Gemfile - LICENSE - README - Rakefile - lib/macaddr.rb - macaddr.gemspec - rvmrc.example - test/data/noifconfig - test/data/osx - test/mac_test.rb - test/testing.rb homepage: https://github.com/ahoward/macaddr licenses: - Ruby metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: codeforpeople rubygems_version: 2.0.3 signing_key: specification_version: 4 summary: macaddr test_files: [] macaddr-1.7.1/lib/0000755000076400007640000000000012421236115012712 5ustar pravipravimacaddr-1.7.1/lib/macaddr.rb0000644000076400007640000000572512421236115014643 0ustar pravipravi## # Cross platform MAC address determination. Works for: # * /sbin/ifconfig # * /bin/ifconfig # * ifconfig # * ipconfig /all # # To return the first MAC address on the system: # # Mac.address # # To return an array of all MAC addresses: # # Mac.address.list # begin require 'rubygems' rescue LoadError nil end require 'systemu' require 'socket' module Mac VERSION = '1.7.1' def Mac.version ::Mac::VERSION end def Mac.dependencies { 'systemu' => [ 'systemu' , '~> 2.6.2' ] } end def Mac.description 'cross platform mac address determination for ruby' end class << self ## # Accessor for the system's first MAC address, requires a call to #address # first attr_accessor "mac_address" ## # Discovers and returns the system's MAC addresses. Returns the first # MAC address, and includes an accessor #list for the remaining addresses: # # Mac.addr # => first address # Mac.addr.list # => all addresses def address return @mac_address if defined? @mac_address and @mac_address @mac_address = from_getifaddrs return @mac_address if @mac_address cmds = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all', 'cat /sys/class/net/*/address' output = nil cmds.each do |cmd| _, stdout, _ = systemu(cmd) rescue next next unless stdout and stdout.size > 0 output = stdout and break end raise "all of #{ cmds.join ' ' } failed" unless output @mac_address = parse(output) end link = Socket::PF_LINK if Socket.const_defined? :PF_LINK packet = Socket::PF_PACKET if Socket.const_defined? :PF_PACKET INTERFACE_PACKET_FAMILY = link || packet # :nodoc: def from_getifaddrs return unless Socket.respond_to? :getifaddrs interfaces = Socket.getifaddrs.select do |addr| addr.addr.pfamily == INTERFACE_PACKET_FAMILY end mac, = if Socket.const_defined? :PF_LINK then interfaces.map do |addr| addr.addr.getnameinfo end.find do |m,| !m.empty? end elsif Socket.const_defined? :PF_PACKET then interfaces.map do |addr| addr.addr.inspect_sockaddr[/hwaddr=([\h:]+)/, 1] end.find do |mac_addr| mac_addr != '00:00:00:00:00:00' end end @mac_address = mac if mac end def parse(output) lines = output.split(/\n/) candidates = lines.select{|line| line =~ RE} raise 'no mac address candidates' unless candidates.first candidates.map!{|c| c[RE].strip} maddr = candidates.first raise 'no mac address found' unless maddr maddr.strip! maddr.instance_eval{ @list = candidates; def list() @list end } maddr end ## # Shorter alias for #address alias_method "addr", "address" end RE = %r/(?:[^:\-]|\A)(?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F](?:[^:\-]|\Z)/io end MacAddr = Macaddr = Mac macaddr-1.7.1/Rakefile0000644000076400007640000002364012421236115013616 0ustar pravipraviThis.rubyforge_project = 'codeforpeople' This.author = "Ara T. Howard" This.email = "ara.t.howard@gmail.com" This.homepage = "https://github.com/ahoward/#{ This.lib }" task :license do open('LICENSE', 'w'){|fd| fd.puts "Ruby"} end task :default do puts((Rake::Task.tasks.map{|task| task.name.gsub(/::/,':')} - ['default']).sort) end task :test do run_tests! end namespace :test do task(:unit){ run_tests!(:unit) } task(:functional){ run_tests!(:functional) } task(:integration){ run_tests!(:integration) } end def run_tests!(which = nil) which ||= '**' test_dir = File.join(This.dir, "test") test_glob ||= File.join(test_dir, "#{ which }/**_test.rb") test_rbs = Dir.glob(test_glob).sort div = ('=' * 119) line = ('-' * 119) test_rbs.each_with_index do |test_rb, index| testno = index + 1 command = "#{ This.ruby } -w -I ./lib -I ./test/lib #{ test_rb }" puts say(div, :color => :cyan, :bold => true) say("@#{ testno } => ", :bold => true, :method => :print) say(command, :color => :cyan, :bold => true) say(line, :color => :cyan, :bold => true) system(command) say(line, :color => :cyan, :bold => true) status = $?.exitstatus if status.zero? say("@#{ testno } <= ", :bold => true, :color => :white, :method => :print) say("SUCCESS", :color => :green, :bold => true) else say("@#{ testno } <= ", :bold => true, :color => :white, :method => :print) say("FAILURE", :color => :red, :bold => true) end say(line, :color => :cyan, :bold => true) exit(status) unless status.zero? end end task :gemspec do ignore_extensions = ['git', 'svn', 'tmp', /sw./, 'bak', 'gem'] ignore_directories = ['pkg'] ignore_files = ['test/log'] shiteless = lambda do |list| list.delete_if do |entry| next unless test(?e, entry) extension = File.basename(entry).split(%r/[.]/).last ignore_extensions.any?{|ext| ext === extension} end list.delete_if do |entry| next unless test(?d, entry) dirname = File.expand_path(entry) ignore_directories.any?{|dir| File.expand_path(dir) == dirname} end list.delete_if do |entry| next unless test(?f, entry) filename = File.expand_path(entry) ignore_files.any?{|file| File.expand_path(file) == filename} end end lib = This.lib object = This.object version = This.version files = shiteless[Dir::glob("**/**")] executables = shiteless[Dir::glob("bin/*")].map{|exe| File.basename(exe)} #has_rdoc = true #File.exist?('doc') test_files = "test/#{ lib }.rb" if File.file?("test/#{ lib }.rb") summary = object.respond_to?(:summary) ? object.summary : "summary: #{ lib } kicks the ass" description = object.respond_to?(:description) ? object.description : "description: #{ lib } kicks the ass" license = object.respond_to?(:license) ? object.license : "Ruby" if This.extensions.nil? This.extensions = [] extensions = This.extensions %w( Makefile configure extconf.rb ).each do |ext| extensions << ext if File.exists?(ext) end end extensions = [extensions].flatten.compact if This.dependencies.nil? dependencies = [] else case This.dependencies when Hash dependencies = This.dependencies.values when Array dependencies = This.dependencies end end template = if test(?e, 'gemspec.erb') Template{ IO.read('gemspec.erb') } else Template { <<-__ ## <%= lib %>.gemspec # Gem::Specification::new do |spec| spec.name = <%= lib.inspect %> spec.version = <%= version.inspect %> spec.platform = Gem::Platform::RUBY spec.summary = <%= lib.inspect %> spec.description = <%= description.inspect %> spec.license = <%= license.inspect %> spec.files =\n<%= files.sort.pretty_inspect %> spec.executables = <%= executables.inspect %> spec.require_path = "lib" spec.test_files = <%= test_files.inspect %> <% dependencies.each do |lib_version| %> spec.add_dependency(*<%= Array(lib_version).flatten.inspect %>) <% end %> spec.extensions.push(*<%= extensions.inspect %>) spec.rubyforge_project = <%= This.rubyforge_project.inspect %> spec.author = <%= This.author.inspect %> spec.email = <%= This.email.inspect %> spec.homepage = <%= This.homepage.inspect %> end __ } end Fu.mkdir_p(This.pkgdir) gemspec = "#{ lib }.gemspec" open(gemspec, "w"){|fd| fd.puts(template)} This.gemspec = gemspec end task :gem => [:clean, :gemspec] do Fu.mkdir_p(This.pkgdir) before = Dir['*.gem'] cmd = "gem build #{ This.gemspec }" `#{ cmd }` after = Dir['*.gem'] gem = ((after - before).first || after.first) or abort('no gem!') Fu.mv(gem, This.pkgdir) This.gem = File.join(This.pkgdir, File.basename(gem)) end task :readme do samples = '' prompt = '~ > ' lib = This.lib version = This.version Dir['sample*/*'].sort.each do |sample| samples << "\n" << " <========< #{ sample } >========>" << "\n\n" cmd = "cat #{ sample }" samples << Util.indent(prompt + cmd, 2) << "\n\n" samples << Util.indent(`#{ cmd }`, 4) << "\n" cmd = "ruby #{ sample }" samples << Util.indent(prompt + cmd, 2) << "\n\n" cmd = "ruby -e'STDOUT.sync=true; exec %(ruby -I ./lib #{ sample })'" samples << Util.indent(`#{ cmd } 2>&1`, 4) << "\n" end template = if test(?e, 'README.erb') Template{ IO.read('README.erb') } else Template { <<-__ NAME #{ lib } DESCRIPTION INSTALL gem install #{ lib } SAMPLES #{ samples } __ } end open("README", "w"){|fd| fd.puts template} end task :clean do Dir[File.join(This.pkgdir, '**/**')].each{|entry| Fu.rm_rf(entry)} end task :release => [:clean, :gemspec, :gem] do gems = Dir[File.join(This.pkgdir, '*.gem')].flatten raise "which one? : #{ gems.inspect }" if gems.size > 1 raise "no gems?" if gems.size < 1 cmd = "gem push #{ This.gem }" puts cmd puts system(cmd) abort("cmd(#{ cmd }) failed with (#{ $?.inspect })") unless $?.exitstatus.zero? cmd = "rubyforge login && rubyforge add_release #{ This.rubyforge_project } #{ This.lib } #{ This.version } #{ This.gem }" puts cmd puts system(cmd) abort("cmd(#{ cmd }) failed with (#{ $?.inspect })") unless $?.exitstatus.zero? end BEGIN { # support for this rakefile # $VERBOSE = nil require 'ostruct' require 'erb' require 'fileutils' require 'rbconfig' require 'pp' # fu shortcut # Fu = FileUtils # cache a bunch of stuff about this rakefile/environment # This = OpenStruct.new This.file = File.expand_path(__FILE__) This.dir = File.dirname(This.file) This.pkgdir = File.join(This.dir, 'pkg') # grok lib # lib = ENV['LIB'] unless lib lib = File.basename(Dir.pwd).sub(/[-].*$/, '') end This.lib = lib # grok version # version = ENV['VERSION'] unless version require "./lib/#{ This.lib }" This.name = lib.capitalize This.object = eval(This.name) version = This.object.send(:version) end This.version = version # see if dependencies are export by the module # if This.object.respond_to?(:dependencies) This.dependencies = This.object.dependencies end # we need to know the name of the lib an it's version # abort('no lib') unless This.lib abort('no version') unless This.version # discover full path to this ruby executable # c = Config::CONFIG bindir = c["bindir"] || c['BINDIR'] ruby_install_name = c['ruby_install_name'] || c['RUBY_INSTALL_NAME'] || 'ruby' ruby_ext = c['EXEEXT'] || '' ruby = File.join(bindir, (ruby_install_name + ruby_ext)) This.ruby = ruby # some utils # module Util def indent(s, n = 2) s = unindent(s) ws = ' ' * n s.gsub(%r/^/, ws) end def unindent(s) indent = nil s.each_line do |line| next if line =~ %r/^\s*$/ indent = line[%r/^\s*/] and break end indent ? s.gsub(%r/^#{ indent }/, "") : s end extend self end # template support # class Template def initialize(&block) @block = block @template = block.call.to_s end def expand(b=nil) ERB.new(Util.unindent(@template)).result((b||@block).binding) end alias_method 'to_s', 'expand' end def Template(*args, &block) Template.new(*args, &block) end # colored console output support # This.ansi = { :clear => "\e[0m", :reset => "\e[0m", :erase_line => "\e[K", :erase_char => "\e[P", :bold => "\e[1m", :dark => "\e[2m", :underline => "\e[4m", :underscore => "\e[4m", :blink => "\e[5m", :reverse => "\e[7m", :concealed => "\e[8m", :black => "\e[30m", :red => "\e[31m", :green => "\e[32m", :yellow => "\e[33m", :blue => "\e[34m", :magenta => "\e[35m", :cyan => "\e[36m", :white => "\e[37m", :on_black => "\e[40m", :on_red => "\e[41m", :on_green => "\e[42m", :on_yellow => "\e[43m", :on_blue => "\e[44m", :on_magenta => "\e[45m", :on_cyan => "\e[46m", :on_white => "\e[47m" } def say(phrase, *args) options = args.last.is_a?(Hash) ? args.pop : {} options[:color] = args.shift.to_s.to_sym unless args.empty? keys = options.keys keys.each{|key| options[key.to_s.to_sym] = options.delete(key)} color = options[:color] bold = options.has_key?(:bold) parts = [phrase] parts.unshift(This.ansi[color]) if color parts.unshift(This.ansi[:bold]) if bold parts.push(This.ansi[:clear]) if parts.size > 1 method = options[:method] || :puts Kernel.send(method, parts.join) end # always run out of the project dir # Dir.chdir(This.dir) }