diff --git a/gitlab/after_deploy.sh b/gitlab/after_deploy.sh
index 2cdd86ce291533a287673eb78989ed4b7fccc74e..dc97ec3a5e72f02e23d5efe1fc691bdec613ca2a 100755
--- a/gitlab/after_deploy.sh
+++ b/gitlab/after_deploy.sh
@@ -3,4 +3,4 @@
 
 source $(dirname ${0})/functions.sh
 
-run_cmd rm -rf ${HOME}/.pypirc
+log_info "*** Not yet implemented ***"
diff --git a/gitlab/before_deploy.sh b/gitlab/before_deploy.sh
index 1fd16d3f5efcf9e2153817328ff2935009c815bd..25fc0bab0f3ef5f875ee4619e5849231a6a70856 100755
--- a/gitlab/before_deploy.sh
+++ b/gitlab/before_deploy.sh
@@ -3,5 +3,4 @@
 
 source $(dirname ${0})/functions.sh
 
-run_cmd rm -f ${HOME}/.pypirc
 run_cmd $(dirname ${0})/before_test.sh
diff --git a/gitlab/deploy.sh b/gitlab/deploy.sh
index fafec8f0b1bb3ca80f6e7633aa823404119f688c..142952541069a6f3f7b6a3e979c5537f054e04e2 100755
--- a/gitlab/deploy.sh
+++ b/gitlab/deploy.sh
@@ -3,18 +3,20 @@
 
 source $(dirname ${0})/functions.sh
 
-dot_pypirc
+lock_pypirc
 
-setup register --repository staging
-setup check sdist --formats zip upload --repository staging
+setup_deploy register --repository staging
+setup_deploy check sdist --formats zip upload --repository staging
 
 # if that worked, uploads documentation to pythonhosted if any exists
 if [ -d sphinx ]; then
   log_info "Uploading documentation to ${PYPISERVER} on behalf of ${PYPIUSER}..."
-  setup upload_docs --upload-dir sphinx --repository production
+  setup_deploy upload_docs --upload-dir sphinx --repository production
 fi
 
 # if that worked, uploads source package to the production index
 log_info "Uploading package to ${PYPISERVER} on behalf of ${PYPIUSER}..."
-setup register --repository production
-setup check sdist --formats zip upload --repository production
+setup_deploy register --repository production
+setup_deploy check sdist --formats zip upload --repository production
+
+unlock_pypirc
diff --git a/gitlab/functions.sh b/gitlab/functions.sh
index 840be0535f977f41630f8c574c32d930a51f79c5..3bdf6ab65c279c05b81046eb64881180a9fe0f78 100644
--- a/gitlab/functions.sh
+++ b/gitlab/functions.sh
@@ -16,12 +16,12 @@ log_info() {
 
 
 log_warn() {
-  echo -e "(`date +%T`) \033[1;35mWarning: ${@}\033[0m"
+  echo -e "(`date +%T`) \033[1;35mWarning: ${@}\033[0m" >&2
 }
 
 
 log_error() {
-  echo -e "(`date +%T`) \033[1;31mError: ${@}\033[0m"
+  echo -e "(`date +%T`) \033[1;31mError: ${@}\033[0m" >&2
 }
 
 
@@ -71,20 +71,29 @@ run_cmd() {
 }
 
 
-# Runs setup.py
-setup() {
-  run_cmd ${PREFIX}/bin/python setup.py ${@}
-}
-
-
 # Prepares ~/.pypirc
-dot_pypirc() {
+lock_pypirc() {
+  local lockfile=/var/tmp/pypirc_lock
   local rc=${HOME}/.pypirc
-  log_info "Creating ${rc}..."
-  if [ -e ${rc} ]; then
-    run_cmd rm -f ${rc}
-  fi
-  cat <<EOT >> ${rc}
+  local maxtries=10
+  local try=0
+  local sleeptime=30 #seconds
+
+  while true; do
+    if [[ ${try} -lt ${maxtries} ]]; then
+      ((try++))
+      if ( set -o noclobber; echo "$$" > "${lockfile}") 2> /dev/null; then
+        log_info "Successfully acquired ${lockfile}"
+        echo $$ > ${lockfile}
+        log_info "trapping on ${lockfile}..."
+        trap 'rm -f "${lockfile}"; exit $?' INT TERM EXIT
+
+        # start: protected code
+        log_info "Creating ${rc}..."
+        if [ -e ${rc} ]; then
+          run_cmd rm -f ${rc}
+        fi
+        cat <<EOT >> ${rc}
 [distutils]
 index-servers =
     production
@@ -100,7 +109,49 @@ repository: ${TESTSERVER}
 username: ${PYPIUSER}
 password: ${PYPIPASS}
 EOT
-  run_cmd chmod 600 ${rc}
+        run_cmd chmod 600 ${rc}
+        # end: protected code
+        break
+      else
+        log_warn "${lockfile} exists, owned by process $(cat $lockfile)"
+        log_info "Will retry after a ${sleeptime} seconds sleep (${try}/${maxtries})"
+        run_cmd sleep ${sleeptime}
+      fi
+    else
+      log_error "I already retried deploying ${try} times. Aborting..."
+      log_error "You can retry this step when less packages are building."
+      exit 1
+    fi
+  done
+}
+
+
+# Cleans ~/.pypirc, if the lock file belongs to us
+unlock_pypirc() {
+  local lockfile=/var/tmp/pypirc_lock
+  local rc=${HOME}/.pypirc
+
+  # untrap if lock belongs to the running process
+  if [[ $(cat ${lockfile}) == $$ ]]; then
+    run_cmd rm -r ${lockfile}
+    run_cmd rm -rf ${rc}
+    log_info "$ trap - INT TERM EXIT"
+    trap - INT TERM EXIT
+  fi
+}
+
+
+# Runs setup.py in a deployment context. If fails, tries to unlock
+# the ${HOME}/.pypirc file lock
+setup_deploy() {
+  log_info "$ ${@}"
+  ${PREFIX}/bin/python setup.py ${@}
+  local status=$?
+  if [ ${status} != 0 ]; then
+    log_error "Command Failed \"${@}\""
+    unlock_pypirc #just tries
+    exit ${status}
+  fi
 }