import os
import re
import platform
import subprocess
import yaml
import distutils.spawn

# define some terminal colours to use
class bcolors:
  GOOD = '\033[92m'
  BAD = '\033[91m'
  RESET = '\033[0m'

# define some icons to use
print_tick = bcolors.GOOD + u"\u2713" + bcolors.RESET
print_cross = 	bcolors.BAD + u"\u2A2F" + bcolors.RESET

# define supported package managers
package_managers = ['yum', 'apt-get', 'pacman', 'emerge']
package_manager = "None"

# detect if an application with the provided name is installed
def is_installed(name):
  return distutils.spawn.find_executable(name) is not None

# check which package manager is installed
for manager in package_managers:
  if is_installed(manager):
    package_manager = manager

# load the manifest file in
with open('manifest.yml', 'r') as f:
  try:
    manifest = yaml.load(f)
  except:
    print "Invalid yaml file"
    exit(0)

print "loaded manifest"

to_install = []
unable_to_satisfy = []

# loop through the manifest setting up the environment
if manifest.has_key('dependancies'):
  print "Checking dependancies"
  if manifest['dependancies'].has_key('commands'):
    cmd = "where" if platform.system() == "Windows" else "which"
    for dependancy in manifest['dependancies']['commands']:
      if isinstance(dependancy, basestring):
        if is_installed(dependancy):
          print (print_tick + dependancy)
        else:
          print (print_cross + dependancy)
          FNULL = open(os.devnull, 'w')
          output = subprocess.Popen([package_manager, 'provides', dependancy], stdout=subprocess.PIPE, stderr=FNULL)
          provides = output.stdout.read()
          found_a_package_to_satisfy_dependancy = False
          for result in provides.split('\n'):
            if dependancy in result:
              package = result.split(' ')[0]
              to_install.append(package)
              found_a_package_to_satisfy_dependancy = True
          if found_a_package_to_satisfy_dependancy == False:
            unable_to_satisfy.append(dependancy)
    print "\r\n"
  if manifest['

@@ -2059,9 +2059,948 @@
 nifest%5B'
+dependancies'%5D.has_key(package_manager):%0A    for package in manifest%5B'dependancies'%5D%5Bpackage_manager%5D:%0A      FNULL = open(os.devnull, 'w')%0A      output = subprocess.Popen(%5Bpackage_manager, 'list', package%5D, stdout=subprocess.PIPE, stderr=FNULL)%0A      package_list = output.stdout.read()%0A      found_a_package_to_satisfy_request = False%0A      for result in package_list.split('%5Cn'):%0A        if package in result:%0A          to_install.append(package.split('.')%5B0%5D)%0A          found_a_package_to_satisfy_request = True%0A      if found_a_package_to_satisfy_request == False:%0A        unable_to_satisfy.append(package)%0A%0Aif len(to_install) %3E 0:%0A  print %22Packages to install%22%0A  for package in to_install:%0A    print bcolors.GOOD + package + bcolors.RESET%0A  print %22%5Cr%5Cn%22%0A%0Aif len(unable_to_satisfy) %3E 0:%0A  print %22Unable to satisfy dependancies:%22%0A  for application in unable_to_satisfy:%0A    print bcolors.BAD + application + bcolors.RESET%0A  print %22%5Cr%5Cn%22
 %0A

Update node.py