svutil 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-04-22
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/svutil.rb
7
+ lib/svutil/log.rb
8
+ lib/svutil/config.rb
9
+ lib/svutil/process_manager.rb
10
+ test/test_helper.rb
11
+ test/test_svutil.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on svutil, see http://svutil.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = svutil
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/svutil'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('svutil', SVUtil::VERSION) do |p|
7
+ p.developer('FIXME full name', 'FIXME email')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,96 @@
1
+ require 'optparse'
2
+ require 'optparse/time'
3
+ require 'ostruct'
4
+ require 'pp'
5
+
6
+ module SVUtil
7
+ def config
8
+ Config.config_class
9
+ end
10
+
11
+ class Config
12
+ class << self
13
+ attr_writer :config_file
14
+
15
+ def config_class
16
+ subclasses_of(self).first || self
17
+ end
18
+
19
+ def config_file
20
+ @config_file || 'settings'
21
+ end
22
+
23
+ def load_and_parse
24
+ process_options
25
+ load_config_file
26
+ apply_all
27
+ validate
28
+ end
29
+
30
+ def set(name, value)
31
+ @temp_hash ||= {}
32
+ @temp_hash[name.to_s] = value
33
+ end
34
+
35
+ def apply(name, value)
36
+ @hash ||= {}
37
+ @hash[name.to_s] = value
38
+ end
39
+
40
+ def apply_all
41
+ @hash ||= {}
42
+ @hash.merge!(@temp_hash) if @temp_hash
43
+ end
44
+
45
+ def method_missing(method_id, *args)
46
+ return nil unless @hash
47
+ @hash[method_id.to_s]
48
+ end
49
+
50
+ def validate
51
+ # TODO: Check file perms
52
+ if (pid_file.nil? or pid_file.empty? or File.directory?(pid_file))
53
+ STDERR.puts "PID file must be a writable file"
54
+ exit 1
55
+ end
56
+ end
57
+
58
+ def parse_options
59
+ OptionParser.new do |opts|
60
+ opts.on("-f", "--config [filename]", "Config file to use (default 'settings')") do |filename|
61
+ config.config_file = filename
62
+ end
63
+ yield opts
64
+ end.parse!
65
+ end
66
+
67
+ private
68
+ def load_config_file
69
+ contents = ""
70
+ File.open(config_file, "r") { |file| contents << file.read }
71
+ contents.split("\n").each_with_index do |line, index|
72
+ pair = line.split("=")
73
+ if pair.size != 2
74
+ STDERR.puts "Invalid config file '#{config_file}'. Syntax error at line #{index + 1}"
75
+ exit 1
76
+ end
77
+ config_class.apply(pair[0].strip, pair[1].strip)
78
+ end
79
+ end
80
+
81
+ def subclasses_of(*superclasses) #:nodoc:
82
+ subclasses = []
83
+ superclasses.each do |sup|
84
+ ObjectSpace.each_object(Class) do |k|
85
+ if superclasses.any? { |superclass| k < superclass } &&
86
+ (k.name.nil? || k.name.empty? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
87
+ subclasses << k
88
+ end
89
+ end
90
+ subclasses.uniq!
91
+ end
92
+ subclasses
93
+ end
94
+ end
95
+ end
96
+ end
data/lib/svutil/log.rb ADDED
@@ -0,0 +1,15 @@
1
+ module SVUtil
2
+ class Log
3
+ class << self
4
+ %w(info warning error).each do |level|
5
+ define_method(level) do |arg|
6
+ log(level, arg)
7
+ end
8
+ end
9
+
10
+ def log(level, arg)
11
+ STDOUT.puts "#{Time.now}: (#{level}) #{arg}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,67 @@
1
+ module SVUtil
2
+ class ProcessManager
3
+ def initialize(klass)
4
+ Signal.trap("INT") { shutdown }
5
+ Signal.trap("TERM") { shutdown }
6
+ if running?
7
+ STDERR.puts "There is already a '#{$0}' process running"
8
+ exit 1
9
+ end
10
+ daemonize if config.daemon
11
+ write_pid_file
12
+ @klass = klass
13
+ end
14
+
15
+ def start
16
+ @klass.new.run
17
+ end
18
+
19
+ private
20
+ def shutdown
21
+ Log.info "Shutting Down"
22
+ remove_pid_file
23
+ exit 0
24
+ end
25
+
26
+ def running?
27
+ pid_file = config.pid_file
28
+ return false unless pid_file
29
+ File.exist?(pid_file)
30
+ end
31
+
32
+ def write_pid_file
33
+ pid_file = config.pid_file
34
+ return unless pid_file
35
+ File.open(pid_file, "w") { |f| f.write(Process.pid) }
36
+ File.chmod(0644, pid_file)
37
+ end
38
+
39
+ def remove_pid_file
40
+ pid_file = config.pid_file
41
+ return unless pid_file
42
+ File.unlink(pid_file) if File.exists?(pid_file)
43
+ end
44
+
45
+ def daemonize
46
+ fork and exit
47
+ redirect_io
48
+ end
49
+
50
+ def redirect_io
51
+ begin; STDIN.reopen('/dev/null'); rescue Exception; end
52
+ if config.log_file
53
+ begin
54
+ STDOUT.reopen(config.log_file, "a")
55
+ STDOUT.sync = true
56
+ rescue Exception
57
+ begin; STDOUT.reopen('/dev/null'); rescue Exception; end
58
+ end
59
+ else
60
+ begin; STDOUT.reopen('/dev/null'); rescue Exception; end
61
+ end
62
+
63
+ begin; STDERR.reopen(STDOUT); rescue Exception; end
64
+ STDERR.sync = true
65
+ end
66
+ end
67
+ end
data/lib/svutil.rb ADDED
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'svutil/config'
5
+ require 'svutil/log'
6
+ require 'svutil/process_manager'
7
+
8
+ module SVUtil
9
+ VERSION = '0.0.1'
10
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/svutil'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestSVUtil < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svutil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - FIXME full name
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-22 00:00:00 +09:30
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.3.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ description: FIX (describe your package)
36
+ email:
37
+ - FIXME email
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - PostInstall.txt
46
+ - README.rdoc
47
+ files:
48
+ - History.txt
49
+ - Manifest.txt
50
+ - PostInstall.txt
51
+ - README.rdoc
52
+ - Rakefile
53
+ - lib/svutil.rb
54
+ - lib/svutil/log.rb
55
+ - lib/svutil/config.rb
56
+ - lib/svutil/process_manager.rb
57
+ - test/test_helper.rb
58
+ - test/test_svutil.rb
59
+ has_rdoc: true
60
+ homepage: FIX (url)
61
+ post_install_message: PostInstall.txt
62
+ rdoc_options:
63
+ - --main
64
+ - README.rdoc
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project: svutil
82
+ rubygems_version: 1.3.1
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: FIX (describe your package)
86
+ test_files:
87
+ - test/test_helper.rb
88
+ - test/test_svutil.rb