#!/usr/bin/env python

# Copyright (C) 2011-2012 CRS4.
#
# This file is part of Seal.
#
# Seal is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Seal is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Seal.  If not, see <http://www.gnu.org/licenses/>.

#########################################################
# You need a working Hadoop cluster to use this.
#########################################################


import site
import os
import sys
import shutil

IntegrationTestDir = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
site.addsitedir(IntegrationTestDir)

from seal_integration_test import SealIntegrationTest, SealTestException
import pydoop.hdfs as hdfs

class TestSeparateReads(SealIntegrationTest):
	def __init__(self):
		super(TestSeparateReads, self).__init__( os.path.realpath(os.path.dirname(__file__)) )

	def setup(self):
		super(TestSeparateReads, self).setup()
		hdfs.put(os.path.join(self.test_dir, "sample_sheet1.csv"), self.make_hdfs_test_path())

	def process_output(self):
		# remove the logs directory so that we can test via a recursive diff
		shutil.rmtree( os.path.join(self.output_dir, '_logs'), ignore_errors=True )
		self.run_cmd_and_output_if_failure([ "diff", "-r", self.make_local_expected_output_path(), self.output_dir])

	def run_program(self, hdfs_input, hdfs_output):
		self.run_cmd_and_output_if_failure([ "%s/scripts/seal" % self.seal_dir, "demux",
		  "--num-reducers", "1",
		  "--separate-reads",
		  "--sample-sheet", os.path.join(self.make_hdfs_test_path(), "sample_sheet1.csv"),
		  hdfs_input, hdfs_output])

if __name__ == '__main__':
	success = TestSeparateReads().test_method()
	sys.exit( 0 if success else 1 )
