# Path: pipelines/pipeline/task.py
# class TaskResult(dict):
#     def __init__(self, status, message='', data={}, return_obj={}):
#         self['status'] = status
#         self['message'] = message
#         self['data'] = data
#         self['return_obj'] = return_obj
# 
#     @property
#     def status(self):
#         return self.get('status')
# 
#     @property
#     def message(self):
#         return self.get('message')
# 
#     @property
#     def data(self):
#         return self.get('data')
# 
#     @property
#     def return_obj(self):
#         return self.get('return_obj')
# 
#     def is_successful(self):
#         return self.status == EXECUTION_SUCCESSFUL
# 
# EXECUTION_SUCCESSFUL = 0
# 
# Path: pipelines/plugins/python_executor.py
# class PythonExecutor(BashExecutor):
#     hook_prefix = 'python'
#     hooks = ('execute',)
# 
#     def _validate_args_dict(self, args_dict):
#         if 'file' not in args_dict and 'script' not in args_dict:
#             raise PluginError('PythonExecutor requires either "file" or "script" arguments. Given: {}'.format(args_dict))
# 
#         if args_dict.get('file') and args_dict.get('script'):
#             raise PluginError(
#                 'PythonExecutor got both "file" and "script" aguments. Only one is supported.')
# 
#         if args_dict.get('workspace') and args_dict.get('script'):
#             raise PluginError('PythonExecutor requires either "file" or "script" arguments. Given: {}'.format(args_dict))
# 
#     def execute(self, args_dict):
#         self._validate_args_dict(args_dict)
#         workdir = args_dict.get('workdir')
#         script = args_dict.get('script')
#         file = args_dict.get('file')
#         virtualenv = args_dict.get('virtualenv')
# 
#         bash_input = ''
#         if workdir:
#             bash_input += 'cd {}\n'.format(workdir)
# 
#         if virtualenv:
#             activate_path = path.join(virtualenv, 'bin', 'activate')
#             if not path.exists(activate_path) or not path.isfile(activate_path):
#                 raise PluginError('Python virtualenv doesn\'t exist: {}'.format(activate_path))
# 
#             bash_input += 'source {}\n'.format(activate_path)
# 
#         filename = file
# 
#         if script:
#             f = NamedTemporaryFile(delete=False)
# 
#             f.write(script)
#             log.debug('Wrote script to file {}, {}'.format(f.name, script))
#             filename = f.name
#             f.close()
# 
#         bash_input += 'python {}'.format(filename)
#         log.debug('Running python script: {}'.format(bash_input))
# 
#         if self.dry_run:
#             return TaskResult(EXECUTION_SUCCESSFUL)
# 
#         return_obj=None
#         try:
#             stdout, return_obj = self._run_bash(bash_input)
#             status = EXECUTION_SUCCESSFUL
#         except BashExecuteError as e:
#             status = EXECUTION_FAILED
#             stdout = e.data.get('stdout')
#         print 'Finished, stdout: %s' % (stdout)
# 
#         return TaskResult(status, 'Execution finished', data={'output': stdout}, return_obj=return_obj)
# 
# Path: pipelines/utils.py
# def conf_logging():
#     logger = logging.getLogger('pipelines')
#     logger.setLevel(logging.DEBUG)
#     formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
#     logger.propagate = False
# 
#     ch = logging.StreamHandler()
#     ch.setFormatter(formatter)
#     ch.setLevel(logging.DEBUG)
# 
#     logger.addHandler(ch)


import os
import unittest
from unittest.case import TestCase
from pipelines.pipeline.task import TaskResult, EXECUTION_SUCCESSFUL
from pipelines.plugins.python_executor import PythonExecutor
from pipelines.utils import conf_logging


conf_logging()

class TestPythonExecutor(TestCase):

    def test_basic_script(self):
        print 'Running test_basic_script'
        executor = PythonExecutor()
        args = {
            'script': 'print "test"'
        }
        res = executor.execute(args)
        self.assertIsInstance(res, TaskResult)

