Ubuntu insights, Programming in groovy, java, et als!

Showing posts with label Scripts. Show all posts
Showing posts with label Scripts. Show all posts

Monday, January 23, 2012

Find Source code Line Numbers of Methods at Runtime


Comparing a source file and a runtime equivalent object of the class, here is a ten liner groovy script that determines the line numbers of every method inside a class. A plausible approach to do this in java is is to throw a stacktrace explicitly wherever needed and examine it to determine the line number at the point of execution. This I find is too clumsy a way. One can also take an other approach by parsing the source code file, creating a tree node structure and manipulate it but then the problem with this approach is that one has to reinvent the whole wheel again.

Groovy's dynamism comes to rescue here exposing the runtime object to the programmer during execution. At runtime, one can inspect an object, study its methods and properties and manipulate them as per the need. This can also be done in java using the Reflection API but I find it too monstrous and verbose. I resort to groovy instead. Here's the ten liner script for the same.

//supply the source file path, the only input to the script
def sourceFilePath = 'C:\\Temp\\GroovyUtils\\Sample.groovy'

//a map that stores method names vs line numbers as key val pairs
def methodToLineMap = [:]

//find all the methods of the Sample class and assign default line number as 0 first.
def listOfMethods = new Sample().metaClass.methods*
listOfMethods .name.sort().unique().each{
    methodToLineMap.put(it,0)
}


//parse src file to find the line numbers of methods & store line number in the methodMap
def idx = 0
new File(sourceFilePath).eachLine{ line ->
    idx++
    methodToLineMap.keySet().each{method ->
        if(line.contains(method)){
            methodToLineMap[method] = idx
        }
    }
}

//print the elements of method vs line number map
methodToLineMap.each{
    if(it.value!=0)
        println it
}

PS : You can also supply a java source code file (in sourceFilePath) to find out the line numbers of the java class's methods but ensure that the .class file is on the classpath while running the script.

Thursday, December 22, 2011

Running Cobertura as Ant Script

I posted about configuring eCobertura plugin for eclipse in the previous blog post. In this post, I will describe the bare minimum steps to run Cobertura as an ant build script from eclipse. Cobertura unlike eCobertura plugin, provides advanced code coverage reporting in xml and html formats. This is much more flexible for custom configuration as it can be controlled via the ant script. Replicate the steps below :

1) Download cobertura zip.

2) Create a java/groovy project named "Sample" in eclipse.

3) Create a simple java/groovy class and a corresponding unit testcase class to test it.. Place them in seperate or same packages as you wish.

4) Create a folder called lib under the Sample project folder (as Sample/lib).. Place junit.jar, apache ant.jar, and all the jars in the downloaded Cobertura zip (most importantly cobertura.jar) into the lib folder and include the jars in the project's classpath.

5) From the downloaded Cobertura zip, pick up the two files build.properties and build.xml and place them in your Sample project base. (as Sample/build.xml and Sample.build.properties)

6) The build.xml is the ant script to run Cobertura and it uses the configuration in build.properties file.

7) Change the contents of build.properties as shown below:

build.properties
---------------------------------------------------------------------------------------------------------------------


# The source code for the examples can be found in this directory
src.dir=src

# The path to cobertura.jar
cobertura.dir=cobertura/complete

# Classes generated by the javac compiler are deposited in this directory
classes.dir=cobertura/complete/classes  

# Instrumented classes are deposited into this directory
instrumented.dir=cobertura/complete/instrumented

# All reports go into this directory
reports.dir=cobertura/complete/reports    


# Unit test reports from JUnit are deposited into this directory                          
reports.xml.dir=${reports.dir}/junit-xml       
reports.html.dir=${reports.dir}/junit-html       

# Coverage reports are deposited into these directories
coverage.xml.dir=${reports.dir}/cobertura-xml 
coverage.summaryxml.dir=${reports.dir}/cobertura-summary-xml
coverage.html.dir=${reports.dir}/cobertura-html

