-
Notifications
You must be signed in to change notification settings - Fork 1
/
ICalDiff.groovy
324 lines (279 loc) · 8.68 KB
/
ICalDiff.groovy
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
/**
ICalDiff.groovy
No dependencies other than Groovy itself.
{@link https://github.com/aaronzirbes/ical-splitter}
@author Aaron J. Zirbes ([email protected])
@version 1.0.0
*/
class ICalDiff {
/** The main method, this parses out the filename, and calls splitFile().
@param args The arguments, in order, of source calendar, and the calendar missing events to compare to.
*/
static void main(String[] args) {
if (args.size() == 2) {
def sourceFileName = args[0]
def compareToFileName = args[1]
def sourceFile = new File(sourceFileName)
def compareToFile = new File(compareToFileName)
if (! sourceFile.exists()) {
println "File not found: ${sourceFile}"
displayHelp()
} else if (! compareToFile.exists()) {
println "File not found: ${compareToFile}"
displayHelp()
} else {
println "Comparing files: '${sourceFile}' and '${compareToFile}'"
diffFiles(sourceFile, compareToFile)
}
} else {
displayHelp()
}
}
/** This just prints out the help message if you don't pass a file name. */
static void displayHelp () {
println """
Usage: groovy ICalDiff.groovy [fullcalendar.ics] [calendarmissingevents.ics]
This program compares one iCal file to another iCal file, and creates a new
iCal file containing all the events missing from the second iCal file.
"""
}
/** This returns a File object pointing to a particular file split chunk.
@param inputFile The file being split
@param splitNumber The number of the file split chunk
@return the file object representing the file split chunk
*/
static File getDiffFile(File inputFile) {
new File(inputFile.name + '.diff')
}
/**
This is the main driver for the script.
@param inputFile the file being processed
@param eventsPerSplit the maximum number of events to put in each file split chunk
*/
static void diffFiles(File sourceFile, File compareToFile) {
def header = readHeader(sourceFile)
def footer = 'END:VCALENDAR'
def diffFile = getDiffFile(sourceFile)
def sourceEvents = loadEvents(sourceFile)
def compareEvents = loadEvents(compareToFile)
def missingEvents = [] as Set
def diffSize = sourceEvents.size() - compareEvents.size()
if (diffSize > 0) {
println "Expecting at least: ${diffSize} events."
}
// find out what's missing
sourceEvents.each{ event ->
def matches = compareEvents.findAll{ it == event }
if ( ! matches ) {
missingEvents << event
} else if ( matches.size() > 1 ) {
println "WARNING: Found duplicate events:"
matches.each{
println "--------------------------------"
println event.uid.padLeft(40) + ' <=> ' + it.uid
if (event.dtStart) {
println event.dtStart.toString().padLeft(40) + ' <=> ' + it?.dtStart?.toString()
}
if (event.dtEnd) {
println event.dtEnd.toString().padLeft(40) + ' <=> ' + it?.dtEnd?.toString()
}
if (event.dtStamp) {
println event.dtStamp.padLeft(40) + ' <=> ' + it?.dtStamp
}
println it
}
println "================================"
}
}
println "'${compareToFile}' is missing ${missingEvents.size()} events from ${sourceFile}."
// Write the missing file
diffFile.write(header)
missingEvents.each { event ->
// println "Missing: ${event.uid}"
diffFile << event
}
diffFile << footer
}
/**
This reads the iCal header from an ical file and
returns it as a string.
@param inputFile the file being processed
@return the iCal header as a string
*/
static String readHeader(File inputFile) {
def header = new StringBuffer()
def finishedReadingHeader = false
inputFile.eachLine{ line ->
if ( ! finishedReadingHeader ) {
// increment our event counter if needed
if ( line == 'BEGIN:VEVENT' ) {
// This marks the end of the header
finishedReadingHeader = true
} else {
// append to the header if we're still in it
header.append(line + '\n')
}
}
}
header.toString()
}
/**
This loads ical event from a file into memory.
@param inputFile the file being processed
@return a Set of ICalEvent instances representing all of the events in the file
*/
static loadEvents(File inputFile) {
def eventCount = 0
def iCalEventInstance = null
def iCalEventInstanceList = [] as Set
inputFile.eachLine{ line ->
if ( line == 'BEGIN:VEVENT' ) {
// increment our event counter
eventCount++
// create a new event
iCalEventInstance = new ICalEvent()
} else if ( line == 'END:VEVENT' ) {
// add the event to the list
if (iCalEventInstance.uid) {
iCalEventInstanceList << iCalEventInstance
} else {
println "!!! FAILED to read UID from event:"
println iCalEventInstance
}
iCalEventInstance = null
} else if (iCalEventInstance) {
// parse a line to set the event parameters
iCalEventInstance.parseLine(line)
}
}
if (iCalEventInstance) {
iCalEventInstanceList.add(iCalEventInstance)
}
println "Processed ${eventCount} of ${iCalEventInstanceList.size()} records from '${inputFile}'."
if (eventCount != iCalEventInstanceList.size()) {
println "WARNING: Some events could not be loaded!"
}
return iCalEventInstanceList
}
}
/**
This represents a single iCal event.
It is used to store the iCal events when loading them into memory.
*/
class ICalEvent {
/** The UID setting for the Event */
String uid = ''
/** The DTSTART setting for the event */
String dtStartString = ''
Date dtStart = new Date()
String dtStartTz = ''
/** The DTEND setting for the event */
String dtEndString = ''
Date dtEnd = new Date()
String dtEndTz = ''
/** The CREATED setting for the Event */
String created
/** The DTSTAMP setting for the Event */
String dtStamp = ''
/** The RRULE setting for the Event */
String rRule = ''
/** The RDATE setting for the Event */
String rDate = ''
/** The full contents of the record as a String */
String record = ''
/** parses a line from an iCal event, and places the data
in the appropriate attribute */
void parseLine(String setting) {
def compoundKey = null
def subKey = null
def key = null
def value = null
def debug = false
if ( setting ==~ /^[A-Z]+[;:].*/ ) {
// grab everything before the :
key = setting.replaceAll(/:.*/, '')
// grab everything before the ;
compoundKey = key.replaceAll(/;.*/, '')
// grab everything after the ${key}:
value = setting.replaceFirst(key + ':', '').trim()
// grab everything before the ; in the key
if (compoundKey != key) {
// we found a compound date key
subKey = key.replaceFirst(compoundKey + ';', '').trim()
}
if (debug) { println "==================" }
if (key == 'UID') { uid = value }
else if (key == 'CREATED') { created = value }
else if (key == 'RRULE') { rRule = value }
else if (key == 'RDATE') { rDate = value }
else if (key == 'DTSTAMP') { dtStamp = parseDate(value) }
else if (compoundKey == 'DTSTART') {
if (debug) {
println "setting: ${setting}"
println "key: ${key}"
println "compoundKey: ${compoundKey}"
println "subKey: ${subKey}"
println "value: ${value}"
}
dtStartString = value
dtStart = parseDate(value)
dtStartTz = subKey
} else if (compoundKey == 'DTEND') {
if (debug) {
println "setting: ${setting}"
println "key: ${key}"
println "compoundKey: ${compoundKey}"
println "subKey: ${subKey}"
println "value: ${value}"
}
dtEndString = value
dtEnd = parseDate(value)
dtEndTz = subKey
} else if (debug) {
println "setting: ${setting}"
println "key: ${key}"
println "compoundKey: ${compoundKey}"
println "subKey: ${subKey}"
}
}
if (setting) {
record += setting + '\n'
}
}
/** this is used to parse iCal formated dates */
private Date parseDate(String value) {
def longDate = ~/[0-9]*T[0-9]*Z/
def mediumDate = ~/[0-9]*T[0-9]*/
def shortDate = ~/[0-9]*/
if ( longDate.matcher(value).matches() ) {
Date.parse("yyyyMMdd'T'HHmmss'Z'", value)
} else if ( mediumDate.matcher(value).matches() ) {
Date.parse("yyyyMMdd'T'HHmmss", value)
} else if ( shortDate.matcher(value).matches() ) {
Date.parse("yyyyMMdd", value)
} else {
println "WARNING: unknown date format: ${value}"
null
}
}
/** The default comparator for this class */
int compareTo(iCalEvent) {
uid.compareTo(iCalEvent.uid)
}
/** The default comparator for this class */
boolean equals(iCalEvent) {
if (iCalEvent instanceof ICalEvent) {
return ( uid.equals(iCalEvent.uid) &&
rDate.equals(iCalEvent.rDate) &&
rRule.equals(iCalEvent.rRule) &&
dtStart.equals(iCalEvent.dtStart) &&
dtEnd.equals(iCalEvent.dtEnd) )
} else {
return false
}
}
/** The default toString() method for this class */
String toString() {
'BEGIN:VEVENT\n' + record + 'END:VEVENT\n'
}
}