Example #1
0
File: db.py Project: rrane/jcvi
def get_profile(sqshrc="~/.sqshrc", connector="Sybase", hostname=None, username=None, password=None):
    """
    get database, username, password from .sqshrc file e.g.
    \set username="******"
    """
    if connector == "Sybase":
        shost, suser, spass = None, None, None
        _ = lambda x: x.split("=")[-1].translate(None, "\"'").strip()
        sqshrc = op.expanduser(sqshrc)
        for row in open(sqshrc):
            row = row.strip()
            if not row.startswith("\\set") or "prompt" in row:
                continue
            if "password" in row:
                spass = _(row)
            if "hostname" in row:
                shost = _(row)
            if "username" in row:
                suser = _(row)

        if suser and spass:
            username, password = suser, spass
        if shost:
            hostname = shost

    dhost, duser, dpass = db_defaults(connector=connector)
    if not password:
        username, password = duser, dpass
    elif not username:
        username = getusername()

    if not hostname:
        hostname = dhost

    return hostname, username, password
Example #2
0
def kill(args):
    """
    %prog kill [options] JOBNAMEPAT/JOBIDs

    Kill jobs based on JOBNAME pattern matching (case-sensitive)
    or list of JOBIDs (comma separated)

    Examples:
    %prog kill "pyth*"                 # Use regex
    %prog kill 160253,160245,160252    # Use list of job ids
    %prog kill all                     # Everything
    """
    import shlex
    from jcvi.apps.base import sh, getusername
    from subprocess import check_output, CalledProcessError
    import xml.etree.ElementTree as ET

    valid_methods = ("pattern", "jobid")
    p = OptionParser(kill.__doc__)
    p.add_option(
        "--method",
        choices=valid_methods,
        help="Identify jobs based on [default: guess]",
    )
    opts, args = p.parse_args(args)

    if len(args) != 1:
        sys.exit(not p.print_help())

    username = getusername()
    (tag, ) = args
    tag = tag.strip()

    if tag == "all":
        sh("qdel -u {0}".format(username))
        return

    valid_jobids = set()
    method = opts.method or guess_method(tag)
    if method == "jobid":
        jobids = tag.split(",")
        valid_jobids |= set(jobids)
    elif method == "pattern":
        qsxmlcmd = 'qstat -u "{}" -j "{}" -nenv -njd -xml'.format(
            username, tag)
        try:
            qsxml = check_output(shlex.split(qsxmlcmd)).strip()
        except CalledProcessError as e:
            qsxml = None
            logging.debug(f'No jobs matching the pattern "{tag}": {e}')

        if qsxml is not None:
            for job in ET.fromstring(qsxml).findall("djob_info"):
                for elem in job.findall("element"):
                    jobid = elem.find("JB_job_number").text
                    valid_jobids.add(jobid)

    if valid_jobids:
        sh("qdel {0}".format(",".join(valid_jobids)))
Example #3
0
def kill(args):
    """
    %prog kill [options] JOBNAMEPAT/JOBIDs

    Kill jobs based on JOBNAME pattern matching (case-sensitive)
    or list of JOBIDs (comma separated)

    Examples:
    %prog kill "pyth*"                 # Use regex
    %prog kill 160253,160245,160252    # Use list of job ids
    %prog kill all                     # Everything
    """
    import shlex
    from jcvi.apps.base import sh, getusername
    from subprocess import check_output, CalledProcessError
    import xml.etree.ElementTree as ET

    valid_methods = ("pattern", "jobid")
    p = OptionParser(kill.__doc__)
    p.add_option("--method", choices=valid_methods,
                 help="Identify jobs based on [default: guess]")
    opts, args = p.parse_args(args)

    if len(args) != 1:
        sys.exit(not p.print_help())

    username = getusername()
    tag, = args
    tag = tag.strip()

    if tag == "all":
        sh("qdel -u {0}".format(username))
        return

    valid_jobids = set()
    method = opts.method or guess_method(tag)
    if method == "jobid":
        jobids = tag.split(",")
        valid_jobids |= set(jobids)
    elif method == "pattern":
        qsxmlcmd = 'qstat -u "{0}" -j "{1}" -nenv -njd -xml'.\
                                format(username, tag)
        try:
            qsxml = check_output(shlex.split(qsxmlcmd)).strip()
        except CalledProcessError as e:
            qsxml = None
            logging.debug('No jobs matching the pattern "{0}"'.format(tag))

        if qsxml is not None:
            for job in ET.fromstring(qsxml).findall("djob_info"):
                for elem in job.findall("element"):
                    jobid = elem.find("JB_job_number").text
                    valid_jobids.add(jobid)

    if valid_jobids:
        sh("qdel {0}".format(",".join(valid_jobids)))
Example #4
0
def get_profile(sqshrc="~/.sqshrc"):
    """
    get database, username, password from .sqshrc file e.g.
    \set username="******"
    """
    _ = lambda x: x.split("=")[-1].translate(None, "\"'").strip()
    sqshrc = op.expanduser(sqshrc)
    for row in open(sqshrc):
        row = row.strip()
        if not row.startswith("\set"):
            continue
        if "password" in row:
            password = _(row)

    hostname = gethostname()
    username = getusername()

    return hostname, username, password
Example #5
0
def get_profile(sqshrc="~/.sqshrc"):
    """
    get database, username, password from .sqshrc file e.g.
    \set username="******"
    """
    _ = lambda x: x.split("=")[-1].translate(None, "\"'").strip()
    sqshrc = op.expanduser(sqshrc)
    for row in open(sqshrc):
        row = row.strip()
        if not row.startswith("\set"):
            continue
        if "password" in row:
            password = _(row)

    hostname = gethostname()
    username = getusername()

    return hostname, username, password
Example #6
0
def get_profile(sqshrc="~/.sqshrc",
                connector='Sybase',
                hostname=None,
                username=None,
                password=None):
    """
    get database, username, password from .sqshrc file e.g.
    \set username="******"
    """
    if connector == 'Sybase':
        shost, suser, spass = None, None, None
        _ = lambda x: x.split("=")[-1].translate(None, "\"'").strip()
        sqshrc = op.expanduser(sqshrc)
        if op.exists(sqshrc):
            for row in open(sqshrc):
                row = row.strip()
                if not row.startswith("\\set") or "prompt" in row:
                    continue
                if "password" in row:
                    spass = _(row)
                if "hostname" in row:
                    shost = _(row)
                if "username" in row:
                    suser = _(row)
        else:
            print >> sys.stderr, \
                "[warning] file `{0}` not found".format(sqshrc)

        if suser and spass:
            username, password = suser, spass
        if shost:
            hostname = shost

    dhost, duser, dpass = db_defaults(connector=connector)
    if not password:
        username, password = duser, dpass
    elif not username:
        username = getusername()

    if not hostname:
        hostname = dhost

    return hostname, username, password