#!/usr/bin/env python
"""Troubleshooting script.

Usage:
  fnss-troubleshoot [--help | -h]

This script prints debugging information about FNSS dependencies currently
installed.

The main purpose of this script is to help users to communicate effectively
with developers when reporting an issue.
"""
import os
import sys
import pkg_resources as pkg


# list of FNSS dependencies, including FNSS itself
packages = [
        'fnss',
        'networkx',
        'numpy',
        'scipy',
        'sphinx',
        'numpydoc',
        'argparse',
        'mininet',
        'mako',
            ]


def usage():
    """Print usage information and exit"""
    sys.exit(__doc__)

def main():
    """Troubleshooting script

    Print useful information for troubleshooting.
    """
    print('FNSS TROUBLESHOOTING INFORMATION')
    sysname, nodename, release, version, machine = os.uname()
    py_version = sys.version
    print('OS sysname: %s' % sysname)
    print('OS release: %s' % release)
    print('OS version: %s' % version)
    print('Machine: %s' % machine)
    print('Python version: %s' % py_version.replace('\n', ''))
    # see what packages and versions are installed
    for package in packages:
        try:
            version = pkg.get_distribution(package).version
            print('Package %s: installed with distutils, version: %s' % (package, version))
        except pkg.DistributionNotFound:
            try:
                exec('import %s' % package)
                if hasattr(eval(package), '__version__'):
                    version = eval('%s.__version__' % package)
                else:
                    version = 'unknown'
                print('Package %s: installed manually, version: %s' % (package, version))
                exec('del %s' % package)
            except ImportError:
                print('Package %s: cannot be imported' % package)

if __name__ == "__main__":
    if len(sys.argv) > 1:
        usage()
    main()
