#!/usr/bin/env python3
# License: MIT
# Author: k2patel <k2patel@live.com>
import sys
import argparse
import subprocess
import json

def main(req_file):     
  # Create array of local modules
  cmd = '/usr/local/bin/pip3 list --user --format json --no-index'
  output = subprocess.check_output(cmd, shell=True)

  pylocal = []
  data = json.loads(output)
  for obj in data:
    pylocal.append(obj['name'])

  # Read requirements.txt file and compare if modulee exists in local
  try: 
    with open(req_file, 'rb') as fin:
      content = fin.readlines()
      fin.close()

    for line in content:
      temp = line.decode("utf-8").rstrip().lstrip().split('==')
      if temp[0] not in pylocal:
        print(line.decode("utf-8").rstrip().lstrip().replace('==', ': '))
  except IOError:
    print('File : ' + req_file + ' : not accessible')

if __name__ == "__main__":
  # Initialize and handle options
  req_file = ''
  parser = argparse.ArgumentParser(prog='reqpydiff', description='Create difference between installed module and required module')
  parser.add_argument('-f', '--file', dest='req_file', type=str, help='location of requirements.txt', default='requirements.txt')
  
  args = parser.parse_args()
  main(args.req_file)
