Commit 34e2b840 authored by Aurélien Campéas's avatar Aurélien Campéas
Browse files

command: provide a command line tool to query a given tshistory database

parent 2298b007e513
import click
from sqlalchemy import create_engine
from tshistory.tsio import TimeSerie
FMT = """
revision: {rev}
author: {author}
date: {date}
""".strip()
def format_rev(rev):
fmt = FMT + '\n'
if rev.get('diff'):
fmt += 'series: {names}\n\n'
lines = []
for ts in rev['diff'].values():
lines.append(ts.to_csv())
fmt += '\n'.join(lines)
else:
fmt += 'series: {names}'
return fmt.format(**rev)
@click.command()
@click.argument('db-uri')
@click.option('--limit', '-l', default=None)
@click.option('--show-diff', is_flag=True, default=False)
def log(db_uri, limit, show_diff):
engine = create_engine(db_uri)
tsh = TimeSerie()
for rev in tsh.log(engine, limit=limit, diff=show_diff):
rev['names'] = ','.join(rev['names'])
print(format_rev(rev))
print()
if __name__ == '__main__':
log()
......@@ -187,15 +187,20 @@ class TimeSerie(object):
).where(tstable.c.csid == cset.c.id)
return cnx.execute(sql).scalar()
def log(self, cnx, diff=False):
def log(self, cnx, limit=0, diff=False):
"""Build a structure showing the history of all the series in the db,
per changeset, in chronological order.
"""
log = []
cset, cset_series, reg = schema.changeset, schema.changeset_series, schema.registry
sql = select([cset.c.id, cset.c.author, cset.c.insertion_date, reg.c.name]
).order_by(asc(cset.c.id)
).where(cset.c.id == cset_series.c.csid
).order_by(desc(cset.c.id))
if limit:
sql = sql.limit(limit)
sql = sql.where(cset.c.id == cset_series.c.csid
).where(cset_series.c.serie == reg.c.name)
rset = cnx.execute(sql)
......@@ -211,6 +216,7 @@ class TimeSerie(object):
rev['diff'] = {name: self._diff(cnx, rev['rev'], name)
for name in rev['names']}
log.sort(key=lambda rev: rev['rev'])
return log
# /API
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment