Saturday, December 14, 2013

X and O puzzle for kids

A very simple game written in python is attached herewith for kids. You can  Download the game and unzip to run on your system.This is perfect for windows platform as a few Windows system commands like “color”, “TMP” file path etc have been used inside the script. However, modifying a few lines the script can be used on other OS like Linux. Please comments if you need source code. The script is converted into exe and zipped to attaché in blog.

Once you double click on the file it will show you the board. Please read the instruction carefully and place your position. Your choice will be placed as "X" and system will place "O" against your choice. 
If none of you win the game the result appears as below.
Different color at end of the game indicates the result. By any chance if you win the game Green Board will congratulate you.
let's enjoy the game and put your comments how is that!!!!


Saturday, August 31, 2013

Search and test all links available in web page

Another small utility to check if all HYPERlinks inside a web page are working operationally. User can modify this utility and use according to their requirement.The nice part of this code is it stores search output in a file and display storage path on GUI console at the end of the process. To test the script simply store the code in a *.py file and double click on it. A popup will appear on desktop. Provide URL/web page and click on "show" button.
Searching will start and result will populate on windows command line console provided, python-3.3.2 is installed and placed in path properly.
#!python
'''
Created on Aug 19, 2013
@requires: Tested in Windows XP
@author: Jaydeb Chakraborty
@version: Python Version-3.3.2

'''
from tkinter import Tk, Label, Button, Entry
import urllib.request
import re
import os
import logging

""" 
Store all URLs in a file and get HTTP return code 
"""
def show():
    strn = entry.get()  
    if re.match('(?:ftp|https)://', strn):
        mesg="Currently HTTPS|FTP is not supported"
        t = Label(w, text=mesg)
        t.pack()
    else:
        l=strn.replace('http://', '')
        mesg='Please check output in ' + (os.environ.get('TEMP', '')) + '\INFO.log'
        t = Label(w, text=mesg)
        t.pack()                
         
    if strn:  
        stdinfo=((os.environ.get('TEMP', ''))+ '\INFO.log')
        logging.basicConfig(filename='%s' % stdinfo, format='%(asctime)s %(message)s', level=logging.INFO)
        logging.warning('*******  Accessing : %s' % strn + ' ********')
        logging.warning('**********************************************')
        local_filename, headers = urllib.request.urlretrieve('http://' + l)
        f = open(local_filename) 
        for lines in f:
            myString_list = [item for item in lines.split(" ")]
            for item in myString_list:      
                try:
                    o = re.search("(?Phttp?://[^\s]+)", item.expandtabs()).group("url")
                    url = re.sub(r'\?.*|".*', "", o)
                    conn = urllib.request.urlopen(url)
                    access = conn.getcode()
                    logging.warning('URL : %s' % url + ' -- Returncode is : %s' % access)
                    print(url, access)
                except :
                    pass 
                
       
w = Tk()
quitBotton = Button(w, text='Quit', command=quit).pack()
showBotton = Button(w, text='Show', command=show).pack()
Label(w, text="        Please provide URL...        ").pack()
entry = Entry(w)
entry.pack()
res = Label(w)
res.pack()
w.title('Test Links in web page')
w.maxsize(1000, 40000)
w.mainloop()

Saturday, August 24, 2013

Windows schedule task: Restart Tomcat automatically

Managing tomcat with a bunch of application sometime becomes problematic. Without investing proper time for root cause analysis it is not possible to isolate the issue perhaps the application from the container which is troubling. Be it a production environment project cannot move forward w farther without resolving the issue. Well, I am talking about development environment where usually we do not think about the impact instead just bounce the server.
For UNIX/Linux it is easy at least can be managed using a few line of script and scheduling the job via cron. Maybe windows expert will do the same very easily. I thought to do the same thing via python. There are many reason and corresponding indication of hanging a server. Having a close look in server log one can found the common foot print that a server left every time it goes to hang state. High Memory utilization is one of them and very common.  Following steps demonstrate how to setup a job written in python in windows.

How to schedule jobs in windows?
>>schtasks /create /tn "PythonCron Job" /tr "path/to/your/executable Arg1 Arg2" /sc hourly
Above command will create job in "Control Panel--> Schedule Task".
In windows, schedule python script does not accept argument.
Therefore converted .py script to .exe file

