This file is indexed.

/usr/lib/python2.7/dist-packages/PyMetrics/PyMetrics.py is in pymetrics 0.8.1-7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#! /usr/bin/env python
""" PyMetrics - Complexity Measurements for Python code.

    Orignally based on grop.py by Jurgen Hermann.
    Modified by Reg. Charney to do Python complexity measurements.
    
    Copyright (c) 2001 by Jurgen Hermann <jh@web.de>
    Copyright (c) 2007 by Reg. Charney <charney@charneyday.com>

    All rights reserved, see LICENSE for details.
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    $Id$
"""
__revision__ = "$Id$"
__author__ = 'Reg. Charney <charney-at-charneyday.com>'

# Imports
import sys
import string
from processargs import ProcessArgs, ProcessArgsError
import token
import sqltokenout
import sqldataout
import csvout
from lexer import Lexer
from compute import ComputeMetrics
from globals import *

PYTHON_VERSION = sys.version[:3]

#############################################################################
### Main script for PyMetrics utility.
#############################################################################

def __importMetricModules( includeMetrics ):
    """ Import the modules specified in the parameter list.
    
    includeMetrics is a list of (metricModuleName, metricClassName) 
    pairs. This function defines a dictionary containing only valid 
    module/class names. When an error is found, the invalid 
    module/class pair is removed from the included list of metrics.
    """
    i = 0
    metricModules = {}
    if PYTHON_VERSION < '2.5':
      pfx = '' # this fix is for Python 2.4
    else:
      pfx = 'PyMetrics.'
    for m,n in includeMetrics:
        try:
            mm = pfx + m
            if PYTHON_VERSION < '2.5':
              mod = __import__( mm, globals(), locals(), [m] )
            else:
              mod = __import__( mm, fromlist=[m] )
            metricModules[m] = mod
            i += 1
        except ImportError:
            sys.stderr.write( "Unable to import metric module %s -- ignored.\n\n" % mm )
            # remove the erroneous metric module/class tuple
            del includeMetrics[i]
            
    return metricModules

def __instantiateMetric( metricModules, context, runMetrics, metrics, pa ):
    """ Instantiate all user specified metric classes.
    
    The code works by finding the desired metric class in a metric module and 
    instantiating the class. It does this by assuming that the metric
    class is in the dictionary of the metric module.
    """
    metricInstance = {}
    inclIndx = -1
    for m,n in pa.includeMetrics:
        inclIndx += 1
        try:
          metricInstance[m] = None        # default value if metric class does not exist.
          metricInstance[m] = metricModules[m].__dict__[n]( context, runMetrics, metrics, pa )
        except KeyError:
          sys.stderr.write( "Module %s does not contain metric class %s -- metric %s ignored.\n\n" % (m,n,m) )
          del( metricInstance[m] )
          del( pa.includeMetrics[inclIndx] )
        
    return metricInstance
            
def __stats( so, m, inFileName, label, *args ):
    """ Print line of statistics."""
    result = string.join(map(str, args), '')
    print "%11s    %s" % (result, label)
    so and so.write( m, inFileName, label, result )
        
def __printSummary( so, context, runMetrics, metrics, pa ):
    """ Print basic summary information."""
    # the following loop is a very, very ugly hack to distinguish between
    # tokens as they appear in the source; semantically generated tokens,
    # like DOCSTRING; and NONTOKENs, like numComments
    
    keys = []
    for k in metrics.keys():
        if str( k ).isdigit():
            keys.append( (token.tok_name[k],k,metrics[k]) )
        elif len( str( k ).split() ) > 1:
            keys.append( (k,SEMTOKEN,metrics[k]) )        
        else:
            keys.append( (k,NONTOKEN,metrics[k]) )
    keys.sort()
    
    inFileName = context['inFile']
    if pa.genKwCntSw:
        hdr = "Counts of Token Types in module %s" % context['inFile']
        print
        print hdr
        print "-"*len(hdr)
        print
        for k,t,v in keys:
            if (pa.zeroSw or v):
                if t != NONTOKEN:
                    __stats( so, 'basic', inFileName, k, v )
        print

    if pa.genBasicSw:
        __displayBasicMetrics( keys, pa, so, inFileName )

def __displayBasicMetrics( keys, pa, so, inFileName ):
    """ Display the Basic metrics that PyMetrics computes."""
    hdr = "Basic Metrics for module %s" % inFileName
    print
    print hdr
    print "-"*len( hdr )
    print
    for k,t,v in keys:
        if t==NONTOKEN:
            if pa.zeroSw or not v in ([],{},(),0,0.00):
                __stats( so, 'basic', inFileName, k, v )
    print
    
