rda-python-common 1.0.32__py3-none-any.whl → 1.0.34__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rda-python-common might be problematic. Click here for more details.

@@ -1849,21 +1849,24 @@ def build_customized_email(table, field, condition, subject, logact = 0):
1849
1849
  estat = PgLOG.FAILURE
1850
1850
  msg = PgLOG.get_email()
1851
1851
  if not msg: return estat
1852
-
1852
+
1853
1853
  sender = PgLOG.PGLOG['CURUID'] + "@ucar.edu"
1854
1854
  receiver = PgLOG.PGLOG['EMLADDR'] if PgLOG.PGLOG['EMLADDR'] else (PgLOG.PGLOG['CURUID'] + "@ucar.edu")
1855
1855
  if receiver.find(sender) < 0: PgLOG.add_carbon_copy(sender, 1)
1856
- ebuf = "From: {}\nTo: {}\n".format(sender, receiver)
1857
- if PgLOG.PGLOG['CCDADDR']: ebuf += "Cc: {}\n".format(PgLOG.PGLOG['CCDADDR'])
1856
+ cc = PgLOG.PGLOG['CCDADDR']
1858
1857
  if not subject: subject = "Message from {}-{}".format(PgLOG.PGLOG['HOSTNAME'], PgLOG.get_command())
1859
- ebuf += "Subject: {}!\n\n{}\n".format(subject, msg)
1860
-
1861
- if PgLOG.PGLOG['EMLSEND']:
1862
- estat = PgLOG.send_customized_email(f"{table}.{condition}", ebuf, logact)
1858
+ estat = PgLOG.send_python_email(subject, receiver, msg, sender, cc, logact)
1863
1859
  if estat != PgLOG.SUCCESS:
1864
- estat = cache_customized_email(table, field, condition, ebuf, 0)
1865
- if estat and logact:
1866
- PgLOG.pglog("Email {} cached to '{}.{}' for {}, Subject: {}".format(receiver, table, field, condition, subject), logact)
1860
+ ebuf = "From: {}\nTo: {}\n".format(sender, receiver)
1861
+ if cc: ebuf += "Cc: {}\n".format(cc)
1862
+ ebuf += "Subject: {}!\n\n{}\n".format(subject, msg)
1863
+
1864
+ if PgLOG.PGLOG['EMLSEND']:
1865
+ estat = PgLOG.send_customized_email(f"{table}.{condition}", ebuf, logact)
1866
+ if estat != PgLOG.SUCCESS:
1867
+ estat = cache_customized_email(table, field, condition, ebuf, 0)
1868
+ if estat and logact:
1869
+ PgLOG.pglog("Email {} cached to '{}.{}' for {}, Subject: {}".format(receiver, table, field, condition, subject), logact)
1867
1870
 
1868
1871
  return estat
1869
1872
 
@@ -20,6 +20,8 @@ import re
20
20
  import pwd
21
21
  import grp
22
22
  import shlex
23
+ import smtplib
24
+ from email.message import EmailMessage
23
25
  from subprocess import Popen, PIPE
24
26
  from os import path as op
25
27
  import time
