Monday, December 28, 2009

Django and Oracle support

To get work Django and Oracle client in Ubuntu:

Download cx_Oracle (choose for your system), Oracle InstantClient and Sql*Plus (in this page you can choose your OS and download the packages (in my case are oracle-instantclient11.2-basic- and oracle-instantclient11.2-sqlplus-

Move the files into one directory for simplicity and run

alien -k *.rpm
dpkg -i *.deb
to convert all the downloaded packages to ubuntu deb-format and install them.

Then make symlink to lib, otherwise module won't be found by Django

cd /usr/lib/python2.6
ln -s site-packages/

To avoid

Traceback (most recent call last):
  File "", line 1, in 
ImportError: cannot open shared object file: No such file or directory
add to ~/.bashrc (or /etc/bash.bashrc to system-wide apply)
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/oracle/11.2/client64/lib

If you see the message
Traceback (most recent call last):
  File "", line 1, in 
ImportError: cannot open shared object file: No such file or directory
sudo apt-get install -y libaio1

P.S. If you choose more modern (or older) version of Oracle IntstantClient you have to create symlinks for necessary version, e.g. you see the following error:

Traceback (most recent call last):
  File "", line 1, in 
ImportError: cannot open shared object file: No such file or directory
but in /usr/lib/oracle/11.2/client64/lib directory exists Just make it symbolic link with
ln -s

Install RPM in Ubuntu

To install Rpm-package in Ubuntu Linux (to run commands needs to be a superuser or using sudo):
  1. install alien package:
    sudo apt-get install alien
  2. Convert Rpm to Deb (in current directory)
    alient -k <package-file>.rpm
  3. Install the new Deb package
    dpkg -i <package-file>.deb

Problems with VPN-connections in Ubuntu Karmic

I need to connect to Cisco VPN. I had got a file with connection settings. Through NetworkManager I created new VPN connection by import the settings file. Trying to connect - no result. In /var/log/syslog I found the line:
NetworkManager: nm-vpn-connection.c.828: NeedSecrets failed: dbus-glib-error-quark Rejected se
nd message, 1 matched rules; type="method_call", sender=":1.5" (uid=0 pid=969 comm="NetworkManager) interface="org.freedeskt
op.NetworkManager.VPN.Plugin" member="NeedSecrets" error name="(unset)" requested_reply=0 destination="org.freedesktop.Netwo
rkManager.vpnc" (uid=0 pid=7971 comm="/usr/lib/network-manager-vpnc/nm-vpnc-service))
Some googling gives me a bit of information - Keyring manager denied connection of the VPN service. And how to grand access I didn't know. Googling more brings me the answer:
In /etc/dbus-1/system.d/nm-vpnc-service.conf
--- nm-vpnc-service.conf.fixed 2009-04-18 17:56:45.000000000 -0500
+++ nm-vpnc-service.conf 2009-04-18 17:57:37.000000000 -0500
@@ -6,6 +6,10 @@
   <allow own="org.freedesktop.NetworkManager.vpnc"/>
   <allow send_destination="org.freedesktop.NetworkManager.vpnc"/>
+ <policy user="at_console">
+  <allow own="org.freedesktop.NetworkManager.vpnc"/>
+  <allow send_destination="org.freedesktop.NetworkManager.vpnc"/>
+ </policy>
  <policy context="default">
   <deny own="org.freedesktop.NetworkManager.vpnc"/>
   <deny send_destination="org.freedesktop.NetworkManager.vpnc"/>

Thursday, December 24, 2009

Ubuntu's Archive Manager and RAR-archives support

To add support of RAR-archives for Ubuntu's Archive Manager run
sudo apt-get install unrar

Wednesday, December 16, 2009

Disabled Mercurial's strip command

Mercurial command hg strip is disabled by default in Ubuntu. To enable it add to hgrc file (in .hg directory of repository or /etc/mercurial/hgrc) these lines
[extensions] =

Wednesday, December 9, 2009

Reinstall Ubuntu automatically

There is a very simple command that reinstalls all the distribution packages and reconfigures them automatically. This is particularly helpful when you have done a partial upgrade and eventually have broken the system dependencies tree.
sudo dpkg-reconfigure -phigh -a
since this command would take a lot of time to process (~1 hour) depending on your hardware in case you have a minor dependency problem you can fix it via
sudo apt-get install -f

Tuesday, December 8, 2009

Create a MySQL database for new user

To create a database and a user, to grant some privileges:

CREATE USER 'user'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON test_db.* TO 'user'@'%';

Mercurial server in haste

Just now I need to pull to spontaneous remote Mercurial repository. I ran hg serve and have tried to push. The server rejects my connection, because it demands secure connection. Attempts to change protocol to HTTPS were failed.
As decision I found the following: in hgrc (or mercurial.ini in Windows) add
allow_push = *
push_ssl = false

Profit :)

Sunday, November 29, 2009

Setting up Postgres database server in Ubuntu 9.10

Do all as the superuser.
apt-get install postgresql postgresql-client postgresql-contrib
# To reset password for postgres user
echo "ALTER USER postgres WITH PASSWORD 'postgres';" | sudo -u postgres psql template1
# reset password for system user
passwd postgres
In bottom of /etc/postgresql/8.4/main/pg_hba.conf file set as the following:
# If you change this first entry you will need to make sure that the
# database
# super user can access the database using some other method.
# Noninteractive
# access to all databases is required during automatic maintenance
# (custom daily cronjobs, replication, and similar tasks).
# Database administrative login by UNIX sockets
local   all         postgres                          ident

# "local" is for Unix domain socket connections only
local   all         all                               md5
# IPv4 local connections:
host    all         all          md5
# IPv6 local connections:
host    all         all         ::1/128               md5

Saturday, November 28, 2009

Mount NTFS-partition in Ubuntu Karmic x64

Do all as the superuser.
  • Get location of the ntfs partition
    fdisk -l
    Disk /dev/sda: 500.1 GB, 500107862016 bytes
    255 heads, 63 sectors/track, 60801 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x14311431
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1       29089   233657361    7  HPFS/NTFS
    /dev/sda2           29090       32129    24418800   83  Linux
    /dev/sda3           60304       60801     4000185   82  Linux swap / Solaris
    /dev/sda4           32130       60303   226307655   83  Linux
    Partition table entries are not in disk order
    OK, /dev/sda1 is the desired location.

  • Create folder in which you want partition will be mounted. E.g.
    mkdir /media/windows
  • Add this line to your /etc/fstab file, and save it after editing. Note, you should input your locale if it's differs from this.
    /dev/sda1       /media/windows    ntfs-3g defaults,locale=en_EN.UTF-8 0 0
  • Test with command
    mount -a

Install Chromium in Ubuntu 9.10 Karmic x64

To install just run this commands as superuser.
echo "deb karmic main
deb-src karmic main" >> /etc/apt/sources.list

apt-key adv --recv-keys --keyserver 0xfbef0d696de1c72ba5a835fe5a9bf3bb4e5e17b5

apt-get update

apt-get install -y chromium-browser

Disabled `hg view` command in Ubuntu

To enable `hg view` command it's needs to add
in your repository hgrc (.hg/hgrc) or system-wide(/etc/mercurial/hgrc)
Or you might also need to install the tk package.

Convert Ext3 filesystem to Ext4 in Ubuntu 9.04

Suppose your disk is /dev/sda1.

e2fsck /dev/sda1 -y
tune2fs -O extents,uninit_bg,dir_index /dev/sda1
fsck -pf /dev/sda1

Profit :)

Friday, November 27, 2009

Ubuntu One Client in 9.10

If you have troubles with Ubuntu One Client in 9.10 look in its config (~/.config/ubuntuone/syncdaemon.conf). It have to look like this

on = False 
read_limit = -1
write_limit = -1

If in your config `on` = True, change to False, restart UO client and get profit :)

spied here.

Monday, November 23, 2009

Setup memcached cache backend in Django in Ubuntu 9

In order to run them together we have to:
  1. Install necessary packages

    sudo apt-get install build-essential python-dev memcached libmemcache-dev python-dev

  2. Install cmemcache (using easy_install)


  3. And, optionally and for test only, run memcached

    memcached -d -m 1024 -i 11211

Sunday, November 22, 2009

Django auto language selection

I want user can select language he prefers. But I didn't found how to do it in Django easy. And I stopped on the following decision.

At first we need middleware which will detects language parameter in GET (name is in settings.LANGUAGE_COOKIE_NAME) and sets session's paramater `django_language`.
import settings

class LocaleDetectMiddleware(object):
    This middleware parse GET request and detects the language user chosen
    def process_request(self, request):
        if settings.LANGUAGE_COOKIE_NAME in request.GET:
            request.session['django_language'] = request.GET[settings.LANGUAGE_COOKIE_NAME]
Note, this middleware must be included in settings.INSTALLED_APPS before django.middleware.locale.LocaleMiddleware, because we use Django's standard locale detection routine.

Saturday, November 21, 2009

South for Django, or how to keep database up to date

South is a migration tool for Django.

