Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Merge beef3d4 into bbc762f
Browse files Browse the repository at this point in the history
  • Loading branch information
Hai Hoang Dang authored Jul 10, 2018
2 parents bbc762f + beef3d4 commit 92f9dba
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 172 deletions.
81 changes: 41 additions & 40 deletions sktm/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
class SktDb(object):
def __init__(self, db):
if not os.path.isfile(db):
self.createdb(db)
self.__createdb(db)

self.conn = sqlite3.connect(db)
self.cur = self.conn.cursor()

def __del__(self):
self.conn.close()

def createdb(self, db):
def __createdb(self, db):
conn = sqlite3.connect(db)
cur = conn.cursor()

Expand Down Expand Up @@ -88,7 +88,7 @@ def createdb(self, db):
cur.close()
conn.close()

def create_repoid(self, baserepo):
def __create_repoid(self, baserepo):
"""Create a repoid for a git repo URL.
Args:
Expand All @@ -101,7 +101,7 @@ def create_repoid(self, baserepo):

return self.cur.lastrowid

def get_repoid(self, baserepo):
def __get_repoid(self, baserepo):
"""Fetch a repoid for a git repo URL.
Args:
Expand All @@ -115,11 +115,11 @@ def get_repoid(self, baserepo):
result = self.cur.fetchone()

if not result:
return self.create_repoid(baserepo)
return self.__create_repoid(baserepo)

return result[0]

def create_sourceid(self, baseurl, project_id):
def __create_sourceid(self, baseurl, project_id):
"""Create a patchsource record that links a baseurl and project_id.
Args:
Expand All @@ -134,7 +134,7 @@ def create_sourceid(self, baseurl, project_id):

return self.cur.lastrowid

def get_sourceid(self, baseurl, project_id):
def __get_sourceid(self, baseurl, project_id):
"""Fetch a patchsource id that links a baseurl and project_id.
Args:
Expand All @@ -150,7 +150,7 @@ def get_sourceid(self, baseurl, project_id):
result = self.cur.fetchone()

if not result:
return self.create_sourceid(baseurl, project_id)
return self.__create_sourceid(baseurl, project_id)

return result[0]

Expand All @@ -162,7 +162,7 @@ def get_last_checked_patch(self, baseurl, project_id):
project_id: Project ID in Patchwork.
"""
sourceid = self.get_sourceid(baseurl, project_id)
sourceid = self.__get_sourceid(baseurl, project_id)

self.cur.execute('SELECT patch.id FROM patch WHERE '
'patchsource_id = ? '
Expand All @@ -183,7 +183,7 @@ def get_last_pending_patch(self, baseurl, project_id):
project_id: Project ID in Patchwork.
"""
sourceid = self.get_sourceid(baseurl, project_id)
sourceid = self.__get_sourceid(baseurl, project_id)