How to convert .py to .exe?
To Convert .py to .exe for Python3.3 I used CX-Freeze.
For more details please have a look in http://cx-freeze.sourceforge.net/index.html
>>cxfreeze hello.py --target-dir dist
.........and schedule task as
>>C:\Path\To\Your\File.exe Arg1 Arg2

How to adjust schedule job in windows?
Open the task and adjust time in Advance Tab. Every task requires a user name and password
Administrator can use "NT AUTHORITY\SYSTEM"
How to check log?
To check if schedule job is running perfectly, go to "Control Panel--> Schedule Task"
Logs are usually available in "C:\WINDOWS\SchedLgU"
The script also populates logs ‘stdout/stderror’ in user "HOMEPATH". You can check logs if there is any error.
How to check login user in windows
>>echo %USERDOMAIN%\%USERNAME%

How to Test script in DEV?
To test this script please follows the following steps:
    1) Defile CATALINA_HOME in User Variable, if not defined.
    2) Start tomcat server
    3) Execute script in command line "python /path/to/the/script.py MAXMEM TOMCAT_PORT"
    4) For testing provide MAXMEM as low as for example '5'
        Example: >>>python /path/to/the/script.py 5 8080

#!python
'''
Python Version-3.3.2
Created on Aug 19, 2013
@author: Jaydeb Chakraborty
??? Restart Tomcat automatically
??? Monitor server behavior and modify script for different threshold 

!!! Assume CATALINA_HOME is defined and Tomcat starts via 'startup.bat'
'''
import logging
import sys
import re
import os
import socket
import subprocess
import urllib.request
from datetime import datetime
from time import sleep

class ServerProcess:
    def __init__(self, portnum):
        self.portnum = portnum
        self.port = (str(portnum).replace("'", "").replace("]", ""))
    def getPID(self):
        try:
            port = self.port
            p1 = subprocess.Popen('netstat -ano', stdout=subprocess.PIPE)
            p2 = subprocess.Popen('findstr :%s' % port, stdin=p1.stdout, stdout=subprocess.PIPE)
            result=p2.communicate()[0].split()
            pid=str(result[4]).lstrip("b,").replace(",", "").replace("'", "")
            return pid 
        except LookupError as l:
            logging.basicConfig(filename='%s' % stderror, format='%(asctime)s %(message)s', level=logging.DEBUG)
            logging.warning('Watch out! : %s' % l)
            return 0       
    def getMemory(self, pid):
            p3 = subprocess.Popen('tasklist /fi "MEMUSAGE ge 10" /fi "PID eq %s' % pid, stdout=subprocess.PIPE)
            mem = str(p3.communicate()[0].split()[17]).lstrip("b,").replace(",", "").replace("'", "")
            return mem
    def getConnect(self):
        try:
            port = int(self.port)
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            remoteServerIP  = socket.gethostbyname(socket.gethostname())
            conn = sock.connect((remoteServerIP, port))
            sock.close()
            return 'Listening'
        except socket.gaierror as name:
            logging.basicConfig(filename='%s' % stderror, format='%(asctime)s %(message)s', level=logging.DEBUG)
            logging.warning('Watch out! : %s' % name)
            sys.exit()
        except socket.error as s:
            logging.basicConfig(filename='%s' % stderror, format='%(asctime)s %(message)s', level=logging.DEBUG)
            logging.warning('Watch out! : %s' % s)
            return 'NotListening'
            sys.exit()
    def getHttpCode (self):
        port = self.port
        req=('http://localhost:'+ (str(Args[2]).replace(",", "").replace("'", "").replace("]", "")))
        status = urllib.request.urlopen(req)
        data=status.getcode()
        return data        
    def setLogentries(loglevel):
        if (loglevel == 'DEBUG'):
            logging.basicConfig(filename='%s' % stderror, format='%(asctime)s %(message)s', level=logging.DEBUG)
        else:
            logging.basicConfig(filename='%s' % stdout, format='%(asctime)s %(message)s', level=logging.INFO)

