https://www.perforce.com/blog/090224/using-p4-user-o-and-i-automate-creating-new-users
https://www.perforce.com/perforce/doc.current/manuals/p4script/03_python.html
http://stackoverflow.com/questions/14522682/execute-perforce-commands-through-python
http://stackoverflow.com/questions/184187/how-do-i-check-out-a-file-from-perforce-in-python
http://stackoverflow.com/questions/589093/python-persistent-popen
http://stackoverflow.com/questions/28318673/login-to-perforce-from-commandline
http://community.scmgalaxy.com/blog/view/15512/ways-to-pass-password-in-triggers-file-or-script-file-in-perforce
http://forums.perforce.com/index.php?/topic/1412-addremove-users-from-group/
https://www.perforce.com/perforce/doc.current/manuals/p4sag/chapter.superuser.html
https://www.perforce.com/perforce/doc.current/manuals/cmdref/p4_passwd.html
#!/usr/bin/env python
# coding: utf-8
import re
import subprocess
import shlex
import os
import argparse, sys
# usage: ./p4_create_user.py user_id manager_id group_id location_id : 4 required arguments
script_objective = '''
Purpose of the script:
Create Perforce user and send email to the user, user's manager, and release engineering group.
'''
# Message display when there are not enough arguments on command line
if len(sys.argv) < 2:
print "Please run the script with argument -h for help"
exit(0)
# argument parsing:
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
add_help=True, description = '', epilog = script_objective)
parser.add_argument('user_id', help = 'user_id for the new perforce user')
parser.add_argument('manager_id', help = 'email_id of user\'s manager, without domain name similar format as user_id')
parser.add_argument('group_id', help = 'perforce group id to which the user_id should be member of')
parser.add_argument('location_id', help = 'one of the location ids hq, bdc, sdc, tdc')
args = parser.parse_args()
user_id = args.user_id
manager_id = args.manager_id
group_id = args.group_id
location_id = args.location_id
email_domain = '@ruckuswireless.com'
email_from = 'perforce-noreply' + email_domain
email_to = user_id + email_domain
email_cc = manager_id + email_domain + ',releng-team' + email_domain
email_bcc = ''
subject = 'Perforce account for ' + user_id
server_proxy_map = {'hq':'perforce:1666', 'bdc':'bdc-p4.video54.local:1999', 'sdc':'new-sdc-p4.video54.local:1666', 'tdc':'tdc-p4.video54.local:1666'}
# perforce groups on 4/21/2016
perforce_groups = ['Qubercomm', 'Qubercomm', 'Tools', 'admin', 'automation', 'bdc', 'bdc-ap-contractors', \
'cdc', 'cdc_simtools', 'espp-contractor', 'espp-employee', 'firmware.txt', 'idc', 'legal', \
'odc', 'odc_automation', 'p4users', 'pkiteam', 'release-leads', 'sdc', 'sdc_fm', 'service_users', \
'small-cell', 'sz34_integ', 'sz35_integ', 'sz35_platform', 'tpmteam', 'xclaim-contractor']
p4_server_name = server_proxy_map[location_id]
account_msg = '''
Dear ''' + user_id + ',' + '''
Welcome to Ruckus Family! We at Ruckus use Perforce as Source Code Management system and here is your account credential details:
Server – ''' + p4_server_name + '''
User name – ''' + user_id + '''
Password - Welcome2Ruck
Please download and install client application of your need (P4V: Visual Client and/or P4: Command-Line Client) from http://www.perforce.com/downloads/latest-components#clients
Feel free to contact us in case you need further guidance regarding Perforce once you go through some of the study materials.
You can also refer in-house information from https://jira-wiki.ruckuswireless.com/display/SCM/Getting+Started+with+Perforce
'''
def runcmd(cmd):
cmd = shlex.split(cmd)
try:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, error = proc.communicate()
return out + error
except:
print 'Invalid Command: ' + str(cmd)
def set_password(user_id, password):
bash_path = runcmd('which bash').strip() # strip() used to trim line feed
p = os.popen('%s' % bash_path, 'w')
cmd = 'p4 passwd -O \"\" -P ' + password + ' ' + user_id
p.write('%s\n' % cmd)
status = p.close()
if status is None:
print 'Commands executed OK'
else:
print 'One or more command failed to run'
return status
# add to group
def add_to_group(user_id, group_id):
bash_path = runcmd('which bash').strip() # strip() used to trim line feed
p = os.popen('%s' % bash_path, 'w')
p.write('%s\n' % 'p4 -u p4build login')
cmd = '(p4 group -o ' + group_id + '; echo \" ' + user_id + '\")|p4 group -i'
p.write('%s\n' % cmd)
status = p.close()
if status is None:
print 'Commands executed OK'
else:
print 'One or more command failed to run'
return status
# add user
def add_user(user_id, email_domain):
bash_path = runcmd('which bash').strip() # strip() used to trim line feed
p = os.popen('%s' % bash_path, 'w')
p.write('%s\n' % 'p4 -u p4build login')
p.write('%s\n' % 'p4 user -i -f')
p.write('%s\n' % ('User: ' + user_id))
p.write('%s\n' % ('Email: ' + user_id + email_domain))
p.write('%s\n' % ('FullName: ' + user_id))
status = p.close()
if status is None:
print 'Commands executed OK'
else:
print 'One or more command failed to run'
return status
def verify_email_address(receivers):
error_message = 'One or more email address is invalid'
valid_emails = []
invalid_emails = []
for email_address in (''.join(receivers.split())).split(','): # Taken care off white spaces in the specified emails list
match = re.match('^[_a-z0-9-\.]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', email_address)
if match == None:
invalid_emails += [email_address]
else:
valid_emails += [email_address]
if len(invalid_emails) > 0:
print(error_message)
raise ValueError(error_message)
return valid_emails
def send_email(email_from, email_to, email_subject, email_message, email_cc='', email_bcc=''):
sendmail_location = runcmd('which sendmail').strip() # strip() used to trim line feed
p = os.popen('%s -t' % sendmail_location, 'w')
p.write('From: %s\n' % email_from)
p.write('To: %s\n' % email_to)
if email_cc is not '':
p.write('Cc: %s\n' % email_cc)
if email_bcc is not '':
p.write('Bcc: %s\n' % email_bcc)
p.write('Subject: %s\n' % email_subject)
p.write('\n') # blank line separating headers from body
p.write(email_message)
status = p.close()
if status is None: status = 'OK'
print "Sendmail exit status: ", str(status)
return status
def send_verified_email():
global email_from, email_to, email_cc, email_bcc, account_msg, subject
status = None
# send email
valid_emails_to = ','.join(verify_email_address(email_to)) # sendmail does not need list
# Validate CC and BCC addresses
if email_cc is not '':
valid_emails_cc = ','.join(verify_email_address(email_cc))
else:
valid_emails_cc = ''
if email_bcc is not '':
valid_emails_bcc = ','.join(verify_email_address(email_bcc))
else:
valid_emails_bcc = ''
if len(valid_emails_to) > 0:
send_email(email_from, valid_emails_to, subject, account_msg, email_cc=valid_emails_cc, email_bcc=valid_emails_bcc)
else:
print 'No valid email specified'
status = 'Error'
def complete_p4_user_create():
global user_id, email_domain_name, group_id
status = add_user(user_id, email_domain)
if status is None:
status = set_password(user_id, 'Welcome2Ruck')
if status is None:
status = add_to_group(user_id, group_id)
if status is None:
status = send_verified_email()
return status
def main():
status = complete_p4_user_create()
if status is None:
print '\nPerforce user created and email sent successfully.'
else:
print '\nError in creation Perforce user and/or sending email.'
if __name__ == '__main__':
main()
No comments:
Post a Comment