From 1f302b8259acb1128004306f8ac46dac88c9ce16 Mon Sep 17 00:00:00 2001
From: Andre Anjos <andre.dos.anjos@gmail.com>
Date: Sat, 24 Feb 2018 10:30:31 +0100
Subject: [PATCH] Closes #69 and #70

---
 gitlab/before_build.sh |  9 +++--
 gitlab/build.sh        | 16 +++------
 gitlab/deploy.sh       |  3 +-
 gitlab/functions.sh    | 77 ++++++++++++++++++++++++++++++------------
 4 files changed, 66 insertions(+), 39 deletions(-)

diff --git a/gitlab/before_build.sh b/gitlab/before_build.sh
index a9e674a..3adced1 100755
--- a/gitlab/before_build.sh
+++ b/gitlab/before_build.sh
@@ -28,11 +28,10 @@ ssl_verify: false #!final
 channels: #!final
 EOF
 
-if [ -z "${CI_COMMIT_TAG}" ]; then
-  echo "  - ${CONDA_BETA_CHANNEL}" >> ${CONDARC}
-fi
-echo "  - ${CONDA_CHANNEL}" >> ${CONDARC}
-echo "  - defaults" >> ${CONDARC}
+set_conda_channels ${VISIBILITY} ${CI_COMMIT_TAG}
+for k in "${CONDA_CHANNELS[@]}"; do
+  echo "  - ${DOCSERVER}/${k}" >> ${CONDARC}
+done
 
 # displays contents of our configuration
 echo "Contents of \`${CONDARC}':"
diff --git a/gitlab/build.sh b/gitlab/build.sh
index 4347b56..e1bd845 100755
--- a/gitlab/build.sh
+++ b/gitlab/build.sh
@@ -7,24 +7,16 @@ run_cmd source ${CONDA_ROOT}/etc/profile.d/conda.sh
 run_cmd conda activate base
 export_env PATH
 
-run_cmd mkdir -p ./_ci/${OS_SLUG}/${PYTHON_VERSION}
-
 if [ -z "${BOB_BUILD_NUMBER}" ]; then
-  if [ -z "${CI_COMMIT_TAG}" ]; then
-    run_cmd ${CONDA_ROOT}/bin/python _ci/channel_support.py ${CONDA_BETA_CHANNEL} ${CI_PROJECT_NAME} ${BOB_PACKAGE_VERSION} ${PYTHON_VERSION} -u --log ./_ci/${OS_SLUG}/${PYTHON_VERSION}/build_number.txt
-  else
-    run_cmd ${CONDA_ROOT}/bin/python _ci/channel_support.py ${CONDA_CHANNEL} ${CI_PROJECT_NAME} ${BOB_PACKAGE_VERSION} ${PYTHON_VERSION} -u --log ./_ci/${OS_SLUG}/${PYTHON_VERSION}/build_number.txt
-  fi
-
-  BOB_BUILD_NUMBER=`head -n 1 ./_ci/${OS_SLUG}/${PYTHON_VERSION}/build_number.txt | tr -d '\n'`
-  export_env BOB_BUILD_NUMBER
+  set_conda_channels ${VISIBILITY} ${CI_COMMIT_TAG}
+  set_next_bob_build_number ${CONDA_CHANNELS[0]}
 fi
 
 # copy the recipe_append.yaml over before build
-run_cmd cp ./_ci/recipe_append.yaml conda/recipe_append.yaml
+run_cmd cp ${SCRIPTS_DIR}/recipe_append.yaml conda/recipe_append.yaml
 
 BLDOPT="--no-anaconda-upload"
-BLDOPT="${BLDOPT} --variant-config-files _ci/conda_build_config.yaml"
+BLDOPT="${BLDOPT} --variant-config-files ${SCRIPTS_DIR}/conda_build_config.yaml"
 BLDOPT="${BLDOPT} --python=${PYTHON_VERSION}"
 if [ -n "${BOB_TEST_ONLY}" ]; then
   BLDOPT="${BLDOPT} --test"
diff --git a/gitlab/deploy.sh b/gitlab/deploy.sh
index 12bff5d..dbe2feb 100755
--- a/gitlab/deploy.sh
+++ b/gitlab/deploy.sh
@@ -3,7 +3,8 @@
 
 source $(dirname ${0})/functions.sh
 
-deploy_conda_packages
+set_conda_channels ${VISIBILITY} ${CI_COMMIT_TAG}
+deploy_conda_packages ${CONDA_CHANNELS[0]} ${CI_PROJECT_NAME}
 
 # upload the docs from the sphinx folder (usually an artifact of Linux Python
 # 3.6 builds)
diff --git a/gitlab/functions.sh b/gitlab/functions.sh
index 894f26a..4762bb3 100644
--- a/gitlab/functions.sh
+++ b/gitlab/functions.sh
@@ -84,6 +84,19 @@ check_env() {
 }
 
 
