#!/usr/bin/python
#coding:utf8

import os
import sys
import yaml
import re
from optparse import OptionParser

global return_code
global project_path

def check_args():
  global project_path

  parser = OptionParser()
  parser.add_option("-p", "--project_path", dest="project_path",
                  help="path to the project")
  (options, args) = parser.parse_args()

  if options.project_path is None:
    print "property project_path is mandatory, exiting ..."
    sys.exit(2)

  if not os.path.exists(options.project_path):
    print "path {} not valid".format(options.project_path)
    sys.exit(2)
  else:
    project_path=options.project_path

def yaml_load(filename):
  with open(filename, 'r') as stream:
      try:
          return (yaml.load(stream))
      except yaml.YAMLError as exc:
          print(exc)

def check_env_vars():
  global project_path
  
  for filename in os.listdir("{}/env_vars".format(project_path)):
    current_env_file = "{}/env_vars/{}".format(project_path, filename)
    try:
      env_vars = yaml_load(current_env_file)

      if env_vars is None:
        print "file {} doesn't have any variables".format(current_env_file)
        return_code = 2
      else:
        for var_name in env_vars:
          if re.match("^env_.*", var_name) is None:
            print "{} propertie dont respect the naming convention prefix env_ into {}".format(
            var_name, 
            current_env_file
            )
            return_code = 2

    except (IOError, KeyError) as e:
      print "Error: unexpected error when open the file {}.".filename
      sys.exit(2)
    

def main():
  global return_code
  return_code = 0

  check_args()
  print "Heather Say :"
  check_env_vars()

if __name__ == '__main__':
  main()
 


@@ -812,16 +812,1218 @@
 t(exc)%0A%0A
+def _check_file_exist_not_empty(current_file):%0A  global project_path%0A  global return_code%0A  try:%0A    assert(os.path.exists(current_file)), %22file %7B%7D not found%22.format(current_file)%0A    assert(os.path.getsize(current_file) %3E 0), %22file %7B%7D is empty%22.format(current_file)%0A  except (AssertionError, OSError) as e:%0A    return_code = 2%0A    print e%0A%0Adef check_default_files():%0A  global project_path%0A%0A  _check_file_exist_not_empty(%22%7B%7D/%7B%7D%22.format(project_path , %22ansible.cfg%22))%0A  _check_file_exist_not_empty(%22%7B%7D/%7B%7D%22.format(project_path , %22env_vars/prod.yml%22))%0A  _check_file_exist_not_empty(%22%7B%7D/%7B%7D%22.format(project_path , %22env_vars/preprod.yml%22))%0A  _check_file_exist_not_empty(%22%7B%7D/%7B%7D%22.format(project_path , %22inventories/prod.ini%22))%0A  _check_file_exist_not_empty(%22%7B%7D/%7B%7D%22.format(project_path , %22inventories/preprod.ini%22))%0A  _check_file_exist_not_empty(%22%7B%7D/%7B%7D%22.format(project_path , %22inventories/prod.ini%22))%0A  _check_file_exist_not_empty(%22%7B%7D/%7B%7D%22.format(project_path , %22requirements.txt%22))%0A%0A  try:%0A    for filename in os.listdir(%22%7B%7D/plays%22.format(project_path)):%0A      _check_file_exist_not_empty(%22%7B%7D/plays/%7B%7D%22.format(project_path , filename))%0A%0A  except (AssertionError, OSError) as e:%0A    code_return = 2%0A    print e%0A%0A
 def chec
@@ -2059,18 +2059,26 @@
 t_path%0A 
- 
 %0A
+  try:%0A  
   for fi
@@ -2131,24 +2131,26 @@
 ect_path)):%0A
+  
     current_
@@ -2208,25 +2208,16 @@
 lename)%0A
-    try:%0A
       en
@@ -2669,19 +2669,16 @@
 ode = 2%0A
-%0A  
   except
@@ -2692,24 +2692,33 @@
 or, KeyError
+, OSError
 ) as e:%0A    
@@ -2713,26 +2713,24 @@
 ) as e:%0A    
- 
- 
 print %22Error
@@ -2735,75 +2735,79 @@
 or: 
-unexpected error when open the file %7B%7D.%22.filename%0A      sys.exit(2)
+folder %7B%7D/env_vars is empty .%22.format(project_path)%0A    return_code = 2
 %0A   
@@ -2900,16 +2900,40 @@
  Say :%22%0A
+  check_default_files()%0A
   check_

check plays files