bin_script 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b2ee1ee087b3240b53907521e8f6c97c2c708b6
4
+ data.tar.gz: 377c4055de2c116da0215a4a79a36f4abca0f0b0
5
+ SHA512:
6
+ metadata.gz: 6f2c8cfd1e6df3a5456320fb99bcf7deaf8b32dcca082290400e559346302cfbae3d17a273ec435d3e6533d5d5df523454cd67eb5200a28a6297acba3dbb66bc
7
+ data.tar.gz: d8c538593c4e25472e13f4c673f9078cdefdcf400cba67701564caaa136aac6353ec999bb61a69d7a15864413da96ce8cc9edae8efedfc624d25410cd414a95f
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gemspec
data/README.md CHANGED
@@ -1,16 +1,17 @@
1
1
  Rails BinScript
2
2
  ===============
3
3
 
4
- Easy writing and executing bins(executable scripts) in Rails Application (especially for crontab or god).
5
- For my purposes much better than Rake, Thor and Rails Runner.
4
+ Gem for easy writing and executing scripts in Rails Application. For my purposes much better than Rake, Thor and Rails Runner.
6
5
 
7
6
  Features:
8
7
 
9
8
  1. Each bin is a class
10
9
  2. Easy writing tests
11
- 3. Bin use lock file and logger with formatter when executing
12
-
13
- Rails 2.3 and 3 compatible
10
+ 3. Custom lock file
11
+ 4. Custom logger with formatter for each script
12
+ 5. Can be daemonized
13
+
14
+ Tested on Rails 2.3 and 3.2
14
15
 
15
16
  ``` ruby
16
17
  gem 'bin_script'
@@ -22,10 +23,10 @@ Call like:
22
23
 
23
24
  $ cd project && ./bin/bla -e production --test -d "2012-04-07" -u "asdf"
24
25
 
25
- Features by default:
26
+ Call examples:
26
27
 
27
28
  $ ./bin/bla -h
28
- $ ./bin/bla -e production
29
+ $ ./bin/bla -e production
29
30
  $ ./bin/bla -e production -L ./locks/bla.lock
30
31
  $ ./bin/bla -e production -l ./log/bla.log
31
32
  $ ./bin/bla -e production --daemonize --pidfile ./tmp/bla.pid
@@ -40,17 +41,17 @@ class BlaScript < BinScript
40
41
  optional :u, "Update string"
41
42
  required :d, :description => "Date in format YYYY-MM-DD or YYYY-MM", :default => "2012-04-01"
42
43
  noarg :t, :decription => "Test run", :alias => 'test'
43
-
44
+
44
45
  self.description = "Super Bla script"
45
-
46
+
46
47
  def test?
47
48
  params(:t)
48
49
  end
49
50
 
50
51
  def do!
51
52
  if test?
52
- logger.info "update string #{params(:u)}"
53
- else
53
+ logger.info "update string #{params(:u)}"
54
+ else
54
55
  logger.info "data #{params(:d)}"
55
56
  end
56
57
  end
@@ -72,9 +73,9 @@ end
72
73
  ``` ruby
73
74
  class BinScript
74
75
  def notify_about_error(exception)
75
- Mailter.some_notification(exception)...
76
+ Mailter.some_notification(exception)...
76
77
  end
77
78
  end