self.cur.execute('SELECT id FROM pendingpatches WHERE '
'patchsource_id = ? '
Expand All @@ -204,7 +204,7 @@ def get_last_checked_patch_date(self, baseurl, project_id):
project_id: Project ID in Patchwork.
"""
sourceid = self.get_sourceid(baseurl, project_id)
sourceid = self.__get_sourceid(baseurl, project_id)

self.cur.execute('SELECT patch.date FROM patch WHERE '
'patchsource_id = ? '
Expand All @@ -225,7 +225,7 @@ def get_last_pending_patch_date(self, baseurl, project_id):
project_id: Project ID in Patchwork.
"""
sourceid = self.get_sourceid(baseurl, project_id)
sourceid = self.__get_sourceid(baseurl, project_id)

self.cur.execute('SELECT pdate FROM pendingpatches WHERE '
'patchsource_id = ? '
Expand Down Expand Up @@ -256,7 +256,7 @@ def get_expired_pending_patches(self, baseurl, project_id, exptime=86400):
List of patch IDs.
"""
patchlist = list()
sourceid = self.get_sourceid(baseurl, project_id)
sourceid = self.__get_sourceid(baseurl, project_id)
tstamp = int(time.time()) - exptime

self.cur.execute('SELECT id FROM pendingpatches WHERE '
Expand All @@ -272,7 +272,7 @@ def get_expired_pending_patches(self, baseurl, project_id, exptime=86400):

return patchlist

def get_baselineid(self, baserepo_id, commithash):
def __get_baselineid(self, baserepo_id, commithash):
"""Get the baseline_id for a particular baserepo_id and commithash.
Args:
Expand All @@ -290,7 +290,7 @@ def get_baselineid(self, baserepo_id, commithash):

return result[0]

def get_commitdate(self, baserepo, commithash):
def __get_commitdate(self, baserepo, commithash):
"""Get the date of a commit in a baseline.
Args:
Expand All @@ -301,7 +301,7 @@ def get_commitdate(self, baserepo, commithash):
Date string or None if the commithash is not found.
"""
baserepo_id = self.get_repoid(baserepo)
baserepo_id = self.__get_repoid(baserepo)

self.cur.execute('SELECT commitdate FROM baseline WHERE '
'commitid = ? AND '
Expand All @@ -314,7 +314,7 @@ def get_commitdate(self, baserepo, commithash):

return result[0]

def get_baselineresult(self, baserepo, commithash):
def __get_baselineresult(self, baserepo, commithash):
"""Get the result of a baseline testrun.
Args:
Expand All @@ -326,7 +326,7 @@ def get_baselineresult(self, baserepo, commithash):
exist.
"""
baserepo_id = self.get_repoid(baserepo)
baserepo_id = self.__get_repoid(baserepo)

self.cur.execute('SELECT testrun.result_id FROM baseline, testrun '
'WHERE baseline.commitid = ? AND '
Expand All @@ -351,7 +351,7 @@ def get_stable(self, baserepo):
Latest stable commit ID, or None, if there are no stable commits.
"""
baserepo_id = self.get_repoid(baserepo)
baserepo_id = self.__get_repoid(baserepo)

self.cur.execute('SELECT commitid FROM baseline, testrun WHERE '
'baseline.baserepo_id = ? AND '
Expand All @@ -367,7 +367,7 @@ def get_stable(self, baserepo):

return result[0]

def get_latest(self, baserepo):
def __get_latest(self, baserepo):
"""Get the commit hash of the latest baseline.
Args:
Expand All @@ -378,7 +378,7 @@ def get_latest(self, baserepo):
exist.
"""
baserepo_id = self.get_repoid(baserepo)
baserepo_id = self.__get_repoid(baserepo)

self.cur.execute('SELECT commitid FROM baseline WHERE '
'baserepo_id = ? '
Expand Down Expand Up @@ -409,7 +409,7 @@ def set_patchset_pending(self, baseurl, project_id, series_data):
patch date string.
"""
sourceid = self.get_sourceid(baseurl, project_id)
sourceid = self.__get_sourceid(baseurl, project_id)
tstamp = int(time.time())

logging.debug("setting patches as pending: %s", series_data)
Expand All @@ -421,7 +421,7 @@ def set_patchset_pending(self, baseurl, project_id, series_data):
(patch_id, patch_date) in series_data])
self.conn.commit()

def unset_patchset_pending(self, baseurl, patch_id_list):
def __unset_patchset_pending(self, baseurl, patch_id_list):
"""Remove a patch from the list of pending patches.
Remove each specified patch from the list of "pending" patches, for
Expand Down Expand Up @@ -456,11 +456,11 @@ def update_baseline(self, baserepo, commithash, commitdate,
build_id: The build ID of the test run.
"""
baserepo_id = self.get_repoid(baserepo)
baserepo_id = self.__get_repoid(baserepo)

testrun_id = self.commit_testrun(result, build_id)
testrun_id = self.__commit_testrun(result, build_id)

prev_res = self.get_baselineresult(baserepo, commithash)
prev_res = self.__get_baselineresult(baserepo, commithash)
logging.debug("previous result: %s", prev_res)

if prev_res is None:
Expand All @@ -487,31 +487,32 @@ def commit_tested(self, patches):
patches: List of patches that were tested
"""
logging.debug("commit_tested: patches=%d", len(patches))
self.commit_series(patches)
self.__commit_series(patches)

for (patch_id, patch_name, patch_url, baseurl, project_id,
patch_date) in patches:
# TODO: Can accumulate per-project list instead of doing it one by
# one
self.unset_patchset_pending(baseurl, [patch_id])
self.__unset_patchset_pending(baseurl, [patch_id])

def commit_testrun(self, result, buildid):
def __commit_testrun(self, result, buildid):
"""Add a test run to the database.
Args:
result: Result of the test run.
build_id: The build ID of the test run.
"""
logging.debug("commit_testrun: result=%s; buildid=%d", result, buildid)
logging.debug("__commit_testrun: result=%s; buildid=%d",
result, buildid)
self.cur.execute('INSERT INTO testrun(result_id, build_id) '
'VALUES(?,?)',
(result.value, buildid))
self.conn.commit()
return self.cur.lastrowid

def commit_patch(self, patch_id, patch_name, patch_url, baseurl,
project_id, patch_date):
def __commit_patch(self, patch_id, patch_name, patch_url, baseurl,
project_id, patch_date):
"""Create/update a patch record in the database.
Args:
Expand All @@ -524,31 +525,31 @@ def commit_patch(self, patch_id, patch_name, patch_url, baseurl,
"""
# pylint: disable=too-many-arguments
logging.debug("commit_patch: pid=%s", patch_id)
source_id = self.get_sourceid(baseurl, project_id)
logging.debug("__commit_patch: pid=%s", patch_id)
source_id = self.__get_sourceid(baseurl, project_id)
self.cur.execute('INSERT OR REPLACE INTO patch(id, name, url, '
'patchsource_id, date) '
'VALUES(?,?,?,?,?)',
(patch_id, patch_name, patch_url, source_id,
patch_date))
self.conn.commit()

def commit_series(self, patches):
def __commit_series(self, patches):
"""Create patch records for a list of patches.
Args:
patches: List of patches to insert into the database.
"""
logging.debug("commit_series: %s", patches)
logging.debug("__commit_series: %s", patches)

for (patch_id, patch_name, patch_url, baseurl, project_id,
patch_date) in patches:
# If the source_id doesn't exist, this method will create it.
self.get_sourceid(baseurl, project_id)
self.__get_sourceid(baseurl, project_id)

# Add the patches to the database
self.commit_patch(patch_id, patch_name, patch_url, baseurl,
project_id, patch_date)
self.__commit_patch(patch_id, patch_name, patch_url, baseurl,
project_id, patch_date)

self.conn.commit()

Expand All @@ -574,7 +575,7 @@ def dump_baserepo_info(self): # pragma: no cover
for (burl,) in self.cur.fetchall():
print("repo url:", burl)
stable = self.get_stable(burl)
latest = self.get_latest(burl)
latest = self.__get_latest(burl)
print("most recent stable commit: {} ({})".format(
stable, self.get_commitdate(burl, stable)))
print("most recent stable commit: {} ({})".format(
Expand Down
Loading

0 comments on commit 92f9dba

Please sign in to comment.