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-11.2.0.0.2-1.x86_64.rpm and oracle-instantclient11.2-sqlplus-11.2.0.0.2-1.x86_64.rpm).

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/cx_Oracle.so

To avoid

Traceback (most recent call last):
  File "", line 1, in 
ImportError: libclntsh.so.10.1: 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: libaio.so.1: cannot open shared object file: No such file or directory
run
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: libclntsh.so.10.1: cannot open shared object file: No such file or directory
but in /usr/lib/oracle/11.2/client64/lib directory exists libclntsh.so.11.1. Just make it symbolic link with
ln -s libclntsh.so.11.1 libclntsh.so.10.1

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>
+ <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]
hgext.mq =

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
Source.

Tuesday, December 8, 2009

Create a MySQL database for new user

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

CREATE DATABASE test_db DEFAULT CHARACTER SET UTF8;
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
[web]
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:
# DO NOT DISABLE!
# 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
# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# "local" is for Unix domain socket connections only
local   all         all                               md5
# IPv4 local connections:
host    all         all         127.0.0.1/32          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 http://ppa.launchpad.net/chromium-daily/ppa/ubuntu karmic main
deb-src http://ppa.launchpad.net/chromium-daily/ppa/ubuntu karmic main" >> /etc/apt/sources.list

apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 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
[extensions]
hgk=
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

[bandwidth_throttling]
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)

    easy_install http://gijsbert.org/downloads/cmemcache/cmemcache-0.95.tar.bz2

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

    memcached -d -m 1024 -i 0.0.0.0 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 http://bitbucket.org/andrewgodwin/south/
    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
    ./manage.py startmigration <app name> --initial
            
  7. After you changed the model
    ./manage.py startmigration <app name>
            
    to create migration
  8. To migrate to current database state
    ./manage.py 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='http://alexgorbatchev.com/pub/sh/2.1.364/styles/shCore.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/2.1.364/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushBash.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCpp.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCSharp.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCss.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushJava.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushJScript.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPhp.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPython.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushRuby.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushSql.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushVb.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushXml.js' type='text/javascript'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPerl.js' type='text/javascript'/> 
<script language='javascript'> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/2.1.364/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>

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
grub
Let's know where os install
find /boot/menu/menu.lst
Output will be like this
(hd0,3)
Tell grub where its files
root (hd0,3)
And install it
setup (hd0)
quit
reboot
After rebooting we can see the GRUB-menu.

Script to easily create a Postgres database

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

DBNAME=$1
USER=$2
PWD=$3

echo "
CREATE USER $USER WITH PASSWORD '$PWD';
CREATE DATABASE "$DBNAME"
    WITH OWNER "$USER"
    ENCODING 'UTF-8'
    LC_COLLATE = 'en_US.UTF-8'
    LC_CTYPE = 'en_US.UTF-8'
    TEMPLATE = "template0";
GRANT ALL PRIVILEGES ON DATABASE $DBNAME to $USER;
" | psql -U postgres -d template1

or if you want just to exec it once:
echo "
CREATE USER $USER WITH PASSWORD '$PWD';
CREATE DATABASE "$DBNAME"
    WITH OWNER "$USER"
    ENCODING 'UTF-8'
    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
        self.before_content(request,*args,**kwargs)
        # "Main" content-generating method. Must be declared in descendants
        self.content(request,*args,**kwargs)
        # After generating content
        self.after_content(request,*args,**kwargs)

        # 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):
        pass
    def after_content(self,request,*args,**kwargs):
        pass


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.