if __name__ == "__main__":
    global stderror, stdout 
    stdout=((os.environ.get('HOMEPATH', ''))+ '\INFO.log')
    stderror=((os.environ.get('HOMEPATH', ''))+ '\ERROR.log')
    Args = str(sys.argv).split()
    # Check Arguments
    if ((len(Args)) != 3):
        ServerProcess.setLogentries('DEBUG')
        logging.warning('Watch out! : Provide Hostname and Port name in arguments')
        print("Uses : python " + str(Args[0]).replace("['", "") + " MAXMEM PORT")
        sys.exit()

maxmem=str(Args[1]).replace(",", "").replace("'", "")        
process = ServerProcess(Args[2])
pid = process.getPID()
memory = process.getMemory(pid)      

for i in range(3):
    sleep(1)
    sys.stdout.flush()
    conn = process.getConnect()
    data = process.getHttpCode()
    """ If socket is listening and used memory is less than MAXMEM """
    if(( conn == 'Listening' ) and eval(maxmem)>eval(memory)  and ( data == 200 )):
        ServerProcess.setLogentries('INFO')
        logging.warning('Watch out! : Socket is %s ' % conn + 'when used memory is %s ' % memory + 'and return code is %s ' % data)
    elif(eval(maxmem) > eval(memory) and data == 200):
        ServerProcess.setLogentries('DEBUG')
        logging.warning('Watch out! : Can access url')              
    else:
        p = subprocess.Popen('tasklist /fi "MEMUSAGE ge 10" /fi "PID eq %s' % pid, stdout=subprocess.PIPE)
        proc = str(p.communicate()[0].split()[13]).lstrip("b,").replace(",", "").replace("'", "")
        os.system('taskkill /f /im %s' % proc)
        try:
            startup=(os.environ.get('CATALINA_HOME', '')+'\/bin\/startup.bat')
            os.system('%s' % startup)
            sys.exit()
        except OSError as sys:
            ServerProcess.setLogentries('DEBUG')
            logging.warning('Watch out! : Socket is %s' % sys) 

Sunday, June 23, 2013

Software Configuration Management

SCM consist three domains and eight disciplines. Each discipline has a lot of tools that support the purpose itself.  Version Identification with syntactically and semantically strong scheme is primary requirement and initial part of  SCM. Selection of tools depends upon SDLC that a project follows. Not mandatory, however best practice is to  use advanced tools with advanced process. Trend shows that project use DVCS instead of VCS in Agile SDLC. However legacy Water Fall SDLC still prefers SVN. Consider Version Controlling discipline where Git continues to take-over developer's primary source code management system and at the expense of Subversion. Interestingly, Subversion continues to be the #1 SCM but just barely. If you combine Git and GitHub usage, Subversion leads by only 1.5% points. eclipse-community-survey-results-for-2013 predicts that next year Git and GitHub will be #1. The survey also sows decline of Ant usage, strongly indicates that Maven dominating Build and Deployment Management discipline. Another two disciplines - Artifact Version and Issue Tracking, that are tightly coupled with configuration management. Managing all input artifacts in build process is required a robust tool like Artifactory or Nexus.
Dependencies management is an important part of building source code. History of version control and history of Issue tracking both contain important information. Many popular issue tracking tools comes with VCS plug-in that helps to have a clear picture of committed changes in respect of a particular issue ID. To be facilitated by the feature of issue tracking tool changes should be committed to VCS with issue ID. Finally Change Control and Release Management put the final touch of product delivery process. Project and product gets change in every release. Customer change management in enterprise policy impacts on project delivery. To add a high value to the customer project change-control and customer change-management should go together. For example, in Agile software development process customer interaction is an important and prime part for having more clarity of the requirement. Release management can be divided in three sub category namely Release Engineering, Release management and Product management. A stable software product release follows 5+1 distinct Release Engineering  phases. Simply consider the five steps like Inception, Integration, Quality-Control and last-minute issue fixing. last (+1) phase of release engineering is to rebuild and delivery final release candidate version as the final release version. Maintaining all eight disciplines with proper documentation will provide real value in software development.

Thursday, March 14, 2013

RubyOnRails - A few steps installing and creating first project

#Install MySql
yum grouplist | grep -i mysql
yum groupinstall "MySQL Database"

# If fail..........
vi /usr/bin/mysql_install_db  # edit hostname=127.0.0.1

# Start MySql....
usr/bin/mysql_install_db --user=mysql
service mysqld start
/usr/bin/mysqladmin version
/usr/bin/mysqlshow
mysql -u root

