Category Archives: Programming

Fix yum error TypeError: can’t multiply sequence by non-int of type ‘float’

I ran into this bug which prevented me to update yum packages, and after a quick search, this helped to fix it:

# vim /usr/lib/python2.7/site-packages/urlgrabber/grabber.py

Go to line 1539, with vim you can use :1539

Replace the line with this:

if cur > (float(max_size)*1.10):

 

How to deploy django with nginx + gunicorn

Django is a great web framework and we have several ways to deploy it in production.

You can use apache too for example, but for django my favorite is nginx + gunicorn.

Nginx is a well-known player and it will be used as front-end. Gunicorn is not as well-known, but it has proved to be quite stable (instagram and pinterest use it).

Installation:

yum install nginx
pip install gunicorn

Previously you have to have installed python pip and epel repository.

Next step is to run gunicorn, which is quite easy. As we are going to run it with a django project, it is possible to use gunicorn_django 

cd /path/to/your/django/project
gunicorn_django --workers=4 -b 127.0.0.1:8888 -D

And that is it. We have told gunicorn to run with 4 workers (processes), to listen on localhost:8888 and to run in background with -D.

Last step is to configure a server for nginx:

upstream app_server_djangoapp {
    server localhost:8888 fail_timeout=0;
}

server {
        listen 80;
        server_name  server.com;

        access_log  /path/log/access.log;
        error_log  /path/log/error.log info;

    keepalive_timeout 5;

    # path for static folder
    location /static {
        root /path/to/static/;
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://app_server_djangoapp;
            break;
        }
    }
}

Restart nginx:

service nginx restart

Our django site is deployed, as easy as that.

Convert m4a files to mp3 with this script.

Some of my library was in m4a files, which I have problems to play, furthermore I prefer having everything in mp3 format.

So I wrote the following script:

