· 7 years ago · Dec 01, 2018, 06:14 PM
1#!/usr/bin/env python3
2#
3# Adjust uid/gid on all files and directories under a given root.
4#
5# Copyright (c) 2018, Alexandre Hamelin <alexandre.hamelin gmail.com>
6# Published under the MIT license.
7
8import sys, os
9from pathlib import Path
10
11
12if __name__ == '__main__':
13 rootpath = sys.argv[1]
14 uid_offset = int(sys.argv[2])
15 gid_offset = int(sys.argv[3])
16
17 stat = os.lstat(rootpath)
18 os.chown(rootpath, stat.st_uid + uid_offset, stat.st_gid + gid_offset, follow_symlinks=False)
19
20 for root, dirs, files in os.walk(rootpath):
21
22 for d in dirs:
23 dpath = Path(root, d)
24 stat = os.lstat(dpath)
25 os.chown(dpath, stat.st_uid + uid_offset, stat.st_gid + gid_offset, follow_symlinks=False)
26 for f in files:
27 fpath = Path(root, f)
28 stat = os.lstat(fpath)
29 os.chown(fpath, stat.st_uid + uid_offset, stat.st_gid + gid_offset, follow_symlinks=False)