try/exception - error block

bruce badouglas at gmail.com
Sun Aug 3 10:29:23 EDT 2014


Hi.

I have a long running process, it generates calls to a separate py
app. The py app appears to generate errors, as indicated in the
/var/log/messages file for the abrtd daemon.. The errors are
intermittent.

So, to quickly capture all possible exceptions/errors, I decided to
wrap the entire "main" block of the test py func in a try/exception
block.

This didn't work, as I'm not getting any output in the err file
generated in the exception block.

I'm posting the test code I'm using. Pointers/comments would be helpful/useful.

////////////////////
 the if that gets run is the fac1 logic which operates on the input
packet/data..
    elif (level=='collegeFaculty1'):
        #getClasses(url, college, termVal,termName,deptName,deptAbbrv)
      ret=getParseCollegeFacultyList1(url,content)
////////////////////

Thanks.

if __name__ == "__main__":
# main app

  try:
    #college="asu"
    #url="https://webapp4.asu.edu/catalog"
    #termurl="https://webapp4.asu.edu/catalog/TooltipTerms.ext"


    #termVal=2141
    #
    # get the input struct, parse it, determine the level
    #

    #cmd='cat /apps/parseapp2/asuclass1.dat'
    #print "cmd= "+cmd
    #proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE)
    #content=proc.communicate()[0].strip()
    #print content
    #sys.exit()

    #s=getClasses(content)

    #print "arg1 =",sys.argv[0]
    if(len(sys.argv)<2):
      print "error\n"
      sys.exit()

    a=sys.argv[1]
    aaa=a

    #
    # data is coming from the parentApp.php
    #        data has been rawurlencode(json_encode(t))
    #        -reverse/split the data..
    #        -do the fetch,
    #        -save the fetched page/content if any
    #        -create the returned struct
    #        -echo/print/return the struct to the
    #         calling parent/call
    #

    ##print urllib.unquote_plus(a).decode('utf8')
    #print "\n"
    #print simplejson.loads(urllib.unquote_plus(a))
    z=simplejson.loads(urllib.unquote_plus(a))
    ##z=simplejson.loads(urllib.unquote(a).decode('utf8'))
    #z=simplejson.loads(urllib2.unquote(a).decode('utf8'))

    #print "aa \n"
    print z
    #print "\n bb \n"

    #
    #-passed in
    #
    url=str(z['currentURL'])
    level=str(z['level'])
    cname=str(z['parseContentFileName'])


    #
    # need to check the contentFname
    # -should have been checked in the parentApp
    # -check it anyway, return err if required
    # -if valid, get/import the content into
    #     the "content" var for the function/parsing
    #

    ##cmd='echo ${yolo_clientFetchOutputDir}/'
    cmd='echo ${yolo_clientParseInputDir}/'
    #print "cmd= "+cmd
    proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE)
    cpath=proc.communicate()[0].strip()

    cname=cpath+cname
    #print "cn = "+cname+"\n"
    #sys.exit()


    cmd='test -e '+cname+' && echo 1'
    #print "cmd= "+cmd
    proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE)
    c1=proc.communicate()[0].strip()

    if(not c1):
      #got an error - process it, return
      print "error in parse"

    #
    # we're here, no err.. got content
    #

    #fff= "sdsu2.dat"
    with open(cname,"r") as myfile:
      content=myfile.read()
      myfile.close()


    #-passed in
    #college="louisville"
    #url="http://htmlaccess.louisville.edu/classSchedule/"
    #termVal="4138"


    #print "term = "+str(termVal)+"\n"
    #print "url = "+url+"\n"

    #jtest()
    #sys.exit()

    #getTerm(url,college,termVal)


    ret={} # null it out to start
    if (level=='rState'):
      #ret=getTerm(content,termVal)
      ret=getParseStates(content)

    elif (level=='stateCollegeList'):
        #getDepts(url,college, termValue,termName)
      ret=getParseStateCollegeList(url,content)

    elif (level=='collegeFaculty1'):
        #getClasses(url, college, termVal,termName,deptName,deptAbbrv)
      ret=getParseCollegeFacultyList1(url,content)

    elif (level=='collegeFaculty2'):
        #getClasses(url, college, termVal,termName,deptName,deptAbbrv)
      ret=getParseCollegeFacultyList2(content)



    #
    # the idea of this section.. we have the resulting
    #     fetched content/page...
    #

    a={}
    status=False
    if(ret['status']==True):

      s=ascii_strip(ret['data'])
      if(((s.find("</html")>-1) or (s.find("</HTML")>-1)) and
          ((s.find("<html")>-1) or (s.find("<HTML")>-1)) and
           level=='classSectionDay'):

        status=True
      #print "herh"
      #sys.exit()

      #
      # build the returned struct
      #
      #

      a['Status']=True
      a['recCount']=ret['count']
      a['data']=ret['data']
      a['nextLevel']=''
      a['timestamp']=''
      a['macAddress']=''
    elif(ret['status']==False):
      a['Status']=False
      a['recCount']=0
      a['data']=''
      a['nextLevel']=''
      a['timestamp']=''
      a['macAddress']=''

    res=urllib.quote(simplejson.dumps(a))
    ##print res

    name=subprocess.Popen('uuidgen -t', shell=True,stdout=subprocess.PIPE)
    name=name.communicate()[0].strip()
    name=name.replace("-","_")


  ##  if status==True:
    name2=tmpParseDir+"/rr_"+name+".dat"
    ofile1=open(name2,"w+")
    ofile1.write(res)
    ofile1.close()
    print name2

    if status==False:
      sname=tmpParseDir+"/serr_"+name+".dat"
      ofile1=open(sname,"w+")
      ofile1.write(aaa)
      ofile1.close()


    sys.exit()




    print "term = "+str(termVal)+"\n"
    print "url = "+url+"\n"

    getTerm(url,college,termVal)


    print "exit"
    sys.exit()

  except Exception, e:
    print e
    print "pycolFac1 - error!! \n";
    name=subprocess.Popen('uuidgen -t', shell=True,stdout=subprocess.PIPE)
    name=name.communicate()[0].strip()
    name=name.replace("-","_")
    name2="/home/ihubuser/parseErrTest/pp_"+name+".dat"
    ofile1=open(name2,"w+")
    ofile1.write(e)
    ofile1.write(aaa)
    ofile1.close()

    sys.exit()



More information about the Python-list mailing list