def RunJob(infile, outpath, tmpdir, email, jobid, g_params):  #{{{
    blastdb = "/data3/data/blastdb/swissprot"
    rootname = os.path.basename(os.path.splitext(infile)[0])
    starttagfile = "%s/runjob.start" % (outpath)
    runjob_errfile = "%s/runjob.err" % (outpath)
    runjob_logfile = "%s/runjob.log" % (outpath)
    finishtagfile = "%s/runjob.finish" % (outpath)
    tmp_outfile = "%s/query.result" % (tmpdir)
    resultpathname = jobid
    outpath_result = "%s/%s" % (outpath, resultpathname)
    outfile = "%s/query.result" % (outpath_result)
    tarball = "%s.tar.gz" % (resultpathname)
    tarball_fullpath = "%s.tar.gz" % (outpath_result)
    isOK = True
    try:
        os.makedirs(outpath_result)
        isOK = True
    except OSError:
        msg = "Failed to create folder %s" % (outpath_result)
        myfunc.WriteFile(msg + "\n", runjob_errfile, "a")
        isOK = False

    if isOK:
        g_params['runjob_log'].append("tmpdir = %s" % (tmpdir))
        #cmd = [script_getseqlen, infile, "-o", tmp_outfile , "-printid"]
        datetime = time.strftime("%Y-%m-%d %H:%M:%S")
        rt_msg = myfunc.WriteFile(datetime, starttagfile)
        if rt_msg:
            g_params['runjob_err'].append(rt_msg)

        cmd = [
            blastall, "-i", infile, "-p", "blastp", "-o", tmp_outfile, "-d",
            blastdb
        ]

        g_params['runjob_log'].append(" ".join(cmd))
        try:
            myfunc.check_output(cmd)
        except subprocess.CalledProcessError, e:
            g_params['runjob_err'].append(str(e))
            suqoutfilelist = glob.glob("%s/*.sh.*.out" % (tmpdir))
            if len(suqoutfilelist) > 0:
                suqoutfile = suqoutfilelist[0]
            g_params['runjob_err'].append(myfunc.ReadFile(suqoutfile))

        if os.path.exists(tmp_outfile):
            cmd = ["cp", "-f", tmp_outfile, outfile]
            try:
                myfunc.check_output(cmd)
            except subprocess.CalledProcessError, e:
                g_params['runjob_err'].append(str(e))
def RunJob(infile, outpath, tmpdir, email, jobid, g_params):#{{{
    blastdb = "/data3/data/blastdb/swissprot"
    rootname = os.path.basename(os.path.splitext(infile)[0])
    starttagfile   = "%s/runjob.start"%(outpath)
    runjob_errfile = "%s/runjob.err"%(outpath)
    runjob_logfile = "%s/runjob.log"%(outpath)
    finishtagfile = "%s/runjob.finish"%(outpath)
    tmp_outfile = "%s/query.result" %(tmpdir)
    resultpathname = jobid
    outpath_result = "%s/%s"%(outpath, resultpathname)
    outfile = "%s/query.result" %(outpath_result)
    tarball = "%s.tar.gz"%(resultpathname)
    tarball_fullpath = "%s.tar.gz"%(outpath_result)
    isOK = True
    try:
        os.makedirs(outpath_result)
        isOK = True
    except OSError:
        msg = "Failed to create folder %s"%(outpath_result)
        myfunc.WriteFile(msg+"\n", runjob_errfile, "a")
        isOK = False

    if isOK:
        g_params['runjob_log'].append("tmpdir = %s"%(tmpdir))
        #cmd = [script_getseqlen, infile, "-o", tmp_outfile , "-printid"]
        datetime = time.strftime("%Y-%m-%d %H:%M:%S")
        rt_msg = myfunc.WriteFile(datetime, starttagfile)
        if rt_msg:
            g_params['runjob_err'].append(rt_msg)

        cmd = [blastall, "-i", infile,  "-p", "blastp","-o", tmp_outfile,  "-d", blastdb  ]

        g_params['runjob_log'].append(" ".join(cmd))
        try:
            myfunc.check_output(cmd)
        except subprocess.CalledProcessError, e:
            g_params['runjob_err'].append(str(e))
            suqoutfilelist = glob.glob("%s/*.sh.*.out"%(tmpdir))
            if len(suqoutfilelist)>0:
                suqoutfile = suqoutfilelist[0]
            g_params['runjob_err'].append(myfunc.ReadFile(suqoutfile))

        if os.path.exists(tmp_outfile):
            cmd = ["cp","-f", tmp_outfile, outfile]
            try:
                myfunc.check_output(cmd)
            except subprocess.CalledProcessError, e:
                g_params['runjob_err'].append(str(e))
