>From 82ea6de87057e3be3ac6ba296925a71634cc9647 Mon Sep 17 00:00:00 2001
From: Arun Babu Neelicattu <abn@redhat.com>
Date: Thu, 10 Oct 2013 17:55:35 +1000
Subject: [PATCH 2/2] Make things hacking friendly

- added common dev envirionment ignores
- added scripts to help developers create/activate virtualenvs for
  development and testing
- added run-tests script to help run tests across python versions
- updated HACKING file
---
 .gitignore               | 40 ++++++++++++++++++++++++++++++--
 HACKING                  | 45 ++++++++++++++++++++++++++++++------
 contrib/activate-dev-env | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
 contrib/run-tests        | 21 +++++++++++++++++
 4 files changed, 156 insertions(+), 9 deletions(-)
 create mode 100644 contrib/activate-dev-env
 create mode 100755 contrib/run-tests

diff --git a/.gitignore b/.gitignore
index 7608296..805c105 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,42 @@
-*.pyc
+*.py[cod]
+
+# vim
 *.swp
-MANIFEST
+
+# C extensions
+*.so
+
+# Packages
+*.egg
+*.egg-info
 dist
 build
+eggs
+parts
+bin
+var
+sdist
+develop-eggs
+.installed.cfg
+lib
+lib64
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
 .coverage
+.tox
+nosetests.xml
+
+# Translations
+*.mo
+
+# Developer
+.mr.developer.cfg
+.project
+.pydevproject
+
+# Misc
+MANIFEST
+dev-env-*
diff --git a/HACKING b/HACKING
index d269b33..258edaf 100644
--- a/HACKING
+++ b/HACKING
@@ -1,23 +1,54 @@
-If submitting any patch, please verify that no new pylint or pep8 violations
-are introduced:
+Hackers Guide
+=============
 
-  python setup.py pylint
+1. Setting up the environment
+-----------------------------
+To start development, you can set-up/activate a virtual environment by using the 
+following command:
+    source contrib/activate-dev-env [python2|python3]
 
-And ensure that the basic unit test suite does not regress
+Note: Providing no arguments will attempt to use python2.
 
-  python setup.py test
+# Manually activating an environment
+    source dev-env-${NAME}/bin/activate
 
+2. Running tests
+----------------
+Once you have already activated an environment, you can use the following.
+
+# Basic unit test suite
+    python setup.py test
+
+# Functional tests
 There are more comprehensive tests that are disabled by default. Readonly
 functional tests that run against several public bugzilla instances. No
 login account is required:
 
-  python setup.py test --ro-functional
+    python setup.py test --ro-functional
 
 And read/write functional tests. These currently run against the test
 bugzilla instance at partner-bugzilla.redhat.com, and requires a valid
 login there:
 
-  python setup.py test --rw-functional
+    python setup.py test --rw-functional
+
+Note: Before running rw-functional tests, make sure you have logged using:
+    python bugzilla-cli --bugzilla="https://partner-bugzilla.redhat.com/xmlrpc.cgi" --user=$USER login
+
+3. pylint and pep8
+------------------
+To test for pylint or pep8 violations, you can run:
+    python setup.py pylint
+
+Note: This expects that you already have pylint and pep8 (installed when 
+setting up virtualenv) isntalled.
+
+4. Patch Submission
+-------------------
+If you are submitting a patch, ensure the following:
+    [REQ] verify that no new pylint or pep8 violations
+    [REQ] run basic unit test suite across all python versions:
+            bash contrib/run-tests
 
 Running any of the functional tests is not a requirement for patch submission,
 but please give them a go if you are interested.
diff --git a/contrib/activate-dev-env b/contrib/activate-dev-env
new file mode 100644
index 0000000..d3c90d3
--- /dev/null
+++ b/contrib/activate-dev-env
@@ -0,0 +1,59 @@
+# Helper script to create (if required) and activate a virtual environment for 
+# a specified PYTHON_BINARY (python|python2|python3) and install dependencies 
+# required for testing and development.
+#
+# Usage: source $0 [PYTHON_BINARY]
+
+# use env python by default
+PYTHON_BIN=python2
+VIRTIALENV_CMD=virtualenv
+SETUP_PY="setup.py"
+DEV_MODULE_DIR="bugzilla"
+DIR_PREFIX="dev-env"
+
+if [ $# -ne 0 ]; then
+    PYTHON_BIN=$1
+fi
+
+function activate()
+{
+    DIR_NAME=$1
+    echo "INFO: Activating virtualenv at ${DIR_NAME}"
+    source ${DIR_NAME}/bin/activate
+}
+
+function virtualize()
+{
+    PROMPT=$(basename ${PYTHON_BIN})
+    DIR_NAME="${DIR_PREFIX}-${PROMPT}"
+    
+    if [ ! -d ${DIR_NAME} ]; then
+        echo "INFO: Creating virtualenv ${DIR_NAME}"
+        ${VIRTIALENV_CMD} -p $(which ${PYTHON_BIN}) --prompt "${PROMPT}" ${DIR_NAME}
+    fi
+
+    if [ -d ${DIR_NAME} ]; then
+        activate $DIR_NAME
+        pip install -qr requirements.txt
+        pip install -qr test-requirements.txt
+    else
+        echo >&2 "ERROR: Failed to activate virtualenv."
+    fi
+}
+
+
+if ! command -v ${VIRTIALENV_CMD} >/dev/null 2>&1; then
+    # We require virtuaenv to be available
+    echo >&2 "ERROR: virtualenv command not found"
+elif ! command -v $PYTHON_BIN >/dev/null 2>&1; then
+    # Ensure PYTHON_BIN is valid
+    echo >&2 "ERROR: ${PYTHON_BIN} command not found"
+elif ! ${PYTHON_BIN} --version 2>&1 | grep -q '^Python'; then
+    # Ensure PYTHON_BIN is python
+    echo >&2 "ERROR: '${PYTHON_BIN} --version' returned an invalid response. Is ${PYTHON_BIN} really python?"
+elif [ ! -f ${SETUP_PY} ]; then
+    echo >&2 "ERROR: ${SETUP_PY} not found"
+else
+    virtualize
+fi
+
diff --git a/contrib/run-tests b/contrib/run-tests
new file mode 100755
index 0000000..644f5d9
--- /dev/null
+++ b/contrib/run-tests
@@ -0,0 +1,21 @@
+#!/usr/bin/env bash
+
+# Helper script to run test for each supported python versions
+# Defaults to running basic tests. The script activates the correct environment
+# as required.
+
+# Usage: $0 [options]
+# Example: $0 --ro-functional
+
+ARGS="$@"
+
+for BIN in python2 python3; do
+    if command -v ${BIN} >/dev/null 2>&1; then
+        echo "INFO: Running tests for ${BIN}"
+        . ./contrib/activate-dev-env ${BIN}
+        python setup.py test ${ARGS}
+        echo "INFO: Tests completed for ${BIN}"
+    else
+        echo "INFO: ${BIN} not found, skipping tests." 
+    fi
+done
-- 
1.8.1.4

