import subprocess
from distutils.command.build import build as _build
import setuptools


with open("README.md", "r") as fh:
	long_description = fh.read()


# This class handles the pip install mechanism.
class build(_build):  # pylint: disable=invalid-name
  sub_commands = _build.sub_commands + [("CustomCommands", None)]

CUSTOM_COMMANDS = [
	["libdir=`ls -1 build|grep \"lib\"`; cd build/$libdir/smfishHmrf/graphColoring && javac GraphColoring.java"]]

class CustomCommands(setuptools.Command):
  """A setuptools Command class able to run arbitrary commands."""

  def initialize_options(self):
    pass

  def finalize_options(self):
    pass

  def RunCustomCommand(self, command_list):
    print "Running command: %s" % command_list
    p = subprocess.Popen(
        command_list,
        stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    # Can use communicate(input='y\n'.encode()) if the command run requires
    # some confirmation.
    stdout_data, _ = p.communicate()
    print "Command output: %s" % stdout_data
    if p.returncode != 0:
      raise RuntimeError(
          "Command %s failed: exit code: %s" % (command_list, p.returncode))

  def run(self):
    for command in CUSTOM_COMMANDS:
      self.RunCustomCommand(command)



setuptools.setup(
	name="smfishHmrf",
	version="1.0.8.1",
	author="Qian Zhu",
	author_email="zqian@jimmy.harvard.edu",
	description="HMRF for spatial transcriptomic data",
	long_description="This tool infers spatial domain-like patterns from spatial gene expression data that are generated by multiplexed single molecule FISH or by other spatial profiling technologies",
	long_description_content_type="text/markdown",
	url="https://bitbucket.org/qzhu/smfish-hmrf",
	packages=setuptools.find_packages(),
	classifiers=(
		"Programming Language :: Python :: 2.7",
		"License :: OSI Approved :: MIT License",
		"Operating System :: OS Independent",
	),
	package_data={"smfishHmrf":  ["graphColoring/Backtracking.java", 
		"graphColoring/Clique.java", "graphColoring/Constants.java", 
		"graphColoring/IteratedGreedy.java", "graphColoring/LocalSearch.java", 
		"graphColoring/MaxClique.java", "graphColoring/Node.java",
		"graphColoring/SortedListNode.java", "graphColoring/Graph.java", 
		"graphColoring/GraphReader.java", "graphColoring/GraphColoring.java", 
		"graphColoring/Clique.class", 
		"graphColoring/Constants.class",
		"graphColoring/Graph.class",
		"graphColoring/GraphColoring.class",
		"graphColoring/GraphColoring$PossibleColorsComparator.class",
		"graphColoring/GraphReader.class",
		"graphColoring/IteratedGreedy.class",
		"graphColoring/IteratedGreedy$ColorClass.class",
		"graphColoring/IteratedGreedy$DecreasingComparator.class",
		"graphColoring/IteratedGreedy$IncreasingComparator.class",
		"graphColoring/LocalSearch.class",
		"graphColoring/MaxClique.class",
		"graphColoring/Node.class",
		"graphColoring/SortedListNode.class"]},
	install_requires=[
		"scipy", "numpy", "pandas", "seaborn", "matplotlib"],
	cmdclass={
		"build": build,
		"CustomCommands": CustomCommands,
		}	
)
	
