#!/usr/bin/env python
# (C) 2022 Cadence Design Systems, Inc. (Cadence) 
# All rights reserved.
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of Cadence products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. Cadence claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable Cadence offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
# liable for any damages or liability in connection with the Sample Code
# or its use.
import os
import sqlite3
import sys

from openeye import oesitehopper


def get_info(shdb_file):
    conn = sqlite3.connect(f"file:{shdb_file}?mode=ro", uri=True)
    cursor = conn.cursor()
    res = cursor.execute("SELECT title,lastModifiedDate,SiteHopperToolkitVersion from Info;")
    row = res.fetchone()
    print(f"Path: {os.path.abspath(shdb_file)}")
    print(f"  Title: {row[0]}")
    print(f"  Modified: {row[1]}")
    print(f"  SiteHopper TK version: {row[2]}")

    res = cursor.execute("SELECT * from Version;")
    row = res.fetchone()
    print(f"  version: {row[0]}")

    res = cursor.execute("SELECT count(*) from patches;")
    row = res.fetchone()
    print(f"  Num Patches: {row[0]}")

    res = cursor.execute("SELECT count(*) from designunit;")
    row = res.fetchone()
    print(f"  Num DUs: {row[0]-1}")


def main(args):
    if len(args) != 2:
        print("usage: shdb_info.py <shdbfile>")
        return 0

    shdb = oesitehopper.OESiteHopperDatabase()
    if not os.path.exists(args[1]) or not shdb.Open(args[1], oesitehopper.OESiteHopperDatabaseMode_READONLY):
        print(f"{args[1]} is not a valid SHDB file")
        return 1
    shdb.Close()

    get_info(args[1])
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv))