Ejemplo n.º 1
0
    def connect(self, dbpath=None, configpath=None):    #pylint: disable=too-many-branches, too-many-statements
        """Open a connection to the jobs database.

           dbpath: path to a JobDB database file.
           configpath: path to a pbs config file.

           If dbpath is not given:
           If PBS_JOB_DB environment variable exists, set dbpath to "$PBS_JOB_DB/jobs.db" file.
           Else, set dbpath to "$HOME/.pbs/jobs.db", where $HOME is the user's home directory

           If configpath is not given:
           If PBS_JOB_DB environment variable exists, set configpath to
            "$PBS_JOB_DB/config.json" file.
           Else, set configpath to "$HOME/.pbs/config.json", where $HOME is
            the user's home directory

        """
        if dbpath is None:
            if "PBS_JOB_DB" in os.environ:
                dbpath = os.environ("PBS_JOB_DB")
                if not os.path.isdir(dbpath):
                    print "Error in pbs.jobdb.JobDB.connect()."
                    print "  PBS_JOB_DB:", dbpath
                    print "  Does not exist"
                    sys.exit()
            else:
                dbpath = os.path.join(os.environ["HOME"], ".pbs")
                if not os.path.isdir(dbpath):
                    print "Creating directory:", dbpath
                    os.mkdir(dbpath)
            dbpath = os.path.join(dbpath, "jobs.db")
        else:
            if not os.path.isfile(dbpath):
                print ("Error in pbs.jobdb.JobDB.connect(). argument dbpath =",
                       dbpath, "is not a file.")
                sys.exit()


        if configpath is None:
            if "PBS_JOB_DB" in os.environ:
                configpath = os.environ("PBS_JOB_DB")
                if not os.path.isdir(configpath):
                    print "Error in pbs.jobdb.JobDB.connect()."
                    print "  PBS_JOB_DB:", configpath
                    print "  Does not exist"
                    sys.exit()
            else:
                configpath = os.path.join(os.environ["HOME"], ".pbs")
                if not os.path.isdir(configpath):
                    print "Creating directory:", configpath
                    os.mkdir(configpath)
            configpath = os.path.join(configpath, "config.json")
        else:
            if not os.path.isfile(configpath):
                print ("Error in pbs.jobdb.JobDB.connect(). argument configpath =",
                       configpath, "is not a file.")
                sys.exit()

        if not os.path.isfile(configpath):
            print "Writing Config:", configpath
            self.config = {"software" : misc.getsoftware(), "version" : misc.getversion()}
            with open(configpath, "w") as my_json:
                json.dump(self.config, my_json, indent=0)
        else:
            with open(configpath) as my_json:
                self.config = json.load(my_json)

        if not os.path.isfile(dbpath):
            print "Creating Database:", dbpath
            self.conn = sqlite3.connect(dbpath)
            self.conn.row_factory = sqlite3.Row
            self.conn.create_function("REGEXP", 2, regexp)
            self.curs = self.conn.cursor()
            self.curs.execute("CREATE TABLE jobs " + sql_create_str())
            self.conn.commit()
        else:
            self.conn = sqlite3.connect(dbpath)
            self.conn.row_factory = sqlite3.Row
            self.conn.create_function("REGEXP", 2, regexp)
            self.curs = self.conn.cursor()
Ejemplo n.º 2
0
def _qstat(jobid=None, username=getlogin(), full=False, version=getversion()):
    """Return the stdout of qstat minus the header lines.

       By default, 'username' is set to the current user.
       'full' is the '-f' option
       'id' is a string or list of strings of job ids

       Returns the text of qstat, minus the header lines
    """

    # -u and -f contradict in earlier versions of Torque
    if full and username is not None and int(
            version.split('.')[0]) < 5 and jobid is None:
        # First get all jobs by the user
        qopt = ["qselect"]
        qopt += ["-u", username]

        # Call 'qselect' using subprocess
        q = subprocess.Popen(qopt,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)  #pylint: disable=invalid-name
        stdout, stderr = q.communicate()  #pylint: disable=unused-variable

        qsout = StringIO.StringIO(stdout)

        # Get the jobids
        jobid = []
        for line in qsout:
            jobid += [line.rstrip("\n")]

    opt = ["qstat"]
    # If there are jobid(s), you don't need a username
    if username is not None and jobid is None:
        opt += ["-u", username]
    # But if there are jobid(s) and a username, you need -a to get full output
    elif username is not None and jobid is not None and not full:
        opt += ["-a"]
    # By this point we're guaranteed torque ver >= 5.0, so -u and -f are safe together
    if full:
        opt += ["-f"]
    if jobid is not None:
        if isinstance(jobid, str) or isinstance(jobid, unicode):
            jobid = [jobid]
        elif isinstance(jobid, list):
            pass
        else:
            print "Error in pbs.misc.qstat(). type(jobid):", type(jobid)
            sys.exit()
        opt += jobid

    # call 'qstat' using subprocess
    # print opt
    p = subprocess.Popen(opt, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)  #pylint: disable=invalid-name
    stdout, stderr = p.communicate()  #pylint: disable=unused-variable

    sout = StringIO.StringIO(stdout)

    # strip the header lines
    if full is False:
        for line in sout:
            if line[0] == "-":
                break

    # return the remaining text
    return sout.read()