#!/usr/local/bin/python
# usermetrics.py
#
# imports
import sys, os, string, re, isitalabel, smtplib
debug = 0
lines = []
diffs = []
mailhost = 'mail.symantec.com'
sleeptime = 60
try:
fromaddr = os.environ['EMAIL']
except:
print "No email address found in environment, using cm@symantec.com\n"
fromaddr = 'cm@symantec.com'
###################################################################################################################
USAGE = """
Usage:
This command requires the current build label as a parameter.
For Example:
python usermetrics.py JACKALOPE.17
"""
###################################################################################################################
# This will send out an email.
def sendit (emailaddress, email):
try:
mailport=smtplib.SMTP(mailhost)
except:
sys.stderr.write('Unable to connect to SMTP host "' + mailhost + '"!\n')
return
message = ""
for line in email:
message = message + line
message = message + """
====================
From: CM Team
cm.symantec.com
====================
"""
mailport.sendmail(fromaddr, emailaddress,'TO: ' + emailaddress + '\nSubject: CM Change report for ' + sys.argv[1] +'\n\n' + message)
mailport.quit()
###################################################################################################################
# This will check to see that file_ownerships.txt exists under the label
def checkforfile (labelname):
# p4 files @label
found = 0
if debug > 0:
print labelname
for line in os.popen ('p4' + ' files @' + labelname, 'r').readlines():
if debug > 0:
print line
name = filename_pattern.match (line)
if name != None:
thestring = name.group()
thelist = string.split(thestring)
if debug > 0:
print thelist[0]
ownerships = thelist[0]
found = found + 1
if found == 0:
print "file_ownerships.txt not found under label '" + labelname + "', ownermetrics will not be generated"
return 1
if found > 1:
print "Multiple copies of file_ownerships.txt were found under the label '" + labelname + "', ownermetrics will not be generated"
return 2
return ownerships
###################################################################################################################
# This will read in the metrics to be parsed for users and files in file_ownerships.txt
def readmetrics (ownerships):
for line in os.popen ('p4 print -q ' + ownerships, 'r').readlines():
x = string.find (line, '#')
if debug > 0:
print x
if x != -1:
if x == 0:
pass
else:
lines.append (line[0,x])
else:
lines.append (line)
return
###################################################################################################################
# This will open the source_code_diffs file
def readdiffs ():
if sys.platform == "win32":
try:
file = open ("c:\\build\\" + sys.argv[1] + ".source_code_diffs")
except:
print "Could not open c:\\build\\" + sys.argv[1] + ".source_code_diffs for reading."
print "You must first run diff.py on this label to generate this file. If you are running the main metrics.py make sure the second label comes second."
return 1
else:
try:
file = open ("metrics" + os.sep + sys.argv[1] + ".source_code_diffs")
except:
print "Could not open metrics" + os.sep + sys.argv[1] + ".source_code_diffs for reading."
print "You must first run diff.py on this label to generate this file. If you are running the main metrics.py make sure the second label comes second."
return 1
for line in file.readlines ():
diffs.append (line)
return 0
###################################################################################################################
# This will match lines with diffs in the diffs file
def parse (line):
if debug > 0:
print "CALLING parse()"
theline = string.split (line,',')
file_loc = string.strip (theline[0])
emailaddress = string.strip (theline [3])
email = []
if file_loc[0] == "\"" and file_loc[-1] == "\"":
file_loc = file_loc [1:-1]
file_pattern = re.compile ('==== ' + file_loc + '#', re.IGNORECASE)
new_file_pattern = re.compile ('==== //.*', re.IGNORECASE)
found = None
newfile = None
if debug > 0:
linenumber = 0
for line in diffs:
if debug > 0:
linenumber = linenumber + 1
print "linenumber = " + str(linenumber) + ";found = " + str(found) + ";newfile = " + str(newfile)
if found == None:
newfile = new_file_pattern.match (line)
if (newfile != None):
found = file_pattern.match (line)
if found != None:
email.append (line)
else:
newfile = new_file_pattern.match (line)
if newfile != None:
found = None
else:
email.append (line)
sendit (emailaddress, email)
###################################################################################################################
# This will create the individual reports for users
def buildfiles ():
return
###################################################################################################################
# This will mail those files out to the users
def mailfiles ():
return 0
###################################################################################################################
if __name__ == '__main__':
if len (sys.argv) < 2:
print USAGE
sys.exit (1)
x = isitalabel.check(sys.argv[1])
if x == 0:
print "There is no label " + sys.argv[1]
sys.exit(1)
filename_pattern = re.compile ('.*/file_ownerships.txt#.*', re.IGNORECASE)
# check for the file_ownerships.txt file
ownerships = checkforfile(sys.argv[1])
if debug > 0:
print ownerships
if (ownerships == 1) or (ownerships == 2):
sys.exit(1)
# check for the source_code_diffs file
error = readdiffs ()
if error != 0:
sys.exit (error)
readmetrics (ownerships)
for line in lines:
parse (line)