[buildout]
extends =
  ../bazel/buildout.cfg
  ../git/buildout.cfg
parts =
  tensorflow_tensorboard

[tensorboard-repository]
recipe = plone.recipe.command
stop-on-error = true
repository = https://github.com/tensorflow/tensorboard
commit = ${versions:tensorflow-tensorboard}
location = ${buildout:parts-directory}/${:_buildout_section_name_}
command =
  set -e
  export PATH=${git:location}/bin:$PATH
  ( git clone --quiet -n ${:repository} ${:location}
    cd ${:location}
    git reset --merge ${:commit}
    git apply ${:_profile_base_location_}/0.4.0.patch
  ) || { rm -fr ${:location}; exit 1; }

[tensorflow_tensorboard]
recipe = slapos.recipe.build
egg = ${:_buildout_section_name_}
workdir = ${tensorboard-repository:location}
egg-name = ${:egg}-${versions:tensorflow-tensorboard}-py${python:version}.egg
location = ${buildout:eggs-directory}/${:egg-name}
numpy-python-command = ${buildout:bin-directory}/${numpy-egg:interpreter}
python-lib = ${python:location}/lib
java_home_bin = ${bazel:java_home}/bin
bazel-bin = ${bazel:location}/bin
install =
  import os, shutil
  from setuptools.archive_util import unpack_archive
  workdir = options['workdir']
  env = {'PATH':':'.join([options['java_home_bin'],
                          options['bazel-bin'],
                          os.environ['PATH']]),
         'PYTHON_BIN_PATH':options['numpy-python-command'],
         'PYTHON_LIB_PATH':options['python-lib'],
         }
  bazel_command = ['bazel', 'build', '--spawn_strategy=standalone', '--verbose_failures', '--sandbox_debug', '//tensorboard/pip_package:build_pip_package']
  os.chdir(workdir)
  try:
    call(bazel_command, env=env)
    call(('tensorboard/pip_package/build_pip_package.sh',
          os.path.join(workdir, 'dist')), env=env)
    unpack_archive(os.path.join('dist', options['egg-name']), location)
  finally:
    for d in 'dist',:
      if os.path.exists(d):
        shutil.rmtree(d)

[versions]
tensorflow-tensorboard = 0.4.0