+# Checks a given environment variable array is set (non-zero size)
+check_array_env() {
+  if [ -z "${1+abc}" ]; then
+    log_error "Variable ${1} is undefined - aborting...";
+    exit 1
+  else
+    for i in "${!foo[@]}"; do
+      log_info "${1}[${i}]=${!1[${i}]}";
+    done
+  fi
+}
+
+
 # Exports a given environment variable, verbosely
 export_env() {
   if [ -z "${1+abc}" ]; then
@@ -333,23 +346,56 @@ install_miniconda() {
 }
 
 
-# deployes all conda packages built up to now
+# deploys all conda packages built up to now
+# $1: the channel to upload to
+# $2: prefix of the package name to upload (may be empty)
 deploy_conda_packages() {
   # Uploads all the built packages
   for os in "osx-64" "noarch" "linux-64"; do
-    for f in ${CONDA_ROOT}/conda-bld/${os}/*.tar.bz2; do
-      if [[ -f $f ]]; then
-        if [ -z "${CI_COMMIT_TAG}" ]; then #beta
-          dav_check_upload "${f}" "private-upload/conda/${os}/"
-        else
-          dav_check_upload "${f}" "public-upload/conda/label/main/${os}/"
-        fi
+    for f in ${CONDA_ROOT}/conda-bld/${os}/${2}*.tar.bz2; do
+      if [[ -f "${f}" ]]; then
+        dav_check_upload "${f}" "${1}/${os}/"
       fi
     done
   done
 }
 
 
+# sets BOB_BUILD_NUMBER to the value of the next build number for the package
+# with the given specifications
+# $1: the channel to lookup
+set_next_bob_build_number() {
+  log_info "$ ${CONDA_ROOT}/bin/python ${SCRIPTS_DIR}/channel_support.py ${DOCSERVER}/${1} ${CI_PROJECT_NAME} ${BOB_PACKAGE_VERSION} ${PYTHON_VERSION}"
+  BOB_BUILD_NUMBER=$(${CONDA_ROOT}/bin/python ${SCRIPTS_DIR}/channel_support.py ${DOCSERVER}/${1} ${CI_PROJECT_NAME} ${BOB_PACKAGE_VERSION} ${PYTHON_VERSION})
+  export_env BOB_BUILD_NUMBER
+}
+
+
+# sets CONDA_CHANNELS to the list of conda channels that should be considered
+# $1: visibility (maybe either "public" or "private")
+# $2: typically, the value of ${CI_COMMIT_TAG} or empty
+# given the current visibility/tagging conditions of the package.
+set_conda_channels() {
+  CONDA_CHANNELS=() #resets bash array
+  if [ -z "${2}" ]; then
+    # there are no tags on this build - add beta channels
+    if [ "${1}" == "private" ]; then
+      # adds private beta builds (only if the package is also private)
+      CONDA_CHANNELS+=('private/conda/label/beta')
+    fi
+    # adds public stable builds (always)
+    CONDA_CHANNELS+=('public/conda/label/beta')
+  fi
+  if [ "${1}" == "private" ]; then
+    # adds private stable builds (only if the package is also private)
+    CONDA_CHANNELS+=('private/conda')
+  fi
+  # adds public stable builds (always)
+  CONDA_CHANNELS+=('public/conda')
+  check_array_env CONDA_CHANNELS
+}
+
+
 check_env PYTHON_VERSION
 check_env CI_PROJECT_URL
 check_env CI_PROJECT_DIR
@@ -373,15 +419,6 @@ check_env IS_MASTER
 
 if [ -z "${DOCSERVER}" ]; then
   DOCSERVER=http://www.idiap.ch
-  export_env DOCSERVER
-fi
-
-if [ -z "${CONDA_CHANNEL}" ]; then
-  CONDA_CHANNEL="${DOCSERVER}/public/conda/label/main"
-fi
-
-if [ -z "${CONDA_BETA_CHANNEL}" ]; then
-  CONDA_BETA_CHANNEL="${DOCSERVER}/private/conda"
 fi
 
 # Sets up the location of our rc file for conda
@@ -394,11 +431,9 @@ fi
 TESTSERVER=https://testpypi.python.org/legacy/
 
 export_env OS_SLUG
-check_env DOCSERVER
+export_env DOCSERVER
 check_env TESTSERVER
 check_env CONDA_ROOT
-check_env CONDA_CHANNEL
-check_env CONDA_BETA_CHANNEL
 export_env CONDARC
 
 # Setup default database server
@@ -471,5 +506,5 @@ export_env LC_ALL
 
 # Set up the location of matplotlibrc:
 # https://matplotlib.org/users/customizing.html#the-matplotlibrc-file
-MATPLOTLIBRC="${CI_PROJECT_DIR}/_ci"
+MATPLOTLIBRC="${SCRIPTS_DIR}"
 export_env MATPLOTLIBRC
-- 
GitLab