#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
#

'''
bentoo-merger.py - merge performance data from different sources

This tool merges collected performance data from different data sources. It
understands the internal structure of performance database and selects merge
keys automatically. Currently, it only supports data generated by Likwid
parser.
'''

import argparse
import sqlite3


def main():
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("ref_db",
                        help="Reference database")
    parser.add_argument("data_db",
                        help="Database to update from")
    parser.parse_args()


if __name__ == "__main__":
    main()

@@ -385,16 +385,1735 @@
 sqlite3%0A
+import fnmatch%0Aimport pandas%0Aimport re%0A%0A%0Adef glob_strings(source, patterns):%0A    if not source or not patterns:%0A        return %5B%5D%0A    return %5Bx for x in source for y in patterns if fnmatch.fnmatch(x, y)%5D%0A%0A%0Adef quote(string):%0A    return %22%5C%22%25s%5C%22%22 %25 string%0A%0A%0Adef is_data_column(column):%0A    if re.match(r%22%5Cw+:%5Cw+%22, column):%0A        return True%0A    if column in %5B%22RDTSC%22, %22CallCount%22%5D:%0A        return True%0A    return False%0A%0A%0Adef extract_column_names(conn, table=%22result%22):%0A    orig_row_factory = conn.row_factory%0A    conn.row_factory = sqlite3.Row%0A    r = conn.execute(%22SELECT * FROM %25s LIMIT 1%22 %25 table).fetchone()%0A    names = list(r.keys())%0A    conn.row_factory = orig_row_factory%0A    return names%0A%0A%0Adef merge_db(main_db, ref_db, out_db, replace=None, append=None):%0A    conn0 = sqlite3.connect(main_db)%0A    conn1 = sqlite3.connect(ref_db)%0A%0A    main_cols = extract_column_names(conn0)%0A    ref_cols = extract_column_names(conn1)%0A%0A    replace_cols = glob_strings(main_cols, replace)%0A    append_cols = glob_strings(ref_cols, append)%0A    index_cols = %5Bx for x in main_cols if not is_data_column(x)%5D%0A%0A    sql = index_cols + replace_cols + append_cols%0A    sql = map(quote, sql)%0A    sql = %22SELECT %25s FROM result%22 %25 %22, %22.join(sql)%0A    ref_data = pandas.read_sql_query(sql, conn1)%0A    ref_data = ref_data.set_index(index_cols)%0A    main_data = pandas.read_sql_query(%22SELECT * FROM result%22, conn0)%0A    main_data = main_data.set_index(index_cols)%0A    for x in append_cols:%0A        assert(x not in main_data)%0A        main_data%5Bx%5D = 0%0A    main_data.update(ref_data)%0A%0A    conn2 = sqlite3.connect(out_db)%0A    main_data.to_sql(%22result%22, conn2, if_exists=%22replace%22)%0A    conn2.commit()%0A    conn2.close()%0A%0A    conn1.close()%0A    conn0.close()%0A
 %0A%0Adef ma
@@ -2272,19 +2272,20 @@
 gument(%22
-ref
+main
 _db%22,%0A  
@@ -2316,26 +2316,124 @@
 lp=%22
-Reference database
+Database to be updated%22)%0A    parser.add_argument(%22ref_db%22,%0A                        help=%22Database to get update data
 %22)%0A 
@@ -2456,20 +2456,19 @@
 gument(%22
-data
+out
 _db%22,%0A  
@@ -2511,25 +2511,323 @@
  to 
-update from
+store output%22)%0A    parser.add_argument(%22-r%22, %22--replace%22, nargs=%22+%22, default=None,%0A                        help=%22Columns to replace, supports shell wildcards %22)%0A    parser.add_argument(%22-a%22, %22--append%22, nargs=%22+%22, default=None,%0A                        help=%22Columns to append, supports shell wildcards
 %22)%0A
+%0A
    
+ args =
  par
@@ -2843,16 +2843,43 @@
 _args()%0A
+    merge_db(**vars(args))%0A
 %0A%0Aif __n

Implement bentoo-merge