Monday, December 12, 2011

Debugging in Python and OpenERP

Python debugger
Python provides a library for debugging called pdb.To use it, import the pdb module and add pdb.set_trace() at the line you want to start debugging.
When execution reaches that point, the console will prompt you for interactive debugging commands.
The most important are:
  • (n)ext, to step over to the next command.
  • (l)ist, to display the code around your current position.
  • (p)rint, to look into variables or test expressions.
  • (pp)rint, to pretty print expressions or variables. Useful for lists and dictionaries.
  • (c)ontinue running the rest of the code
You can also step in and out of subroutines:
  • (s)tep into the subroutines, used instead of (n)
  • (r)eturn, continues until the end of the current subroutine
Try it out using this script:

For more details on pdb  visit

Debugging using Spyder IDE
Spyder allows you to debug your programs without having to add the pdb module to your code, using the Run » Debug menu command. You can set breakpoints in the editor windows and follow the next statement to be executed, highlighted in sync with the pdb console. The variable explorer window also helps you to inspect variables values at run time.

However, the execution control is done through console commands. Fortunately Spyder  integrates with winpdb. If you install this,  you will be able to control the step-by-step debug execution through a graphical window.

Debugging the OpenERP server
The simplest way to debug the OpenERP server is using it's native debug mode. To activate it you need to add the --debug option when starting the server. With this, it will go into (pdb) command mode when an exception is found. This is why it's important to be familiar with the pdb commands.

You can also import pdb and add pdb.set_trace() command to the specific module you want to examine. The set_trace() will open a (pdb) command line in the console, even is the server is not in debug mode, and you can examine the variables in memory (with print) and step into the next commands.

Tuesday, December 6, 2011

Setting up the development environment

With the server files available through a file sharing service, you just need to edit them with your favourite editor. A good option on Windows is Notepad++.

For Pyhton development it's advisable to have a good IDE, and there are several options availableSpyder can be a good option. It provides features like code tree outline, selected text highlighting, automatic code analysis function, and a step-by-step debugger (with winpdb).

Changes to module's Python code require a server restart in order to reload it to memory. You will find  running the server manually from the terminal to be more flexible and practical when developing modules.
To do this, stop the openerp-server service and manually start an instance, as in the example below. You can then follow the log in the web-console's window:

root@openerp ~# service openerp-server stop                                                   
Stopping openerp-server: openerp-server.                                                     
root@openerp ~# su openerp                                                                   
openerp@openerp:/root$ cd /opt/openerp/ 
openerp@openerp:~$ server/bin/ --config=openerp-server.conf --log-level=debug

[2011-12-06 09:34:45,523][?] INFO:server:OpenERP version - 6.0.3                              
[2011-12-06 09:34:45,523][?] INFO:server:addons_path - /opt/openerp/server/bin/addons        
[2011-12-06 09:34:45,523][?] INFO:server:database hostname - localhost                       
[2011-12-06 09:34:45,523][?] INFO:server:database port - 5432                                
[2011-12-06 09:34:45,523][?] INFO:server:database user - openerp                             
[2011-12-06 09:34:45,523][?] INFO:server:initialising distributed objects services           
[2011-12-06 09:34:45,665][?] DEBUG:psycopg2:installed. Logging using Python logging module   
[2011-12-06 09:34:45,666][?] DEBUG:web-services:Registered an exported service: db           
[2011-12-06 09:34:45,666][?] DEBUG:web-services:Registered an exported service: common        
[2011-12-06 09:34:45,667][?] DEBUG:web-services:Registered an exported service: object       
[2011-12-06 09:34:45,667][?] DEBUG:web-services:Registered an exported service: wizard       
[2011-12-06 09:34:45,667][?] DEBUG:web-services:Registered an exported service: report       
[2011-12-06 09:34:45,684][?] INFO:web-services:starting HTTP service at port 8069    
[2011-12-06 09:34:45,686][?] INFO:web-services:starting HTTPS service at port 8071   
[2011-12-06 09:34:45,686][?] INFO:web-services:Registered XML-RPC over HTTP                  
[2011-12-06 09:34:45,687][?] INFO:web-services:starting NET-RPC service at port 8070 
[2011-12-06 09:34:45,687][?] INFO:server:Starting 3 services                                 
[2011-12-06 09:34:45,688][?] INFO:server:OpenERP server is running, waiting for connections...

If the server fails to start, it may be that there is still an instance running. If that's the case you can solve it by killing the process. List the OpenERP running processes, find the process ID and kill it: On the terminal type:
ps aux|grep openerp-server
sudo kill -9 [PID]

Running openerp-server with the --help option will present you all the options available.
In certain cases you may want to set the --log-level to debug_rpc or to debug_answer to get more detail on the client-server interactions.
And adding the --debug option allows you to enter Python's debugger (pdb) when an exception is raised.
To restart the server, press CTRL+C and execute the start command again. Note that there is no need exit and re-login the client sessions, because they are stateless.

Monday, December 5, 2011

Installing an OpenERP appliance

For serious work with OpenERP the server should be installed in Linux
We'll assume that will want to have your work environment hosted on Windows.

This can be achieved by running the OpenERP server on a Linux virtual machine.
In broad steps, you should:

In order to have easy acces to the server files, like addons folders and log files, you should also install Samba, the file sharing service. Share the /opt/openerp folder, where all your OpenERP should be stored.

If you configure the log file to be written there, you can then follow it on your Windows host, using a log file viewer like BareTail.

The result is a very portable server with a memory footprint below 300 kb.
All you need to do to start working is to start the virtual machine, check the IP address assigned to it, and point your browser at the corresponding 8080 port. Point Windows Explorer to \\IPAddress and you will access the servers files. Open the log in your log viewer and you will be following the server messages.

A roadmap for OpenERP implementation


I'll be embracing OpenERP as the platform for the development of business applications. The product has a learning curve, but I'm betting that in the long run it will pay for effort. I'm thankful for the help provided by other developer's blogs, so I decided to also share with others my results and solutions.
I am starting small, but I have ambitious plans for my OpenERP deployment. The first deliverable will be an issue management application, for a technical service management business unit.

Initial roadmap:
* Set up development environment: app server, programming tools and IDE.
* Design solution proof of concept, selecting and configuring standard modules.
* Extend standard modules to address specific needs.
* Prepare proof of concept for technical requirements: Active Directory integration, e-mail notifications to customers, SQL Server data import.

I'll be posting as long as I get something useful to share.