#!/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

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 TestSimpleDemux(SealIntegrationTest):
	def __init__(self):
		super(TestSimpleDemux, self).__init__( os.path.realpath(os.path.dirname(__file__)) )

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


	def process_output(self):
		if os.path.isdir( os.path.join(self.output_dir, "DefaultProject", "csct_xxxxxx") ):
			raise SealTestException("Demux created sample directory csct_xxxxxx but it shouldn't have!")
		if not os.path.isdir( os.path.join(self.output_dir, "DefaultProject", "csct_007107") ):
			raise SealTestException("sample directory doesn't exist")
		self.verify_sorted_output( self.make_local_expected_output_path() , os.path.join(self.output_dir, "DefaultProject", "csct_007107"))

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

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