# Install RubyOnRails
wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
tar -zxvf yaml-0.1.4.tar.gz
cd yaml-0.1.4
./configure
./configure --prefix=/usr/local
make
make install

wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p194.tar.gz
tar -zxvf ruby-1.9.3-p194.tar.gz
cd ruby-1.9.3-p194
./configure
make
make install

# Check Ruby Version
ruby --version

# Install Rubygems
wget http://rubyforge.org/frs/download.php/76073/rubygems-1.8.24.tgz
tar -zxvf rubygems-1.8.24.tgz
cd rubygems-1.8.24
ruby setup.rb

# Check Gems Version
gem --version

# Install Rake
gem update --system
gem install therubyracer -v '0.9.8'
gem install rake
gem install rails

# Check Installed Gems
cd ..
gem list

# Create your first Application
rails new welcome -d mysql
cd welcome
rails generate controller welcome index
vi Gemfile # Add... gem 'therubyracer', require: "v8"
bundle install
rake db:create

# Configure route to access your first application
vi config/routes.rb # Uncomment root :to => 'welcome#index'

# Start rail server
rails server

# Check your application
http://127.0.0.1:3000/welcome/index


Wednesday, March 13, 2013

TataPhoton USB modem Configuration in RHEL5


Tata Photon Data card never get detected by RHEL-5 automatically. By default the device is available as "USB-storage (Mass Storage / SD Storage)”.  In RHEL5 even the device never initialized itself. In a dual booting system first we need to initialize the card in Windows and then restart the system (without power off) in Linux.Off course this is not the way of handling a device in RHEL, there must be some proper way, but at this moment I am unaware about that.

To get vendor ID and product ID use "lsusb" as a super user if data card is not detected by default.

#lsusb
Bus 003 Device 003: ID 12d1:140b
Bus 003 Device 001: ID 0000:0000
Bus 004 Device 001: ID 0000:0000
Bus 005 Device 001: ID 0000:0000
Bus 001 Device 001: ID 0000:0000
Bus 002 Device 001: ID 0000:0000

For more details information use following command 
#lsusb -v | grep -B4 HUAWEI

Here the first number is vendor id (0x12d1) and the second one (0x0140b) is product id. These numbers may differ depending on the make and model of the device.

To get generic USB serial driver bind to the device, USB vendor and product IDs need to be specified as a parameter when the module “usbserial” is loaded. For example, to bind the previously described device with a vendor ID of 0x12d1 and product ID of 0x140b, execute following command as a super user

#modprobe usbserial vendor=0x12d1 product=0x140b

Now we need usb_modeswitch. If it is not installed yet, please install latest available package. usb_modeswitch can send bulk messages (most likely a mass storage command) to the device which is known to initiate the mode switching. 
#usb_modeswitch -v 0x12d1 -p 0x140b -H -W

Next we need to run WvDial command-line pppd driver. Wvdialconf generates  configuration file containing information of your modem and ISP details. To avoid changing configuration file parameters, let’s create a master copy of wvdial.conf file.

#cat wvdial.conf-master
[Modem0]
Modem = /dev/ttyUSB2
Baud = 115200
SetVolume = 0
Dial Command = ATDT
Init1 = ATZ
Init3 = ATM0
FlowControl = CRTSCTS
[Dialer tataphoton]
Username = internet
Password = internet
Phone = #777
Stupid Mode = 1
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Inherits = Modem0

[Dialer Defaults]
Modem = /dev/ttyUSB2
Baud = 460800
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Stupid Mode = 1
ISDN = 0
Modem Type = USB Modem
Phone = #777
Username = internet
Password = internet

Now execute following command to connect internet through the device.

#wvdialconf /etc/wvdial.conf
#wvdial

It’s better to bring all command together in a script and run the script to connect internet through Tata-Photon USB modem.

#!/bin/bash
modprobe usbserial vendor=0x12d1 product=0x140b
usb_modeswitch -v 0x12d1 -p 0x140b -d 1
usb_modeswitch -v 0x12d1 -p 0x140b -H 1
cp /etc/wvdial.conf-master /etc/wvdial.conf
wvdialconf /etc/wvdial.conf
wvdial