#!/usr/bin/python3 import argparse import logging import os import sys def remap_file(fpath, src, dst, count, dry_run): stat = os.lstat(fpath) uid = stat.st_uid if uid >= src and uid < src + count: uid += dst - src gid = stat.st_gid if gid >= src and gid < src + count: gid += dst - src logging.debug('chown(%s, %d->%d, %d->%d)' % (fpath, stat.st_uid, uid, stat.st_gid, gid)) if not dry_run: os.chown(fpath, uid, gid, follow_symlinks = False) parser = argparse.ArgumentParser( description = "Remap UIDs/GIDs in a directory tree from one range to another" ) parser.add_argument('-c', '--count', type = int, default = 65536, help = 'Number of IDs in range') parser.add_argument('-n', '--dry-run', action = 'store_true') parser.add_argument('-v', '--verbose', action = 'count', default = 0) parser.add_argument('src_start', type = int, help = 'First ID of the range to remap from') parser.add_argument('dst_start', type = int, help = 'First ID of the range to remap to') parser.add_argument('targets', nargs = '+') args = parser.parse_args(sys.argv[1:]) logging.basicConfig(level = max(3 - args.verbose, 0) * 10) for tgt in args.targets: logging.info('Processing target: %s' % tgt) for dirpath, dirnames, filenames in os.walk(tgt): logging.debug('At directory: %s' % dirpath) remap_file(dirpath, args.src_start, args.dst_start, args.count, args.dry_run) for fn in filenames: remap_file(os.path.join(dirpath, fn), args.src_start, args.dst_start, args.count, args.dry_run)