Exemplo n.º 1
0
def timeit(function):
    """
    Decorator to measure function call time.
    """

    func_name = function.__name__

    def debug_wrapper(*args, **kwargs):
        """
        Log measured time.
        """
        before = datetime.now()
        res = function(*args, **kwargs)
        after = datetime.now()
        timediff = after - before
        diff = timediff.microseconds / 1000
        LOG.debug('[' + str(diff) + 'ms] ' + func_name)
        return res

    def release_wrapper(*args, **kwargs):
        """
        No logging and measuring.
        """
        res = function(*args, **kwargs)
        return res

    if LoggerFactory.get_log_level() == logger.DEBUG:
        return debug_wrapper
    else:
        return release_wrapper
Exemplo n.º 2
0
    def start(self, db_version_info, wait_for_start=True, init=False):
        """
        Start a PostgreSQL instance with given path, host and port.
        Return with process instance.
        """

        LOG.debug('Starting/connecting to database.')
        if not self._is_running():
            if not util.is_localhost(self.host):
                LOG.info('Database is not running yet.')
                sys.exit(1)

            if not self._is_database_data_exist():
                if not init:
                    # The database does not exists.
                    LOG.error('Database data is missing!')
                    LOG.error('Please check your configuration!')
                    sys.exit(1)
                elif not self._initialize_database_data():
                    # The database does not exist and cannot create.
                    LOG.error('Database data is missing and '
                              'the initialization of a new failed!')
                    LOG.error('Please check your configuration!')
                    sys.exit(1)

            LOG.info('Starting database')
            LOG.debug('Starting database at ' + self.host + ':' +
                      str(self.port) + ' ' + self.path)

            db_logfile = os.path.join(self.workspace, 'postgresql.log') \
                if LoggerFactory.get_log_level() == logger.DEBUG \
                else os.devnull
            self._db_log = open(db_logfile, 'wb')

            start_db = [
                'postgres', '-i', '-D', self.path, '-p',
                str(self.port), '-h', self.host
            ]
            self.proc = subprocess.Popen(start_db,
                                         bufsize=-1,
                                         env=self.run_env,
                                         stdout=self._db_log,
                                         stderr=subprocess.STDOUT)

        add_version = False
        if init:
            self._wait_or_die()
            self._create_database()
            add_version = not self.check_db_version(db_version_info)
            self._create_or_update_schema()
        elif wait_for_start:
            self._wait_or_die()
            add_version = not self.check_db_version(db_version_info)

        if add_version:
            self._add_version(db_version_info)

        atexit.register(self.stop)
        LOG.debug('Done')
Exemplo n.º 3
0
    def handle_results(self, client=None):
        """
        Send the plist content to the database.
        Server API calls should be used in one connection.
         - addBuildAction
         - addReport
         - needFileContent
         - addFileContent
         - finishBuildAction
        """
        if not client:
            LOG.error("Client needs to be set to store the "
                      "results to the database")
            return

        LOG.debug('Storing original build and analyzer command '
                  'to the database.')

        _, source_file_name = os.path.split(self.analyzed_source_file)

        if LoggerFactory.get_log_level() == logger.DEBUG:
            analyzer_cmd = ' '.join(self.analyzer_cmd)
        else:
            analyzer_cmd = ''

        build_cmd_hash = self.buildaction.original_command_hash
        analysis_id = \
            client.addBuildAction(self.__run_id,
                                  build_cmd_hash,
                                  analyzer_cmd,
                                  self.buildaction.analyzer_type,
                                  source_file_name)

        plist_file = self.analyzer_result_file

        try:
            files, reports = plist_parser.parse_plist(plist_file)
        except Exception as ex:
            LOG.debug(str(ex))
            msg = 'Parsing the generated result file failed.'
            LOG.error(msg + ' ' + plist_file)
            client.finishBuildAction(analysis_id, msg)
            return 1

        try:
            self.__store_bugs(files, reports, client, analysis_id)
        except Exception as ex:
            LOG.error("Failed to store results")
            traceback.print_stack()
            LOG.error(ex)
            return 1
        finally:
            LOG.debug("Finishing buildaction")
            client.finishBuildAction(analysis_id, self.analyzer_stderr)
            LOG.debug("Finishing buildaction done.")
Exemplo n.º 4
0
    def handle_results(self):
        """
        Send the plist content to the database.
        Server API calls should be used in one connection.
         - addBuildAction
         - addReport
         - needFileContent
         - addFileContent
         - finishBuildAction
        """

        with client.get_connection() as connection:

            LOG.debug('Storing original build and analyzer command '
                      'to the database.')

            _, source_file_name = os.path.split(self.analyzed_source_file)

            if LoggerFactory.get_log_level() == logger.DEBUG:
                analyzer_cmd = ' '.join(self.analyzer_cmd)
            else:
                analyzer_cmd = ''

            build_cmd_hash = self.buildaction.original_command_hash
            analysis_id = \
                connection.add_build_action(self.__run_id,
                                            build_cmd_hash,
                                            analyzer_cmd,
                                            self.buildaction.analyzer_type,
                                            source_file_name)

            assert self.analyzer_returncode == 0

            plist_file = self.analyzer_result_file

            try:
                files, reports = plist_parser.parse_plist(plist_file)
            except Exception as ex:
                LOG.debug(str(ex))
                msg = 'Parsing the generated result file failed.'
                LOG.error(msg + ' ' + plist_file)
                connection.finish_build_action(analysis_id, msg)
                return 1

            self.__store_bugs(files, reports, connection, analysis_id)

            connection.finish_build_action(analysis_id, self.analyzer_stderr)