-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
432 lines (381 loc) · 16.4 KB
/
sync.py
File metadata and controls
432 lines (381 loc) · 16.4 KB
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
#!/usr/bin/env/ python3
# coding=utf-8
#
# Sync Alma - ILLiad Database
# Developer: Ganesh Anand Velu
# Version: 0.9 (05222018)
#
# Description: Downloads email report sent from Alma and extracts the document(.txt report). Then,
# creates a new document with UTF-8 encoding, parses the hard-coded UserValidation lines and
# and the report itself. And finally, uploads the generated documented to the specified FTP server.
# Note: Entries with barcodes having "-" are discarded to avoid import errors w/ ILLiad. (line: 91-108)
# Usage: python3 sync.py
import sys, os, codecs, datetime, time, logging, signal
import argparse
import csv
from configparser import ConfigParser
import imaplib, email, ftplib, smtplib
from zipfile import ZipFile as zip
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# Input and Output file declarationn
TARGET_FILE = (
'ILLiad UserValidation.txt') # filename in the report archive(zip)
OUTPUT_FILE = ('UserValidation.txt') # output filename
LOGGING = True
BACKGROUND_ENABLED = True
SLEEP_INTERVAL = 900 # seconds (BACKGROUND MUST BE ENABLED!)
# EMAIL (IMAP & SMTP) Login Credentials
EMAIL_USER = ""
EMAIL_PASS = ""
IMAP_SERVER = ""
IMAP_PORT = 993
SMTP_SERVER = "smtp-mail.outlook.com"
SMTP_PORT = 587
# Alma Report Dispatcher's email address
EMAIL_SENDER = "Your.Department@organization.com" # e-mail address of the incoming reports
EMAIL_AGE_LIMIT = 5 # email expiry (days)
EMAIL_FROM = EMAIL_USER
# Payload destination FTP credentials
FTP_SERVER = ''
FTP_PORT = 21
FTP_USERNAME = ''
FTP_PASSWORD = ''
FTP_DIRECTORY = '/illiad/import/'
# Config
if os.path.exists("/var/run"):
PID_DIR = "/var/run"
else:
PID_DIR = "./"
PID_FILE = "%s/illiad_sync.pid" % PID_DIR
cg = ConfigParser()
parser = argparse.ArgumentParser()
new_mail = False
# ILLiad File Import Notes: Carriage-Return, Line-feed \r\n (CRLF)
# hardcoded ILLiad Validation text Identifier
illiad_header = (
'separator=,\r\nUserName, UserValidationType, LastName, FirstName, SSN, Status, EMailAddress'
', Phone, MobilePhone,Department, NVTGC, Password, NotificationMethod, DeliveryMethod, LoanDeliveryMethod,'
' AuthorizedUsers, Web, Address, Address2, City, State, Zip, Site, Number, Organization, Fax, '
'ArticleBillingCategory, LoanBillingCategory, Country, SAddress, SAddress2, SCity, SState, SZip, PasswordHint,'
' SCountry, Blocked, PlainTextPassword, UserRequestLimit, UserInfo1, UserInfo2, UserInfo3, UserInfo4, UserInfo5\r\n'
)
class Logger:
def write(self, *args, **kwargs):
self.out1.write(*args, **kwargs)
self.out2.write(*args, **kwargs)
self.out1.flush()
self.out2.flush()
def flush(self):
pass
def __init__(self, out1, out2):
self.out1 = out1
self.out2 = out2
def pid_exists(pid):
if pid < 0:
return False # NOTE: pid == 0 returns True
try:
os.kill(pid, 0)
except ProcessLookupError: # errno.ESRCH
return False # No such process
except PermissionError: # errno.EPERM
return True # Operation not permitted (i.e., process exists)
else:
return True # no error, we can send a signal to the process
def parse_alma_data(target_path):
conv_success = False
# CSV approach for Alma analytics report (.txt)
with codecs.open(
target_path + "/" + OUTPUT_FILE, 'wb+',
encoding='utf-8') as illiad_file: # utf16/8/cp1252 output
illiad_file.write(illiad_header)
with open(
target_path + "/" + TARGET_FILE, newline='',
encoding='utf-16-le') as alma_input: # Alma report(utf-16-le)
alma_data = csv.reader(alma_input, dialect="excel-tab")
for index, line in enumerate(alma_data):
error_strings = "Line " + (str(index + 1))
# discard anomalies from input (illiad import case)
if "-" in line[1]: # barcode anomaly (-)
error_strings = error_strings + " [Barcode: " + line[1] + "]"
continue
if "," in line[3]: # last name anomaly (,)
error_strings = error_strings + " [Last Name: " + line[3] + "]"
anamoly = line[3].find(",")
if (len(line[3]) - anamoly) == 1:
line[3] = line[3].replace(",", "")
elif line[3][anamoly + 1] == " ":
line[3] = line[3].replace(",", "")
else:
line[3] = line[3].replace(",", " ")
if "," in line[4]: # first name anomaly (,)
error_strings = error_strings + " [First Name: " + line[4] + "]"
anamoly = line[4].find(",")
if (len(line[4]) - anamoly) == 1:
line[4] = line[4].replace(",", "")
elif line[4][anamoly + 1] == " ":
line[4] = line[4].replace(",", "")
else:
line[4] = line[4].replace(",", " ")
# anomaly detector ends here
if line[0] == "Barcode":
lastname = line[3].lower(
) # convert the last name to lowercase (hard coded output)
illiad_file.write(
"{},Auth,{},{},{},{},,,,,ILL,,E-Mail,Hold for Pickup,Hold for Pickup,,,,,,,,,,,,,,,,,,,,Your last name,,,{},,,,,,\r\n"
.format(line[1], line[3], line[4], line[5], line[6],
lastname))
conv_success = True
else:
if line[0] == "Identifier":
print("Bad data at index: " + (str(index + 1)))
if error_strings != "Line " + (str(index + 1)):
with open(target_path + "/" + "errors.txt",
"a") as error_dump:
error_dump.write(error_strings + "\r\n")
if conv_success:
logprint("[info]", "processed total of " + str(
(index)) + " entries.")
logprint("[info]", "translation completed!")
def upload_ftp(target_path):
logprint("[ftp-info]", "establishing connection to: %s" % FTP_SERVER)
session = ftplib.FTP()
try:
session.connect(FTP_SERVER, FTP_PORT)
session.login(FTP_USERNAME, FTP_PASSWORD)
logprint("[ftp-info]", "connected and logged into FTP server")
session.cwd(FTP_DIRECTORY) # change the directory
logprint("[ftp-info]", "uploading file....")
file = open(target_path, 'rb') # file to upload
session.storbinary('STOR %s' % OUTPUT_FILE, file) # upload the file
file.close() # close file and FTP
logprint("[ftp-info]", "file successfully uploaded")
except ftplib.all_errors as e:
# print(str(e).split(None, 1)[0]) # get only error code
logprint("[ftp-error]", "FTP: %s" % e) # display the error
session.quit()
def extractAll(zipName, dir):
z = zip(zipName)
for f in z.namelist():
if f.endswith('/'):
os.makedirs(f)
else:
if os.path.exists(dir):
z.extract(f, dir)
else:
z.extract(f)
def get_mail(target_path):
global new_mail
att_path = ""
m = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
try:
logprint("[mail-info]", "logging into mail")
m.login(EMAIL_USER, EMAIL_PASS)
m.select('Inbox')
(result, messages) = m.search(None, ('UNSEEN'), '(FROM {0})'.format(
EMAIL_SENDER.strip()), '(SUBJECT "ILLiad UserValidation")')
if result == "OK":
if len(messages[0].split()) > 0:
logprint("[mail-info]", "total new %s mail(s)" % len(
messages[0].split()))
for message_index, message in enumerate(messages[0].split()):
try:
resp, data = m.fetch(message, '(RFC822)')
except Exception as e:
logprint("[mail-error]", "unable to load mail, %s" % e)
m.close()
exit()
msg = email.message_from_bytes(data[0][1])
# check mail's age (time difference)
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_mail_date = datetime.datetime.fromtimestamp(
email.utils.mktime_tz(date_tuple))
time_diff = datetime.datetime.now() - local_mail_date
if time_diff.days > EMAIL_AGE_LIMIT:
logprint(
"[mail-info] skipping email %s received more than %s days ago ( %s )"
% (message_index + 1, EMAIL_AGE_LIMIT,
local_mail_date.strftime("%Y-%m-%d %H:%M")))
continue
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
if "zip" in filename:
# set pid flag
if not os.path.isdir(target_path):
os.mkdir(target_path)
att_path = os.path.join(target_path, filename)
with open(PID_FILE, 'w') as pidfile:
cg.set('sync', 'clean_exit', 'false')
cg.set('sync', 'file', '%s' % att_path)
cg.set('sync', 'path', '%s' % target_path)
cg.write(pidfile)
try:
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
extractAll(att_path, target_path)
new_mail = True
logprint('[mail-info]', 'attachment: %s' % att_path)
except Exception as e:
logprint('[mail-info]', "%s" % e)
att_path = "unable to extract attachment"
except KeyboardInterrupt:
logprint("[mail-error]", "login interrupted")
os._exit(0)
except Exception as e:
logprint("[mail-error]", "%s" % e)
os._exit(0)
m.shutdown()
return att_path
def send_mail(from_addr, to_addrs, msg):
try:
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.ehlo()
server.starttls()
server.ehlo()
server.login(EMAIL_USER, EMAIL_PASS)
server.sendmail(from_addr, to_addrs, msg.as_string())
server.quit()
except Exception as e:
logprint(
"[mail-error]", "unknown error occurred when sending email\n%s"
% e)
def send_notification(status, log_time, target_path):
# read email list
email_list = [line.strip() for line in open('./email.txt')]
for to_addrs in email_list:
msg = MIMEMultipart("alternative")
msg['Subject'] = "Alma - ILLiad Sync Notification"
msg['From'] = EMAIL_FROM
msg['To'] = to_addrs
if status == "success":
html = open('./success.html', 'rb').read()
else:
html = open('./failed.html', "rb").read()
logs = MIMEApplication(open('./logs/sync-log_' + log_time))
# Attach HTML to the email with logs
body = MIMEText(html, 'html', 'UTF-8')
msg.attach(body)
if os.path.isfile(target_path + "/errors.txt"):
error_list = MIMEApplication(
open(target_path + "/errors.txt", "rb").read())
error_list.add_header(
'Content-Disposition', 'attachment', filename="error_log.txt")
msg.attach(error_list)
msg['Subject'] = "Alma - ILLiad Sync Notification"
try:
send_mail(EMAIL_FROM, to_addrs, msg)
logprint("[mail-info]", "email sent to " + to_addrs)
except SMTPAuthenticationError as e:
logprint("[mail-error]", e)
def logprint(code, stdout):
# print(
# datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\t" + stdout)
print("%-*s %-*s %s" %
(23, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 15, code,
stdout))
def sync_process(att_file_path, down_folder):
global new_mail
if os.path.isfile(att_file_path):
# convert the data
parse_alma_data(down_folder)
# upload to ftp server
upload_ftp(os.path.join(down_folder, OUTPUT_FILE))
# send email notifications
send_notification("success", date_time, down_folder)
# unset new email flag
with open(PID_FILE, 'w') as pidfile:
cg.set('sync', 'clean_exit', 'true')
cg.write(pidfile)
new_mail = False
logprint("[info]", "process completed")
elif att_file_path == "":
logprint("[info]", "no new mail!")
# heartbeat - BG TIME
with open(PID_FILE, 'w') as pidfile:
cg.set('sync', 'PID', '%s' % str(os.getpid()))
cg.write(pidfile)
else:
logprint("[mail-error]", att_file_path)
def process_args():
global args
# parser.add_argument('--pid-file', help='PID file path. Default: Current Directory')
parser.add_argument('--daemon', help='Run in daemon mode', action='store_true')
parser.add_argument('--logging', help='Run in daemon mode', action='store_true')
parser.add_argument(
'--stop', help='Shutdown the current process', action='store_true')
args = parser.parse_args()
# Evaluate the previous exit & terminate duplicate process
try:
cg.read(PID_FILE)
if (args.stop or pid_exists(cg.getint('sync', 'pid'))):
try:
os.kill(cg.getint('sync', 'pid'), signal.SIGTERM)
print("Terminated process %s" % cg.getint('sync', 'pid'))
if(args.stop):
os._exit(0)
except PermissionError:
print("Unable to temrinate process %s" % cg.getint('sync', 'pid'))
os._exit(0)
except Exception as e:
print("%s" % e)
os._exit(0)
if (not(cg.getboolean('sync', 'clean_exit'))):
logprint(
"[warn]",
"previous run did not exit clean, attempting to process again")
# finish it before proceeding
sync_process(str(cg.get('sync', 'file')), str(cg.get('sync', 'path')))
except:
pass
# initialize pid file
with open(PID_FILE, 'w') as pidfile:
try:
cg.add_section('sync')
except:
pass
cg.set('sync', 'PID', '%s' % str(os.getpid()))
cg.set('sync', 'clean_exit', 'true')
cg.write(pidfile)
if __name__ == '__main__':
if sys.version_info <= (3, 0):
sys.stdout.write(
"Sorry, this application requires Python 3.0 or greater. You're running %s\n"
% sys.version_info)
os._exit(0)
# Read & Process arguments
process_args()
if not (EMAIL_USER and EMAIL_PASS and IMAP_SERVER and IMAP_PORT):
logprint("\n[error]", "email credentials or server parameters are empty!\n")
os._exit(0)
if not (FTP_SERVER and FTP_PORT and FTP_USERNAME and FTP_PASSWORD):
logprint("\n[error]", "ftp server credentials or parameters are empty!\n")
os._exit(0)
if LOGGING or args.logging:
if not os.path.exists("./logs"):
os.mkdir("./logs")
log_file = open(
"./logs/sync-log_" +
datetime.datetime.now().strftime("%Y-%m-%d_%H-%M") + ".txt", "w")
sys.stdout = Logger(log_file, sys.stdout)
logprint("[info]", "initiating...")
while True:
date_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M")
DOWNLOAD_FOLDER = os.path.join(
os.path.dirname(os.path.realpath(__file__)), date_time)
TARGET_PATH = get_mail(DOWNLOAD_FOLDER) # attachment file target path
sync_process(TARGET_PATH, DOWNLOAD_FOLDER)
if (BACKGROUND_ENABLED or args.daemon):
try:
time.sleep(SLEEP_INTERVAL)
except KeyboardInterrupt:
logprint("[debug]", "interrupted")
os._exit(0)
else:
break