<s> import sys <EOL> sys . path . append ( "<STR_LIT>" ) <EOL> import subprocess <EOL> import json <EOL> import uuid <EOL> import urllib <EOL> import urllib2 <EOL> import time <EOL> import os . path <EOL> import shutil <EOL> import smtplib <EOL> import email <EOL> import email . parser <EOL> import pprint <EOL> import traceback <EOL> from optparse import OptionParser <EOL> from foam . config import TASKLOGDIR , TASK_QUEUE_DIR , TASK_COMPLETED_DIR , TEMPLATE_DIR , USER_TEMPLATE_DIR <EOL> import foam . version <EOL> from foam . core . configdb import ConfigDB <EOL> URL = "<STR_LIT>" <EOL> LOC = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> CMD = "<STR_LIT>" % ( LOC ) <EOL> def generateJobID ( ) : <EOL> return "<STR_LIT:%s>" % ( uuid . uuid4 ( ) ) <EOL> class Job ( object ) : <EOL> def __init__ ( self , typ , jobdata ) : <EOL> self . jobtype = typ <EOL> self . jobid = generateJobID ( ) <EOL> d = { "<STR_LIT>" : typ , "<STR_LIT>" : jobdata } <EOL> path = "<STR_LIT>" % ( TASK_QUEUE_DIR , self . jobid ) <EOL> f = open ( path , "<STR_LIT>" ) <EOL> json . dump ( d , f ) <EOL> f . close ( ) <EOL> def execute ( self ) : <EOL> p = subprocess . Popen ( "<STR_LIT>" % ( CMD , self . jobtype , self . jobid ) , shell = True ) <EOL> print "<STR_LIT>" % ( self . jobtype , self . jobid ) <EOL> return p . pid <EOL> def getEventTemplateData ( event ) : <EOL> if os . path . exists ( "<STR_LIT>" % ( USER_TEMPLATE_DIR , event ) ) : <EOL> return open ( "<STR_LIT>" % ( USER_TEMPLATE_DIR , event ) , "<STR_LIT:r>" ) . read ( ) <EOL> else : <EOL> return open ( "<STR_LIT>" % ( TEMPLATE_DIR , event ) , "<STR_LIT:r>" ) . read ( ) <EOL> def sendEmail ( event , tmpl_data , email_info , to_addr ) : <EOL> data = { "<STR_LIT>" : event , "<STR_LIT>" : tmpl_data , "<STR_LIT>" : email_info , "<STR_LIT>" : to_addr } <EOL> job = Job ( "<STR_LIT>" , data ) <EOL> return job . execute ( ) <EOL> def getExperimenterEmail ( data ) : <EOL> if data . has_key ( "<STR_LIT:email>" ) : <EOL> return data [ "<STR_LIT:email>" ] <EOL> else : <EOL> return None <EOL> def dispatchEmail ( event , data ) : <EOL> dispatch_admin = False <EOL> dispatch_exp = False <EOL> exp_email = None <EOL> try : <EOL> exp_email = getExperimenterEmail ( data ) <EOL> dispatch_exp = ConfigDB . getConfigItemByKey ( "<STR_LIT>" % ( event ) ) . getValue ( ) <EOL> except Exception , e : <EOL> print traceback . format_exc ( ) <EOL> admin_email = ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) <EOL> dispatch_admin = ConfigDB . getConfigItemByKey ( "<STR_LIT>" % ( event ) ) . getValue ( ) <EOL> email_info = { "<STR_LIT>" : ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) , <EOL> "<STR_LIT>" : ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) , <EOL> "<STR_LIT>" : ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) } <EOL> data [ "<STR_LIT>" ] = admin_email <EOL> data [ "<STR_LIT>" ] = ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) <EOL> if dispatch_admin : <EOL> sendEmail ( "<STR_LIT>" % ( event ) , data , email_info , admin_email ) <EOL> if dispatch_exp and exp_email : <EOL> sendEmail ( "<STR_LIT>" % ( event ) , data , email_info , exp_email ) <EOL> def approveSliver ( sliver_urn , priority ) : <EOL> data = { "<STR_LIT>" : sliver_urn , "<STR_LIT>" : priority } <EOL> job = Job ( "<STR_LIT>" , data ) <EOL> return job . execute ( ) <EOL> def emailCreateSliver ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailGAPIDeleteSliver ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailJSONDeleteSliver ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailApproveSliver ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailRenewSliver ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailRejectSliver ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailDisableSliver ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailExpireSliver ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailSliverExpWeek ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailSliverExpDay ( sliver_data ) : <EOL> dispatchEmail ( "<STR_LIT>" , sliver_data ) <EOL> def emailPendingQueue ( data ) : <EOL> dispatchEmail ( "<STR_LIT>" , data ) <EOL> def taskApproveSliver ( opts ) : <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> url = URL % ( "<STR_LIT>" ) <EOL> data = { "<STR_LIT>" : opts [ "<STR_LIT>" ] , "<STR_LIT>" : opts [ "<STR_LIT>" ] } <EOL> d = json . dumps ( data ) <EOL> h = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } <EOL> req = urllib2 . Request ( url , d , h ) <EOL> response = urllib2 . urlopen ( req ) <EOL> pprint . pprint ( response . read ( ) ) <EOL> def taskExpireSlivers ( opts ) : <EOL> url = URL % ( "<STR_LIT>" ) <EOL> req = urllib2 . Request ( url ) <EOL> response = urllib2 . urlopen ( req ) <EOL> pprint . pprint ( response . read ( ) ) <EOL> def taskEmailSlivers ( opts ) : <EOL> url = URL % ( "<STR_LIT>" ) <EOL> req = urllib2 . Request ( url ) <EOL> response = urllib2 . urlopen ( req ) <EOL> pprint . pprint ( response . read ( ) ) <EOL> def taskSendEmail ( opts ) : <EOL> s = smtplib . SMTP ( opts [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> tmplbuf = getEventTemplateData ( opts [ "<STR_LIT>" ] ) <EOL> tmpl = tmplbuf % ( opts [ "<STR_LIT>" ] ) <EOL> if isinstance ( tmpl , unicode ) : <EOL> tmpl = tmpl . encode ( '<STR_LIT:ascii>' ) <EOL> msg = email . message_from_string ( tmpl ) <EOL> msg [ "<STR_LIT>" ] = opts [ "<STR_LIT>" ] <EOL> msg [ "<STR_LIT>" ] = opts [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> rplyto = opts [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> if rplyto : <EOL> msg [ "<STR_LIT>" ] = rplyto <EOL> print opts <EOL> print msg . _headers <EOL> s . sendmail ( msg [ "<STR_LIT>" ] , msg [ "<STR_LIT>" ] , msg . as_string ( ) ) <EOL> s . quit ( ) <EOL> def taskDailyQueue ( opts ) : <EOL> url = URL % ( "<STR_LIT>" ) <EOL> req = urllib2 . Request ( url ) <EOL> response = urllib2 . urlopen ( req ) <EOL> printJSONRPC ( response . read ( ) ) <EOL> def printJSONRPC ( data ) : <EOL> rpc = json . loads ( data ) <EOL> retcode = rpc [ "<STR_LIT>" ] <EOL> if retcode == <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> if rpc [ "<STR_LIT>" ] != "<STR_LIT>" : <EOL> print rpc [ "<STR_LIT>" ] <EOL> if rpc [ "<STR_LIT:value>" ] : <EOL> print rpc [ "<STR_LIT:value>" ] <EOL> elif retcode == <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> print rpc [ "<STR_LIT>" ] <EOL> elif retcode == <NUM_LIT:2> : <EOL> print "<STR_LIT>" <EOL> print rpc [ "<STR_LIT>" ] <EOL> def nullParseArgs ( args ) : <EOL> return { } <EOL> def jobParseArgs ( args ) : <EOL> parser = OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:str>" ) <EOL> ( opts , args ) = parser . parse_args ( args ) <EOL> opts = parseTaskFile ( opts . jobid ) <EOL> return opts <EOL> def parseTaskFile ( jobid , jobdata = True ) : <EOL> print "<STR_LIT>" % jobid <EOL> path = "<STR_LIT>" % ( TASK_QUEUE_DIR , jobid ) <EOL> f = open ( path , "<STR_LIT:r>" ) <EOL> data = json . load ( f ) <EOL> if jobdata : <EOL> jdata = data [ "<STR_LIT>" ] <EOL> jdata [ "<STR_LIT>" ] = jobid <EOL> return jdata <EOL> else : <EOL> data [ "<STR_LIT>" ] = jobid <EOL> return data <EOL> def cleanup ( opts ) : <EOL> try : <EOL> jobid = opts [ "<STR_LIT>" ] <EOL> path = "<STR_LIT>" % ( TASK_QUEUE_DIR , jobid ) <EOL> shutil . move ( path , TASK_COMPLETED_DIR ) <EOL> except KeyError : <EOL> pass <EOL> except AttributeError : <EOL> pass <EOL> CMDS = { '<STR_LIT>' : ( taskApproveSliver , jobParseArgs ) , <EOL> '<STR_LIT>' : ( taskExpireSlivers , nullParseArgs ) , <EOL> '<STR_LIT>' : ( taskEmailSlivers , nullParseArgs ) , <EOL> '<STR_LIT>' : ( taskSendEmail , jobParseArgs ) , <EOL> '<STR_LIT>' : ( taskDailyQueue , nullParseArgs ) } <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> task_name = sys . argv [ <NUM_LIT:1> ] <EOL> lpath = "<STR_LIT>" % ( TASKLOGDIR , task_name , time . time ( ) ) <EOL> lfile = open ( lpath , "<STR_LIT>" ) <EOL> lfile . write ( "<STR_LIT>" ) <EOL> lfile . write ( "<STR_LIT>" % ( foam . version . VERSION ) ) <EOL> lfile . write ( "<STR_LIT>" ) <EOL> sys . stderr = lfile <EOL> sys . stdout = lfile <EOL> ( task_func , parse_args ) = CMDS [ sys . argv [ <NUM_LIT:1> ] ] <EOL> opts = parse_args ( sys . argv [ <NUM_LIT:2> : ] ) <EOL> try : <EOL> task_func ( opts ) <EOL> cleanup ( opts ) <EOL> except Exception , e : <EOL> print traceback . format_exc ( ) <EOL> lfile . close ( ) </s>
