crntb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7098325dce85f1836393f1a6e509b52cc8dee461
4
+ data.tar.gz: 641ce5840bdc17aac7f2be6f2d0c084322ab05c8
5
+ SHA512:
6
+ metadata.gz: 99cb97810cdeec9ddfe1302226910a1ed2e17dae58c549f5090511d418e44c0ac8c13662bca3b9af1bbe8f68ac16522265bd0852b833621a2157a0b0c1542abf
7
+ data.tar.gz: bed6742c8ba7f9c110b133aa4730184c9e5ad839fe3da6270ea12cf5758a3e76170767b6f366c46f8bed9db0cc0747870bf5b62a3468d0fe49e8fab81f423bf5
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
6
+
7
+ group :development do
8
+ gem "pry-byebug"
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ crntb (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (10.0.2)
10
+ coderay (1.1.2)
11
+ method_source (0.9.0)
12
+ minitest (5.11.3)
13
+ pry (0.11.3)
14
+ coderay (~> 1.1.0)
15
+ method_source (~> 0.9.0)
16
+ pry-byebug (3.6.0)
17
+ byebug (~> 10.0)
18
+ pry (~> 0.10)
19
+ rake (10.5.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.16)
26
+ crntb!
27
+ minitest
28
+ pry
29
+ pry-byebug
30
+ rake (~> 10.0)
31
+
32
+ BUNDLED WITH
33
+ 1.16.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Kosuke Nakamura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'crntb'
5
+ require 'pry'
6
+ require 'pry-byebug'
7
+ Pry.start
data/crntb.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'crntb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "crntb"
8
+ spec.version = Crntb::VERSION
9
+ spec.authors = ["litencatt"]
10
+ spec.email = ["ncl0709@gmail.com"]
11
+ spec.summary = %q{Crontab format parser}
12
+ spec.description = %q{Simple crontab parser written by ruby.}
13
+ spec.homepage = "https://github.com/litencatt/crntb"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ #spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.16"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "pry"
35
+ spec.add_development_dependency "minitest"
36
+ end
@@ -0,0 +1,202 @@
1
+ module Crntb
2
+ class Field
3
+ attr_reader :field
4
+
5
+ class << self
6
+ def parse(field)
7
+ self.new(field).parse
8
+ end
9
+ end
10
+
11
+ def initialize(field)
12
+ @field = field
13
+ end
14
+
15
+ def parse
16
+ #return nil unless valid?
17
+ return '*' if first_to_last?
18
+ interpretation
19
+ end
20
+
21
+ # 先頭文字列を処理
22
+ # */...
23
+ # n...
24
+ # それ以降はカンマ区切りでsplitして-はloop処理しての繰り返し
25
+ def interpretation
26
+ step_collections = field.split(',')
27
+ collections = []
28
+ step_collections.each do |step_collection|
29
+ # /でsplitした結果
30
+ step = get_step(step_collection)
31
+
32
+ # /が使われてればstep[1]には分割数が来るはず, なければ1
33
+ # 次のrange処理で使う
34
+ if step[1] && step[1].match(/^[0-9]+$/)
35
+ step_size = step[1].to_i
36
+ else
37
+ step_size = 1
38
+ end
39
+
40
+ # step[0] : * => min..max
41
+ # step[0] : n-m => n..m
42
+ range = get_range(step[0])
43
+ if range.length > 1
44
+ s = range[0].to_i
45
+ e = range[1].to_i
46
+ s.step(e, step_size) { |r| collections << r }
47
+ end
48
+ end
49
+ collections.uniq.sort
50
+ # 曜日や月は文字列にして追加
51
+ # そうでなければ数字を追加
52
+ result = collections.inject '' do |res, collection|
53
+ res += collection.to_s
54
+ res += ", "
55
+ end
56
+ result.slice!(result.size - 2, 2)
57
+ result
58
+ end
59
+
60
+ def get_collection
61
+
62
+ end
63
+
64
+ # e.g
65
+ # "*/1" => ["*", "1"]
66
+ # "*/1,2-4" => ["*", "1,2-4"]
67
+ def get_range(value)
68
+ if value == '*'
69
+ [field_range.first, field_range.last]
70
+ else
71
+ value.split('-')
72
+ end
73
+ end
74
+
75
+ def get_step(value)
76
+ value.split('/')
77
+ end
78
+
79
+ def expand_range(collections)
80
+ result = []
81
+ collections.each do |collection|
82
+ if collection.include?('-')
83
+ min, max = collection.split('-', 2)
84
+ for i in min..max do
85
+ result << i
86
+ end
87
+ else
88
+ result << collection
89
+ end
90
+ end
91
+ end
92
+
93
+ def first_to_last?
94
+ field == '*'
95
+ end
96
+
97
+ def range_time?
98
+ end
99
+
100
+ def every_time?
101
+ end
102
+
103
+ private
104
+
105
+ def have_asterisk?
106
+ field.include?('*')
107
+ end
108
+
109
+ def have_hyphen?
110
+ field.include?('-')
111
+ end
112
+
113
+ def have_slash?
114
+ field.include?('/')
115
+ end
116
+
117
+ def have_comma?
118
+ field.include?(',')
119
+ end
120
+ end
121
+
122
+ class Minute < Field
123
+ def field_range
124
+ 0..59
125
+ end
126
+
127
+ def parse
128
+ case field
129
+ when '*'
130
+ "every minute"
131
+ else
132
+ "when minute equals #{super}"
133
+ end
134
+ end
135
+ end
136
+
137
+ class Hour < Field
138
+ def parse
139
+ case field
140
+ when '*'
141
+ "every hour "
142
+ else
143
+ "at #{super}"
144
+ end
145
+ end
146
+
147
+ def field_range
148
+ 0..23
149
+ end
150
+ end
151
+
152
+ class DayOfMonth < Field
153
+ def parse
154
+ case field
155
+ when '*'
156
+ ""
157
+ else
158
+ "on the #{super}"
159
+ end
160
+
161
+ end
162
+
163
+ def field_range
164
+ 1..31
165
+ end
166
+ end
167
+
168
+ class Month < Field
169
+ def parse
170
+ case field
171
+ when '*'
172
+ "every day "
173
+ else
174
+ "In #{super}"
175
+ end
176
+
177
+ end
178
+
179
+ def field_range
180
+ 1..12
181
+ end
182
+ end
183
+
184
+ class DayOfWeek < Field
185
+ def parse
186
+ case field
187
+ when '*'
188
+ ""
189
+ else
190
+ "and on #{super}"
191
+ end
192
+ end
193
+
194
+ def field_range
195
+ 0..7
196
+ end
197
+ end
198
+ class Command < Field
199
+ def parse
200
+ end
201
+ end
202
+ end
data/lib/crntb/file.rb ADDED
@@ -0,0 +1,38 @@
1
+ module Crntb
2
+ class File
3
+ def self.parse(file)
4
+ ::File.readlines(file).each do |line|
5
+ Crntb::Line.new(line).parse
6
+ end
7
+ end
8
+ end
9
+
10
+ class Line
11
+ attr_reader :minute, :hour, :day_of_month, :month, :day_of_week, :command
12
+
13
+ def initialize(line)
14
+ fields = manipuate(line)
15
+ @minute = Minute.parse(fields[0])
16
+ @hour = Hour.parse(fields[1])
17
+ @day_of_month = DayOfWeek.parse(fields[2])
18
+ @month = Month.parse(fields[3])
19
+ @day_of_week = DayOfMonth.parse(fields[4])
20
+ @command = fields[5].chomp
21
+ end
22
+
23
+ def parse
24
+ [
25
+ day_of_week,
26
+ month,
27
+ day_of_month,
28
+ hour,
29
+ minute,
30
+ "\n exec #{@command}",
31
+ ].join
32
+ end
33
+
34
+ def manipuate(line)
35
+ line.split(' ', 6)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Crntb
2
+ VERSION = '0.1.0'
3
+ end
data/lib/crntb.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'crntb/file'
2
+ require 'crntb/field'
3
+
4
+ module Crntb
5
+ def self.parse(file)
6
+ Crntb::File.parse(file)
7
+ end
8
+
9
+ def self.parse_line(line)
10
+ Crntb::Line.new(line).parse
11
+ end
12
+ end
data/sample.cron ADDED
@@ -0,0 +1 @@
1
+ * * * * * sample.sh
data/sample.cron1 ADDED
@@ -0,0 +1,14 @@
1
+ * * * * * sample.sh && foo.sh && bar.sh
2
+ 0 * * * * sample.sh
3
+ 00 * * * * sample.sh
4
+ */10 * * * * sample.sh
5
+ 00 05 * * * sample.sh
6
+ 30 08 * * * sample.sh
7
+ 0,30 8-22 * * * sample.sh
8
+ 00,30 * * * * sample.sh
9
+ 00 00 1 * * sample.sh
10
+ 00 01 1 * * sample.sh
11
+ 0 9 25-29 * * sample.sh
12
+ 00 09 13-17 * * sample.sh
13
+ 00 02,05,08 * * * sample.sh
14
+ 30 23 28-31 * * sample.sh
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crntb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - litencatt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple crontab parser written by ruby.
70
+ email:
71
+ - ncl0709@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - LICENSE
79
+ - bin/console
80
+ - crntb.gemspec
81
+ - lib/crntb.rb
82
+ - lib/crntb/field.rb
83
+ - lib/crntb/file.rb
84
+ - lib/crntb/version.rb
85
+ - sample.cron
86
+ - sample.cron1
87
+ homepage: https://github.com/litencatt/crntb
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.6.14
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Crontab format parser
111
+ test_files: []