import os
import sys
from subprocess import Popen, PIPE

from config.settings import *
from settings import *

def load_logs():

    e = { }
    for k, v in os.environ.iteritems(): e[k] = v
    if 'PYTHONPATH' not in e:
        e['PYTHONPATH'] = PROJECT_PATH
    else:
        e['PYTHONPATH'] = PROJECT_PATH + ':' + e['PYTHONPATH']
    
    logs_error_filename = os.path.join(LOGS_PATH, 'rejects')
    
    filter = Popen([ 'python', SCRIPTS_DIR_PATH + '/filter_logs.py', logs_error_filename ], 
        stdout=PIPE, stderr=sys.stderr,
        env = e
        )
    copy = Popen([ 'python', SCRIPTS_DIR_PATH + '/copy_logs.py' ], 
        stdin=filter.stdout, stdout=sys.stdout, stderr=sys.stderr,
        env = e
        )
    
    filter.stdout.close()   # Magic!
    
    copy.communicate()