---------------------------------------------------------------------------------------------------------------------


8) Now change the contents of build.xml as shown below:


build.xml 

---------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<project name="Sample" default="coverage" basedir=".">

 <description>
    Cobertura - http://cobertura.sourceforge.net/
    Copyright (C) 2003 jcoverage ltd.
    Copyright (C) 2005 Mark Doliner &lt;thekingant@users.sourceforge.net&gt;
    Copyright (C) 2006 Dan Godfrey
    Cobertura is licensed under the GNU General Public License
    Cobertura comes with ABSOLUTELY NO WARRANTY
    </description>

 <property file="build.properties" />

 <path id="cobertura.classpath">
      <fileset dir="lib">
           <include name="*.jar"/>
       </fileset>
 </path>

 <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
 <target name="init"> 
  <mkdir dir="${classes.dir}" />
  <mkdir dir="${instrumented.dir}" />
  <mkdir dir="${reports.xml.dir}" />
  <mkdir dir="${reports.html.dir}" />
  <mkdir dir="${coverage.xml.dir}" />
  <mkdir dir="${coverage.summaryxml.dir}" />
  <mkdir dir="${coverage.html.dir}" />
 </target>



 <target name="compile" depends="init">
  <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
   <classpath refid="cobertura.classpath" />
  </javac>
 </target>

 <target name="instrument" depends="init,compile">
  <!--
   Remove the coverage data file and any old instrumentation.
  -->
  <delete file="cobertura.ser"/>
  <delete dir="${instrumented.dir}" />
  <!--
   Instrument the application classes, writing the
   instrumented classes into ${build.instrumented.dir}.
  -->
  <cobertura-instrument todir="${instrumented.dir}">
   <!--
    The following line causes instrument to ignore any
    source line containing a reference to log4j, for the
    purposes of coverage reporting.
   -->
   <ignore regex="org.apache.log4j.*" />
   <fileset dir="${classes.dir}">
    <!--
     Instrument all the application classes, but
     don't instrument the test classes.
    -->
    <include name="**/*.class" />
    <exclude name="**/*Test.class" />
   </fileset>
  </cobertura-instrument>
 </target>

 <target name="test" depends="init,compile">
  <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
   <!--
    Note the classpath order: instrumented classes are before the
    original (uninstrumented) classes.  This is important.
   -->
   <classpath location="${instrumented.dir}" />
   <classpath location="${classes.dir}" />
   <!--
    The instrumented classes reference classes used by the
    Cobertura runtime, so Cobertura and its dependencies
    must be on your classpath.
   -->
   <classpath refid="cobertura.classpath" />
   <formatter type="xml" />
   <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
   <batchtest todir="${reports.xml.dir}" unless="testcase">
    <fileset dir="${src.dir}">
     <include name="**/*Test.java" />
    </fileset>
   </batchtest>
  </junit>



  <junitreport todir="${reports.xml.dir}">

   <fileset dir="${reports.xml.dir}">

    <include name="TEST-*.xml" />

   </fileset>

   <report format="frames" todir="${reports.html.dir}" />

  </junitreport>

 </target>



 <target name="coverage-check">

  <cobertura-check branchrate="34" totallinerate="100" />

 </target>



 <target name="coverage-report">

  <!--

   Generate an XML file containing the coverage data using

   the "srcdir" attribute.

  -->

  <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />

 </target>



 <target name="summary-coverage-report">

  <!--

   Generate an summary XML file containing the coverage data using

   the "srcdir" attribute.

  -->

  <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />

 </target>



 <target name="alternate-coverage-report">

  <!--

   Generate a series of HTML files containing the coverage

   data in a user-readable form using nested source filesets.

  -->

  <cobertura-report destdir="${coverage.html.dir}">

   <fileset dir="${src.dir}">

    <include name="**/*.java"/>

    <include name="**/*.groovy"/>

   </fileset>

  </cobertura-report>

 </target>



 <target name="clean" description="Remove all files created by the build/test process.">

  <delete dir="${classes.dir}" />

  <delete dir="${instrumented.dir}" />

  <delete dir="${reports.dir}" />

  <delete file="cobertura.log" />

  <delete file="cobertura.ser" />

 </target>



 <target name="coverage" depends="compile,instrument,test,coverage-report,summary-coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>