def main(g_params):#{{{
    argv = sys.argv
    numArgv = len(argv)
    if numArgv < 2:
        PrintHelp()
        return 1

    outpath = ""
    infile = ""
    tmpdir = ""
    email = ""
    jobid = ""

    i = 1
    isNonOptionArg=False
    while i < numArgv:
        if isNonOptionArg == True:
            infile = argv[i]
            isNonOptionArg = False
            i += 1
        elif argv[i] == "--":
            isNonOptionArg = True
            i += 1
        elif argv[i][0] == "-":
            if argv[i] in ["-h", "--help"]:
                PrintHelp()
                return 1
            elif argv[i] in ["-outpath", "--outpath"]:
                (outpath, i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-tmpdir", "--tmpdir"] :
                (tmpdir, i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-jobid", "--jobid"] :
                (jobid, i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-baseurl", "--baseurl"] :
                (g_params['base_www_url'], i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-email", "--email"] :
                (email, i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-q", "--q"]:
                g_params['isQuiet'] = True
                i += 1
            else:
                print >> sys.stderr, "Error! Wrong argument:", argv[i]
                return 1
        else:
            infile = argv[i]
            i += 1

    if jobid == "":
        print >> sys.stderr, "%s: jobid not set. exit"%(sys.argv[0])
        return 1

    if myfunc.checkfile(infile, "infile"):
        return 1
    if outpath == "":
        print >> sys.stderr, "outpath not set. exit"
        return 1
    elif not os.path.exists(outpath):
        try:
            myfunc.check_output(["mkdir", "-p", outpath])
        except subprocess.CalledProcessError, e:
            print >> sys.stderr, e
            return 1
            rt_msg = myfunc.WriteFile("\n".join(g_params['runjob_log']), runjob_logfile, "w")
            if rt_msg:
                g_params['runjob_err'].append(rt_msg)

        datetime = time.strftime("%Y-%m-%d %H:%M:%S")
        if os.path.exists(outfile):
            rt_msg = myfunc.WriteFile(datetime, finishtagfile)
            if rt_msg:
                g_params['runjob_err'].append(rt_msg)

        # make the tarball
        pwd = os.getcwd()
        os.chdir(outpath)
        cmd = ["tar", "-czf", tarball, resultpathname]
        try:
            myfunc.check_output(cmd)
        except subprocess.CalledProcessError, e:
            g_params['runjob_err'].append(str(e))
        os.chdir(pwd)

    isSuccess = False
    if (os.path.exists(finishtagfile) and os.path.exists(outfile) and
            os.path.exists(tarball_fullpath)):
        isSuccess = True
    else:
        isSuccess = False
        failtagfile = "%s/runjob.failed"%(outpath)
        datetime = time.strftime("%Y-%m-%d %H:%M:%S")
        rt_msg = myfunc.WriteFile(datetime, failtagfile)
        if rt_msg:
            g_params['runjob_err'].append(rt_msg)
def main(g_params):  #{{{
    argv = sys.argv
    numArgv = len(argv)
    if numArgv < 2:
        PrintHelp()
        return 1

    outpath = ""
    infile = ""
    tmpdir = ""
    email = ""
    jobid = ""

    i = 1
    isNonOptionArg = False
    while i < numArgv:
        if isNonOptionArg == True:
            infile = argv[i]
            isNonOptionArg = False
            i += 1
        elif argv[i] == "--":
            isNonOptionArg = True
            i += 1
        elif argv[i][0] == "-":
            if argv[i] in ["-h", "--help"]:
                PrintHelp()
                return 1
            elif argv[i] in ["-outpath", "--outpath"]:
                (outpath, i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-tmpdir", "--tmpdir"]:
                (tmpdir, i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-jobid", "--jobid"]:
                (jobid, i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-baseurl", "--baseurl"]:
                (g_params['base_www_url'], i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-email", "--email"]:
                (email, i) = myfunc.my_getopt_str(argv, i)
            elif argv[i] in ["-q", "--q"]:
                g_params['isQuiet'] = True
                i += 1
            else:
                print >> sys.stderr, "Error! Wrong argument:", argv[i]
                return 1
        else:
            infile = argv[i]
            i += 1

    if jobid == "":
        print >> sys.stderr, "%s: jobid not set. exit" % (sys.argv[0])
        return 1

    if myfunc.checkfile(infile, "infile"):
        return 1
    if outpath == "":
        print >> sys.stderr, "outpath not set. exit"
        return 1
    elif not os.path.exists(outpath):
        try:
            myfunc.check_output(["mkdir", "-p", outpath])
        except subprocess.CalledProcessError, e:
            print >> sys.stderr, e
            return 1
                                      runjob_logfile, "w")
            if rt_msg:
                g_params['runjob_err'].append(rt_msg)

        datetime = time.strftime("%Y-%m-%d %H:%M:%S")
        if os.path.exists(outfile):
            rt_msg = myfunc.WriteFile(datetime, finishtagfile)
            if rt_msg:
                g_params['runjob_err'].append(rt_msg)

        # make the tarball
        pwd = os.getcwd()
        os.chdir(outpath)
        cmd = ["tar", "-czf", tarball, resultpathname]
        try:
            myfunc.check_output(cmd)
        except subprocess.CalledProcessError, e:
            g_params['runjob_err'].append(str(e))
        os.chdir(pwd)

    isSuccess = False
    if (os.path.exists(finishtagfile) and os.path.exists(outfile)
            and os.path.exists(tarball_fullpath)):
        isSuccess = True
    else:
        isSuccess = False
        failtagfile = "%s/runjob.failed" % (outpath)
        datetime = time.strftime("%Y-%m-%d %H:%M:%S")
        rt_msg = myfunc.WriteFile(datetime, failtagfile)
        if rt_msg:
            g_params['runjob_err'].append(rt_msg)