echo "Dependencies: mplayer and lame"
if [ $# -lt 1 ]; then
	echo "Usage: sh m4a2mp3.sh '/path/to/files'"
else 
	echo "This could take sometime. Converting to 320kbps MP3 files..."
	sleep 1
	cd "$1"
	for i in *.m4a; do mplayer -ao pcm "$i" -ao pcm:file="$i.wav"; done
	for i in *.wav; do lame -h -b 320 "$i" "$i.mp3"; done
	echo "Cleaning the house..."
	rm -rf *.m4a
	rm -rf *.wav
fi
echo "All files converted. Thank you."

Usage is pretty straightforward:

sh m4a2mp3.sh /path/to/your/songs

The only dependencies, as you can see also in the script are mplayer and lame

You can install them with the following command:

# Yum based (Fedora, CentOS, SuSE)
yum install mplayer lame
# Apt based (Debian, Ubuntu, Mint) (NOT TESTED)
apt-get install mplayer lame

As always, you also can find the script on my Github.

Quick tip: Adding and substracting dates in PHP

More simple than it may seems. It will take care of adjusting months and days automatically:

Working with days:

// My date Y-m-d
$mydate = "2012-05-06";
//Adding five days
$newdate = date(“Y-m-d”, strtotime($mydate. " +5 days”));
// Substracting five days
$newdate = date(“Y-m-d”, strtotime($mydate. " -5 days”));

Working with weeks:

// My date Y-m-d
$mydate = "2012-05-06";
//Adding two weeks
$newdate = date(“Y-m-d”, strtotime($mydate. " +2 weeks”));
// Substracting one week
$newdate = date(“Y-m-d”, strtotime($mydate. " -1 week”));

Working with months

// My date Y-m-d
$mydate = "2012-05-06";
//Adding six months
$newdate = date(“Y-m-d”, strtotime($mydate. " +6 months”));
// Substracting three months
$newdate = date(“Y-m-d”, strtotime($mydate. " -3 months”));

Working with years

// My date Y-m-d
$mydate = "2012-05-06";
//Adding ten years
$newdate = date(“Y-m-d”, strtotime($mydate. " +10 years”));
// Substracting two years
$newdate = date(“Y-m-d”, strtotime($mydate. " -2 years”));

Be aware of the Unix Millenium Bug

Python script to backup and restore rpm packages

I’ve written a simple Python script to backup and restore installed rpm packages.

On Ubuntu, I used APTonCD for this matter, but in this case, for Fedora I decided to use my own script.

The usage is quite simple, only two parameters:

Backup:

python rpm-backup.py --backup

Restore:

python rpm-backup.py --restore

 

This is source code, although you can download it from my github (link to the gh-page of the entire repository)

#!/usr/bin/python

''' Backup script for installed packages.
Only RPM.
Tested under Fedora
Error list:
Code 1: Missing modules
Code 2: Not root
Code 3: Tried to restore without backup file'''

print "     ____  ____  __  ___"
print "    / __ \/ __ \/  |/  /"
print "   / /_/ / /_/ / /|_/ / "
print "  / _, _/ ____/ /  / /  "
print " /_/ |_/_/   /_/  /_/   "


print "        ____  ___   ________ ____  ______ "
print "       / __ )/   | / ____/ //_/ / / / __ \ "
print "      / __  / /| |/ /   / ,< / / / / /_/ /"
print "     / /_/ / ___ / /___/ /| / /_/ / ____/ "
print "    /_____/_/  |_\____/_/ |_\____/_/      "

#Import modules
try:
    import os
    import argparse
except ImportError:
    print "Could not find required modules. Exiting..."
    exit(1)

# Create parser
parser = argparse.ArgumentParser(description='Backup your installed packages.', epilog="Written by Adrian Espinosa (aesptux).")


# if user types --backup it will be stored as true in variable backup
#if user types --restore it will be stored as true in variable backup
parser.add_argument('--backup', action='store_true', dest='backup', help='Backup your packages.')
parser.add_argument('--restore',  action='store_true', dest='restore', help='Restore your packages.')


args = parser.parse_args()
#print args
#print(args.option(args.integers))

#variables
userlogged = os.environ['LOGNAME']
directory = '/%s' % (userlogged)
filename = 'packages-installed.bak'
path = directory + '/' + filename
rpm_list = []
print " "
print "Hello %s!" % (userlogged)
print "If you are having troubles using the script, use the argument '--help'"


def dobackup():

    ''' Create backup '''
    print "Starting copy..."
    command = 'rpm -qa > ' + path
    #os.system('touch '+path)
    os.system(command)
    os.system('echo Total packages: ; cat ' + path + ' | wc -l ')
    print "Done."


def dorestore():

    ''' Restore packages'''
    print "Starting restore..."
    global rpm_list
    # try, except, to see if the backup file exists
    try:
        for line in open(path, 'r'):
            rpm_list.append(line)
    except IOError:
        print "The backup file does not exist. Please, create a backup first. "
        exit(3)
    # take the list and join it
    rpm_list = ''.join(rpm_list)
    # remove \n
    rpm_list = rpm_list.replace('\n', ' ')
    os.system('yum install ' + rpm_list)


#This script must be run as root
if userlogged != 'root':
    print "You have to run the script as root"
    exit(2)


# if backup is true, dobackup. Else if restore is true, dorestore
if args.backup == True:
    dobackup()
elif args.restore == True:
    dorestore()

 

Then, you can set up a cron job like this:

0 22 * * * /usr/bin/python /root/rpm-backup.py --backup

 

Short and useful tip: How to make “tail -f” beep on each new line.

Just in case you do not know, tail prints the last ten lines of the indicated file. Like this:

tail /var/log/yum.log

Furthermore, if you want to print more lines you can do it with -n parameter:

tail -n 20 /var/log/yum.log

But I think the most interesting parameter for tail  is -f. This parameter allows tail to “follow” the file. New data is displayed as the file grows.

Now, using sed  we can append to each new line, a bell sound:

tail -f /var/log/yum.log | sed -e $'s/$/\a/'

Notice that ‘$’ escape sequences will only work on bash.

This was tested on Fedora 16.

Functions: How to return more than one value.

We cannot return two variables from a function, but we could use an array for that.

So for example, in JavaScript, it would be like this:

function foo() {
	// do something here
	// whatever
	return [myvalue, myothervalue, otherthing];
}

And then, for use those values:

foo()[0] // this is myvalue
foo()[1] // this is myothervalue
foo()[2] // this is otherthing

This can be applied to any other language  you may know. And that’s how we return more than one value from a function