lsof 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES ADDED
File without changes
data/README ADDED
@@ -0,0 +1,8 @@
1
+ = Lsof
2
+ Lsof has some utility functions that allow you to manage processes using the unix lsof command.
3
+ Functions include getting the pid and killing a process that listens on a particular port.
4
+
5
+ == Usage
6
+ Lsof.running?(6666) # returns true if there is the process that listens on port 6666
7
+ Lsof.kill(6666) # kill the process that listens on port 6666
8
+ Lsof.listener_pid(6666) # returns the process id for the process that listens on port 6666
data/Rakefile ADDED
@@ -0,0 +1,69 @@
1
+ require "rake"
2
+ require 'rake/gempackagetask'
3
+ require 'rake/contrib/rubyforgepublisher'
4
+ require 'rake/clean'
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
7
+
8
+ desc "Runs the Rspec suite"
9
+ task(:default) do
10
+ run_suite
11
+ end
12
+
13
+ desc "Runs the Rspec suite"
14
+ task(:spec) do
15
+ run_suite
16
+ end
17
+
18
+ desc "Copies the trunk to a tag with the name of the current release"
19
+ task(:tag_release) do
20
+ tag_release
21
+ end
22
+
23
+ def run_suite
24
+ dir = File.dirname(__FILE__)
25
+ system("ruby #{dir}/spec/spec_suite.rb") || raise("Spec Suite failed")
26
+ end
27
+
28
+ PKG_NAME = "lsof"
29
+ PKG_VERSION = "0.1.0"
30
+ PKG_FILES = FileList[
31
+ '[A-Z]*',
32
+ '*.rb',
33
+ 'lib/**/*.rb',
34
+ 'spec/**/*.rb'
35
+ ]
36
+
37
+ spec = Gem::Specification.new do |s|
38
+ s.name = PKG_NAME
39
+ s.version = PKG_VERSION
40
+ s.summary = "A library utilizing the lsof unix command for process management."
41
+ s.test_files = "spec/spec_suite.rb"
42
+ s.description = s.summary
43
+
44
+ s.files = PKG_FILES.to_a
45
+ s.require_path = 'lib'
46
+
47
+ s.has_rdoc = true
48
+ s.extra_rdoc_files = [ "README", "CHANGES" ]
49
+ s.rdoc_options = ["--main", "README", "--inline-source", "--line-numbers"]
50
+
51
+ s.test_files = Dir.glob('spec/*_spec.rb')
52
+ s.require_path = 'lib'
53
+ s.autorequire = 'lsof'
54
+ s.author = "Corey Innis + Brian Takita"
55
+ s.email = "corey@pivotallabs.com;brian@pivotallabs.com;"
56
+ s.homepage = "http://pivotallabs.com"
57
+ s.rubyforge_project = "pivotalrb"
58
+ end
59
+
60
+ Rake::GemPackageTask.new(spec) do |pkg|
61
+ pkg.need_zip = true
62
+ pkg.need_tar = true
63
+ end
64
+
65
+ def tag_release
66
+ dashed_version = PKG_VERSION.gsub('.', '-')
67
+ svn_user = "#{ENV["SVN_USER"]}@" || ""
68
+ `svn cp svn+ssh://#{svn_user}rubyforge.org/var/svn/pivotalrb/lsof/trunk svn+ssh://#{svn_user}rubyforge.org/var/svn/pivotalrb/lsof/tags/REL-#{dashed_version} -m 'Version #{PKG_VERSION}'`
69
+ end
data/lib/lsof.rb ADDED
@@ -0,0 +1,21 @@
1
+ class Lsof
2
+ class << self
3
+ def kill(port)
4
+ pid = listener_pid(port)
5
+ system "kill -9 #{pid}" if pid
6
+ end
7
+
8
+ def running?(port)
9
+ listener_pid(port) ? true : false
10
+ end
11
+
12
+ def listener_pid(port)
13
+ port = `lsof -i tcp:#{port} | grep '(LISTEN)' | awk '{print $2}'`
14
+ if port.empty?
15
+ nil
16
+ else
17
+ Integer(port)
18
+ end
19
+ end
20
+ end
21
+ end
data/spec/lsof_spec.rb ADDED
@@ -0,0 +1,90 @@
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
+
3
+ describe Lsof do
4
+ include WaitFor
5
+ include FileUtils
6
+ attr_reader :port, :pid_of_process
7
+ before do
8
+ @port = 6666
9
+ end
10
+
11
+ after do
12
+ hide_error_stream do
13
+ Lsof.kill port
14
+ end
15
+
16
+ rm_rf "lsof.pid"
17
+ end
18
+
19
+ def start_process_using_port
20
+ Thread.start do
21
+ cmd = <<-CMD
22
+ ruby -e '
23
+ File.open("lsof.pid", "w") do |file|
24
+ file.print Process.pid.to_s
25
+ end
26
+ require "rubygems"
27
+ require "eventmachine"
28
+ EventMachine::run do
29
+ EventMachine::start_server "127.0.0.1", #{port}, EventMachine::Protocols::LineAndTextProtocol
30
+ end
31
+ '2>&1
32
+ CMD
33
+ `#{cmd}`
34
+ end
35
+ wait_for do
36
+ Lsof.running?(port)
37
+ end
38
+ @pid_of_process = Integer(File.read("lsof.pid"))
39
+ pid_of_process.should_not be_nil
40
+ pid_of_process.class.should == Fixnum
41
+ end
42
+
43
+ def hide_error_stream
44
+ old_stderr = $stderr
45
+ begin
46
+ $stderr = StringIO.new
47
+ yield
48
+ rescue Exception => e
49
+ $stderr = old_stderr
50
+ raise e
51
+ ensure
52
+ $stderr = old_stderr
53
+ end
54
+ end
55
+
56
+ describe '.kill' do
57
+ it "kills all processes associated with a provided port" do
58
+ start_process_using_port
59
+ hide_error_stream do
60
+ Lsof.kill port
61
+ end
62
+ wait_for do
63
+ !Lsof.running?(port)
64
+ end
65
+ end
66
+ end
67
+
68
+ describe '.running?' do
69
+ it "when there is a process is using the port, returns true" do
70
+ start_process_using_port
71
+ Lsof.running?(port).should be_true
72
+ end
73
+
74
+ it "when there is no process using the port, returns false" do
75
+ Lsof.running?(port).should be_false
76
+ end
77
+ end
78
+
79
+ describe ".listener_pid" do
80
+ it "when there is a process listening on a port, returns the process id" do
81
+ start_process_using_port
82
+ pid = Lsof.listener_pid(port)
83
+ pid.should == pid_of_process
84
+ end
85
+
86
+ it "when there is no process listening on a port, returns nil" do
87
+ Lsof.listener_pid(port).should == nil
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,22 @@
1
+ require "rubygems"
2
+ require "spec"
3
+ dir = File.dirname(__FILE__)
4
+ $LOAD_PATH.unshift File.expand_path("#{dir}/../lib")
5
+ require "lsof"
6
+ require "rr"
7
+ require "fileutils"
8
+ require "eventmachine"
9
+
10
+ Spec::Runner.configure do |config|
11
+ config.mock_with :rr
12
+ end
13
+
14
+ module WaitFor
15
+ def wait_for(seconds=5)
16
+ Timeout.timeout(seconds) do
17
+ loop do
18
+ break if yield
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ class SpecSuite
2
+ def run
3
+ dir = File.dirname(__FILE__)
4
+ Dir["#{dir}/**/*_spec.rb"].each do |file|
5
+ require file
6
+ end
7
+ end
8
+ end
9
+
10
+ if __FILE__ == $0
11
+ SpecSuite.new.run
12
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: lsof
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-12-28 00:00:00 -08:00
8
+ summary: A library utilizing the lsof unix command for process management.
9
+ require_paths:
10
+ - lib
11
+ email: corey@pivotallabs.com;brian@pivotallabs.com;
12
+ homepage: http://pivotallabs.com
13
+ rubyforge_project: pivotalrb
14
+ description: A library utilizing the lsof unix command for process management.
15
+ autorequire: lsof
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Corey Innis + Brian Takita
31
+ files:
32
+ - CHANGES
33
+ - Rakefile
34
+ - README
35
+ - lib/lsof.rb
36
+ - spec/lsof_spec.rb
37
+ - spec/spec_helper.rb
38
+ - spec/spec_suite.rb
39
+ test_files:
40
+ - spec/lsof_spec.rb
41
+ rdoc_options:
42
+ - --main
43
+ - README
44
+ - --inline-source
45
+ - --line-numbers
46
+ extra_rdoc_files:
47
+ - README
48
+ - CHANGES
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ requirements: []
54
+
55
+ dependencies: []
56
+