Path: blob/master/Tools/scripts/build_binaries_history.py
9688 views
import os1import sqlite3234class BuildBinariesHistory():5def __init__(self, db_filepath):6self.db_filepath = db_filepath7self.assure_db_present()89def progress(self, msg):10print("BBHIST: %s" % msg)1112def conn(self):13return sqlite3.connect(self.db_filepath)1415def create_schema(self, c):16'''create our tables and whatnot'''17schema_version = 118c.execute("create table version (version integer)")19c.execute("insert into version (version) values (?)", (schema_version,))20# at some stage we should probably directly associate build with runs....21c.execute("create table build (hash text, tag text, vehicle text, board text, "22"frame text, text integer, data integer, bss integer, start_time real, duration real)")23c.execute("create table run (hash text, tag text, start_time real, duration real)")24c.commit()2526def sizes_for_file(self, filepath):27cmd = "size %s" % (filepath,)28stuff = os.popen(cmd).read()29lines = stuff.split("\n")30sizes = lines[1].split("\t")31text = int(sizes[0])32data = int(sizes[1])33bss = int(sizes[2])34self.progress("Binary size of %s:" % filepath)35self.progress("text=%u" % text)36self.progress("data=%u" % data)37self.progress("bss=%u" % bss)38return (text, data, bss)3940def assure_db_present(self):41c = self.conn()42need_schema_create = False43try:44version_cursor = c.execute("select version from version")45except sqlite3.OperationalError as e:46if "no such table" in str(e): # FIXME: do better here? what's in "e"?47print("need schema create")48need_schema_create = True4950if need_schema_create:51self.create_schema(c)52version_cursor = c.execute("select version from version")5354version_results = version_cursor.fetchall()5556if len(version_results) == 0:57raise IOError("No version number?")58if len(version_results) > 1:59raise IOError("More than one version result?")60first = version_results[0]61want_version = 162got_version = first[0]63if got_version != want_version:64raise IOError("Bad version number (want=%u got=%u" %65(want_version, got_version))66self.progress("Got history version %u" % got_version)6768def record_build(self, hash, tag, vehicle, board, frame, bare_path, start_time, duration):69if bare_path is None:70(text, data, bss) = (None, None, None)71else:72(text, data, bss) = self.sizes_for_file(bare_path)73c = self.conn()74c.execute("replace into build (hash, tag, vehicle, board, frame, text, data, bss, start_time, duration) "75"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",76(hash, tag, vehicle, board, frame, text, data, bss, start_time, duration))77c.commit()7879def record_run(self, hash, tag, start_time, duration):80c = self.conn()81c.execute("replace into run (hash, tag, start_time, duration) "82"values (?, ?, ?, ?)",83(hash, tag, start_time, duration))84c.commit()858687