10/26/15

Convert rpm to deb

#Install alien
sudo apt-get install alien

# Convert rpm to deb
alien my_package-1.1-1.rpm

# Install deb package
dpkg -i my_package-1.1-1.deb

# Convert deb to rpm
alien -r deb_package-1.1-1.deb


10/25/15

Clean pyc file from python project


How do I remove all .pyc files from a project?

find . -name '*.pyc' -delete

You can use Flask-Script:
# manage.py
from flask.ext.script import Manager
from myapp import app

manager = Manager(app)

@manager.command
def clearpyc():
    os.system('find . -name \'*.pyc\' -delete')

if __name__ == "__main__":
    manager.run()


10/24/15

VIM Configuration for Python Development.

# Install CTags
sudo apt-get install exuberant-ctags

# Clone repo:
git clone https://github.com/rmk135/vimrc.git ~/vimrc

# Copy .vimrc file to home dir
cp ~/vimrc/.vimrc ~/.vimrc

# Copy color schemes to .vim home dir
mkdir ~/.vim && cp -r ~/vimrc/.vim ~/

# Setup Vundle (Vim Plugin Manager)
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

# Run VIM (ignore errors)
vim

# Install VIM Plugins (inside VIM)
:PluginInstall

# Rehash terminal
hash -r


10/17/15

Python DB wrappers


https://github.com/ZanMax/DBwrappers
  • MySQL
  • SQLite
  • Mongo - soon ...

Before proceeding, you make sure you have MySQLdb installed on your machine.
To install MySQLdb module:

apt-get install build-essential python-dev libmysqlclient-dev
pip install mysql-python


Python Mock

Mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.

Example:
import os
from mock import patch

def file_extension(file_path):
    if os.path.isfile(file_path):
        return os.path.splitext(file_path)[1]

def is_date(val):
try:
    datetimeobject = datetime.datetime.strptime(val, '%Y%m%d')
    return isinstance(datetimeobject, datetime.date)
 except:
    return False

if __name__ == '__main__':
    with patch('is_date'as mock_is_date:
        mock_is_date.return_value = True

 with patch('os.path.isfile'as mock_isfile:
        mock_isfile.return_value = True

Python work with files and directories

Very often there is a need to work with files in Python.
shutil.copy() # copy the file
shutil.copytree() # copy the directory
shutil.move() # moves files
shutil.rmtree() # remove directory

tmpfile.mkstemp() # temporary file
filecmp.cmp() # compare file
filecmp.cmpfiles() # compare directory
os.getcwd() # get working directory
os.chdir() # change working directory
os.access() # access check
os.listdir() # list of directories and files
os.stat() # file information
os.mkdir() # make directory
os.makedirs() # create a directory and intermediate directories
os.rmdir() # remove empty directories
os.removedirs() # remove empty directories
os.remove() # remove
os.walk() # execute all the directories tree
glob.glob() # get a list of files and folders on a mask

os.path.abspath() # absolute path
os.path.isabs() # check that the path absalyutno
os.path.realpath() # the canonical path to the OS
os.path.dirname() # get out of the way only to directory
os.path.basename () # get out of the way the file name
os.path.join() # join path directory and file
os.path.isfile() # check if this object is file
os.path.isdir() # check if this object is a directory
os.path.getsize() # get size file or directory
os.path.exists() # check if this object exists
os.path.getctime() # get time when file was created
os.path.getmtime() # get time when file was changed


10/16/15

Back Up MySQL Databases From The Command Line

Creating A Backup

mysqldump database_name > database_name.sql

mysqldump --databases database_1 database_2 > two_databases.sql

mysqldump --all-databases > all_databases.sql

Restoring a Backup

mysql database_name < database_name.sql

mysql --one-database database_name < all_databases.sql


Rsync + SSH + Cron

Ssh-keygen generates keys:
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.200.10

Creae sh file:
#!/bin/bash
rsync -avuz /var/www/example.com/ root@192.168.200.10:/var/www/example.com/

a - archive mode
v - increase verbose
u - skip files that are newer on the receiver
z - compress file data during the transfer

Edit crontab:
run crontab -e and add:
*/5 * * * * /home/ramesh/backup.sh