Although Git is very popular now, but I believe that with SVN's still a lot of companies, so as SVN Configuration Manager, it is inevitable that the issue of account maintenance, today we will talk about how to achieve user's quick maintenance through Python script.
If you manually maintain a user, you typically need three steps:
1. Manually Add Users
2. Manually set the genus group
3. Notify the user to set the results
Using the script is also three steps, but the efficiency is greatly improved:
1. Enter the user name
2. Enter the name of the group you want to set
3. Press ENTER to get it done.
This is the key point of setting up the user and the genus Group, and we need to automate the operation, the following is the implementation of the Code:
def add_user(user_name): """如果用户不存在则调用htpasswd.exe添加用户""" htpasswd_path = cur_file_dir() + ‘\\bin\\htpasswd.exe -b ‘ pwd_path = REP_PATH + ‘\\htpasswd ‘ user_list = readfiles(REP_PATH + ‘\\htpasswd‘) for line in user_list.split(‘\n‘): if ‘:‘ in line and user_name.lower() == line.split(‘:‘)[0]: print(‘用户 ‘ + user_name + ‘ 已存在,不需要创建‘) return "见之前邮件通知" pwd = ‘ [email protected]‘ + str(random.randrange(1000, 9999, 1)) print(execut_ret(htpasswd_path + pwd_path + user_name + pwd)[1]) return pwd.strip()
This is the code to create the user, the following description:
The function of the Cur_file_dir () function is to obtain the path where the execution script resides;
Htpasswd.exe is a tool copied from the Apache directory, you can add the specified user name and password information in the specified file, the command line using the method "htpasswd.exe-b [password file] [username] [Password]", more instructions please Google;
Rep_path is my definition of a global variable, is the root directory of my SVN repository, the directory will hold the user and group settings configuration file;
HTPASSWD file is the above-mentioned user information storage file;
PWD is the 13-bit password that I generated by random number with [email protected];
Execut_ret () function is to execute the specified program and return the results of execution, the current is to execute Htpasswd.exe add the specified user information;
Let's take a look at the code for setting up group information:
def add_group(user_name, user_grp): """添加用户到指定用户组""" grp_path = REP_PATH + "\\groups.conf" grp_context = readfiles(grp_path) new_context = "" isadd = False for line in grp_context.split(‘\n‘): if ‘=‘ in line and user_grp.lower() == line.split(‘=‘)[0].lower(): if user_name.lower() in line.lower(): print("用户 " + user_name + " 已经属于 " + user_grp) return False if line.split(‘=‘)[1] != ‘‘: new_line = line + "," + user_name else: new_line = line + user_name new_context = grp_context.replace(line, new_line) isadd = True break if isadd: writetofile(grp_path, new_context) print("用户 " + user_name + " 成功添加到组 " + user_grp) return True else: print("组设置失败,请检查组名是否正确") return True
A description of this function:
This function is to read the group settings file groups.conf, check whether the current user exists in the target group, if there is a direct return, otherwise add users to the group inside;
The function of the Readfiles () function is to read all the contents of the target file at once;
The function of the WriteToFile () function is to write the specified content to the specified file;
Here is the final unified call function, and the entry function implementation:
def useradd(user_name, user_grp): """"添加用户+添加属组+邮件通知""" ret_grp = False pwd = add_user(user_name) if ‘,‘ in user_grp: for each_group in user_grp.split(‘,‘): if add_group(user_name, each_group): ret_grp = True elif add_group(user_name, user_grp): ret_grp = True if ret_grp: sendcontextmail(user_name, pwd, user_grp)if __name__ == "__main__": while True: user_name = input("请输入用户名(多个用户请用英文逗号分隔):") user_group = input("请输入要加入的属组(多个组请用英文逗号分隔):") for usr in user_name.split(‘,‘): useradd(usr, user_group)
Description
The Sendcontextmail () function is a public mail notification function;
The unified processing function can handle the situation where a user adds multiple user groups;
The entry function can handle the situation that multiple users add at the same time, and make an infinite loop, so that the window can be attached to the server.
The above code is based on Python3.4 validation, other versions should be the same;
The above instructions are implemented based on Windows;
The above implementation is based on SVN's own account and group management system;
If the Windows-based account and group setup system, the code is simpler than this:
def useradd(username, usergroup): """添加 windows 账号,并设置属组""" pwd = ‘ [email protected]‘ + str(random.randrange(1000, 9999, 1)) retinfo = execut_ret(‘net.exe user ‘ + username + pwd + ‘ /add‘) if ‘命令成功完成‘ not in retinfo[0]: print(‘用户已创建失败:‘ + retinfo[1]) print(‘用户创建成功:‘ + username) print(‘随机密码:‘ + pwd) for groupname in usergroup.split(‘,‘): retinfo = execut_ret(‘net.exe localgroup ‘ + groupname + ‘ ‘ + username + ‘ /add‘) if ‘命令成功完成‘ not in retinfo[0]: print(‘设置用户属组失败:‘ + retinfo[1]) print(‘用户已加入属组:‘ + groupname) sendcontextmail(username, pwd, usergroup)
Well, through such a small number of code can be instantaneous to solve the user configuration problems, maintenance is not so easy (if the students need complete code, please in the public background tease me ha).
Use Python to configure SVN account and group information in one click