# Path: pipelines/pipeline/task.py
# class TaskResult(dict):
#     def __init__(self, status, message='', data={}, return_obj={}):
#         self['status'] = status
#         self['message'] = message
#         self['data'] = data
#         self['return_obj'] = return_obj
# 
#     @property
#     def status(self):
#         return self.get('status')
# 
#     @property
#     def message(self):
#         return self.get('message')
# 
#     @property
#     def data(self):
#         return self.get('data')
# 
#     @property
#     def return_obj(self):
#         return self.get('return_obj')
# 
#     def is_successful(self):
#         return self.status == EXECUTION_SUCCESSFUL
# 
# EXECUTION_SUCCESSFUL = 0
# 
# Path: pipelines/plugins/python_executor.py
# class PythonExecutor(BashExecutor):
#     hook_prefix = 'python'
#     hooks = ('execute',)
# 
#     def _validate_args_dict(self, args_dict):
#         if 'file' not in args_dict and 'script' not in args_dict:
#             raise PluginError('PythonExecutor requires either "file" or "script" arguments. Given: {}'.format(args_dict))
# 
#         if args_dict.get('file') and args_dict.get('script'):
#             raise PluginError(
#                 'PythonExecutor got both "file" and "script" aguments. Only one is supported.')
# 
#         if args_dict.get('workspace') and args_dict.get('script'):
#             raise PluginError('PythonExecutor requires either "file" or "script" arguments. Given: {}'.format(args_dict))
# 
#     def execute(self, args_dict):
#         self._validate_args_dict(args_dict)
#         workdir = args_dict.get('workdir')
#         script = args_dict.get('script')
#         file = args_dict.get('file')
#         virtualenv = args_dict.get('virtualenv')
# 
#         bash_input = ''
#         if workdir:
#             bash_input += 'cd {}\n'.format(workdir)
# 
#         if virtualenv:
#             activate_path = path.join(virtualenv, 'bin', 'activate')
#             if not path.exists(activate_path) or not path.isfile(activate_path):
#                 raise PluginError('Python virtualenv doesn\'t exist: {}'.format(activate_path))
# 
#             bash_input += 'source {}\n'.format(activate_path)
# 
#         filename = file
# 
#         if script:
#             f = NamedTemporaryFile(delete=False)
# 
#             f.write(script)
#             log.debug('Wrote script to file {}, {}'.format(f.name, script))
#             filename = f.name
#             f.close()
# 
#         bash_input += 'python {}'.format(filename)
#         log.debug('Running python script: {}'.format(bash_input))
# 
#         if self.dry_run:
#             return TaskResult(EXECUTION_SUCCESSFUL)
# 
#         return_obj=None
#         try:
#             stdout, return_obj = self._run_bash(bash_input)
#             status = EXECUTION_SUCCESSFUL
#         except BashExecuteError as e:
#             status = EXECUTION_FAILED
#             stdout = e.data.get('stdout')
#         print 'Finished, stdout: %s' % (stdout)
# 
#         return TaskResult(status, 'Execution finished', data={'output': stdout}, return_obj=return_obj)
# 
# Path: pipelines/utils.py
# def conf_logging():
#     logger = logging.getLogger('pipelines')
#     logger.setLevel(logging.DEBUG)
#     formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
#     logger.propagate = False
# 
#     ch = logging.StreamHandler()
#     ch.setFormatter(formatter)
#     ch.setLevel(logging.DEBUG)
# 
#     logger.addHandler(ch)


# Path: test/plugins/test_python.py
import os
import unittest
from unittest.case import TestCase
from pipelines.pipeline.task import TaskResult, EXECUTION_SUCCESSFUL
from pipelines.plugins.python_executor import PythonExecutor
from pipelines.utils import conf_logging



conf_logging()

class TestPythonExecutor(TestCase):

    def test_basic_script(self):
        print 'Running test_basic_script'
        executor = PythonExecutor()
        args = {
            'script': 'print "test"'
        }
        res = executor.execute(args)
        self.assertIsInstance(res, TaskResult)

        self.assertEqual(res.status, EXECUTION_SUCCESSFUL)