Newer
Older
pystat / pystat.py
@tundra tundra on 17 Feb 2010 5 KB First cut.
#!/usr/bin/env python
# pystat.py - Count Program And Comment Lines In A Python Program
# Copyright (c) 2010 TundraWare Inc.
# For Updates See:  http://www.tundraware.com/Software/pystat

# Program Information

PROGNAME = "pystat"
BASENAME = PROGNAME.split(".py")[0]
PROGENV  = BASENAME.upper()
RCSID = "$Id: pystat.py,v 1.101 2010/02/18 01:18:46 tundra Exp $"
VERSION = RCSID.split()[2]

# Copyright Information

CPRT         = "(c)"
DATE         = "2010"
OWNER        = "TundraWare Inc."
RIGHTS       = "All Rights Reserved."
COPYRIGHT    = "Copyright %s %s, %s  %s" % (CPRT, DATE, OWNER, RIGHTS)


PROGVER      = PROGNAME + " " + VERSION + (" - %s" % COPYRIGHT)
HOMEPAGE     = "http://www.tundraware.com/Software/%s\n" % BASENAME


#----------------------------------------------------------#
#            Variables User Might Change                   #
#----------------------------------------------------------#



#------------------- Nothing Below Here Should Need Changing ------------------#


#----------------------------------------------------------#
#                       Imports                            #
#----------------------------------------------------------#

import getopt
import os
import sys


#----------------------------------------------------------#
#                 Aliases & Redefinitions                  #
#----------------------------------------------------------#



#----------------------------------------------------------#
#                Constants & Literals                      #
#----------------------------------------------------------#



#####
# Constants
#####



#####
# Literals
#####



#----------------------------------------------------------#
#              Prompts, & Application Strings              #
#----------------------------------------------------------#


#####
# Debug Messages
#####

#####
# Debug Messages
#####

DEBUGFLAG     =   "-d"
dDEBUG        =   "DEBUG"
dPROGENV      =   "$" + PROGENV

#####
# Error Messages
#####

eBADARG  =  "Invalid command line: %s!"
eERROR   =  "ERROR"


#####
# Informational Messages
#####


#####
# Usage Prompts
#####

uTable = [PROGVER + " - Count Program And Comment Lines In A Python Program",
          HOMEPAGE,
          "usage:  " + PROGNAME + " [-fhv] file, file, ...",
          "   where,",
          "          -h       print this help information",
          "          -v       print detailed version information",
          ]


#----------------------------------------------------------#
#          Global Variables & Data Structures              #
#----------------------------------------------------------#



#--------------------------- Code Begins Here ---------------------------------#


#----------------------------------------------------------#
#             Object Base Class Definitions                #
#----------------------------------------------------------#

    

#----------------------------------------------------------#
#             Supporting Function Definitions              #
#----------------------------------------------------------#


def ColumnPad(list, padchar=" ", padwidth=20):

    retval = ""
    for l in list:
        l = str(l)
        retval += l + ((padwidth - len(l)) * padchar)

    return retval.strip()

# End of 'ColumnPad()'


#####
# Print A Debug Message
#####

def DebugMsg(msg):
    PrintStderr(PROGNAME + " " + dDEBUG + ": " + msg)

# End of 'DebugMsg()'


#####
# Dump The State Of The Program
#####

def DumpState():

    # Dump the command line
    DebugMsg(ColumnPad(["Command Line", sys.argv]))

    # Names of all the state variables we want dumped
    state = [
            ]

    for k in state:
        DebugMsg(ColumnPad([k, eval(k)]))

# End of 'DumpState()'


#####
# Print An Error Message
#####

def ErrorMsg(emsg):
    PrintStderr(PROGNAME + " " + eERROR + ": " + emsg)

# End of 'ErrorMsg()'


#####
# Print An Error Message
#####

def InfoMsg(imsg):
    PrintStderr(PROGNAME + ": " + imsg)

# End of 'InfoMsg()'


#####
# Print To stderr
#####

def PrintStderr(msg, trailing="\n"):
    sys.stderr.write(msg + trailing)

# End of 'PrintStderr()'


#####
# Print To stdout
#####

def PrintStdout(msg, trailing="\n"):
    sys.stdout.write(msg + trailing)

# End of 'PrintStdout'


#####
# Print Usage Information
#####

def Usage():
    for line in uTable:
        PrintStdout(line)

# End of 'Usage()'


#----------------------------------------------------------#
#                    Program Entry Point                   #
#----------------------------------------------------------#

# Command line processing - Process any options set in the
# environment first, and then those given on the command line

OPTIONS = sys.argv[1:]
envopt = os.getenv(PROGENV)
if envopt:
    OPTIONS = envopt.split() + OPTIONS

try:
    opts, args = getopt.getopt(OPTIONS, '-f:hv')
except getopt.GetoptError as e:
    ErrorMsg(eBADARG % e.args[0])
    sys.exit(1)

for opt, val in opts:
    if opt == "-f":
        CFGFILE=val
    if opt == "-h":
        Usage()
        sys.exit(0)
    if opt == "-v":
        print RCSID
        sys.exit(0)


for filename in args:

    f = open(filename)
    lines = f.readlines()
    f.close()

    program = 0
    comment = 0
    total   = 0
    for line in lines:

        total += 1
        l = line.split("#")
        if len(l) == 1:
            if l[0].strip():
                program += 1

        else:
            comment += 1

    InfoMsg("'%s': %s Program Lines  %s Comments  %s Total" % (filename, program, comment, total))