"""
Requires python 3
Accepts name or id
Usage:
chown.py -p /temp/folder -u user -g group -r true
or
chown.py -p /temp/folder -u uid -g gid -r 1
user, group, and recursive are optional
But must supply at least one of user or group
Example: sudo chown.py -p /temp/filename -u some_user
"""
import argparse, os, sys
from shutil import chown
user = group = recursive = ''
parser=argparse.ArgumentParser()
parser.add_argument('-p', '--path') # help='file/path'
parser.add_argument('-u', '--user') # , help='user'
parser.add_argument( '-g','--group') # , help='group'
parser.add_argument('-r', '--recursive', help=1) # , help='recursive'
args=parser.parse_args()
path = args.path
if not path:
raise Exception('missing path')
if args.user:
user = args.user
if args.group:
user = args.group
if args.recursive:
recursive = True
if not user and not group:
raise Exception('must supply user, group, or both')
def change_owner(path, user='', group='')
if user and not group:
chown(path, user=user)
elif not user and group:
chown(path, group=group)
else:
chown(path, user, group)
change_owner(path, user, group)
if recursive:
for dirpath, dirnames, filenames in os.walk(path):
for dname in dirnames:
change_owner(os.path.join(dirpath, dname), user, group)
for fname in filenames:
change_owner(os.path.join(dirpath, fname), user, group)