createusers.py #6

  • //
  • guest/
  • perforce_software/
  • sdp/
  • main/
  • Maintenance/
  • createusers.py
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#!/usr/bin/env python
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------

# This script will create a group of users all at once based on an input file.
# The input file should contain the users in the following format, one per line:
# user,email,fullname
#
# Run as python createusers.py userlist.csv <instance>
# Instance defaults to 1 if not passed in.

import sys
import os
import re
from subprocess import *
import shutil
import setpass
import platform
import ConfigParser

if len(sys.argv) > 2:
  SDP_INSTANCE = str(sys.argv[2])
else:
  SDP_INSTANCE = '1'

config = ConfigParser.RawConfigParser()
config.read('maintenance.cfg')

##########################################################################
#####                                                                #####
#####  CONFIGURATION VARIABLES: Modify in maintenance.cfg as needed. #####
#####                                                                #####

server = (config.get(SDP_INSTANCE, 'server'))
p4user = (config.get(SDP_INSTANCE, 'p4user'))
passwd = (config.get(SDP_INSTANCE, 'passwd'))
P4BIN = os.getenv ('P4BIN', 'p4')

if platform.system() == "Windows": 
  p4="p4.exe -p %s -u %s" % (server, p4user)
else:
  p4="%s -p %s -u %s" % (P4BIN, server, p4user)

os.system('echo %s| %s login' % (passwd, p4))

def loadconfig():
    user = ()
    users = []
    
    usersfile = sys.argv[1]
    
    usersfile = open(usersfile, "r")

    for line in usersfile.readlines():
        user = (re.match("^(.*),(.*),(.*)", line).groups())
#        log("DEBUG", "User %s added." % (user[0]))
        users.append(user)

    if users == []:
        log("ERROR", "No users found in config file.")

    usersfile.close()
    return users

def log(msglevel="DEBUG", message=""):
    if msglevel == "ERROR":
        print(message)
        sys.exit(1)
    else:
        print(message)

def runp4cmd(cmd):
    try:
        pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE, universal_newlines=True)
        stdout, stderr = pipe.communicate()
        log("DEBUG", stdout)
        if pipe.returncode != 0:
            log("ERROR", "%s%s generated the following error: %s" % (p4, cmd, stderr))
        else:
            return stdout
    except OSError as err:
        log("ERROR", "Execution failed: %s" % (err))

def process_servers(users):
    """
    Each user line contains three components as follows:
    user,email,full name
    """
    
    file = "user.txt"
    
    for user in users:
        userfile = open(file, "w")
        userfile.write("User:\t%s\nEmail:\t%s\nFullName:\t%s\n" % (user[0], user[1], user[2]))
        userfile.close()
        cmd = " user -f -i < user.txt"
        runp4cmd(cmd)
        os.remove(file)
#	Commented out setpass since most people are choosing to use AD now.
#        setpass.setpassword(user[0])

def main():
    if len(sys.argv) < 1:
        print("This program requires the file containing the list of users as a paramter.\nThe users file should contain 'username,email,full name' 1 per line.")
    users = loadconfig()
    process_servers(users)

if __name__ == "__main__":
    main()
# Change User Description Committed
#8 27331 C. Thomas Tyler Released SDP 2020.1.27325 (2021/01/29).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#7 26161 C. Thomas Tyler Released SDP 2019.3.26159 (2019/11/06).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#6 22185 C. Thomas Tyler Released SDP 2017.2.22177 (2017/05/17).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#5 16326 Russell C. Jackson (Rusty) Bug fixes.
#4 15856 C. Thomas Tyler Replaced the big license comment block with a shortened
form referencing the LICENSE file included with the SDP
package, and also by the URL for the license file in
The Workshop.
#3 13908 C. Thomas Tyler Pushing SDP 2015.1.13906.
#2 11524 Russell C. Jackson (Rusty) Released updated version of the SDP from Dev.
#1 10148 C. Thomas Tyler Promoted the Perforce Server Deployment Package to The Workshop.