#!/usr/bin/env python3
import os
import sys
import json
from stdlib_list import stdlib_list


def usage(exit_code):
    print("Usage: python_package_analyze [options] <python-script> <json-output-file>")
    print("where options are:")
    print(" -h, --help\tShow this help screen")
    exit(exit_code)


# Parse command line arguments
if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
    usage(0)
if len(sys.argv) != 3:
    usage(1)
python_script = sys.argv[1]
json_output_file = sys.argv[2]
if not os.path.exists(python_script):
    print("Python script does not exist")
    exit(2)

# Find Python version and obtain list of standard library modules 
version = ".".join(sys.version.split()[0].split(".")[:2])
libraries = stdlib_list(version)

# Parse the Python script for all import statements
dependencies = []
source = open(python_script, "r")
for line in source.readlines():
    words = line.split()
    isList = False
    isFrom = False
    # Iterate through each word in the line
    for i in range(0, len(words)):
        # Signals that you are importing a module
        if words[i] == "from" or words[i] == "import":
            if words[i] == "from":
                isFrom = True
            i += 1
            name = words[i]
            if name[-1] == ",":
                name = name[:-1]
                isList = True
            if name not in libraries:
                dependencies.append(name)
        # Iterate through multiple imports if multiple listed on one line
        while isList:
            i += 1
            nane = words[i]
            if name[-1] == ",":
                name = name[:-1]
            else:
                isList = False
            if name not in libraries:
                dependencies.append(name)
        if isFrom:
            break

# Put the JSON data into a file
python_info = {}
python_info["python"] = sys.version.split()[0]
python_info["modules"] = dependencies
output = open(json_output_file, "w")
json.dump(python_info, output)
exit(0)
