#!/usr/bin/env python
#
#*******************************************************************************
#
#Copyright (c) 2009, Perforce Software, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#*******************************************************************************
#
#Author: Stephen Moon
#Summary: Create a large number of users using a CSV file and a template
#
#*******************************************************************************
from subprocess import Popen,PIPE,STDOUT
from subprocess import Popen,PIPE,STDOUT
import optparse,csv,re
def processFile(file,row,lines):
patUser = re.compile('^(User:)\s+(\S+).*$')
patEmail = re.compile('^(Email:)\s+(\S+).*$')
patFName = re.compile('^(FullName:)\s+(.*)$')
fd = open(file,'w')
for eachline in lines:
#return an object for each regex match
vUser = patUser.match(eachline)
vEmail = patEmail.match(eachline)
vFName = patFName.match(eachline)
priority = 0
match = 0
for i,v in enumerate([vUser,vEmail,vFName]):
#print "user: %s, email: %s, fname: %s" % (vUser,vEmail,vFName)
if v is not None:
priority = i
match = 1
if not match:
priority = 99
#print "p: %d",priority
if priority == 99:
fd.write(eachline)
elif priority == 0:
fd.write(eachline.replace(vUser.group(2),row[0]))
print "user: %s, user1: %s" % (vUser.group(2),row[0])
print "priority: %d line: %s" % (priority,eachline)
elif priority == 1:
fd.write(eachline.replace(vEmail.group(2),row[1]))
print "email: %s, email1: %s" % (vEmail.group(2),row[1])
print "priority: %d line: %s" % (priority,eachline)
elif priority == 2:
fd.write(eachline.replace(vFName.group(2),row[2]))
print "fname: %s, fname1: %s" % (vFName.group(2),row[2])
print "priority: %d line: %s" % (priority,eachline)
vUser = None
vEmail = None
vFName = None
fd.close()
return file
def main():
parser = optparse.OptionParser(usage="%prog [-h,-v] csvName userTempl", version="%prog v0.9")
parser.add_option("-v","--verbose",action="store_true",dest="verbose",help="Print debug messages to stdout")
(options,args) = parser.parse_args() #by default sys.argv[1:]
if len(args) != 2:
parser.error("Incorrect number of arguments")
#read a CSV file
file = csv.reader(open(args[0], "r"),delimiter='\n',quoting=csv.QUOTE_NONE)
rlist = []
clist = []
for row in file: #CSV file which contains the list of users
rlist = row[0].split(",")
clist.append(rlist)
p4userOut = ['p4','-u','smoon','-p','20092','user','-i','-f']
lines = open(args[1]).readlines() # all lines in the template file
for row in clist: #for each user
eachFile = processFile(args[1],row,lines)
openedFile = open(eachFile,"r") #modified template file
p1 = Popen(p4userOut,stdin=openedFile,stdout=PIPE)
output = p1.communicate()[0]
openedFile.close() #close the modified template file
#print output
if __name__ == '__main__':
main()