-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathice_recur
executable file
·534 lines (446 loc) · 20.2 KB
/
ice_recur
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/usr/bin/env ruby
require 'ice_cube'
require 'optimist'
require 'fileutils'
$VERSION='1.6'
$opts = Optimist::options do
version "ice_recur #{$VERSION} by Robin Lee Powell, MIT license. Some code taken from https://github.com/chuckbjones/todo.txt-cli-addons/blob/master/recur , license unknown"
synopsis <<-EOS
ice_recur
A recurring item generator for todo.txt. Yes, there are like 14 of these, but I couldn't find a single one that could do "every 2 weeks", so I wrote my own.
It's called ice_recur because it relies, heavily, on the ice_cube recurring schedule library, and to avoid collision with the other recur action I was using at the time.
This script goes in $TODO_DIR/actions/
It requires ice_cube and optimist, although I can't see how you'd be seeing this message if you hadn't figured that out.
You put your entries in $TODO_DIR/ice_recur.txt , and add something like this:
~/bin/todo.sh ice_recur
to your crontab, to run once a day.
Every entry that matches the current day will be added, as long as there is no other entry with the same text content.
Recurrence Format
-----------------
Entries look like:
@[optional starting date] @@[optional ending date] [timing] - [task]
like:
@2016-02-03 @@2018-03-03 Weekly ; day Monday - (B) Mon Recur Test
Where [timing] is a sequence of timing specifiers seperated by " ; ". Each timing specifier makes the item more specific.
The starting date is entirely optional; if specified it
Timing Specifiers
-----------------
All the timing specifiers, and the sequencing behaviour, is just https://github.com/seejohnrun/ice_cube , except as plain text.
The code just calls IceCube::Rule methods using whatever you specify.
Checking The Run
----------------
Run "todo ice_recur --check" to check if ice_recur has run to completion in the last 2 days. Pass an email address as an argument; if the check fails, it'll send email to that address.
Examples
--------
In general, you can check if a timing setup does what you want using the "-s" argument, which will show you when that line will next trigger.
daily - (A) Runs every day; includes today
daily 2 - (B) Runs every other day; includes today
@2016-03-10 daily 2 - Runs every other day, starting on the day specified (which may or may not include today)
@2016-03-10 @@2016-03-20 daily 2 - Same as the above, but also stops on the day specified (but may or may not actually occur on that day)
weekly ; day Friday, Sunday - Runs every Friday and Saturday
monthly ; day_of_month 11, 13 - Runs on the 11th and 13th of every month
@2016-03-07 Weekly 2 ; day Thursday - Runs on Thursday every second week starting on the first Thursday after the day specified.
@2016-03-01 Monthly 3 - Runs every 3 months starting on the day specified (so, occurs on the first day of the month; next occurence is 2016-06-01)
@2016-01-04 Yearly - Runs every year starting on the day specifiod (so, occurs on the 4th of January)
Some more examples of just the date syntax:
"weekly ; day 2, 4" == "Weekly on Tuesdays and Thursdays"
"weekly ; day 2, 3" == "Weekly on Tuesdays and Wednesdays"
"weekly ; day Tuesday, Thursday" == "Weekly on Tuesdays and Thursdays"
"weekly ; day Tuesday, Wednesday" == "Weekly on Tuesdays and Wednesdays"
"monthly ; day_of_month 16, 18" == "Monthly on the 16th and 18th days of the month"
"monthly ; day_of_month 1, -3" == "Monthly on the 1st and -3th to last days of the month"
"monthly 2 ; day_of_month 15" == "Every 2 months on the 15th day of the month"
"monthly ; day_of_week Tuesday => 1, -1" == "Monthly on the 1st Tuesday and last Tuesday"
"monthly 2 ; day_of_week :monday => 1 ; day_of_week 3 => -1" == "Every 2 months on the 1st Monday and last Wednesday"
"yearly ; day_of_year 16" == "Yearly on the 16th day of the year"
"yearly 4 ; day_of_year -1" == "Every 4 years on the last day of the year"
"yearly ; month_of_year March, April" == "Yearly in March and April"
EOS
opt :test, "Run tests on the recurrence code."
opt :check, "Check the completion file to see if ice_recur has actually run in the last two days. Takes an email address to complain to if not as argument.", :type => String
opt :show_next, "Instead of generating entries, show the next date that each entry *would* run."
opt :dry_run, "Show the resulting entries but do not add them to the todo file", :short => :n
opt :ignore_projects, %Q{By default, projects ("+foo") are counted as part of the text when deciding if ice_recur is about to add a duplicate entry, so "new entry" would be added even if "new entry +foo" already exists. If you use this flag, those two will be considered the same entry and so it wouldn't be added.}, :short => :p
opt :ignore_contexts, %Q{By default, contexts ("@foo") are counted as part of the text when deciding if ice_recur is about to add a duplicate entry, so "new entry" would be added even if "new entry @foo" already exists. If you use this flag, those two will be considered the same entry and so it wouldn't be added.}, :short => :o
opt :force, %Q{Run even if we appear to have already run today.}
end
# Probably should have just used a parser library :P
def make_args( argstr )
args = []
if argstr
# Returning a hash
if argstr =~ %r{=>}
argsarr = argstr.strip.split(%r{\s*=>\s*})
if argsarr.length != 2
raise "This string seems to have too many => in it; needs to be only one: #{argstr}"
end
argsarr.map! do |arg|
arg = make_args(arg)
end
# The key should not be an array
argsarr[0] = argsarr[0].first
args = Hash[*argsarr]
else
args = argstr.strip.split(%r{\s*,\s*})
args.map! do |arg|
# Turn "January" into :january
if arg =~ %r{^:?([a-zA-Z]+)\s*$}
arg = $1.downcase.to_sym
elsif arg =~ %r{[0-9]+}
arg = arg.to_i
else
arg
end
end
end
end
return args
end
def make_schedule( rulestr, startdate = nil )
# Get the start date, if any
startdate ||= Time.new(2018,01,01).to_date
if rulestr =~ %r{(^|\s)@[0-9-]+\s}
startdate = Date.parse(rulestr.sub(%r{(?:^|\s)@([0-9-]+)\s.*},'\1'))
rulestr = rulestr.dup.sub!(%r{(^|\s)@[0-9-]+\s},'\1')
end
enddate = nil
if rulestr =~ %r{(^|\s)@@[0-9-]+\s}
enddate = Date.parse(rulestr.sub(%r{(?:^|\s)@@([0-9-]+)\s.*},'\1'))
rulestr = rulestr.dup.sub!(%r{(^|\s)@@[0-9-]+\s},'\1')
end
rule=IceCube::Rule
schedule = IceCube::Schedule.new(startdate.to_time)
rulestr.split(%r{\s;\s}).each do |rulebit|
method, argstr = rulebit.strip.split(%r{\s+}, 2)
method.downcase!
if argstr =~ %r{;}
puts "I'm seeing a ; in the arguments for a rule. This makes me think that you have a ; that you meant to split up rule chunks, but you didn't put spaces on both sides."
puts "The full rule is: #{rulestr}"
end
args = make_args( argstr )
if args.is_a?(Hash)
rule = rule.send(method, args)
else
rule = rule.send(method, *args)
end
end
if enddate
rule = rule.until(enddate)
end
schedule.add_recurrence_rule rule
return schedule
end
# Turn a task to the empty string if it's completed, otherwise strip
# out tags, projects, dates and key/value tags
def task_to_text(task)
if task =~ %r{^\s*[xX]}
task = ''
else
task = task.gsub( %r{^\s*\([A-Z]\)\s*}, '' )
task = task.gsub( %r{^\s*\d\d\d\d-\d\d-\d\d\s*}, '' )
task = task.gsub( %r{^\s*\d\d\d\d-\d\d-\d\d\s*}, '' )
if $opts[:ignore_projects]
task = task.gsub( %r{(?:\s|^)\+\S+}, '' )
end
if $opts[:ignore_contexts]
task = task.gsub( %r{(?:\s|^)@\S+}, '' )
end
task = task.gsub( %r{(?:\s|^)t:\d\d\d\d-\d\d-\d\d}, '' )
task = task.gsub( %r{(?:\s|^)note:\S+}, '' )
task = task.gsub( %r{(?:\s|^)due:\d\d\d\d-\d\d-\d\d}, '' )
task.chomp!
end
return task
end
$tapnum=1
def tapprint( tstring, schedule, bool, text )
if bool
puts %Q{ok #{$tapnum} - "#{tstring}", aka "#{schedule.to_s}", #{text}}
else
puts %Q{not ok #{$tapnum} - "#{tstring}", aka "#{schedule.to_s}", #{text}}
end
$tapnum += 1
end
if ! $opts[:test]
recur_file = File.join(ENV['TODO_DIR'], 'ice_recur.txt')
completed_file = File.join(ENV['TODO_DIR'], '.ice_recur_completed')
end
if $opts[:test]
test_today = Date.parse("16 Jan 2018")
puts %q{# **NB** All tests use 16 Jan 2018 as "today".}
puts "1..18"
tstring="daily"
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_today), "includes today" )
tstring="daily 2"
schedule = make_schedule( tstring, test_today )
tapprint( tstring, schedule,
schedule.occurs_on?(test_today) &&
! schedule.occurs_on?(test_today + 1) &&
schedule.occurs_on?(test_today + 2),
"includes today, and 2 days from now, but not tomorrow" )
tstring="@2018-01-15 daily 2"
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_today - 1) &&
! schedule.occurs_on?(test_today) &&
schedule.occurs_on?(test_today + 1) &&
! schedule.occurs_on?(test_today + 2) &&
schedule.occurs_on?(test_today + 3) &&
! schedule.occurs_on?(test_today + 4),
"includes some days and not others, and today is not one of them" )
tstring="weekly ; day 2, 4"
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_today) &&
! schedule.occurs_on?(test_today + 1) &&
schedule.occurs_on?(test_today + 2) &&
! schedule.occurs_on?(test_today + 4) &&
! schedule.occurs_on?(test_today + 5),
"includes some days and not others" )
tstring="weekly ; day 2, 3"
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_today) &&
schedule.occurs_on?(test_today + 1) &&
! schedule.occurs_on?(test_today + 2) &&
! schedule.occurs_on?(test_today + 4) &&
! schedule.occurs_on?(test_today + 5),
"includes a different set of days" )
tstring=%Q{weekly ; day Tuesday, Thursday}
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_today) &&
! schedule.occurs_on?(test_today + 1) &&
schedule.occurs_on?(test_today + 2) &&
! schedule.occurs_on?(test_today + 4) &&
! schedule.occurs_on?(test_today + 5),
"includes some days (including today) and not others" )
tstring=%Q{weekly ; day Tuesday, Wednesday}
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_today) &&
schedule.occurs_on?(test_today + 1) &&
! schedule.occurs_on?(test_today + 2) &&
! schedule.occurs_on?(test_today + 4) &&
! schedule.occurs_on?(test_today + 5),
"includes some days (not including today) and not others" )
tstring="monthly ; day_of_month 16, 18"
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_today) &&
! schedule.occurs_on?(test_today + 1) &&
schedule.occurs_on?(test_today + 2) &&
! schedule.occurs_on?(test_today + 4) &&
! schedule.occurs_on?(test_today + 5),
"includes a different set of days" )
tstring="monthly ; day_of_month 1, -3"
first_day_month_num = Date.parse("1 Mar 2018")
last_day_month_num = Date.parse("29 Mar 2018")
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(first_day_month_num) &&
! schedule.occurs_on?(first_day_month_num + 1) &&
! schedule.occurs_on?(first_day_month_num - 1) &&
schedule.occurs_on?(last_day_month_num) &&
! schedule.occurs_on?(last_day_month_num + 1) &&
! schedule.occurs_on?(last_day_month_num - 1) ,
"includes the days in question, but not the days before and after those" )
tstring="monthly 2 ; day_of_month 15"
test_day_1 = Date.parse("15 Mar 2018")
test_day_2 = Date.parse("15 Feb 2018")
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_day_1) &&
! schedule.occurs_on?(test_day_1 + 1) &&
! schedule.occurs_on?(test_day_1 - 1) &&
! schedule.occurs_on?(test_day_2),
"includes the day in question, but not the days before and after that, nor that day in an off month" )
tstring="monthly ; day_of_week Tuesday => 1, -1"
test_day_1 = Date.parse("6 Mar 2018")
test_day_2 = Date.parse("27 Mar 2018")
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_day_1) &&
! schedule.occurs_on?(test_day_1 + 1) &&
! schedule.occurs_on?(test_day_1 - 1) &&
schedule.occurs_on?(test_day_2) &&
! schedule.occurs_on?(test_day_2 + 1) &&
! schedule.occurs_on?(test_day_2 - 1),
"includes the days in question, but not the days before and after those" )
tstring="monthly 2 ; day_of_week :monday => 1 ; day_of_week 3 => -1"
test_day_1 = Date.parse("5 Mar 2018")
test_day_2 = Date.parse("28 Mar 2018")
test_day_3 = Date.parse("5 Feb 2018")
test_day_4 = Date.parse("28 Feb 2018")
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_day_1) &&
! schedule.occurs_on?(test_day_1 + 1) &&
! schedule.occurs_on?(test_day_1 - 1) &&
schedule.occurs_on?(test_day_2) &&
! schedule.occurs_on?(test_day_2 + 1) &&
! schedule.occurs_on?(test_day_2 - 1) &&
! schedule.occurs_on?(test_day_3) &&
! schedule.occurs_on?(test_day_4),
"includes the days in question, but not the days before and after those, nor those days in an off month" )
tstring="yearly ; day_of_year 16"
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_today) &&
! schedule.occurs_on?(test_today + 1) &&
! schedule.occurs_on?(test_today - 1),
"includes today, but not the days before and after" )
tstring="yearly 4 ; day_of_year -1"
test_day_1 = Date.parse("31 Dec 2018")
test_day_2 = Date.parse("31 Dec 2019")
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(test_day_1) &&
! schedule.occurs_on?(test_day_1 + 1) &&
! schedule.occurs_on?(test_day_1 - 1) &&
! schedule.occurs_on?(test_day_2),
"includes 31 Dec 2018, but not the days before and after, and not 31 Dec 2019" )
tstring="yearly ; month_of_year March, April"
test_day_1 = Date.parse("16 Mar 2019")
test_day_2 = Date.parse("16 Apr 2019")
test_day_3 = Date.parse("16 Jan 2019")
schedule = make_schedule( tstring, test_today )
tapprint( tstring, schedule,
schedule.occurs_on?(test_day_1) &&
! schedule.occurs_on?(test_day_1 + 1) &&
! schedule.occurs_on?(test_day_1 - 1) &&
schedule.occurs_on?(test_day_2) &&
! schedule.occurs_on?(test_day_2 + 1) &&
! schedule.occurs_on?(test_day_2 - 1) &&
! schedule.occurs_on?(test_day_3),
"includes 16 Mar 2018 and 16 Apr 2018, but not the days before and after, and not 16 Jan 2018" )
tstring="@2020-01-01 daily"
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(Date.parse("01 Mar 2020")) &&
schedule.occurs_on?(Date.parse("01 Mar 2021")) &&
! schedule.occurs_on?(Date.parse("01 Mar 2019")),
"includes dates in 2020 and 2021, but not 2019" )
tstring="@2020-01-01 @@2020-04-01 daily"
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
schedule.occurs_on?(Date.parse("01 Mar 2020")) &&
! schedule.occurs_on?(Date.parse("01 Mar 2021")) &&
! schedule.occurs_on?(Date.parse("01 Mar 2019")),
"includes dates in 2020 but not 2021 or 2019" )
tstring="@@2020-02-01 daily"
schedule = make_schedule( tstring )
tapprint( tstring, schedule,
! schedule.occurs_on?(Date.parse("01 Mar 2020")) &&
! schedule.occurs_on?(Date.parse("01 Mar 2021")) &&
schedule.occurs_on?(Date.parse("01 Mar 2019")),
"includes dates in 2019, but not 2020 or 2021" )
elsif $opts[:show_next]
recur_entries = File.read(recur_file).split("\n").reject { |e| e =~ %r{^#} }
recur_entries.each do |recur|
schedstr, taskstr = recur.strip.split(%r{\s+-\s+}, 2)
if schedstr && taskstr
puts "Schedule: #{schedstr} -- Schedule In English: #{make_schedule( schedstr ).to_s} -- Next Day: #{make_schedule( schedstr ).next_occurrence.strftime("%Y-%m-%d")} -- Text: #{taskstr}"
end
end
elsif $opts[:check]
email = $opts[:check]
send_mail = false
e = nil
begin
if Time.now.to_i - File.mtime(completed_file).to_i > 172800
send_mail = true
end
rescue => e
send_mail = true
end
if e
puts e
puts e.backtrace
end
if send_mail
print "Sending email.\n"
print %x{echo "File #{completed_file} is more than two days old, or something else weird happened.\n" | mailx -s 'ERROR: ice_recur has not run in 2 days!' #{email}}
else
print "File #{completed_file} has been touched recently; looks good.\n"
end
else
# Get our recur entries
# Drop everything that looks like a comment or blank
recur_entries = File.read(recur_file).split("\n").reject { |e| e =~ %r{(^\s*#|^\s*$)} }
bad_entries = recur_entries.reject { |e| e =~ %r{^(@[0-9-]+ )?[A-Za-z;,-_0-9\s]+ - } }
if bad_entries.length > 0
raise "Bad entries found in #{recur_file}: \n#{bad_entries.join("\n")}"
end
# Make a backup
todo_file = File.join(ENV['TODO_DIR'], 'todo.txt')
orig_todo_data = File.read(todo_file)
orig_todo_time = File.mtime(todo_file).to_i
begin
todo_list = File.readlines(todo_file)
todo_list_new = []
# Get just the non-complete items; strip out tags, projects,
# dates and key/value tags
todo_list_trimmed = todo_list.map { |item| [task_to_text(item), item] }
end_day = Date.today
if File.exists?(completed_file) && ! $opts[:force]
start_day = File.mtime(completed_file).to_date + 1
unless $opts[:dry_run]
if start_day > end_day
puts "ice_recur has already run today; if you think this is in error, use the -f option. Exiting."
exit 0
end
end
else
start_day = Date.today
end
cur_day = start_day
while cur_day <= end_day
puts "Working on day #{cur_day.to_s}:"
recur_entries.each do |recur|
schedstr, taskstr = recur.strip.split(%r{\s+-\s+}, 2)
if make_schedule( schedstr ).occurs_on?(cur_day)
puts "- Recur matches #{cur_day.to_s}: #{schedstr} --- #{taskstr}"
# Find all non-completed items that match this one
found_task = todo_list_trimmed.select { |x,y| x == task_to_text(taskstr) }
if found_task.length > 0
puts " - Duplicate task exists: #{found_task.first[1].chomp}"
else
puts " - No duplicate found for #{taskstr}"
puts " - Adding #{taskstr}"
# Pull the priority in front of the date
taskstr = taskstr.gsub(%r{^\s*([xX]\s*)?(\([A-Z]\)\s*)?}, '')
task = "#{$1}#{$2}#{cur_day.to_s} #{taskstr}\n"
todo_list_new << task
todo_list_trimmed << [task_to_text(task), task]
end
end
end
cur_day += 1
end
if $opts[:dry_run]
puts "\n\nIn dry run mode; here's the entries we would be adding:\n\n"
puts todo_list_new.join
puts
else
puts "\n\nHere's the entries we're adding:\n\n"
puts todo_list_new.join
puts
File.write( todo_file, (todo_list + todo_list_new).join )
end
rescue => e
if File.mtime(todo_file).to_i != orig_todo_time
puts "FAILURE: Something went wrong; reverting #{todo_file}: #{e}; #{e.backtrace.join("\n")}"
File.open(todo_file, 'w') { |file| file.puts orig_todo_data }
else
puts "FAILURE: Something went wrong: #{e}; #{e.backtrace.join("\n")}"
end
exit 1
end
# Mark the "we've actually run" file
unless $opts[:dry_run]
FileUtils.touch completed_file
end
end