Ejemplo n.º 1
0
    def updatewftasks(self):
        """ Handles the addition of new WF tasks """

        print("")
        print("Checking WF scheme status DB")

        mydb = MyConnectionBase()
        mydb.setResource(resourceName="STATUS")
        ok = mydb.openConnection()
        if not ok:
                print("ERROR: Could not open resource %s" % 'STATUS')
                return

        defpath = self.__ci.get('SITE_WF_XML_PATH')

        for taskid, fname in self.__wftasks:

            fpath = os.path.join(defpath, fname)

            if not os.path.exists(fpath):
                # print("Skipping %s as does not exist" % fname)
                continue

            myq = MyDbQuery(dbcon=mydb._dbCon)
            query = "select wf_class_id from wf_class_dict where wf_class_id='{}'".format(taskid)

            rows = myq.selectRows(queryString=query)
            if len(rows) == 0:
                print("About to install WF schema %s with name %s" % (taskid, fname))
                cmd = 'python -m wwpdb.apps.wf_engine.wf_engine_utils.tasks.WFTaskRequestExec --verbose --load_wf_def_file={}'.format(fname)
                self.__exec(cmd)
                
        mydb.closeConnection()
Ejemplo n.º 2
0
    def updatedb(self):
        mydb = MyConnectionBase()
        mydb.setResource(resourceName="STATUS")
        ok = mydb.openConnection()
        if not ok:
            print("ERROR: Could not open status db")
            return

        myq = MyDbQuery(dbcon=mydb._dbCon)
        query = "select count(ordinal) from taxonomy "

        rows = myq.selectRows(queryString=query)

        count = rows[0][0]

        mydb.closeConnection()

        if count >= self.__taxdbsize and count < self.__maxsize:
            print("Taxdb at least as big as expected")
            return

        if self.__useftp:
            command = "python -m wwpdb.apps.deposit.depui.taxonomy.loadTaxonomyFromFTP --write_sql"
        else:
            taxfile = self.__cI.get("TAXONOMY_FILE_NAME")
            if not taxfile:
                print("Could not find site-config TAXONOMY_FILE_NAME -- cannot load taxonomy")
                return

            command = "python -m wwpdb.apps.deposit.depui.taxonomy.loadData --input_csv {}".format(taxfile)
        self.__exec(command)
Ejemplo n.º 3
0
 def _notexists(self, dbconn, table, colname):
     """Checks if colname exists in table. Returns True if does not exist"""
     myq = MyDbQuery(dbcon=dbconn)
     query = "show columns from `{}` LIKE '{}'".format(table, colname)
     rows = myq.selectRows(queryString=query)
     if len(rows) == 0:
         return True
     return False
Ejemplo n.º 4
0
    def _nottableexists(self, dbconn, table):
        """Checks if table does not exist. Returns True if does not exist"""
        myq = MyDbQuery(dbcon=dbconn)
        query = "SELECT count(*) FROM information_schema.TABLES WHERE TABLE_NAME = '{}'  AND TABLE_SCHEMA in (SELECT DATABASE())".format(table)

        rows = myq.selectRows(queryString=query)
        if len(rows) != 1:
            return True
        val = rows[0][0]
        if val == 0:
            return True
        return False
Ejemplo n.º 5
0
    def _colwidth(self, dbconn, table, colname, width):
        """Returns True if colname is not width characters"""
        myq = MyDbQuery(dbcon=dbconn)
        query = "select character_maximum_length from information_schema.columns where table_schema=Database() and table_name='{}' and column_name='{}'".format(table,colname);
        rows = myq.selectRows(queryString=query)
        if len(rows) == 0:
            print("ERROR  {}.{} does not exist!!!!!!!".format(table,colname))
            return False

        size = rows[0][0]
        if size != width:
            return True
        return False