78
79
  ```
79
80
 
80
-
81
+
@@ -7,14 +7,13 @@ Gem::Specification.new do |s|
7
7
 
8
8
  s.authors = ["Makarchev Konstantin", "Lifshits Dmitry"]
9
9
  s.autorequire = %q{init}
10
-
11
- s.description = %q{Easy writing and executing bins(executable scripts) in Rails Application (especially for crontab or god)}
12
-
13
- s.summary = %q{Easy writing and executing bins(executable scripts) in Rails Application (especially for crontab or god).
14
- For my purposes much better than Rake, Thor and Rails Runner.}
10
+
11
+ s.description = s.summary = \
12
+ %q{Gem for easy writing and executing scripts in Rails Application. For my purposes much better than Rake, Thor and Rails Runner.}
15
13
 
16
14
  s.email = %q{kostya27@gmail.com}
17
15
  s.homepage = %q{http://github.com/kostya/bin_script}
16
+ s.license = "MIT"
18
17
 
19
18
  s.files = `git ls-files`.split("\n")
20
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -23,7 +22,8 @@ For my purposes much better than Rake, Thor and Rails Runner.}
23
22
 
24
23
  s.add_dependency 'activesupport'
25
24
  s.add_dependency 'rails'
25
+
26
26
  s.add_development_dependency "rspec"
27
27
  s.add_development_dependency "rake"
28
-
29
- end
28
+
29
+ end
@@ -11,7 +11,7 @@ require File.dirname(__FILE__) + '/class_inheritable_attributes'
11
11
  class BinScript
12
12
  include ClassLevelInheritableAttributes
13
13
  class_inheritable_attributes :parameters, :log_level, :enable_locking, :enable_logging, :date_log_postfix, :disable_puts_for_tests, :description
14
-
14
+
15
15
  # Default parameters
16
16
  @parameters = [
17
17
  {:key => :e, :type => :required, :description => "Rails environment ID (default - development)"},
@@ -25,10 +25,10 @@ class BinScript
25
25
 
26
26
  # Enable logging by default
27
27
  @enable_logging = true
28
-
28
+
29
29
  # Default log level INFO or DEBUG for test env
30
30
  @log_level = (RailsStub.env == 'development' ? XLogger::DEBUG : XLogger::INFO)
31
-
31
+
32
32
  # Bin Description
33
33
  @description = nil
34
34
 
@@ -72,7 +72,7 @@ class BinScript
72
72
  @logger.send(method,message)
73
73
  end
74
74
  end
75
-
75
+
76
76
  class << self
77
77
  # Get parameter by key
78
78
  def get_parameter(key)
@@ -85,7 +85,7 @@ class BinScript
85
85
  def script_name
86
86
  self.to_s.underscore.gsub('/','__')
87
87
  end
88
-
88
+
89
89
  def bin_name
90
90
  script_name.gsub('_script', '')
91
91
  end
@@ -121,14 +121,14 @@ class BinScript
121
121
  def calculate_script_class_filename(parts)
122
122
  files = []
123
123
 
124
- # Calculate and add to list file with script class itself
125
- class_file = File.join(%w{app bins}, parts)
126
- class_file += '_nagios' if(parts.length > 1)
127
- class_file += '_script.rb'
128
- files << class_file
124
+ # Collect from disk, needed class files
125
+ parts.each do |p|
126
+ off = "app/{bin,bins,models,script,scripts}/**/#{p}_script.rb"
127
+ files += Dir[File.join(RailsStub.root, off)]
129
128
 
130
- # Add intermediate classes
131
- parts[0..-2].each { |p| files << "app/models/#{p}_script.rb" }
129
+ off = "script/**/#{p}_script.rb"
130
+ files += Dir[File.join(RailsStub.root, off)]
131
+ end
132
132
 
133
133
  files.reverse
134
134
  end
@@ -136,7 +136,7 @@ class BinScript
136
136
  # Run script detected by the filename of source script file
137
137
  def run_script(filename = $0)
138
138
  cfg = parse_script_file_name(Pathname.new(filename).realpath.to_s)
139
- cfg[:files].each { |f| require File.join(RailsStub.root, f) }
139
+ cfg[:files].each { |f| require f }
140
140
 
141
141
  # Create instance and call run! for script class
142
142
  klass = cfg[:class].constantize
@@ -170,7 +170,7 @@ class BinScript
170
170
  @parameters.each { |p| new << p if p[:key] != key }
171
171
  @parameters = new
172
172
  end
173
-
173
+
174
174
  # Prepare ARGV parameters as hash
175
175
  def get_argv_values
176
176
  values = {}
@@ -187,7 +187,7 @@ class BinScript
187
187
  usage_msg += "Use: ./bin/#{bin_name}.rb [OPTIONS]\n\n"
188
188
  usage_msg += "\"#{self.description}\"\n\n" if message.nil? && self.description.present?
189
189
  usage_msg += "Availible options:\n\n"
190
-
190
+
191
191
  @parameters.each do |param|
192
192
  arg = case param[:type]
193
193
  when :required then " v "
@@ -200,7 +200,8 @@ class BinScript
200
200
  usage_msg
201
201
  end
202
202
 
203
- private
203
+ private
204
+
204
205
  # Prepare parameters in Getoptlong lib format
205
206
  def get_getoptlong_params
206
207
  result = []
@@ -242,7 +243,7 @@ class BinScript
242
243
  usage_exit e.message
243
244
  end
244
245
  end
245
-
246
+
246
247
  def check_required_params
247
248
  self.class.parameters.each do |param|
248
249
  if param[:type] == :required && @params_values.has_key?(param[:key])
@@ -259,14 +260,14 @@ class BinScript
259
260
 
260
261
  # Print usage and exit if asked
261
262
  usage_exit if params(:h)
262
-
263
+
263
264
  check_required_params
264
-
265
+
265
266
  # Create and check lock file if enabled
266
267
  if self.class.enable_locking
267
268
  @lock = LockFile.new(lock_filename)
268
269
  @lock.quiet = true # Don't write errors to STDERR
269
-
270
+
270
271
  if(@lock.lock)
271
272
  msg = "--- Try start. Buy lock file '#{@lock.path}' already open in exclusive mode. Exit! ---"
272
273
  # puts msg # puts is not good idea, because cron will mail it, but this is not error
@@ -274,16 +275,16 @@ class BinScript
274
275
  exit
275
276
  end
276
277
  end
277
-
278
+
278
279
  begin
279
280
  # Log important info and call script job
280
281
  info ""
281
-
282
+
282
283
  log_params = {:env => RailsStub.env, :log_level => (self.class.enable_logging ? @logger.level : nil), :lock_file => (self.class.enable_locking ? @lock.path : nil)}
283
-
284
+
284
285
  info "> Script #{self.class.script_name} started! (#{log_params.inspect})"
285
286
  info "- Parameters: #{@params_values.inspect}"
286
-
287
+
287
288
  start = Time.now
288
289
 
289
290
  # Инкрементируем счетчик запусков этого скрипта
@@ -301,11 +302,15 @@ class BinScript
301
302
  log_benchmarker_data
302
303
  rescue Exception => e
303
304
  # Print error info if it's not test env or exit
304
- if RailsStub.env != 'test' && e.class != SystemExit && e.class != Interrupt
305
+
306
+ exit_mes = (e.class == SystemExit) || (e.class == Interrupt) || (e.class == SignalException) || (RailsStub.env == 'test')
307
+ unless exit_mes
305
308
  msg = self.class.prepare_exception_message(e)
306
309
  puts "\n" + msg
307
310
  fatal msg
308
311
  notify_about_error(e)
312
+ else
313
+ error "Get exit message! #{e.message}"
309
314
  end
310
315
 
311
316
  # Инкрементируем счетчик ошибок этого скрипта
@@ -365,7 +370,7 @@ class BinScript
365
370
  params(:l).blank? ? File.join(RailsStub.root, 'log', "#{self.class.script_name}#{log_filename_time_part}.log") : params(:l)
366
371
  end
367
372
 
368
- private
373
+ private
369
374
 
370
375
  # Current time logname part.
371
376
  def log_filename_time_part
@@ -404,11 +409,11 @@ EXCEPTION
404
409
  end
405
410
 
406
411
  def inc_counter(id, counter = 1)
407
- # stub
412
+ # stub
408
413
  end
409
-
414
+
410
415
  def notify_about_error(ex)
411
416
  # stub
412
417
  end
413
-
414
- end
418
+
419
+ end
@@ -8,11 +8,11 @@ module RailsStub
8
8
  def self.env
9
9
  defined?(Rails) ? Rails.env : ENV['RAILS_ENV']
10
10
  end
11
-
11
+
12
12
  def self.logger
13
13
  defined?(Rails) ? Rails.logger : nil
14
14
  end
15
-
15
+
16
16
  def self.root
17
17
  path = defined?(Rails) ? Rails.root : nil
18
18
  path ||= defined?(APP_ROOT) ? APP_ROOT : '.'
@@ -1,4 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  class BinScript
3
- VERSION = "0.1.7"
4
- end
3
+ VERSION = "0.1.8"
4
+ end
@@ -17,7 +17,7 @@ class XLogger < Logger
17
17
  # Don't change default logger if asked
18
18
  if hint[:dont_touch_rails_logger].blank? && defined?(ActiveRecord::Base)
19
19
  ActiveRecord::Base.logger = self
20
-
20
+
21
21
  def Rails.logger
22
22
  ActiveRecord::Base.logger
23
23
  end
@@ -46,7 +46,7 @@ class XLogger < Logger
46
46
  def rails_env_log_level
47
47
  prod_env? ? Logger::INFO : Logger::DEBUG
48
48
  end
49
-
49
+
50
50
  def prod_env?
51
51
  defined?(Rails) && Rails.env == 'production'
52
52
  end
@@ -6,6 +6,7 @@ if Rails::VERSION::MAJOR >= 3
6
6
  source_root File.expand_path("../templates", __FILE__)
7
7
 
8
8
  def add_files
9
+ file_name.sub!('_script', '')
9
10
  template "script.rb", "bin/#{file_path}"
10
11
  template "script_class.rb", "app/bins/#{file_path}_script.rb"
11
12
  template "spec.rb", "spec/bins/#{file_path}_script_spec.rb"
@@ -22,6 +23,7 @@ if Rails::VERSION::MAJOR == 2
22
23
  class BinGenerator < Rails::Generator::NamedBase
23
24
  def manifest
24
25
  record do |m|
26
+ file_name.sub!('_script', '')
25
27
  m.template "script.rb", "bin/#{file_path}", :chmod => 0755
26
28
  m.template "script_class.rb", "app/bins/#{file_path}_script.rb"
27
29
  m.directory "spec/bins"
@@ -37,5 +37,5 @@ class <%= class_name %>Script < BinScript
37
37
  def do!
38
38
  logger.info "Script <%= class_name %> works!"
39
39
  end
40
-
40
+
41
41
  end
@@ -1,12 +1,12 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe <%= class_name %>Script do
4
4
  before :each do
5
5
  @bin = <%= class_name %>Script.new
6
-
6
+
7
7
  # if want to point some params into bin => @bin.override_parameters({:i => 10, :a => '20'})
8
8
  end
9
-
9
+
10
10
  it 'should do! and not raise' do
11
11
  @bin.do!
12
12
  end
@@ -2,10 +2,10 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe BinScript do
4
4
  before :each do
5
- root = Pathname.new(File.dirname(__FILE__) + '/../').realpath.to_s
6
- Rails.stub!(:root).and_return(root)
5
+ root = Pathname.new(File.dirname(__FILE__) + '/test_dir/').realpath.to_s
6
+ Rails.stub(:root).and_return(root)
7
7
  end
8
-
8
+
9
9
  class TestScript < BinScript
10
10
  noarg :n, "Test parameter that can't have argument"
11
11
  optional :o, :description => "Test parameter that can have argument", :alias => :oo
@@ -13,10 +13,14 @@ describe BinScript do
13
13
  end
14
14
 
15
15
  describe "class name detection" do
16
- before(:all) do
16
+ before(:each) do
17
17
  @test_date = [
18
- {:filename => "/prj/bin/nagios/some.rb", :parts => ['nagios','some'], :class => "Nagios::SomeNagiosScript", :files => ["app/models/nagios_script.rb", "app/bins/nagios/some_nagios_script.rb"]},
19
- {:filename => "/prj/bin/another.rb", :parts => ['another'], :class => "AnotherScript", :files => ["app/bins/another_script.rb"]}
18
+ {:filename => "/prj/bin/bluh", :parts => ['bluh'], :class => "BluhScript", :files => [File.join(Rails.root, "/app/models/bluh_script.rb")]},
19
+ {:filename => "/prj/bin/blah", :parts => ['blah'], :class => "BlahScript", :files => [File.join(Rails.root, "/app/bins/blah_script.rb")]},
20
+ {:filename => "/prj/bin/bleh", :parts => ['bleh'], :class => "BlehScript", :files => [File.join(Rails.root, "/app/models/bin/bleh_script.rb")]},
21
+ {:filename => "/prj/bin/bloh", :parts => ['bloh'], :class => "BlohScript", :files => [File.join(Rails.root, "/app/bins/1/bloh_script.rb")]},
22
+ {:filename => "/prj/bin/blih", :parts => ['blih'], :class => "BlihScript", :files => [File.join(Rails.root, "/script/blih_script.rb")]},
23
+ {:filename => "/prj/bin/bljh", :parts => ['bljh'], :class => "BljhScript", :files => [File.join(Rails.root, "/app/scripts/66/bljh_script.rb")]},
20
24
  ]
21
25
  @test_keys = [:parts, :class, :files]
22
26
  end
@@ -0,0 +1,6 @@
1
+ class BlohScript < BinScript
2
+
3
+ def do!
4
+ end
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ class BlahScript < BinScript
2
+
3
+ def do!
4
+ end
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ class BlehScript < BinScript
2
+
3
+ def do!
4
+ end
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ class BluhScript < BinScript
2
+
3
+ def do!
4
+ end
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ class BljhScript < BinScript
2
+
3
+ def do!
4
+ end
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ class BlihScript < BinScript
2
+
3
+ def do!
4
+ end
5
+
6
+ end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bin_script
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
5
- prerelease:
4
+ version: 0.1.8
6
5
  platform: ruby
7
6
  authors:
8
7
  - Makarchev Konstantin
@@ -10,74 +9,66 @@ authors:
10
9
  autorequire: init
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2012-08-25 00:00:00.000000000 Z
12
+ date: 2013-08-24 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: activesupport
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
- - - ! '>='
18
+ - - '>='
21
19
  - !ruby/object:Gem::Version
22
20
  version: '0'
23
21
  type: :runtime
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
- - - ! '>='
25
+ - - '>='
29
26
  - !ruby/object:Gem::Version
30
27
  version: '0'
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: rails
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
- - - ! '>='
32
+ - - '>='
37
33
  - !ruby/object:Gem::Version
38
34
  version: '0'
39
35
  type: :runtime
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
- - - ! '>='
39
+ - - '>='
45
40
  - !ruby/object:Gem::Version
46
41
  version: '0'
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: rspec
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
- - - ! '>='
46
+ - - '>='
53
47
  - !ruby/object:Gem::Version
54
48
  version: '0'
55
49
  type: :development
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
- - - ! '>='
53
+ - - '>='
61
54
  - !ruby/object:Gem::Version
62
55
  version: '0'
63
56
  - !ruby/object:Gem::Dependency
64
57
  name: rake
65
58
  requirement: !ruby/object:Gem::Requirement
66
- none: false
67
59
  requirements:
68
- - - ! '>='
60
+ - - '>='
69
61
  - !ruby/object:Gem::Version
70
62
  version: '0'
71
63
  type: :development
72
64
  prerelease: false
73
65
  version_requirements: !ruby/object:Gem::Requirement
74
- none: false
75
66
  requirements:
76
- - - ! '>='
67
+ - - '>='
77
68
  - !ruby/object:Gem::Version
78
69
  version: '0'
79
- description: Easy writing and executing bins(executable scripts) in Rails Application
80
- (especially for crontab or god)
70
+ description: Gem for easy writing and executing scripts in Rails Application. For
71
+ my purposes much better than Rake, Thor and Rails Runner.
81
72
  email: kostya27@gmail.com
82
73
  executables:
83
74
  - bin_helper
@@ -105,36 +96,35 @@ files:
105
96
  - lib/generators/bin/templates/spec.rb
106
97
  - spec/bin_script_spec.rb
107
98
  - spec/spec_helper.rb
99
+ - spec/test_dir/app/bins/1/bloh_script.rb
100
+ - spec/test_dir/app/bins/blah_script.rb
101
+ - spec/test_dir/app/models/bin/bleh_script.rb
102
+ - spec/test_dir/app/models/bluh_script.rb
103
+ - spec/test_dir/app/scripts/66/bljh_script.rb
104
+ - spec/test_dir/script/blih_script.rb
108
105
  homepage: http://github.com/kostya/bin_script
109
- licenses: []
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
110
109
  post_install_message:
111
110
  rdoc_options: []
112
111
  require_paths:
113
112
  - lib
114
113
  required_ruby_version: !ruby/object:Gem::Requirement
115
- none: false
116
114
  requirements:
117
- - - ! '>='
115
+ - - '>='
118
116
  - !ruby/object:Gem::Version
119
117
  version: '0'
120
- segments:
121
- - 0
122
- hash: 319463119
123
118
  required_rubygems_version: !ruby/object:Gem::Requirement
124
- none: false
125
119
  requirements:
126
- - - ! '>='
120
+ - - '>='
127
121
  - !ruby/object:Gem::Version
128
122
  version: '0'
129
- segments:
130
- - 0
131
- hash: 319463119
132
123
  requirements: []
133
124
  rubyforge_project:
134
- rubygems_version: 1.8.24
125
+ rubygems_version: 2.0.2
135
126
  signing_key:
136
- specification_version: 3
137
- summary: Easy writing and executing bins(executable scripts) in Rails Application
138
- (especially for crontab or god). For my purposes much better than Rake, Thor and
139
- Rails Runner.
127
+ specification_version: 4
128
+ summary: Gem for easy writing and executing scripts in Rails Application. For my purposes
129
+ much better than Rake, Thor and Rails Runner.
140
130
  test_files: []