Beispiel #1
0
 def __make_att(bn, lst):
     gafile = os.path.join(tmpdir, utils.temp_name(bn)) 
     att.append((gafile, bn))
     open(gafile, "w").write("\n".join(lst))
Beispiel #2
0
def main(sf_csv_file, sp_email_map, maxupds, sendto):
    logger = utils.createLogger("spsfsync.py")



    b = utils.getSFSiteCred()
    workdir = os.path.join(tempdir, utils.temp_name("sfupdworkdir"))
    os.mkdir(workdir)
    
    rc,upds, splits, manifest_map  = \
        __create_sf_updates(sf_csv_file, workdir, sp_email_map, 
        maxupds)
    if rc != 0 or upds == 0:
        if rc != 0:
            logger.error("sp_get_status returned with error %d"%rc)
        else:
            logger.info("There were no updates")
        return rc
    
    logger.debug("THERE ARE %d updates and %d splits"%(upds, len(splits)))
    
    # loop splits here
    # then cat the logfile
    cgood = 0
    cbad = 0

    goodfile = os.path.join(tempdir, utils.temp_name("goodups.txt"))
    badfile = os.path.join(tempdir, utils.temp_name("badupds.txt"))
    logger.debug("GOODFILE "+goodfile)
    
    good = open(goodfile, "w")
    bad = open(badfile, "w")

    updlogfile = os.path.join(tempdir, utils.temp_name("spsynclogfile.csv")) # yes reuse

    ktest = 0
    for spl in splits:
        rc = sfupdspstatus.main(b[0], b[1], b[2], spl, updlogfile)

        csv_spl = csv.reader(open(spl))
        up_file = open(updlogfile)
        csv_log = csv.reader(up_file)

        first = True
        for row_upd in csv_spl:
            row_log = csv_log.next()
            if first:
                first = False
                continue
            lid = row_upd[0]
            try:
                (email, fromv) = manifest_map[lid]
                cto = row_upd[1]

                success = row_log[1].lower()
                error = row_log[3]
                if success == "true":
                    cgood = cgood + 1
                    good.write(email+" changed from "+fromv+" to "+cto+"\n")
                    good.flush()
                else:
                    cbad = cbad + 1
                    bad.write(email+" failed to update from "+fromv+" to "+cto+" "+error+"\n")
                    bad.flush()

            except KeyError:
                logger.error("ID not found "+lid)
        up_file.close()
        ktest = ktest + 1
#        if ktest > 4: break ## remove this
    
    good.close()
    bad.close()
    att = []
    att.append((goodfile, "goodups.txt"))
    if cbad > 0: att.append((badfile, "badupds.txt"))
    logger.debug(str(att))
    sendMail(sendto, "%d SF Stormpost_Reason__c updated, %d failed"%(cgood, cbad), "spsfcync", att)    
    return rc
Beispiel #3
0
def main(archive_dir, debug = 0):
    if not os.path.isdir(archive_dir):
        print "archive_dir "+archive_dir+" not found"
        return -1

    logger.debug("DEBUG = "+str(debug))
    fpns_to_sync = [x for x in [x.strip() for x in open(os.path.join(utils.getScriptPath(), "fpns_to_sync.txt")).read().\
        split("\n")] if x[0] != "#"] # there!
    logger.debug("WILL SYNC "+",".join(fpns_to_sync))
    if len(fpns_to_sync) == 0: 
        logger.error("Nothing to sync")
        return -1

    logger.debug("connecting to sp")
    spsoap = spsoapclient.spsoapclient()
    

    sp_list_map = spsoap.get_lists_as_map()

    def _check_lists():
        bad = False
        for ed in fpns_to_sync:
            try:
                sp_list_map[ed]
            except KeyError:
                logger.error("List to sync:%s not in SP"%ed)
                bad = True
        return bad
    if _check_lists(): 
        return -1


    rmap = spsoap.get_recips_as_map(os.path.join(tempdir, utils.temp_name("sp_export.csv")))
    logger.debug("rmap len %d"%len(rmap))
    


    now = datetime.datetime.now()
    nowfile = os.path.join(archive_dir, "now.csv")
    prevfile = os.path.join(archive_dir, "prev.csv")



    if debug != 1:
        if not os.path.isfile(nowfile):
            logger.debug("there is no now file - wtf do we do")
        else:
            if os.path.isfile(prevfile):
                n = os.path.join(archive_dir,
                    "prev_%04d%02d%02d.csv"%(now.year, now.month, now.day))
                if os.path.isfile(n):
                    os.remove(n)
                os.rename(prevfile, n)
            os.rename(nowfile, prevfile)
        __dumpSF(nowfile)

            
    rc = spsfsync.main(nowfile, rmap, None, send_emails_to)
    
        
    import diff
    adds, deletes = diff.diff(nowfile, prevfile)
    
    if len(adds) == 0 and len(deletes) == 0:
        logger.debug("Nothing to do")
        return 0
    good_adds = []
    bad_adds = []
    good_dels = []
    bad_dels = []
    
    for ed in fpns_to_sync:
        listid = sp_list_map[ed]

        logger.debug("DOING %s - %d"%(ed, listid))
        if ed in adds:
            for a in adds[ed]:
                logger.debug("add %s to %s id %d"%(a, ed, listid))
                if a in rmap:
                    recip_id = rmap[a][0]
                else:
                    try:
                        recip_id = spsoap.create_recip(a)
                    except spsoapclient.spsoapclient_exception, e:
                        bad_adds.append("%s to %s failed with %s"%(a,ed,e.what))
                        continue
                rc = spsoap.add_recip_to_list(int(listid), int(recip_id))
                good_adds.append("%s to %s"%(a, ed))

        logger.debug("DOING dels %d"%(len(deletes)))
        if ed in deletes:
            for a in deletes[ed]:
                logger.debug("delete %s from %s id %d"%(a, ed, listid))
                if a in rmap:
                    recip_id = rmap[a][0]
                    mailing_id = rmap[a][2]
                    rc = spsoap.delete_recip_from_list(int(listid), int(recip_id), int(mailing_id))
                    good_dels.append("%s from %s"%(a, ed))
                else:
                    bad_dels.append("%s unsub from %s is not in recip list of SP"%(a, ed))