Ejemplo n.º 6
0
    def __testDirectiveWithHistoryQuery(self, qdL, domD):
        """Test case -  selection everything for a simple condition -"""
        startTime = time.time()
        self.__lfh.write(
            "\nStarting MyQueryDirectivesTest __testDirectiveWithHistoryQuery at %s\n"
            % time.strftime("%Y %m %d %H:%M:%S", time.localtime()))
        try:
            self.__databaseName = "da_internal"
            self.open()
            sd = DaInternalSchemaDef(verbose=self.__verbose, log=self.__lfh)
            mqd = MyQueryDirectives(schemaDefObj=sd,
                                    verbose=self.__verbose,
                                    log=self.__lfh)
            sqlS = mqd.build(queryDirL=qdL,
                             domD=domD,
                             appendValueConditonsToSelect=True)
            if self.__verbose:
                self.__lfh.write(
                    "\n\n+testDirectiveWithHistoryQuery SQL\n %s\n\n" % sqlS)
            self.__lfh.flush()
            myQ = MyDbQuery(dbcon=self.__dbCon,
                            verbose=self.__verbose,
                            log=self.__lfh)
            rowList = myQ.selectRows(queryString=sqlS)
            if self.__verbose:
                self.__lfh.write(
                    "\n+testDirectiveWithHistoryQuery mysql server returns row length %d\n"
                    % len(rowList))
                self.__lfh.flush()
                for ii, row in enumerate(rowList[:30]):
                    self.__lfh.write("   %6d  %r\n" % (ii, row))
            self.close()
        except:  # noqa: E722  pylint: disable=bare-except
            traceback.print_exc(file=self.__lfh)
            self.fail()

        endTime = time.time()
        self.__lfh.write(
            "\nCompleted MyQueryDirectivesTest __testDirectiveWithHistoryQuery at %s (%d seconds)\n"
            % (time.strftime("%Y %m %d %H:%M:%S",
                             time.localtime()), endTime - startTime))
Ejemplo n.º 7
0
    def _select(self, tableId, **kwargs):
        """Construct a selection query for input table and optional constraints provided as keyword value pairs in the
        input arguments.  Return a list of dictionaries of these query details including all table attributes.
        """
        startTime = time.time()
        if self.__debug:
            logger.debug("Starting _select at %s",
                         time.strftime("%Y %m %d %H:%M:%S", time.localtime()))
        rdList = []
        try:
            iOpened = False
            if self.__dbCon is None:
                self._open()
                iOpened = True
            #
            tableDefObj = self.__sd.getTable(tableId)
            myQ = MyDbQuery(dbcon=self.__dbCon,
                            verbose=self.__verbose,
                            log=self.__lfh)
            sqlGen = MyDbQuerySqlGen(schemaDefObj=self.__sd,
                                     verbose=self.__verbose,
                                     log=self.__lfh)
            sqlGen.setDatabase(databaseName=self.__databaseName)
            sqlConstraint = MyDbConditionSqlGen(schemaDefObj=self.__sd,
                                                verbose=self.__verbose,
                                                log=self.__lfh)
            #
            atMapL = self._getAttributeParameterMap(tableId=tableId)
            for kwArg, kwVal in kwargs.items():  # pylint: disable=unused-variable
                for atId, kwId in atMapL:
                    if kwId == kwArg:
                        if tableDefObj.isAttributeStringType(atId):
                            cTup = ((tableId, atId), "EQ", (kwargs[kwId],
                                                            "CHAR"))
                        else:
                            cTup = ((tableId, atId), "EQ", (kwargs[kwId],
                                                            "OTHER"))
                        sqlConstraint.addValueCondition(
                            cTup[0], cTup[1], cTup[2])
                        break
            #
            # Add optional constraints OR ordering by primary key attributes
            if len(sqlConstraint.get()) > 0:
                sqlGen.setCondition(sqlConstraint)
            else:
                for atId in tableDefObj.getPrimaryKeyAttributeIdList():
                    sqlGen.addOrderByAttributeId(attributeTuple=(tableId,
                                                                 atId))

            atIdList = self.__sd.getAttributeIdList(tableId)
            for atId in atIdList:
                sqlGen.addSelectAttributeId(attributeTuple=(tableId, atId))
            #
            sqlS = sqlGen.getSql()
            if self.__debug:
                logger.debug("_select selection sql: %s", sqlS)

            rowList = myQ.selectRows(queryString=sqlS)
            sqlGen.clear()
            #
            # return the result set as a list of dictionaries
            #
            for iRow, row in enumerate(rowList):
                rD = {}
                for colVal, atId in zip(row, atIdList):
                    rD[atId] = colVal
                if self.__debug:
                    logger.debug("_select result set row %d dictionary %r",
                                 iRow, rD.items())
                rdList.append(rD)
            if iOpened:
                self._close()
        except Exception as e:
            status = " operation error " + str(e)
            logger.error("_select %s", status)
            if self.__verbose:
                logger.eception("_select failed")

        if self.__debug:
            endTime = time.time()
            logger.debug("Completed _select at %s (%.3f seconds)",
                         time.strftime("%Y %m %d %H:%M:%S", time.localtime()),
                         endTime - startTime)
        return rdList