Development on Linux - mf4dl1/jaksafe GitHub Wiki

Development under Gnu/Linux

InaSAFE is written in python and runs as a plugin in |QGIS|.

Quick Installation Guide - Linux (Debian based)

These instructions are for setting up a development environment on a Debian based linux system such as Ubuntu or Mint.

There are actually three ways of setting up a development environment for InaSAFE:

1. Setting up the bash-script way

  1. Goto the area where you do development, e.g cd ~/sandbox
  2. wget bit.ly/inasafe-install
  3. source ./inasafe-install

To verify that the installation works you can run the test suite from the command line:

cd inasafe-dev
make test

This will run all the regression tests and also highlight any code issues. Note that first time the tests are run they will pull 250MB of test data from our git repository. See further notes on running tests below.

To run the plugin start QGIS and enable it from the :menuselection:`Plugins --> Manage Plugins` menu.

If this doesn't work see section towards the end of this document about dependencies and try to do a manual install.

Note

This way is not recommended as it makes sweeping changes to your system and the script will not run equally well on all debian based OS's.

2. Setting up the fabric/fabgis way

There is a much easier and more advanced way to control the setup of the environment. It is done with Fabric.

Obviously you need to have fabric installed. Fabric itself is Python based and you can install it with pip. If you don't have pip installed you should install it too. We assume you have sudo rights on your own machine.:

mkdir -p ~/dev/python
cd ~/dev/python
sudo apt-get install python-pip openssh-server
pip install fabgis
wget -O fabfile.py https://raw2.github.com/AIFDR/inasafe/master/fabfile.py
fab -H localhost setup_inasafe

Note