def main():
    """ Main routine for PyMetrics."""
    # process command line args
    try:
        pa = ProcessArgs()
    except ProcessArgsError, e:
        sys.stderr.writelines( str(e) )
        return
      
    if pa.genNewSw:
        __deleteOldOutputFiles( pa )
        
    so, od = __genNewSqlCmdFiles( pa )
    co = __genNewCsvFile( pa )
    
    # import all the needed metric modules
    metricModules = __importMetricModules( pa.includeMetrics )

    runMetrics = {} # metrics for whole run
    metrics = {}    # metrics for this module
    context = {}    # context in which token was used
    
    # main loop - where all the work is done
    for inFileName in pa.inFileNames:

        metrics.clear()
        context.clear()
        context['inFile'] = inFileName
        
        # instantiate all the desired metric classes
        metricInstance = __instantiateMetric( metricModules, context, runMetrics, metrics, pa )
        
        cm = ComputeMetrics( metricInstance, context, runMetrics, metrics, pa, so, co )
    
        # define lexographical scanner to use for this run
        # later, this may vary with file and language.
        lex = Lexer()
        
        if not pa.quietSw:
            print "=== File: %s ===" % inFileName

        try:
            lex.parse( inFileName ) # parse input file
            
            metrics["numCharacters"] = len(lex.srcLines)
            metrics["numLines"] = lex.lineCount                  # lines of code
        
            metrics = cm( lex )
            
            # if printing desired, output summary and desired metrics
            # also, note that this preserves the order of the metrics desired
            if not pa.quietSw:
                __printSummary( od, context, runMetrics, metrics, pa )
                for m,n in pa.includeMetrics:
                    if metricInstance[m]:
                        result = metricInstance[m].display()
                        if metrics.has_key(m):
                            metrics[m].append( result )
                        else:
                            metrics[m] = result
                        for r in result.keys():
                            od and od.write( m, inFileName, r, result[r] )
        except IOError, e:
            sys.stderr.writelines( str(e) + " -- Skipping input file.\n\n")
            
    co and co.close()
    
    result = {}
    if len( pa.inFileNames ) > 0:
      for m,n in pa.includeMetrics:
          if metricInstance[m]:
              result = metricInstance[m].processRun( None )
              if result:
                  for r in result.keys():
                      od and od.write( m, None, r, result[r] )
    od and od.close()
    
    if not pa.quietSw:
        n = len( pa.inFileNames )
        print
        print "*** Processed %s module%s in run ***" % (n,(n>1) and 's' or '')

def __genNewCsvFile( pa ):
    """ Determine output CSV data file, if any, 
    and check it can be created."""
    co = None
    try:
        if pa.genCsvSw:
            co = csvout.CsvOut( pa.csvFileName, genHdrSw=pa.genHdrSw, genNewSw=pa.genNewSw )
    except StandardError, e:
        # this should not occur - it should be handled in processArgs.
        sys.stderr.writelines( str(e) + " -- No CSV file will be generated\n\n" )
        pa.genCsvSw = False
    
    return co

def __genNewSqlCmdFiles( pa ):
    """ determine output SQL tokens command file, if any, 
    and check it can be created ."""
    so = None
    co = None
    od = None
    fd = None
    
    try:
        if pa.genSqlSw:
            if pa.sqlFileName:
                fd = open( pa.sqlFileName, 'a' )
            else:
                fd = sys.stdout
            so = sqltokenout.SqlTokenOut( fd, pa.libName, pa.sqlFileName, pa.sqlTokenTableName, pa.genNewSw, pa.genExistsSw )
            od = sqldataout.SqlDataOut( fd, pa.libName, pa.sqlFileName, pa.sqlMetricsTableName, pa.genNewSw, pa.genExistsSw )
    except StandardError, e:
        # this should not occur - it should be handled in processArgs.
        sys.stderr.writelines( str(e) + " -- No SQL command file will be generated\n\n" )
        pa.genSqlSw = False
        so and so.close()
        od and od.close()
        so = None
        od = None
        
    return so, od

def __deleteOldOutputFiles( pa ):
    """ Generate new output files by ensuring old files deleted."""
    import os
    try:
        if pa.genSqlSw and os.path.exists( pa.sqlFileName ):
            os.remove( pa.sqlFileName )
    except IOError, e:
        sys.stderr.writelines( str(e) )
        pa.genSqlSw = False
        
    try:
        if pa.genCsvSw and os.path.exists( pa.csvFileName ):
            os.remove( pa.csvFileName )
    except IOError, e:
        sys.stderr.writelines( str(e) )
        pa.genCsvSw = False
    return

if __name__ == "__main__":
    main()
    sys.exit(0)