</project>

---------------------------------------------------------------------------------------------------------------------

9) Running the above build.xml as an ant script in eclipse will create a folder hierarchy. The generated xml and html format reports can be found at Sample/cobertura/complete folder.

PS: Instead of running the script from eclipse you can also run the build.xml from a command line using the ant -[options] command.





Monday, December 05, 2011

Pharo Beginner : My first Smalltalk Program


DockingBarMorph new
  position: 0@225;

        addMorph: (SimpleButtonMorph new
                          label: 'Close';
                          target: [DockingBarMorph allInstances last delete];
     height: 55;
                          actionSelector: #value);

        addMorph: (SimpleButtonMorph new
            label: 'Open Transcript';
                          target: [Transcript open.
                                        Transcript show: '*** Default text in Transcript ***'
                                        ];
                          actionSelector: #value);

addMorph: (SimpleButtonMorph new
                        label: 'Open Browser';
     target: [Browser open.];
                        actionSelector:#value);

       addMorph: (SimpleButtonMorph new
                            label: 'New Workspace ';
                            target: [Workspace new open.];
                            actionSelector:#value);

addMorph: (SimpleButtonMorph new
                          label: 'Dock';
                          target: [UIManager inform: 'Hello world.. This is a sample Dock..'];
                         height: 55;
                          actionSelector: #value);
  openInWorld.


*************************

Tried to add up custom launchers for pharo development utilities. Currently implemented new Workspace open, System Browser, Transcript, etc.. Will have to make it complete so that the dock should be able to launch every component under the conventional right click popup in the pharo environment..

A simple pharo starter program which you can fiddle and extend, after a thorough understanding on ProfStef go.




Friday, December 02, 2011

Tutorial : List Operations in Python

#!usr/bin/python
""" All text within triple quotes is treated as comments in python """
""" This tutorial explains lists in python with the simplest operations as examples"""
""" Standard string concatenation using + operator """
""" Prints the message in a new line """
def printMessage(str):
print ">>>>> "+str
return


""" For loop : A similiar equivalent of each closure in groovy """
""" Note that indents are the only way to tell the interpreter about the blocks """
"""Also note that the below print it, prints elements in same line with space separated i.e a typical equivalent of System.out.print in java"""
def printList(aList):
for it in aList :
print it,
print
return



""" ********************** Start scripting : list operations ********************** """
list = [10, 1, 2, 3, 4, 5, 6]

"""*** Print the elements of the list *** """
list.append(9)
printMessage("Initial elements in the list : ")
printList(list)

"""*** add an element to the end of the list *** """
lastVal = 9
list.append(lastVal)
printMessage("Elements after appending a new element "+str(lastVal)+" at the end of the list : ")
printList(list)

"""*** insert element at index i *** """
insertVal = 8
indice = 7
list.insert(indice, insertVal)
printMessage("Elements after inserting : "+str(insertVal)+" at index : "+str(indice))
printList(list)

"""*** sort the elements ascending order by default *** """
list.sort()
printMessage("Elements after sort : ")
printList(list)

"""*** Reverse the elements : same as groovy *** """
list.reverse()
printMessage("Elements after reversal : ")
printList(list)

"""*** Removes the last element *** """
list.pop()
printMessage("Elements after removing last element : ")
printList(list)

"""*** Removes element at specified index *** """
index=3
list.pop(index)
"""Note the string cast below..A typical toString() equivalent in java"""
printMessage("Elements after removing element of index at : "+str(index))
printList(list)

"""*** Removes element with the value specified *** """
value=9
list.remove(value)
printMessage("Elements after removing the value : "+str(value))
printList(list)

""" *** number of times the element 1 occurs in a *** """
countFor = 1
printMessage("The number of times element : "+str(countFor)+" occurs in the list")
print list.count(countFor)

""" ****************** Some more looping and branch conditions ****************** """

""" *** Find the smallest element in the list using a typical for equivalent of eachWIthIndex groovy closure*** """
small = list[0]
smallestElementIndex = 0
for index, item in enumerate(list):
if item < small :
small = item
smallestElementIndex = index
print  "The smallest element of the list is "+str(small)+" at index "+str(smallestElementIndex)


""" *** While loop implementation *** """
printMessage("A simple while loop in python to convey : ")
sizeOfList = len(list)
i=0
while i<sizeOfList:
print "Python is fun \m/"
i = i + 1

--------------------------------------------------------------------------------------------------------------

Output for the above list operations performed :


>python -u "PythonBasicListOps.py"
>>>>> Initial elements in the list : 
10 1 2 3 4 5 6 9
>>>>> Elements after appending a new element 9 at the end of the list : 
10 1 2 3 4 5 6 9 9
>>>>> Elements after inserting : 8 at index : 7
10 1 2 3 4 5 6 8 9 9
>>>>> Elements after sort : 
1 2 3 4 5 6 8 9 9 10
>>>>> Elements after reversal : 
10 9 9 8 6 5 4 3 2 1
>>>>> Elements after removing last element : 
10 9 9 8 6 5 4 3 2
>>>>> Elements after removing element of index at : 3
10 9 9 6 5 4 3 2
>>>>> Elements after removing the value : 9
10 9 6 5 4 3 2
>>>>> The number of times element : 1 occurs in the list
0
The smallest element of the list is 2 at index 6
>>>>> A simple while loop in python to convey : 
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
>Exit code: 0






Tuesday, November 15, 2011

Count the Lines of Code in Your Project

I was searching through eclipse if there was an option somewhere within the IDE that could count the total lines of code in my project. After 5 min of Google struggle, I got to know that I need to install a metrics plugin to be able to do so. Reluctant to install any additional plugin, I resorted to groovy, spending an equivalent 5 min of time for this piece of code that counts the total number of lines in all the files within a directory at any depth.

Run the below script with dirPath variable pointing to your project base and addup comma seperated extensions in the filterFileExtensions variable list to specify the filetypes you wish to index , as in java, groovy or any text rendering file extensions like txt, html, etc. 

//------------------- Start Groovy Script --------------->


def filterFileExtensions = ["groovy", "java"] 
def dirPath = 'C:\\VKWorks\\Sampleproject'


def base = new File(dirPath)
def obj = new FileUtil(list:filterFileExtensions)
obj.recurseCountCodeLines(base)
obj.printResult()
//------------------- End Groovy Script --------------->


class FileUtil{
def lineCount = 0
def filesProcessed = 0
def list 


def recurseCountCodeLines(file){
    if(file.isDirectory()){
        file.listFiles().each{recurseCountCodeLines(it)}
    }


    else{
    def fileExtn = file.getName().tokenize(".").last()
    if(list.contains(fileExtn)){
        println "processing file $file"
            file.getText().eachLine{ line ->
            def isCommentedCode = line.startsWith("/") ||
                                  line.startsWith("*")                      
                if(line.size()>0 && !isCommentedCode)
                    lineCount++
            }
            filesProcessed ++
        }
    }
}
    
def printResult(){
    println "---------------------------------"
    println "files processed : $filesProcessed "
    println "Code line count : $lineCount"
    println "---------------------------------"
}
}



PS on performance: 12,000 lines of code in 150 odd files within various depths of the specified directory were retrieved in less than a second.