WS2012R2/lisa/tools/middleware_bench/report/db_utils.py
def upload_results(localpath=None, table_name=None, results_path=None, parser=None,
                   other_table=False, **kwargs):
    """
    Connect to DB and upload results
    """
    if localpath:
        log.info('Looking up DB details in {}\*.config.' .format(localpath))
        db_creds_file = [os.path.join(localpath, c) for c in os.listdir(localpath)
                         if c.endswith('.config')][0]
        # read credentials from file - should be present in the localpath provided to runner
        config = ConfigParser.ConfigParser()
        config.read(db_creds_file)
    else:
        log.error('No credentials file path provided. Skipping results upload.')
        return None

    test_results = parser(log_path=results_path, **kwargs).process_logs()

    pprint.pprint(test_results)
    if 'linux' in sys.platform:
        driver = config.get('Credentials', 'Driver_linux')
    else:
        driver = config.get('Credentials', 'Driver_windows')

    if other_table:
        temp = table_name.split('_')
        temp.insert(2, config.get('Credentials', 'Other_table'))
        table_name = '_'.join(temp)

    e = create_engine('mssql+pyodbc://{}:{}@{}/{}?driver={}'.format(
            config.get('Credentials', 'User'), config.get('Credentials', 'Password'),
            config.get('Credentials', 'Server'), config.get('Credentials', 'Database'), driver,
            poolclass=NullPool, convert_unicode=True, echo=True))
    metadata = MetaData(bind=e)

    table_columns = [column for column in COLUMNS if column['name'] in test_results[0]]
    t = Table(table_name, metadata,
              Column('TestId', BIGINT, primary_key=True, nullable=False, index=True),
              *(Column(column['name'], column['type']) for column in table_columns))

    # When creating db is also necessary
    # metadata.create_all(checkfirst=True)

    class TestResults(object):
        def __getitem__(self, item):
            return getattr(self, item)

        def __setitem__(self, item, value):
            return setattr(self, item, value)

    mapper(TestResults, t)
    session = create_session(bind=e, autocommit=False, autoflush=True)

    for row in test_results:
        test_data = TestResults()
        for k, v in row.items():
            test_data[k] = v
        session.add(test_data)
        try:
            session.commit()
        except Exception as ex:
            log.exception(ex)
            print("Failed to commit {} data. Rolling back.".format(row))
            session.rollback()
            raise

    Connect to DB and upload results
    
Connect to DB and upload results
Connect to DB and upload results
def upload_results(localpath=None, table_name=None, results_path=None, parser=None,
                   other_table=False, **kwargs):
    if localpath:
        log.info('Looking up DB details in {}\*.config.' .format(localpath))
        db_creds_file = [os.path.join(localpath, c) for c in os.listdir(localpath)
                         if c.endswith('.config')][0]
        config = ConfigParser.ConfigParser()
        config.read(db_creds_file)
    else:
        log.error('No credentials file path provided. Skipping results upload.')
        return None
    test_results = parser(log_path=results_path, **kwargs).process_logs()
    pprint.pprint(test_results)
    if 'linux' in sys.platform:
        driver = config.get('Credentials', 'Driver_linux')
    else:
        driver = config.get('Credentials', 'Driver_windows')
    if other_table:
        temp = table_name.split('_')
        temp.insert(2, config.get('Credentials', 'Other_table'))
        table_name = '_'.join(temp)
    e = create_engine('mssql+pyodbc://{}:{}@{}/{}?driver={}'.format(
            config.get('Credentials', 'User'), config.get('Credentials', 'Password'),
            config.get('Credentials', 'Server'), config.get('Credentials', 'Database'), driver,
            poolclass=NullPool, convert_unicode=True, echo=True))
    metadata = MetaData(bind=e)
    table_columns = [column for column in COLUMNS if column['name'] in test_results[0]]
    t = Table(table_name, metadata,
              Column('TestId', BIGINT, primary_key=True, nullable=False, index=True),
              *(Column(column['name'], column['type']) for column in table_columns))
    class TestResults(object):
        def __getitem__(self, item):
            return getattr(self, item)
        def __setitem__(self, item, value):
            return setattr(self, item, value)
    mapper(TestResults, t)
    session = create_session(bind=e, autocommit=False, autoflush=True)
    for row in test_results:
        test_data = TestResults()
        for k, v in row.items():
            test_data[k] = v
        session.add(test_data)
        try:
            session.commit()
        except Exception as ex:
            log.exception(ex)
            print("Failed to commit {} data. Rolling back.".format(row))
            session.rollback()
            raise
def upload_results ( localpath = None , table_name = None , results_path = None , parser = None , other_table = False , ** kwargs ) : if localpath : log . info ( 'Looking up DB details in {}\*.config.' . format ( localpath ) ) db_creds_file = [ os . path . join ( localpath , c ) for c in os . listdir ( localpath ) if c . endswith ( '.config' ) ] [ 0 ] config = ConfigParser . ConfigParser ( ) config . read ( db_creds_file ) else : log . error ( 'No credentials file path provided. Skipping results upload.' ) return None test_results = parser ( log_path = results_path , ** kwargs ) . process_logs ( ) pprint . pprint ( test_results ) if 'linux' in sys . platform : driver = config . get ( 'Credentials' , 'Driver_linux' ) else : driver = config . get ( 'Credentials' , 'Driver_windows' ) if other_table : temp = table_name . split ( '_' ) temp . insert ( 2 , config . get ( 'Credentials' , 'Other_table' ) ) table_name = '_' . join ( temp ) e = create_engine ( 'mssql+pyodbc://{}:{}@{}/{}?driver={}' . format ( config . get ( 'Credentials' , 'User' ) , config . get ( 'Credentials' , 'Password' ) , config . get ( 'Credentials' , 'Server' ) , config . get ( 'Credentials' , 'Database' ) , driver , poolclass = NullPool , convert_unicode = True , echo = True ) ) metadata = MetaData ( bind = e ) table_columns = [ column for column in COLUMNS if column [ 'name' ] in test_results [ 0 ] ] t = Table ( table_name , metadata , Column ( 'TestId' , BIGINT , primary_key = True , nullable = False , index = True ) , * ( Column ( column [ 'name' ] , column [ 'type' ] ) for column in table_columns ) ) class TestResults ( object ) : def __getitem__ ( self , item ) : return getattr ( self , item ) def __setitem__ ( self , item , value ) : return setattr ( self , item , value ) mapper ( TestResults , t ) session = create_session ( bind = e , autocommit = False , autoflush = True ) for row in test_results : test_data = TestResults ( ) for k , v in row . items ( ) : test_data [ k ] = v session . add ( test_data ) try : session . commit ( ) except Exception as ex : log . exception ( ex ) print ( "Failed to commit {} data. Rolling back." . format ( row ) ) session . rollback ( ) raise
Connect to DB and upload results
Connect to DB and upload results
"""
    Connect to DB and upload results
    """ # read credentials from file - should be present in the localpath provided to runner # When creating db is also necessary # metadata.create_all(checkfirst=True)
localpath
table_name
results_path
parser
other_table