There two ways to install South: system-wide or per-project. I choose the second way because I always know that it's works with the project.

  1. Get from repository.
    hg clone
    hg update -C 0.6.2 #this is stable version now
  2. copy south dir to project
  3. Add it to INSTALLED_APPS
  4. In order to create all system models
    ./manage syncdb 
  5. Further you should create necessary apps and its models (don't forget to add them to INSTALLED_APPS)
  6. To create initial migration for an application run this command
    ./ startmigration <app name> --initial
  7. After you changed the model
    ./ startmigration <app name>
    to create migration
  8. To migrate to current database state
    ./ migrate [app]

These are the most often operations. More detailed info you get in official documentation.

Javascript SyntaxHighlighter

In this blog for code highlighting I use the cool SyntaxHighlighter. And for installing it I spend some time to way to include in blogspot's template. Now just copy the html-code and paste after <head> tag.

<link href='' rel='stylesheet' type='text/css'/> 
<link href='' rel='stylesheet' type='text/css'/> 
<script src='' type='text/javascript'/>
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script src='' type='text/javascript'/> 
<script language='javascript'> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = '';

Oh, to escape HTML to form accepted blog engine I use this.

Django authentication

How to login a user in Django? There are two ways.

First, "django way", use the function authenticate which returns User model with very necessary attribute backend. This way is good when you log a user in from a form when you know its username and password. But what do to do if needs to log the user in which loaded directly from User model, when you don't know its password (you just have a strongly encoded password's version)?

After a bit of googling I just was able to found a dirty hack - manually append backend attribute to User's object instance. Does anyone know a "django way" to this?

Here's the "Django way" code snippet

user = authenticate(username='user', password='password')
login(request, user)

And here's 'hack':

user = user_info.user
# a dirty hack ;(
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)

Tuesday, November 17, 2009

Restore GRUB boot loader after other OS has been installed

After updating Windows XP to Windows 7 Grub-loader has been lost. I can't work without Linux. And I found this solution:

At first Load from live-cd
Let's know where os install
find /boot/menu/menu.lst
Output will be like this
Tell grub where its files
root (hd0,3)
And install it
setup (hd0)
After rebooting we can see the GRUB-menu.

Script to easily create a Postgres database

#! /bin/sh
if [ ${#} -ne 3 ]
 echo "Usage: $0 db_name username password"
 exit 2


echo "
    LC_COLLATE = 'en_US.UTF-8'
    LC_CTYPE = 'en_US.UTF-8'
    TEMPLATE = "template0";
" | psql -U postgres -d template1

or if you want just to exec it once:
echo "
    LC_COLLATE = 'en_US.UTF-8'
    LC_CTYPE = 'en_US.UTF-8'
TEMPLATE = "template0"; GRANT ALL PRIVILEGES ON DATABASE $DBNAME to $USER; " | sudo -u postgres psql -U postgres -d template1

Friday, November 13, 2009

Generate random password in Linux

cat /dev/urandom | tr -d -c 'a-zA-Z0-9' | fold -w 8 | head -1

Saturday, October 10, 2009

Javascript function delegate

Function.prototype.createDelegate = function(obj, args, ignoreArgs) {
 var method = this;
 return function {
  return method.apply(obj || window, (!args) ? arguments : (ignoreArgs === true) ? args : args.concat(Array.apply(null, arguments)));

Saturday, September 26, 2009

Use objects as a django view functions

To use objects instead of simple functions I apply the following method:

class Page(object):
    # Variables
    template = "main.html"
    vars = {}
    redirect_url = ""

    def __call__(self,request,*args,**kwargs):
        # Before calling a "main" content-generating method
        # "Main" content-generating method. Must be declared in descendants
        # After generating content

        # if need to redirect - do it
        if self.redirect_url:
            # if is present special redirect-field in GET
            url = request.GET.get(REDIRECT_FIELD_NAME)
            return HttpResponseRedirect(url if url else self.redirect_url)
        # Just return early declared template
        return render_to_response(self.template, context_instance=RequestContext(request, self.vars))

    def before_content(self,request,*args,**kwargs):
    def after_content(self,request,*args,**kwargs):

class PageAuth(Page):
    def __call__(self,request,*args,**kwargs):
        if request.user.is_authenticated():
            return super(PageAuth,self).__call__(request,*args,**kwargs)
        return HttpResponseRedirect("{0}?{1}={2}".format(reverse(settings.LOGIN_VIEW),REDIRECT_FIELD_NAME,request.path))

class MainView(Page):
    def content(self,request,*args,**kwargs):
        self.template = 'template_name.html'
        self.vars['list'] = []
And has been changed url declarations. An example:
url(r'^$', MainView(), name="main_page")
(I couldn't find decision without using name parameter).

Optimize and speed up Netbeans

In /etc/netbeans.conf set netbeans_default_options to
-J-client -J-Xms32m -J-Xmx512m -J-XX:PermSize=32m -J-XX:MaxPermSize=200m -J-Xverify:none -J-XX:CompileThreshold=100 -XX:+CompressedOOPS -XX:+AggressiveOpts -XX:+TieredCompilation -XX:+DoEscapeAnalysis -XX:+UseConcMarkSweepGC -J-XX:+CMSClassUnloadingEnabled -J-XX:+CMSPermGenSweepingEnabled
So, we allow to use up to 512Mb RAM, compile to byte-code and optimize most of functions.