tildetown-admin/scripts/rename_user.py

38 lines
831 B
Python
Raw Normal View History

2018-02-23 21:58:54 +00:00
#!/usr/bin/env python3
"""This script wraps the usermod command to allow user account renames via sudoers."""
import os
import sys
import subprocess
def rename_user(username, new_username):
# usermod -l new_username -m -d /home/{new_username} username
args = [
'usermod',
'-l',
new_username,
'-m',
'-d',
os.path.join('/home', new_username),
username
]
subprocess.run(args, check=True)
def main(argv):
if len(argv) < 3:
print('[rename_user] Too few arguments passed.', file=sys.stderr)
return 1
try:
rename_user(argv[1], argv[2])
except subprocess.CalledProcessError as e:
print('[rename_user] {}'.format(e), file=sys.stderr)
return 2
return 0
if __name__ == '__main__':
exit(main(sys.argv))