This is optimised for Ubuntu based distros (please let us know if it works well on your distro. We recommend this approach.

Warning

The setup procedure described above will cause a considerable amount of bandwidth to be used since it checks out the inasafe-doc, inasafe_data and inasafe directories. If you are on a capped bandwidth system be warned.

3. Manual Installation Guide - Linux (Debian based)

Dependencies

The Graphical User Interface components are built using PyQt4 and the QGIS plugin API (useful resources: the QGIS Python Cookbook and the QGIS C++ API documentation). As such it is helpful if you are familiar with these technologies (python, Qt4, PyQt4, QGIS). In addition, the following are needed on your machine in order to work effectively with the code base:

  • git
  • rsync
  • pep8
  • nosetests (with coverage plugin)
  • python-numpy (for numerical computations)
  • python-gdal (python bindings to underlying gis functionality)
  • python-sphinx (compilation of documents)
  • cloud-sptheme (sphinx theme)
  • pyqt4-dev-tools (compiling ui and resources)
  • qt4-doc (qt4 API documentation)
  • pyflakes (test for bad coding style like unused imports / vars)
  • python-nosexcover and python-coverage (code coverage reporting)

On an ubuntu system you can install these requirements using apt:

sudo apt-get install git rsync pep8 python-nose python-coverage \
python-gdal python-numpy python-sphinx pyqt4-dev-tools pyflakes \
python-nosexcover

In some cases these dependencies may already be on your system via installation process you followed for QGIS.

Cloning the source code from git

To develop on the plugin, you first need to copy it to your local system. If you are a developer, the simplest way to do that is go to :file:`~/.qgis2/python/plugins` and clone InaSAFE from our GitHub repository page like this:

git clone git://github.com/AIFDR/inasafe.git inasafe-dev        (for read only)
git clone git@github.com:AIFDR/inasafe.git inasafe-dev   (to commit changes)

Note

If you followed the fabgis approach above, the git checkout is done for you. you fetch the plugin from the QGIS plugin manager it will overwrite your git copy if you don't alias it as inasafe-dev. For this reason we suggest to rather do your checkout to a directory called inasafe-dev.

QGIS installed in a non-standard location

For running unit tests that need QGIS, you may need to adjust PYTHONPATH and QGIS_PREFIX_PATH if QGIS is running in a non standard location. For example with QGIS built from source into /usr/local (and python bindings global install option disabled), you could run these commands (or add them to your ~/ .bashrc):

export QGIS_PREFIX_PATH=/usr/local
export PYTHONPATH=$PYTHONPATH:/usr/local/share/qgis/python/

Note

The above can be set within Eclipse's project properties if you are running your tests using the PyDev IDE environment. We also provide an example helper script run-env-linux.sh that you can use to set up your environment (it will work with the fabgis install described above). To use it do source run-env-linux.sh after which make targets etc. should work.

Adding InaSAFE to your python path:

Lastly, you should add the InaSAFE plugin folder to your PYTHONPATH so that package and module paths can be resolved correctly. E.g:

export PYTHONPATH=$PYTHONPATH:${HOME}/.qgis2/python/plugins/inasafe-dev

Once again you could add this to your .bashrc or set it in Eclipse for convenience if needed.

Running tests

You can run all tests (which includes code coverage reports and other diagnostics) by doing this within the InaSAFE plugin folder:

source run-env-linux.sh     (optional, depends on your setup)
make test

You can also run individual tests using nose. For example to run the safe_qgis tools tests you would do:

nosetests -v safe_qgis.tools.test

If you wish to run tests under xvfb (a virtual framebuffer), you can do:

sudo apt-get install xvfb

To run tests under xvfb you can now do:

xvfb-run --server-args="-screen 0, 1024x768x24" make guitest

Achievements

Note

This is optional and thus not hard coded into the Makefile.

Optionally you can enable nose achievments which is a motivational tool that gives you little achievement awards based on your test results:

sudo pip install git+git://github.com/exogen/nose-achievements.git

Now create this file in the root of your InaSAFE git checkout :file:`setup.cfg`:

[nosetests]
with-achievements=1

When you run tests occasionally achievements will be displayed to you at the end of the test run. See the achievements home page at http://exogen.github.com/nose-achievements/.

Developing using Eclipse (Linux)

Note

This is optional - you can use any environment you like for editing python, or even a simple text editor.

Warning

This section of the documentation is no longer maintained - we recommend using PyCharm Professional rather. We have a project wide license for PyCharm Professional so please contact us if you need it.

If you wish to use an IDE for development, please refer to this article for detailed information on how to get the basic Eclipse with PyDev setup.

Creating a project

The procedure for doing this is to do: :menuselection:`File --> New --> Project...` and then from the resulting dialog do :menuselection:`PyDev --> PyDev Project`.

In the resulting project dialog, set the following details:

At this point you should should click the link entitled 'Please configure an interpreter in related preferences before continuing.' And on the resulting dialog do:

In the dialog that appears do:

Another dialog will appear. Tick the first entry in the list that points to your:

~/.eclipse/org.eclipse.platform_3.7.0_155965261/plugins/org.python.pydev_2.3.0.2011121518/

(or simply click the 'Select All' button)

You will be returned to the Python Interpreters list and should see an entry for System Python 2.7 listed there. Now do in the Libraries tab:

Remote Debugging with Eclipse

For remote debugging, you should add pydevd to your PYTHONPATH before starting QGIS for example (you will need to adjust these paths to match your system):

export PYTHONPATH=$PYTHONPATH:/home/timlinux/.eclipse/org.eclipse.platform_3.7.0_155965261/plugins/org.python.pydev.debug_2.3.0.2011121518/pysrc/

Note

If you are running with remote debugging enabled, be sure to start the PyDev debug server first before launching the InaSAFE QGIS plugin otherwise QGIS will likely crash when it can't find the debug server.

You will need to ensure that the PYTHONPATH containing your pydev package folder is set before you launch QGIS - for example by adding the above line to your ~/.bashrc or by making a small batch file containing the above export and then sourcing the file before launching QGIS e.g.:

source riab_paths.sh
/usr/local/bin/qgis

Running Unit tests from the IDE

Python has very good integrated support for unit testing. The first thing you should do after setting up the IDE project is to run the tests. You can run tests in the following ways:

  • For the entire InaSAFE package
  • For individual sub packages (e.g. engine, gui, storage, impact_functions)
  • for an individual test module within a package
  • for an class within a test module
  • for an individual method within a test class

You can view these individual entities by browsing and expanding nodes in the project panel in the left of the IDE.

Note

If you run the test suite for the entire InaSAFE package, it will mistakenly treat the sphinx documentation conf.py (docs.source.conf) as a test and fail for that test. This is 'normal' and can be ignored.

⚠️ **GitHub.com Fallback** ⚠️