Wednesday, January 8, 2014

Function to download file from Google Spreadsheet

 Function to download file from Google Spreadsheet and then convert it into text files as according to our requirement after reading from Excel File downloaded from Google SpreadSheet.

Sample usage : python lang_parsing.py (path where we want to store our files)

Note : I am assuming spreadsheet doc name  "Music App Static Labels List v b1.0" and Sheet name "Sheet1". If this name changed in doc then change also has to be done in code to change name else all functions are generic.

##################### Start of the Program

#!/usr/bin/python

import os
import xlrd
import sys
import time
import datetime

dir_path=sys.argv[1]

## Iterating over the Excel sheet given in the Google spreadsheet
def iter_workbook(xls_file):
    workbook = xlrd.open_workbook(dir_path + "/" + xls_file)
    worksheet = workbook.sheet_by_name('Sheet1')
    num_rows = worksheet.nrows - 1
    num_cells=worksheet.ncols-1
    open_file(num_rows,num_cells,worksheet)

## Function to open file in write mode as language given in Excel Sheet
def open_file(num_rows,num_cells,worksheet):
    i=0
    while num_cells>=0:
        file_name=worksheet.cell_value(0,i)
        file_obj=open(dir_path + "/" + file_name+".txt","w")
        write_file(num_rows,worksheet,file_obj,i)
        file_obj.close()
        num_rows=worksheet.nrows - 1
        num_cells=num_cells - 1
        i=i+1

## Function to write file with the language name as given in Excel Sheet

def write_file(num_rows,worksheet,file_obj,i):
    j=1
    while num_rows>0:
        obj='"'+worksheet.cell_value(j,0)+'"="'+worksheet.cell_value(j,i)+'"'
        file_obj.write(obj.encode('utf8'))
        file_obj.write("\n")
        j=j+1
        num_rows=num_rows - 1

## Function to check Excel file downloaded from Google Spreadsheet
def calling_func():
    postfix = "xls"
    for xls_file in os.listdir(dir_path):
        if not xls_file.lower().endswith( postfix ):
            continue
        else:
            iter_workbook(xls_file)

## Function to download file from Google Spreadsheet with python inbuilt module googlecl

def downl_xls():
    os.system("google docs get --title='Music App Static Labels List v b1.0' --dest=languages ")
    try:
        os.system("cp languages.xls " + dir_path + " ")
        calling_func()
    except:
        print "Mentioned directory doesn't exist"

if __name__=="__main__":
    downl_xls()

######################## End of the Program

Tuesday, December 31, 2013

Writing CRON jobs on a Mac

Mac OSX is essentially a UNIX distribution, so we have all the cool under-the-bonnet tricks and tools of any operating system with a UNIX backbone. One of the utilities I’ve always found useful in a UNIX OS is the CRON tab:

“Cron is a time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.”

To set up a new Cron job, open your Terminal and type:
sudo crontab -e

You’ll be prompted for your password, and upon correct submission, will open a blank VI window.

The basic format of a Cron is:
0 0 * * * sh /directory/script.sh
    1.    The first 0 denotes minutes past the hour (0 is on the hour)
    2.    The second 0 denotes the hour of the day (0 is 12:00am)
    3.    The first * denotes day of the week (* means every day, 1 is Monday etc)
    4.    The second * is the week of the month (* means every week, 1 is the first week of the month)
    5.    The final * is the month of the year (* means every month, 1 is the first month of the year)

We specify the type of script we wish to execute; in the example above, we’re executing a basic shell script, and the final parameter details the absolute location of the script you wish to execute at regular intervals.

I often execute daily PHP scripts on my server, an example of a PHP script (backup.php) running every day at 6pm is:
0 18 * * * php ~/site/cleanup.php

Note the script type is now php, as we’re executing a php script, not a shell script.
Finally, let’s write a script that backs up my ssh keys from ~/.ssh to my Dropbox directory, once a week, on a Monday at 9:30am.
Using Terminal.app, open a new file for editing anywhere on your system (I use vi).
sudo vi ~/src/backup.sh

Copy and paste the script below, write to backup.sh and exit vi.
#!/bin/bash
sudo rsync -av --progress --delete --log-file=/Users/*yourmachine*/logs/$(date +%Y%m%d)_rsync.log /Users/*yourmachine*/.ssh /Users/*yourmachine*/Dropbox

The script uses rsync to backup the directory at “/Users/*yourmachine*/.ssh” to your Dropbox directory at “/Users/*yourmachine*/Dropbox”. It’ll also log the backup in a logs directory in your home directory. Now for the Cron.

30 9 1 * * sh ~/src/backup.sh

Hopefully this will get you started with using Cron on your OSX system.

My Profile

My photo
can be reached at 09916017317