Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
wait_notify/user_notify.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
115 lines (96 sloc)
3.24 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from user_notify_config import DB_DIR, DB_FILE, MAIL_SERVER, DEF_FROM, DEF_HELO, MAIL_TEMPLATE | |
import os, sys, time, smtplib, sqlite3 | |
# Provide stateful DB access accessible across functions | |
class DB: | |
def __init__(self,dbdir=DB_DIR,dbfile=DB_FILE): | |
if not os.path.isdir(dbdir): | |
os.makedirs(dbdir) | |
fpath = dbdir + "/" + dbfile | |
self.db = sqlite3.connect(fpath) | |
self.dbc = self.db.cursor() | |
def write(self,sqls): | |
# If sqls is a single string, convert to list of one | |
if type(sqls)!=type(()) or type(sqls)!=type([]): sqls = [ sqls ] | |
for sql in sqls: | |
self.dbc.execute(sql) | |
self.db.commit() | |
def read(self,sql): | |
self.dbc.execute(sql) | |
return self.dbc.fetchall() | |
def close(self): | |
self.db.close() | |
class LOG: | |
def __init__(self,logpath): | |
self.logpath = logpath | |
self.flog = None | |
def write(self,msg): | |
if not self.flog: | |
self.flog = open(self.logpath,"a") | |
datetime = time.strftime("%Y-%m-%d %H:%M:%S") | |
print(datetime, msg, file=self.flog) | |
def close(self): | |
self.flog.close() | |
# Strip new lines | |
def nls(s): | |
return s.strip("\n") | |
#----------------------------------------------------------------------- | |
# Public functions | |
#----------------------------------------------------------------------- | |
# Return True if one or more of the eventids were last sent more then daysago | |
def need_new_notice(eventids,daysago=7): | |
time_interval_sec = daysago * 24 * 3600 | |
recent_date = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()-time_interval_sec)) | |
eventids_str = ','.join( [ '"%s"' % eventid for eventid in eventids ] ) | |
sql = 'select eventid from notify_log where eventid in (%s) and noticetime>"%s";' % (eventids_str,recent_date) | |
db = DB() | |
rows = db.read(sql) | |
return not bool(rows) | |
# Get username and email address from account name | |
def get_username_email(useracct): | |
import pwd | |
try: | |
values = pwd.getpwnam(useracct) | |
# Exclude this uid, it is not in system | |
except KeyError: | |
return '' | |
if '{' in values.pw_gecos: | |
username, email = values.pw_gecos.split('{',1) | |
email = email.strip(' }').strip() | |
else: | |
username = useracct | |
email = '' | |
return username, email | |
##def user_notify(jobid,useracct,username,email,reason,subreason,subject,body): | |
def send_mail(username,email,subject,body,force=False): | |
fromaddr = "hpc@uconn.edu" | |
toaddr = "%s <%s>" % (username,email) | |
send_msg(email,fromaddr,toaddr,subject,body) | |
def update_notify_log(eventids): | |
db = DB() | |
datetime = time.strftime("%Y-%m-%d %H:%M:%S") | |
for eventid in eventids: | |
sql = 'insert into notify_log values ("%s","%s");' % (datetime, eventid) | |
db.write(sql) | |
def send_msg(email,fromaddr,toaddr,subject,body): | |
msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" % (nls(fromaddr),nls(toaddr),nls(subject),nls(body)) | |
sm = smtplib.SMTP(MAIL_SERVER) | |
sm.helo(DEF_HELO) | |
sm.sendmail(DEF_FROM, email, msg) | |
# Only needed when creating the database | |
def create_table(): | |
CREATE_TABLE_SQL = """CREATE TABLE notify_log( | |
noticetime text, | |
eventid text | |
); | |
""" | |
CREATE_INDEX_EVENTID_SQL = """CREATE INDEX notify_jobid_index ON notify_log (eventid);""" | |
try: | |
db = DB() | |
db.write(CREATE_TABLE_SQL) | |
db.write(CREATE_INDEX_EVENTID_SQL) | |
db.close() | |
except sqlite3.OperationalError: | |
return "Database already exists" | |
else: | |
return "Database has been created" |