# Ensure we fail the job if any steps fail.
 set -eu -o pipefail
 
-set -x  # Trace commands for this script to make debugging easier.
-
-ARCHIVE_ARTIFACTS="${ARCHIVE_ARTIFACTS:-}"
-LOGS_SERVER="${LOGS_SERVER:-None}"
-
-if [ "${LOGS_SERVER}" == 'None' ]
-then
-    set +x # Disable trace since we no longer need it
-
+if [[ -z $LOGS_SERVER ]]; then
     echo "WARNING: Logging server not set"
 else
-    NEXUS_URL="${NEXUSPROXY:-$NEXUS_URL}"
-    NEXUS_PATH="${SILO}/${JENKINS_HOSTNAME}/${JOB_NAME}/${BUILD_NUMBER}"
-    BUILD_URL="${BUILD_URL}"
-
-    lftools deploy archives -p "$ARCHIVE_ARTIFACTS" "$NEXUS_URL" "$NEXUS_PATH" "$WORKSPACE"
-    lftools deploy logs "$NEXUS_URL" "$NEXUS_PATH" "$BUILD_URL"
+    nexus_url="${NEXUSPROXY:-$NEXUS_URL}"
+    nexus_path="${SILO}/${JENKINS_HOSTNAME}/${JOB_NAME}/${BUILD_NUMBER}"
 
-    set +x  # Disable trace since we no longer need it.
+    lftools deploy archives -p "${ARCHIVE_ARTIFACTS:-}" "$nexus_url" \
+            "$nexus_path" "$WORKSPACE"
+    lftools deploy logs "$nexus_url" "$nexus_path" "${BUILD_URL:-}"
 
-    echo "Build logs: <a href=\"$LOGS_SERVER/$NEXUS_PATH\">$LOGS_SERVER/$NEXUS_PATH</a>"
+    echo "Build logs: <a href=\"$LOGS_SERVER/$nexus_path\">$LOGS_SERVER/$nexus_path</a>"
 fi
 
 ##############################################################################
 echo "---> python-tools-install.sh"
 
-set -eux -o pipefail
+set -eu -o pipefail
 
-REQUIREMENTS_FILE=$(mktemp /tmp/requirements-XXXX.txt)
+# Generate a list of 'pip' packages pre-build/post-build
+# During post-build, perform a diff on the two lists and copy files to archive directory
+echo "Listing pip packages"
+pip_list_pre=/tmp/pip-list-pre.txt
+pip_list_post=/tmp/pip-list-post.txt
+pip_list_diffs=/tmp/pip-list-diffs.txt
+if [[ -f $pip_list_pre ]]; then
+    pip list > $pip_list_post
+    echo "Compare pip packages before/after..."
+    if diff --suppress-common-lines $pip_list_pre $pip_list_post \
+            | tee $pip_list_diffs; then
+        echo "No diffs" | tee $pip_list_diffs
+    fi
+    mkdir -p $WORKSPACE/archives
+    cp $pip_list_pre $pip_list_post $pip_list_diffs $WORKSPACE/archives
+    rm -rf $pip_list_pre $pip_list_post $pip_list_diffs
+    ls $WORKSPACE/archives
+    # Would just like to 'exit 0' here but we can't because the
+    # log-deploy.sh script is 'appended' to this file and it would not
+    # be executed.
+else
+    pip list > $pip_list_pre
+    # These 'pip installs' only need to be executed during pre-build
 
-# Note: To test lftools master branch change the lftools configuration below in
-#       the requirements file from "lftools[openstack]~=#.##.#" to
-#       git+https://github.com/lfit/releng-lftools.git#egg=lftools[openstack]
+    requirements_file=$(mktemp /tmp/requirements-XXXX.txt)
 
-cat << EOF > "$REQUIREMENTS_FILE"
+    # Note: To test lftools master branch change the lftools configuration below in
+    #       the requirements file from "lftools[openstack]~=#.##.#" to
+    #       git+https://github.com/lfit/releng-lftools.git#egg=lftools[openstack]
+
+    echo "Generating Requirements File"
+    cat << 'EOF' > "$requirements_file"
 lftools[openstack]~=0.23.1
 python-heatclient~=1.16.1
 python-openstackclient~=3.16.0
 niet~=1.4.2 # Extract values from yaml
 EOF
 
-echo "Requirements file"
-echo "-----------------"
-cat "$REQUIREMENTS_FILE"
-
-# Use `python -m pip` to ensure we are using the latest version of pip
-python -m pip install --user --quiet --upgrade pip
-python -m pip install --user --quiet --upgrade setuptools
-python -m pip install --user --quiet --upgrade -r "$REQUIREMENTS_FILE"
-pip freeze
+    # Use `python -m pip` to ensure we are using the latest version of pip
+    python -m pip install --user --quiet --upgrade pip
+    python -m pip install --user --quiet --upgrade setuptools
+    python -m pip install --user --quiet --upgrade -r "$requirements_file"
+    rm -rf $requirements_file
+fi