[buildout]
extends =
  ../../stack/slapos.cfg
  ../gcc/buildout.cfg
  ../cython/buildout.cfg
  ../scipy/buildout.cfg
parts =
  python-cocoapi-build-install-egg

[python-cocoapi-repository]
recipe = plone.recipe.command
stop-on-error = true
repository = https://github.com/cocodataset/cocoapi.git
tag = master
git-binary = ${git:location}/bin/git
patch-binary = ${patch:location}/bin/patch
location = ${buildout:parts-directory}/${:_buildout_section_name_}
command = export HOME=${:location}; (${:git-binary} clone --quiet -b ${:tag} ${:repository} ${:location}; cd ${:location}; ${:patch-binary} -p1 -d . < ${:_profile_base_location_}/setup.py.patch ) || (rm -fr ${:location}; exit 1)

[python-cocoapi-build-interpreter]
recipe = zc.recipe.egg
initialization =
  import scipy.spatial.ckdtree # load our own libstdc++ explicitly at the very beginning
eggs =
  setuptools
  ${cython:egg}
  ${scipy:egg}
  ${numpy:egg}
interpreter = python-cocoapi-build-interpreter
scripts = python-cocoapi-build-interpreter
location = ${buildout:parts-directory}/${:_buildout_section_name_}

[python-cocoapi-build]
recipe = slapos.recipe.build
location = ${buildout:parts-directory}/${:_buildout_section_name_}
workdir = ${python-cocoapi-repository:location}/PythonAPI
python-bin = ${buildout:bin-directory}/${python-cocoapi-build-interpreter:interpreter}
gcc-location = ${gcc:location}
script =
  os.makedirs(location)
  workdir = self.options['workdir']
  python_bin = self.options['python-bin']
  gcc_location = self.options['gcc-location']
  env = {'PATH':':'.join([gcc_location+'/bin',
                          os.environ['PATH']]),
         'CC':gcc_location+'/bin/gcc',
         'CXX':gcc_location+'/bin/g++',
         }
  call([python_bin, 'setup.py', 'build_ext'], cwd=workdir, env=env)
  call([python_bin, 'setup.py', 'bdist_egg'], cwd=workdir, env=env)

[python-cocoapi-build-install-egg]
recipe = slapos.recipe.build
unzip-binary = ${unzip:location}/bin/unzip
python-cocoapi-repository-path = ${python-cocoapi-repository:location}
location = ${buildout:parts-directory}/${:_buildout_section_name_}
need-python-cocoapi-build = ${python-cocoapi-build:location}
egg = pycocotools
script =
  os.makedirs(location)
  workdir = self.options['python-cocoapi-repository-path']+'/PythonAPI'
  egg_name = 'pycocotools-2.0-py2.7-linux-x86_64.egg'
  dist_dir = os.path.join(workdir, 'dist')
  dest_dir = os.path.join(self.buildout['buildout']['eggs-directory'], egg_name)
  call([self.options['unzip-binary'], '-o', os.path.join(dist_dir, egg_name), '-d', dest_dir])
