#!/usr/bin/env python
# -*- coding: utf-8 -*-
##----------------------------------------------------------------------
## System Version Report
##----------------------------------------------------------------------
## Copyright (C) 2007-2010 The NOC Project
## See LICENSE for details
##----------------------------------------------------------------------
from __future__ import with_statement
import os,sys,csv

##
## Return NOC version
##
def get_version():
    with open("VERSION") as f:
        v=f.read().split("\n")[0].strip()
    try:
        from mercurial import ui,localrepo
        
        try:
            lr=localrepo.localrepository(ui.ui(),path=".")
            rev=lr.changelog.rev(lr.changelog.tip())
            v+="r%s"%rev
        except:
            pass
    except ImportError:
        pass
    return v
##
def about():
    with os.popen("pg_config --version") as f:
        pg_version=f.read().strip()
    versions=[
        ["NOC"    ,    get_version()],
        ["OS"     ,    " ".join(os.uname())],
        ["Python" ,    sys.version],
        ["PostgreSQL", pg_version],
        ["Python Path", ":".join(sys.path)]
    ]
    cv_path=os.path.join("contrib","lib","VERSION.csv")
    if os.path.exists(cv_path):
        with open(cv_path) as f:
            r=csv.reader(f)
            for row in r:
                versions+=[row]
    for c,v in versions:
        print "|%s|%s|"%(c,v.replace("\n"," "))

if __name__=="__main__":
    about()
