示例#1
0
文件: probe.py 项目: juanurquijo/SF
def main(err_file, loginURL, username, password):
    logger = utils.createLogger("probe.py")
    sf.setLogger(logger)

    df =  open(err_file, "a")
    try:
        sf.login(username, password, loginURL, True)
    except sf.SFException, e:
        df.write(str(e)+"\n")
        sys.exit(1)
示例#2
0
文件: upsert.py 项目: juanurquijo/SF
def main(oper, fn, fo, loginURL, username, password):
    
    print oper, fn, fo
    rcount = 0
    fcount = 0
    res_msg = ""
    logger = utils.createLogger("upsert")
    sf.setLogger(logger)
    op = open(fo, "w")

    lx = sf.login(username, password, loginURL)
    if not lx: return
    site = lx[0]
    sessionid = lx[1]    
    jobid = sf.createJob(oper, "Contact", "CSV", site, sessionid)
    logger.debug( "JOBID: "+jobid)

    
    state = sf.checkJobStatus(jobid, site, sessionid)
    logger.debug("JOB state: "+state)
    
    batchid = sf.submitUpdate(jobid, site, sessionid, fn)
    logger.debug("BATCH "+batchid)
    while True:
        # loop till we complete or error out
        bat = sf.checkBatch(jobid, batchid, site, sessionid)
        logger.debug("BATCH state "+ str(bat))
        batchState = str(bat["state"])
        
        if batchState == "Completed":
            rcount = int(str(bat["numberRecordsProcessed"]))
            fcount = int(str(bat["numberRecordsFailed"]))
            logger.info("NUMBER OF RECORDS PROCESSED %d"%rcount)
            logger.info("NUMBER OF RECORDS FAILED %d"%fcount)
            r = sf.getUpdateResults(jobid, batchid, site, sessionid)
            op.write(r)
            break
        if batchState == "Failed":
            logger.error("QUERY FAILED")
            res_msg = str(bat["stateMessage"])
            logger.error(res_msg)
            break
            
        import time
        time.sleep(sleepTime)
    
    logger.debug("JOB state AFTER: "+state)
    sf.closeJob(jobid, site, sessionid)
    logger.debug("JOB state AFTER CLOSE: "+state)
    return res_msg, rcount, fcount
示例#3
0
文件: p1_a.py 项目: juanurquijo/SF
def main(fn, loginURL, username, password, whereClause = None):
    
    logger = utils.createLogger("p1.py")
    sf.setLogger(logger)
    
    lx = sf.login(username, password, loginURL)
    if not lx: return
    site = lx[0]
    sessionid = lx[1]
    jobid = sf.createJob("query", "Contact", "CSV", site, sessionid)
    logger.debug( "JOBID: "+jobid)
    sfSQL = "select id, Email,  firstname, lastname, FPN_Subscriptions__c,Stormpost_Reason_Date__c,Stormpost_Reason__c from contact"
    if whereClause is not None:
        sfSQL = sfSQL + " " + whereClause
    logger.debug("SQL : "+sfSQL)
    batchid = sf.submitSelect(jobid, site, sessionid, sfSQL)
    logger.debug("BATCH "+batchid)
    state = sf.checkJobStatus(jobid, site, sessionid)
    logger.debug("JOB state: "+state)
    
    while True:
        # loop till we complete or error out
        bat = sf.checkBatch(jobid, batchid, site, sessionid)
        logger.debug("BATCH state "+ str(bat))
        batchState = str(bat["state"])
        
        if batchState == "Completed":
            rcount = int(str(bat["numberRecordsProcessed"]))
            logger.info("NUMBER OF RECORDS %d"%rcount)
            r = sf.getResults(jobid, batchid, site, sessionid)
            op = open(fn, "w").write(r)
            break
        if batchState == "Failed":
            logger.error("QUERY FAILED")
            break
            
        import time
        time.sleep(sleepTime)
    logger.debug("JOB state AFTER: "+state)
    sf.closeJob(jobid, site, sessionid)
    logger.debug("JOB state AFTER CLOSE: "+state)
示例#4
0
文件: runner.py 项目: juanurquijo/SF
def main(workDir):
    import ConfigParser
    config = ConfigParser.RawConfigParser()
    config.read(configPath)
    advisenRestURL = config.get('main', 'advisenRestURL') 
    advisenUser = config.get('main', 'advisenUser') 
    advisenPassword = config.get('main', 'advisenPassword') 
    ftpURL = config.get('main', 'ftpURL') 
    ftpUser = config.get('main', 'ftpUser') 
    ftpPassword = config.get('main', 'ftpPassword') 
    sfurl = config.get('main', 'sfurl') 


    logger.debug("Starting %s - %s - %s"%(advisenRestURL, advisenUser, advisenPassword))
    lr = submitAdvRequest(advisenRestURL, "login", 
        {"username":  advisenUser, "password": advisenPassword})
    logger.debug("After advisen login rc = "+str(lr["error"]))
    if lr["error"] != 0:
        logger.error("Cannot login to advisen REST server "+str(lr["error"]))
        mailError("Cannot login to advisen REST server "+str(lr["error"]))
        return lr["error"];
    
    lr = submitAdvRequest(advisenRestURL, "getsfpushreports")
    if lr["error"] != 0:
        logger.error("Cannot get list of records to work on "+str(lr["error"]))
        mailError("Cannot get list of records to work on "+str(lr["error"]))
        return lr["error"];
    aList = lr["result"]
    
    lr = submitAdvRequest(advisenRestURL, "logout")
    # drop all files from the FTP server
    if len(aList) == 0:
        logger.debug("Nothing to do")
        return 0
    mapFTPOK = {}
    mapBad = {}
    mapBadSFUpload = {}
    
    listEmail = []
    
    def genReportName(row):
        return row["reportname"]+".pdf"
        
    # i need 2 loops - to prevent downloading duplicates
    for r in aList:
        report = genReportName(r)
        if report not in mapFTPOK:
            logger.debug("Need to retrieve "+r["reportname"]+".pdf")
            cmd = "ncftpget -u%s -p%s %s %s /sfreports/%s"%(ftpUser, ftpPassword, ftpURL, workDir, report)
            rc,txt = commands.getstatusoutput(cmd)
            logger.debug("After retrieve %d"%rc)
            if rc:
                mapBad[report] = r
            else:
                mapFTPOK[report] = r

    ## FIXME - should be the SF account of the target
    loginURL, username, password = getSFSiteCred(configFile)
    sf.setLogger(logger)
    lx = sf.login(username, password, loginURL)
    
    if not lx:
        logger.error("Unable to login to SF")
        mailError("Unable to login to SF")
    else:
        site = lx[0]
        sessionid = lx[1]
        logger.debug("FIRST SF site: %s sess: %s"%(site, sessionid))
    
        # inGood is the collector for the DB update IN clause
        inGood = []
        for r in aList:
            report = genReportName(r)
            # this is correct
            # we only upload what we got from FTP server
            if report in mapFTPOK:
                accountid = r["accountid"]
                description = r["description"]
                companyname = r["companyname"]
                logger.debug("UPLOADING TO SF account %s - %s"%(accountid, report))
                rc = uploadFile(site, sessionid, workDir, report, accountid, workDir, description)
                if not rc:
                    inGood.append(r["reportid"])
                    sql = "select email, lastname, firstname from user where id = '%s'"%(str(r["userid"]))
                    uset = runSFQuery(site, sessionid, "User", sql)
                    logger.debug(str(uset))
                    if uset[0] == 1:
                        sql = "SELECT Id FROM Attachment where parentid = '%s' ORDER BY CreatedDate DESC NULLS FIRST"%(str(accountid))
                        aset = runSFQuery(site, sessionid, "Attachment", sql)
                        logger.debug(str(aset))
                        if aset[0]:
                            # take the top
                            link = sfurl + str(aset[1][0][0])
                            logger.debug(link)
                            addy = uset[1][0][0]
                            lastname = uset[1][0][1]
                            firstname = uset[1][0][2]
                            listEmail.append((addy, description, link, companyname, lastname, firstname))
                        else:
                            logger.error("Unable to get the ID of the attachment")
                            mailError("Unable to get the ID of the attachment")
                    else:
                        logger.error("USER: %s not found in SF"%(r["userid"]))
                        mailError("USER: %s not found in SF"%(r["userid"]))
                    
                else:
                    mapBadSFUpload[report] = r
        if len(inGood):
            lr = submitAdvRequest(advisenRestURL, "login", 
                {"username":  advisenUser, "password": advisenPassword})
            logger.debug("After advisen login rc = "+str(lr["error"]))
            if lr["error"] != 0:
                logger.error("Cannot login to advisen REST server "+str(lr["error"]))
                mailError("Cannot login to advisen REST server "+str(lr["error"]))
                return lr["error"];
            lr = submitAdvRequest(advisenRestURL, "updsfreportstat", inGood)
            if lr["error"] != 0:
                logger.error("FAILED to update status "+lr["message"])
                mailError("FAILED to update status "+lr["message"])
            
            
    if len(listEmail):
        mm = Mailer(mailhost, 25, logger, administrators, mailsender)
        for r in listEmail:
            body = "%s is now ready for review.  <a href=%s>Click here</a> or sign-on to your SalesForce account"%(r[1], r[2])
            msg = mm.mailAttach("SFPUSH REPORTS TEST CONAME: "+r[3], body, "html")
            mm.doMail(r[0], mailsender, msg)
            
            msg = mm.mailAttach("*** DEBUG *** SFPUSH REPORTS TEST CONAME: %s was sent to %s"%(r[3], r[0]), body, "html")
            mm.doMail("*****@*****.**", mailsender, msg)
            mm.doMail("*****@*****.**", mailsender, msg)
        
    if len(mapBad):
        logger.error("FAILED TO RETRIEVE FILES from FTP")
        msg = "Failed to retrieve the following files from FTP "
        for k in mapBad.keys():
            logger.error(k)
            msg = msg + k
        mailError(msg)
    if len(mapBadSFUpload):
        logger.error("FAILED TO UPLOAD FILES TO SF")
        msg = "Failed to upload the following files from SF "
        for k in mapBadSFUpload.keys():
            logger.error(k)
            msg = msg + k
        mailError(msg)
    
    
    
    return 0