# Path: performanceplatform/collector/crontab.py
# def remove_existing_crontab_for_app(crontab, unique_id):
#     new_crontab = []
#     should_append = True
#     for line in crontab:
#         if line == crontab_begin_comment(unique_id):
#             should_append = False
#         if should_append:
#             new_crontab.append(line)
#         if line == crontab_end_comment(unique_id):
#             should_append = True
#     return new_crontab
# 
# Path: performanceplatform/collector/crontab.py
# NUMBER_OF_HOSTS = 2
# class ParseError(StandardError):
# def crontab_begin_comment(unique_id):
# def crontab_end_comment(unique_id):
# def remove_existing_crontab_for_app(crontab, unique_id):
# def parse_job_line(line):
# def skip_job(counter):
# def generate_crontab(current_crontab, path_to_jobs, path_to_app, unique_id):


import subprocess
import sys
from nose.tools import *
from hamcrest import *
from performanceplatform.collector.crontab import remove_existing_crontab_for_app
from tests.performanceplatform.collector.tools import temp_file
from performanceplatform.collector import crontab
from mock import patch
        self.mock_gethostname.return_value = 'nonnumerichostname'

    def run_crontab_script(self, current_crontab, path_to_app, path_to_jobs,
                           unique_id):
        with temp_file(current_crontab) as stdin_path:
            args = [
                sys.executable,
                '-m', 'performanceplatform.collector.crontab',
                path_to_app, path_to_jobs, unique_id
            ]
            # Bleh Python 2.6 :(
            proc = subprocess.Popen(args,
                                    stdin=open(stdin_path),
                                    stderr=subprocess.STDOUT,
                                    stdout=subprocess.PIPE)
            output = proc.communicate()
            if proc.returncode != 0:
                raise ProcessFailureError(
                    proc.returncode, ' '.join(args), output=output[0])
            return output[0]

    def test_happy_path(self):
        with temp_file('') as path_to_jobs:
            output = self.run_crontab_script(
                'current crontab', '/path/to/app', path_to_jobs, 'some_id')
            assert_that(output.strip(),
                        is_('current crontab'))

    def test_failure_results_in_non_zero_exit_status(self):
        assert_raises(ProcessFailureError,

# Path: performanceplatform/collector/crontab.py
# def remove_existing_crontab_for_app(crontab, unique_id):
#     new_crontab = []
#     should_append = True
#     for line in crontab:
#         if line == crontab_begin_comment(unique_id):
#             should_append = False
#         if should_append:
#             new_crontab.append(line)
#         if line == crontab_end_comment(unique_id):
#             should_append = True
#     return new_crontab
# 
# Path: performanceplatform/collector/crontab.py
# NUMBER_OF_HOSTS = 2
# class ParseError(StandardError):
# def crontab_begin_comment(unique_id):
# def crontab_end_comment(unique_id):
# def remove_existing_crontab_for_app(crontab, unique_id):
# def parse_job_line(line):
# def skip_job(counter):
# def generate_crontab(current_crontab, path_to_jobs, path_to_app, unique_id):


# Path: tests/performanceplatform/collector/test_crontab.py
import subprocess
import sys
from nose.tools import *
from hamcrest import *
from performanceplatform.collector.crontab import remove_existing_crontab_for_app
from tests.performanceplatform.collector.tools import temp_file
from performanceplatform.collector import crontab
from mock import patch

        self.mock_gethostname.return_value = 'nonnumerichostname'

    def run_crontab_script(self, current_crontab, path_to_app, path_to_jobs,
                           unique_id):
        with temp_file(current_crontab) as stdin_path:
            args = [
                sys.executable,
                '-m', 'performanceplatform.collector.crontab',
                path_to_app, path_to_jobs, unique_id
            ]
            # Bleh Python 2.6 :(
            proc = subprocess.Popen(args,
                                    stdin=open(stdin_path),
                                    stderr=subprocess.STDOUT,
                                    stdout=subprocess.PIPE)
            output = proc.communicate()
            if proc.returncode != 0:
                raise ProcessFailureError(
                    proc.returncode, ' '.join(args), output=output[0])
            return output[0]

    def test_happy_path(self):
        with temp_file('') as path_to_jobs:
            output = self.run_crontab_script(
                'current crontab', '/path/to/app', path_to_jobs, 'some_id')
            assert_that(output.strip(),
                        is_('current crontab'))

    def test_failure_results_in_non_zero_exit_status(self):
        assert_raises(ProcessFailureError,

                      self.run_crontab_script,