个人记录1

pkgname_ = "***"

version_ = "1.0"


import sys

import os



basepath_ = os.path.dirname(os.path.realpath(__file__))


modulepath_  = os.path.join(basepath_,"modules")

custompath_ = os.path.join(basepath_,"custom")

configpath_ = os.path.join(basepath_,"config")


sys.path.append(os.path.join(modulepath_,"config"))

sys.path.append(os.path.join(modulepath_,"parser"))

sys.path.append(os.path.join(modulepath_,"requester"))

sys.path.append(os.path.join(modulepath_,"transporter"))

sys.path.append(os.path.join(modulepath_,"decision"))


import optparse

import datetime

import imp

import time


import parser_

import requester

import decision

import transporter


def routine_(source):

    

    #start parse!

    parser = parser_.Parser(source)

    urls = parser.execute()

    if (parser.errorCode()):

        code = parser.errorCode()

        print "error occured while parssing. code: %s ; msg: %s " % \

                (code, parser.errorMsg())

        sys.exit(1)


    #start request

    req = requester.Requester(urls)

    rResult = req.execute()

    if (req.errorCode()):

        print "error occured while request. code: %s ; msg: %s " % \

                (req.errorCode(), req.errorMsg())

        sys.exit(1)


    #start decision

    dec = decision.Decision(rResult)

    dResult = dec.execute()


    if (dec.errorCode()):

        print "error eccoured while request. code: %s ; msg: %s " % \

                dec.errorCode(), dec.errorMsg()

   

    #result transport to log , show or mysql 

    trans = transporter.Transporter(result=dResult, mysql=saver["mysql"], \

            erronly=erronly, log=saver["log"], stdout=saver["stdout"], \

            alert=opt.alert, zabbix=saver["zabbix"])

    tResult = trans.execute()

    if (trans.errorCode()):

        print "error occured while transport result. code : %s ;"+\

                "msg: %s " % trans.errorCode(), trans.errorMsg()

        sys.exit(1);



if __name__ == "__main__":

    start = datetime.datetime.now()


    #Option parser

    optparser = optparse.OptionParser(usage="%s" % __doc__.strip(),\

                                    version=pkgname_+' '+version_)


    optparser.add_option("-m", "--mysql", dest="mysql", help="specify mysql source"\

                                +" of the data config,specify a mysql config file")

    optparser.add_option("-d", "--dmysql", dest="dmysql", help="specify mysql source"\

                                +" of the dynamic ips ,specify a mysql config file")

    optparser.add_option("-f", "--file", dest="file", help="specify a local ini file"\

                                                    +" as the source config data")

    optparser.add_option("-p", "--param", dest="param", help="write the config source"\

                                                        +" data directly")

    optparser.add_option("-y", "--resultmysql", dest="rmysql", help="set a sql"\

        +" to store the results,set 1 to use the source mysql config.if only want"\

        +" to save error result add ':error' at the end"\

        +" e.g. -y /home/louis/sql.ini:error")

    optparser.add_option("-l", "--resultfile", dest="rlog", help="set a file"\

        +" to save the results,error message only plus :error"\

        +" e.g. -l /home/louis/log.txt:error")

    optparser.add_option("-s", "--resultstdout", dest="rstdout", help="if set"\

        +" as stdout,result will be printed  plus :error if only want to print"\

        "error result,e.g. -s stdout:error")

    optparser.add_option("-a", "--alert", dest="alert", action="store_true", help="if set"\

        +" , urlwatch will send alert message if error occured")

    optparser.add_option("-z", "--zabbix", dest="zabbix", help="location of the l2zabbix"\

        +" source file")

    optparser.add_option("-e", "--deamon", dest="daemon", help="do routine in a unlimited"\

        +" loop")


    optparser.set_defaults(mysql=False, inifile=False, param=False, rstdout=False,

            rmysql=False, rlog=False)


    (opt, args) = optparser.parse_args(sys.argv)


    if not (opt.file or opt.mysql or opt.param or opt.dmysql):

        print "at least one source should be specified"

        sys.exit(1)


    if not (opt.rstdout or opt.rmysql or opt.rlog or opt.zabbix):

        print "at least one result saver should be specified"

        sys.exit(1)


    source = {

            "local": opt.file,

            "mysql": opt.mysql,

            "param": opt.param,

            "dmysql": opt.dmysql,

            }


    rmysql = opt.rmysql.split(":") if opt.rmysql else False

    rlog = opt.rlog.split(":") if opt.rlog else False

    rstdout = opt.rstdout.split(":") if opt.rstdout else False


    saver = {

            "mysql": rmysql[0] if rmysql else False,

            "log": rlog[0] if rlog else False,

            "stdout": rstdout[0] if rstdout else False,

            "zabbix": opt.zabbix

            }


    erronly = {

            "mysql": True if (rmysql and len(rmysql) == 2 and rmysql[1] == "error")\

                    else False,

            "log": True if (rlog and len(rlog) == 2 and rlog[1] == "error")\

                    else False,

            "stdout": True if (rstdout and len(rstdout) == 2 and rstdout[1] == "error")\

                    else False,

            }


    if saver["mysql"] == "1" and source["mysql"]:

        saver["mysql"] = source["mysql"]


    

    

    if opt.daemon:

        while True:

            routine_(source)

            time.sleep(float(opt.daemon))

    else:

        routine_(source)


本文出自 “expect批量同步数据” 博客,谢绝转载!

你可能感兴趣的:(File,version,import,记录,custom)