@@ -116,7 +118,7 @@ PGLOG = { # more defined in untaint_suid() with environment variables
116
118
  'PGBATCH' : '', # current batch service name, SLURM or PBS
117
119
  'PGBINDIR' : '',
118
120
  'SLMTIME' : 604800, # max runtime for SLURM bath job, (7x24x60x60 seconds)
119
- 'PBSTIME' : 86400, # max runtime for SLURM bath job, (7x24x60x60 seconds)
121
+ 'PBSTIME' : 86400, # max runtime for PBS bath job, (24x60x60 seconds)
120
122
  'MSSGRP' : None, # set if set to different HPSS group
121
123
  'RDAGRP' : "decs",
122
124
  'EMLSEND' : None, # path to sendmail, None if not exists
@@ -131,7 +133,9 @@ PGLOG = { # more defined in untaint_suid() with environment variables
131
133
  'ERR2STD' : [], # if non-empty reference to array of strings, change stderr to stdout if match
132
134
  'STD2ERR' : [], # if non-empty reference to array of strings, change stdout to stderr if match
133
135
  'MISSFILE': "No such file or directory",
134
- 'GITHUB' : "https://github.com" # github server
136
+ 'GITHUB' : "https://github.com" , # github server
137
+ 'EMLSRVR' : "ndir.ucar.edu", # UCAR email server and port
138
+ 'EMLPORT' : 25
135
139
  }
136
140
 
137
141
  HOSTTYPES = {
@@ -255,34 +259,44 @@ def send_customized_email(logmsg, emlmsg, logact = 0):
255
259
  logmsg += ': '
256
260
  else:
257
261
  logmsg = ''
262
+ msg = emlmsg
258
263
  for ekey in entries:
259
264
  entry = entries[ekey][0]
260
- if re.search(r'(^|\n){}:\s*\n'.format(entry), emlmsg, re.I):
261
- ms = None
262
- else:
263
- ms = re.search(r'(^|\n){}:\s*(.+)\n'.format(entry), emlmsg, re.I)
265
+ ms = re.search(r'(^|\n)({}: *(.*)\n)'.format(entry), emlmsg, re.I)
264
266
  if ms:
265
- entries[ekey][2] = ms.group(2)
267
+ vals = ms.groups()
268
+ msg = re.sub(vals[1], '', msg)
269
+ if vals[2]: entries[ekey][2] = vals[2]
266
270
  elif logact and entries[ekey][1]:
267
271
  return pglog("{}Missing Entry '{}' for sending email".format(logmsg, entry), logact|ERRLOG)
268
272
 
273
+ ret = send_python_email(entries['sb'][2], entries['to'][2], msg, entries['fr'][2], entries['cc'][2])
274
+ if ret != SUCCESS and PGLOG['EMLSEND']: ret = pgsystem(PGLOG['EMLSEND'], logact, 4, emlmsg)
275
+
269
276
  logmsg += "Email " + entries['to'][2]
270
277
  if entries['cc'][2]: logmsg += " Cc'd " + entries['cc'][2]
271
278
  logmsg += " Subject: " + entries['sb'][2]
272
-
273
- if pgsystem(PGLOG['EMLSEND'], logact, 4, emlmsg):
279
+ if ret:
274
280
  log_email(emlmsg)
275
281
  if logact: pglog(logmsg, logact&(~EXITLG))
276
- return SUCCESS
277
282
  else:
278
283
  errmsg = "Error sending email: " + logmsg
279
- return pglog(logmsg, (logact|ERRLOG)&~EXITLG)
284
+ pglog(errmsg, (logact|ERRLOG)&~EXITLG)
285
+
286
+ return ret
280
287
 
281
288
  #
282
- # send an email, if empty msg; send email message saved in PGLOG['EMLMSG'] instead
289
+ # send an email; if empty msg send email message saved in PGLOG['EMLMSG'] instead
283
290
  #
284
291
  def send_email(subject = None, receiver = None, msg = None, sender = None, logact = 0):
285
292
 
293
+ return send_python_email(subject, receiver, msg, sender, None, logact)
294
+
295
+ #
296
+ # send an email via python module smtplib; if empty msg send email message saved in PGLOG['EMLMSG'] instead
297
+ #
298
+ def send_python_email(subject = None, receiver = None, msg = None, sender = None, cc = None, logact = 0):
299
+
286
300
  if not msg:
287
301
  if PGLOG['EMLMSG']:
288
302
  msg = PGLOG['EMLMSG']
@@ -290,10 +304,10 @@ def send_email(subject = None, receiver = None, msg = None, sender = None, logac
290
304
  else:
291
305
  return ''
292
306
 
293
- docc = 0
307
+ docc = False if cc else True
294
308
  if not sender:
295
309
  sender = PGLOG['CURUID']
296
- if sender != PGLOG['RDAUSER']: docc = 1
310
+ if sender != PGLOG['RDAUSER']: docc = False
297
311
  if sender == PGLOG['RDAUSER']: sender = PGLOG['RDAEMAIL']
298
312
  if sender.find('@') == -1: sender += "@ucar.edu"
299
313
  if not receiver:
@@ -302,26 +316,31 @@ def send_email(subject = None, receiver = None, msg = None, sender = None, logac
302
316
  if receiver.find('@') == -1: receiver += "@ucar.edu"
303
317
 
304
318
  if docc and not re.match(PGLOG['RDAUSER'], sender): add_carbon_copy(sender, 1)
305
-
306
- emlmsg = "From: {}\nTo: {}\n".format(sender, receiver)
319
+ emlmsg = EmailMessage()
320
+ emlmsg.set_content(msg)
321
+ emlmsg['From'] = sender
322
+ emlmsg['To'] = receiver
307
323
  logmsg = "Email " + receiver
308
- if PGLOG['CCDADDR']:
309
- emlmsg += "Cc: {}\n".format(PGLOG['CCDADDR'])
310
- logmsg += " Cc'd " + PGLOG['CCDADDR']
324
+ if not cc: cc = PGLOG['CCDADDR']
325
+ if cc:
326
+ emlmsg['Cc'] = cc
327
+ logmsg += " Cc'd " + cc
311
328
  if not subject: subject = "Message from {}-{}".format(PGLOG['HOSTNAME'], get_command())
312
329
  if not re.search(r'!$', subject): subject += '!'
313
- emlmsg += "Subject: {}\n{}\n".format(subject, msg)
330
+ emlmsg['Subject'] = subject
314
331
  if CPID['CPID']: logmsg += " in " + CPID['CPID']
315
332
  logmsg += ", Subject: {}\n".format(subject)
316
-
317
- if pgsystem(PGLOG['EMLSEND'], logact, 4, emlmsg):
318
- log_email(emlmsg)
333
+ try:
334
+ eml = smtplib.SMTP(PGLOG['EMLSRVR'], PGLOG['EMLPORT'])
335
+ eml.send_message(emlmsg)
336
+ except smtplib.SMTPException as err:
337
+ errmsg = f"Error sending email:\n{err}\n{logmsg}"
338
+ return pglog(errmsg, (logact|ERRLOG)&~EXITLG)
339
+ finally:
340
+ eml.quit()
341
+ log_email(str(emlmsg))
319
342
  if logact: pglog(logmsg, logact&~EXITLG)
320
- return logmsg
321
- else:
322
- errmsg = "Error sending email: " + logmsg
323
- pglog(logmsg, (logact|ERRLOG)&~EXITLG)
324
- return errmsg
343
+ return SUCCESS
325
344
 
326
345
  #
327
346
  # log email sent
@@ -1692,7 +1692,7 @@ def send_request_email_notice(pgrqst, errmsg, fcount, rstat, readyfile = None, p
1692
1692
  tbl = "dsrqst"
1693
1693
  cnd = "rindex = {}".format(pgrqst['rindex'])
1694
1694
 
1695
- if PgLOG.PGLOG['EMLSEND'] and PgLOG.send_customized_email(f"{tbl}.{cnd}", ebuf, 0):
1695
+ if PgLOG.send_customized_email(f"{tbl}.{cnd}", ebuf, 0):
1696
1696
  if errmsg:
1697
1697
  PgLOG.pglog("Error Email sent to {} for {}.{}:\n{}".format(einfo['SENDER'], tbl, cnd, errmsg), PGOPT['errlog'])
1698
1698
  readyfile = None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rda_python_common
3
- Version: 1.0.32
3
+ Version: 1.0.34
4
4
  Summary: RDA Python common library codes shared by other RDA python packages
5
5
  Author-email: Zaihua Ji <zji@ucar.edu>
6
6
  Project-URL: Homepage, https://github.com/NCAR/rda-python-common
@@ -1,15 +1,15 @@
1
1
  rda_python_common/PgCMD.py,sha256=EYjG2Z4zEnvsXE1z-jt5UaNoEKxnOYYiMMzvW6HrKA4,20597
2
- rda_python_common/PgDBI.py,sha256=MyTXj7vXTvP0a06DaXcXEHq649-P9XSv2_1m69Vy4es,74532
2
+ rda_python_common/PgDBI.py,sha256=12qnRXO7pWv8togtTsOvtVuGtwixnR-s2vLGPpdDfHw,74658
3
3
  rda_python_common/PgFile.py,sha256=KnG9S0bgXAAhQIoSJWf3zON8xIua3do4New4O_qdlwg,98468
4
- rda_python_common/PgLOG.py,sha256=g0O3_sz5ZHkfZYr4cRuRjYz9qKXF3QUW-kyoSZozUjM,54515
4
+ rda_python_common/PgLOG.py,sha256=Wb31QP6CgrbgKpyPSnVWuCSMf3NfPPmh9vLAmA5u2b8,55221
5
5
  rda_python_common/PgLock.py,sha256=12i84nsGBuifSyPnm8IR63LvHvRuVU573D5QKFlHdOI,22623
6
- rda_python_common/PgOPT.py,sha256=UyOBhFNfWsMs9x8YTr2WCH7mFhfq6aKq0UcRcXx1-JE,55963
6
+ rda_python_common/PgOPT.py,sha256=zX8lWFdfV6GxjLKfMc9eM76mjIjss_ulPieerejcmks,55936
7
7
  rda_python_common/PgSIG.py,sha256=ZVM9Qz6yIFurwIQJtV5-CFbKOTdFsZ-Rs95SEpDFgNk,35795
8
8
  rda_python_common/PgSplit.py,sha256=QKPbF55m8KCTGmwVwL3uG_nuylCC4FSVfLuXeLjJHbE,8816
9
9
  rda_python_common/PgUtil.py,sha256=OqESKCd72b9g8m8jwjPJhXDtPYlW6G8oSOhwChvz2Cg,48600
10
10
  rda_python_common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
- rda_python_common-1.0.32.dist-info/licenses/LICENSE,sha256=1dck4EAQwv8QweDWCXDx-4Or0S8YwiCstaso_H57Pno,1097
12
- rda_python_common-1.0.32.dist-info/METADATA,sha256=7HME0kugWo2k-1z4Aahtk2kCUuCcHvPEN-Xoxh2dB-o,716
13
- rda_python_common-1.0.32.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
14
- rda_python_common-1.0.32.dist-info/top_level.txt,sha256=KVQmx7D3DD-jsiheqL8HdTrRE14hpRnZY5_ioMArA5k,18
15
- rda_python_common-1.0.32.dist-info/RECORD,,
11
+ rda_python_common-1.0.34.dist-info/licenses/LICENSE,sha256=1dck4EAQwv8QweDWCXDx-4Or0S8YwiCstaso_H57Pno,1097
12
+ rda_python_common-1.0.34.dist-info/METADATA,sha256=uQ5m1dTYb0t-y9f6CFRwX27QZkWU5D8yEmuD-pSt8dM,716
13
+ rda_python_common-1.0.34.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
14
+ rda_python_common-1.0.34.dist-info/top_level.txt,sha256=KVQmx7D3DD-jsiheqL8HdTrRE14hpRnZY5_ioMArA5k,18
15
+ rda_python_common-1.0.34.dist-info/RECORD,,