Showing posts with label Develop. Show all posts
Showing posts with label Develop. Show all posts

Monday, May 21, 2012

Connect to SQL Server from Ubuntu

Connecting an Ubuntu box to a MS SQL Server can by achieved through the FreeTDS ODBC driver.

Here goes a small recipe to install it:

sudo apt-get update
sudo apt-get install tdsodbc
sudo apt-get install unixodbc unixodbc-dev
sudo apt-get install python-setuptools
sudo easy_install pyodbc
sudo echo -e "[FreeTDS]\nDriver=/usr/lib/odbc/libtdsodbc.so" >> /etc/odbcinst.ini

Now you can open a Python interpreter and test the connection using pyodbc.
This may speed you up if you're trying to import SQL Server data to OpenERP.

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 http://docs.python.org/library/pdb.html.

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 available. Spyder 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/openerp-server.py --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 0.0.0.0 port 8069    
[2011-12-06 09:34:45,686][?] INFO:web-services:starting HTTPS service at 0.0.0.0 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 0.0.0.0 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.