crontab-parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.1.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.1"
12
+ gem "rcov", ">= 0"
13
+ gem "reek", "~> 1.2.8"
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 uu59
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = crontab-ruby
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to crontab-ruby
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 uu59. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "crontab-parser"
16
+ gem.homepage = "http://github.com/tt25/crontab-parser"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{TODO: one-line summary of your gem}
19
+ gem.description = %Q{TODO: longer description of your gem}
20
+ gem.email = "a@tt25.org"
21
+ gem.authors = ["uu59"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ require 'reek/rake/task'
41
+ Reek::Rake::Task.new do |t|
42
+ t.fail_on_error = true
43
+ t.verbose = false
44
+ t.source_files = 'lib/**/*.rb'
45
+ end
46
+
47
+ task :default => :spec
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "crontab-parser#{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/benchmark/test.rb ADDED
@@ -0,0 +1,57 @@
1
+ # -- coding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../spec/spec_helper')
4
+ require "benchmark"
5
+ require "ruby-prof"
6
+
7
+ crontab = <<-CRON
8
+ # should ignore comment only line and blank line
9
+
10
+ #m h d m w cmd
11
+ * * * * * foo # always run. this comment was ignored
12
+ 3 * * * * bar
13
+ */1 0-22 * * * baz
14
+ CRON
15
+ crontab *= 64
16
+ range = {
17
+ 0 => 0..59,
18
+ 1 => 0..23,
19
+ 2 => 1..31,
20
+ 3 => 1..12,
21
+ 4 => 0..6,
22
+ }
23
+
24
+ 5000.times{
25
+ line = ""
26
+ 5.times {|n|
27
+ ra = range[n].to_a.shuffle
28
+ line << case rand(10)
29
+ when 0..7
30
+ ra.first.to_s
31
+ when 8
32
+ x,y = [ra.first, ra.last].sort
33
+ "#{x}-#{y}"
34
+ else
35
+ ra.take(rand(ra.length - 1) + 1).sort.join(",")
36
+ end
37
+ if rand(2) == 1
38
+ line << "/" + (rand(range[n].last) + 1).to_s
39
+ end
40
+ line << " "
41
+ }
42
+ line << "cmd" + Time.now.to_f.to_s
43
+ line << "\n"
44
+ crontab += line
45
+ }
46
+ crontab += "k k k k k"
47
+ File.open('foo.txt', 'w'){|f| f.puts crontab}
48
+
49
+ now = Time.utc(2010,1,1,0,0,0)
50
+ c = CrontabParser.new("foo.txt")
51
+
52
+ GC.disable
53
+ RubyProf.start
54
+ puts c.find_all{|row| row.times}.length
55
+ result = RubyProf.stop
56
+ GC.enable
57
+ RubyProf::FlatPrinter.new(result).print
@@ -0,0 +1,29 @@
1
+ # -- coding: utf-8
2
+
3
+ require "stringio"
4
+ require "crontab-parser/record.rb"
5
+ require "crontab-parser/time_parser.rb"
6
+
7
+ class CrontabParser
8
+ include Enumerable
9
+
10
+ def initialize(crontab, options={})
11
+ @crontab = crontab
12
+ @options = options
13
+ end
14
+
15
+ def each
16
+ io = if File.exists?(@crontab)
17
+ open(@crontab, 'r')
18
+ else
19
+ StringIO.new(@crontab)
20
+ end
21
+ until io.eof?
22
+ line = io.gets
23
+ line.strip!
24
+ if line.length > 0 && line.index('#') != 0
25
+ yield Record.new(line, @options)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,65 @@
1
+ # -- coding: utf-8
2
+
3
+ class CrontabParser
4
+ class Record
5
+ attr_reader :line
6
+
7
+ def initialize(line, options={})
8
+ @line = line
9
+ @options = options
10
+ end
11
+
12
+ def cmd
13
+ times
14
+ @cmd
15
+ end
16
+
17
+ def to_s
18
+ @line
19
+ end
20
+
21
+ def should_run?(time)
22
+ time.utc
23
+ times[:min].include?(time.min) &&
24
+ times[:hour].include?(time.hour) &&
25
+ times[:day].include?(time.day) &&
26
+ times[:month].include?(time.month) &&
27
+ times[:week].include?(time.wday)
28
+ end
29
+
30
+ def times
31
+ @times ||= begin
32
+ base = @line.strip.gsub(/#.*/, "").gsub(%r!^@(yearly|annually|monthly|weekly|daily|midnight|hourly)!){|m|
33
+ case $1
34
+ when 'yearly','annually'
35
+ '0 0 1 1 *'
36
+ when 'monthly'
37
+ '0 0 1 * *'
38
+ when 'weekly'
39
+ '0 0 * * 0'
40
+ when 'daily','midnight'
41
+ '0 0 * * *'
42
+ when 'hourly'
43
+ '0 * * * *'
44
+ end
45
+ }.strip
46
+ min,hour,day,month,week,@cmd = *base.split(/[\t\s]+/, 6)
47
+ base = [min,hour,day,month,week].join(" ")
48
+ if week.nil?
49
+ if @options[:silent]
50
+ return nil
51
+ else
52
+ raise "invalid line #{@line}"
53
+ end
54
+ end
55
+ {
56
+ :month => TimeParser.parse(month, 1, 12),
57
+ :day => TimeParser.parse(day, 1, 31),
58
+ :hour => TimeParser.parse(hour, 0, 23),
59
+ :min => TimeParser.parse(min, 0, 59),
60
+ :week => TimeParser.parse(week, 0, 6),
61
+ }
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,42 @@
1
+ # -- coding: utf-8
2
+
3
+ class CrontabParser
4
+ class TimeParser
5
+ CACHE = {}
6
+
7
+ def self.parse(column, first=0, last=59)
8
+ base = column + "_#{first}-#{last}"
9
+ if CACHE[base]
10
+ return CACHE[base]
11
+ end
12
+ column = column.gsub(%r!/1$!,"").gsub('*', "#{first}-#{last}")
13
+ if column.index('/')
14
+ times,filter = *column.split("/")
15
+ else
16
+ times = column
17
+ filter = nil
18
+ end
19
+
20
+ result = if times.index(',')
21
+ times.split(',').map{|col| separetor(col)}
22
+ else
23
+ separetor(times)
24
+ end.flatten
25
+
26
+ if filter
27
+ result = result.find_all{|n| n % filter.to_i == 0}
28
+ end
29
+
30
+ CACHE[base] = result
31
+ end
32
+
33
+ def self.separetor(col)
34
+ if col.index('-')
35
+ m,n = *col.split("-").map{|n| n.to_i}
36
+ (m..n).to_a
37
+ else
38
+ [col.to_i]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,135 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "CrontabRuby" do
4
+ it 'should parse time column' do
5
+ CrontabParser::TimeParser.parse('3').should == [3]
6
+ CrontabParser::TimeParser.parse('3,4,5').should == [3,4,5]
7
+ CrontabParser::TimeParser.parse('3,4,5/2').should == [4]
8
+ CrontabParser::TimeParser.parse('0-10/3').should == [0,3,6,9]
9
+ CrontabParser::TimeParser.parse('0-10,20-23/3').should == [0,3,6,9,21]
10
+ CrontabParser::TimeParser.parse('0-3,4-6,9').should == [0,1,2,3,4,5,6,9]
11
+ CrontabParser::TimeParser.parse('*/9').should == [0,9,18,27,36,45,54]
12
+ CrontabParser::TimeParser.parse('*/9', 0, 23).should == [0,9,18]
13
+ end
14
+
15
+ it 'should parse each row' do
16
+ # min,hour,day,mon,week,cmd
17
+ record = CrontabParser::Record.new(<<-CRON)
18
+ * * * * * foo
19
+ CRON
20
+ record.cmd.should == 'foo'
21
+ record.times.should == {
22
+ :month => (1..12).to_a,
23
+ :day => (1..31).to_a,
24
+ :hour => (0..23).to_a,
25
+ :min => (0..59).to_a,
26
+ :week => (0..6).to_a,
27
+ }
28
+ record.should_run?(Time.now).should be_true
29
+
30
+ record = CrontabParser::Record.new(<<-CRON)
31
+ 1,2,3,5-10,21-27/5 2-5 */3 * * bar
32
+ CRON
33
+ record.cmd.should == 'bar'
34
+ record.times.should == {
35
+ :month => (1..12).to_a,
36
+ :day => [3,6,9,12,15,18,21,24,27,30],
37
+ :hour => [2,3,4,5],
38
+ :min => [5,10,25],
39
+ :week => (0..6).to_a,
40
+ }
41
+ record.should_run?(Time.utc(2010,1,1,0,0,0)).should be_false
42
+ record.should_run?(Time.utc(2010,1,3,2,5,0)).should be_true
43
+
44
+ record = CrontabParser::Record.new(<<-CRON)
45
+ @daily baz
46
+ CRON
47
+ record.cmd.should == 'baz'
48
+ record.times.should == {
49
+ :month => (1..12).to_a,
50
+ :day => (1..31).to_a,
51
+ :hour => [0],
52
+ :min => [0],
53
+ :week => (0..6).to_a,
54
+ }
55
+ end
56
+
57
+ it 'should parse annotations' do
58
+ c = CrontabParser.new(<<-CRON)
59
+ @yearly 1/1 only
60
+ @annually 1/1 only
61
+ @monthly day 1 only
62
+ @weekly week==0 only
63
+ @daily 00:00 only
64
+ @midnight 00:00 only
65
+ @hourly *:00 only
66
+ CRON
67
+ rows = c.to_a
68
+ rows[0].times.should == {
69
+ :month => [1],
70
+ :day => [1],
71
+ :hour => [0],
72
+ :min => [0],
73
+ :week => (0..6).to_a,
74
+ }
75
+ rows[1].times.should == rows[0].times
76
+ rows[2].times.should == {
77
+ :month => (1..12).to_a,
78
+ :day => [1],
79
+ :hour => [0],
80
+ :min => [0],
81
+ :week => (0..6).to_a,
82
+ }
83
+ rows[3].times.should == {
84
+ :month => (1..12).to_a,
85
+ :day => (1..31).to_a,
86
+ :hour => [0],
87
+ :min => [0],
88
+ :week => [0],
89
+ }
90
+ rows[4].times.should == {
91
+ :month => (1..12).to_a,
92
+ :day => (1..31).to_a,
93
+ :hour => [0],
94
+ :min => [0],
95
+ :week => (0..6).to_a,
96
+ }
97
+ rows[5].times.should == rows[4].times
98
+ rows[6].times.should == {
99
+ :month => (1..12).to_a,
100
+ :day => (1..31).to_a,
101
+ :hour => (0..23).to_a,
102
+ :min => [0],
103
+ :week => (0..6).to_a,
104
+ }
105
+ end
106
+
107
+ it 'should parse crontab' do
108
+ c = CrontabParser.new(<<-CRON)
109
+ # should ignore comment only line and blank line
110
+
111
+ #m h d m w cmd
112
+ * * * * * foo # always run. this comment was ignored
113
+ 3 * * * * bar
114
+ */1 0-22 * * * baz
115
+ CRON
116
+ now = Time.utc(2010,1,1,0,0,0)
117
+ c.find_all{|row|
118
+ row.should_run?(now)
119
+ }.map{|row| row.cmd}.should == ["foo","baz"]
120
+
121
+ c = CrontabParser.new(<<-CRON)
122
+ should raise invalid line
123
+ CRON
124
+ lambda {
125
+ c.each{|r| r.cmd}
126
+ }.should raise_error
127
+
128
+ c = CrontabParser.new(<<-CRON, :silent => true)
129
+ should raise invalid line
130
+ CRON
131
+ lambda {
132
+ c.each{|r| r.cmd}
133
+ }.should_not raise_error
134
+ end
135
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'crontab-parser'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crontab-parser
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - uu59
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-08 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 1
31
+ - 0
32
+ version: 2.1.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: jeweler
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 1
60
+ - 5
61
+ - 1
62
+ version: 1.5.1
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: rcov
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ type: :development
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: reek
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 1
88
+ - 2
89
+ - 8
90
+ version: 1.2.8
91
+ type: :development
92
+ version_requirements: *id005
93
+ description: crontab parser for ruby
94
+ email: a@tt25.org
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.rdoc
102
+ files:
103
+ - .document
104
+ - .rspec
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.rdoc
108
+ - Rakefile
109
+ - VERSION
110
+ - benchmark/test.rb
111
+ - lib/crontab-parser.rb
112
+ - lib/crontab-parser/record.rb
113
+ - lib/crontab-parser/time_parser.rb
114
+ - spec/crontab-ruby_spec.rb
115
+ - spec/spec_helper.rb
116
+ has_rdoc: true
117
+ homepage: http://github.com/tt25/crontab-parser
118
+ licenses:
119
+ - MIT
120
+ post_install_message:
121
+ rdoc_options: []
122
+
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ segments:
139
+ - 0
140
+ version: "0"
141
+ requirements: []
142
+
143
+ rubyforge_project:
144
+ rubygems_version: 1.3.7
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: crontab parser for ruby
148
+ test_files:
149
+ - spec/crontab-ruby_spec.rb
150
+ - spec/spec_helper.rb