#!/usr/software/bin/python # # File: common_plot.py # Version: 1.1 # Date: 9 Aug 2011 # Description: Plots charts from NetApp input file # Usage: common_plot.py # Input File : _python.txt # Example: /usr/software/bin/python common_plot.py filer-iso.txt # Author : arunak@netapp.com # Change : 30 Aug 2011 from author: # Description of the change: < Enhanced Title and font size added minor ticks > # ############### # EXTERNAL LIB ############### import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import sys, string, re, os, getopt from collections import deque from pylab import * from matplotlib.ticker import MultipleLocator, FormatStrFormatter ############### #GLOBAL VARIABLES ############### count =0 xvalue = [] yvalue = [] xname = "" yname = "" total_count = "" app_name = "" infh = None chart_list ="" file = "" header_name = "" output_fmt = "png" test_name = "" outputdir = "." kernel_name = "" prd_id = "" legend_name ="" ############## #COMMAND LINE FUNCTIONS ############# def help_content(): print "COMMON PLOT HELP FILE" print "========================================================" print "This script will plot the chart for the given input-file" print "========================================================" print "" print "Usage:" print " common_plot.py " print "" print "Help Option:" print "-h displays help an usage of program" print "" print "Advance Option:" print "-o directory where output files are created" print " defaults to '.'" print " directory must exist before using this option" print "" print "Advance Option Usage:" print " common_plot.py -o " print "" print "Note : Input file should be in specified format, to plot chart successfully" ############### #COMMAND LINE ARGUMENTS ############### app_name = os.path.basename(sys.argv[0]) try: opts, args = getopt.getopt(sys.argv[1:],"ho:",["help","outdir"]) except getopt.GetoptError, err: print str(err) sys.exit(0) for opt, optval in opts: if opt in ("-h", "--help"): # help with usage help_content() sys.exit(0) elif opt in ("-o", "--outdir"): # output directory outputdir = optval else: assert False, "unhandled command line option" #cmdl_arg = len(sys.argv) if len(args) > 1: print "%s: too many input files specified" % app_name sys.exit(0) if len(args) == 0: print "%s: missing " % app_name sys.exit(0) else: file = args[0] ############### #FUNCTIONS TO PLOT GRAPH AND TO RESET VALUES ############### def plot_graph(title,x,y,xlab,ylab,tstname): outputdir minorLocator = LinearLocator() print "Title of graph", title print "X-axis",x print "Y-axis",y #Set chart propertieis # plt.figure plt.figure(figsize=(9,6)) # plt.title(title) plt.xlabel("---- %s ---->" % xlab) plt.ylabel("---- %s ---->" % ylab) # plt.grid(True, which ='both') plt.grid(True) #plot graph pltx = subplot(111) plt.gcf().set_size_inches(10,10) plt.plot(x, y, "o-",label=title) # plt.legend((legend_name),loc=1) plt.suptitle(header_name_title,fontsize="medium") pltx.xaxis.set_minor_locator(minorLocator) chart_name = "%s%s%s_%s.%s" % (outputdir,os.sep,tstname,header_name,output_fmt) plt.savefig(chart_name) plt.clf() reset_values() def reset_values(): title = "" x = "" y = "" xlab = "" ylab = "" tstname = "" xvalue = [] yvalue = [] ############### #MAIN PROGRAM STARTS HERE, GET INPUT FILE TO READ VALUES ############### try: infh = open(file, "r") except IOError: print "%s: unable to open '%s'" % (app_name, file) exit # Open the file for reading if infh: data = infh.read() chart_list = re.findall(r"\w+\s\w\d",data) # print len(chart_list) total_count = len(chart_list) # print chart_list infh = open(file, "r") for line in infh: if line.isspace(): # print "Blank Line" line = infh.next() test= re.match(r"^(\w+)\s+=\s+(\w+)",line) if test != None: test_name = test.group(2) # print "TEST NAME :",test_name #break line = infh.next() kern = re.match(r"^(\w+)\s+=\s+(.*)",line) if kern != None: kernel_name = kern.group(2) # print "Kernel :",kernel_name #break line = infh.next() pid = re.match(r"^(\w+)\s+=\s+(.*)",line) if pid != None: prd_id = pid.group(2) # print "Product ID:",prd_id break else: print "Test Name Not found in input file" sys.exit(0) count = 0 while (count < total_count): #Get item from chart-list list_item = chart_list[count] # print list_item infh = open(file, "r") for line in infh: if re.match(list_item,line): header = re.match(r"(\w+\s\w\d)\:(\w+)", line) if header != None: header_name = header.group(2) # legend_name = header_name # print "header name",header_name else: print "Please Provide header name" break line = infh.next() # print "Next 1line",line line=line.replace('=',',') line=line.strip() # print "after strip",line xaxis_list = line.split(',') xname = xaxis_list.pop(0) # print "Name-",xname xvalue = [] xvalue = xaxis_list # print xname,xvalue line = infh.next() # print "Next 1line",line line=line.replace('=',',') line=line.strip() # print "after strip",line yaxis_list = line.split(',') yname = yaxis_list.pop(0) # print "Name-",yname yvalue = [] yvalue = yaxis_list # print yname,yvalue header_name_title = "" header_name_title += "Test_Name : %s\n " %(test_name) header_name_title += "Kernel_Name : %s \n" %(kernel_name) if prd_id: print "Product id found" header_name_title += "Product_ID : %s \n" %(prd_id) header_name_title += "Title : %s \n" %(header_name) plot_graph(header_name_title,xvalue,yvalue,xname,yname,test_name) print "Completed plotting of graph",header_name print "" count = count + 1