Created
November 6, 2017 10:45
-
-
Save aolwas/49eff56a51e344f71ffe09cb82d7a90e to your computer and use it in GitHub Desktop.
Recursively change uid/gid according a map of old to new uid/gid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import os | |
import sys | |
UID_MAP = { | |
# OLD_UID: NEW_UID | |
} | |
GID_MAP = { | |
# OLD_GID: NEW_GID | |
} | |
def _progress(count, total, status=''): | |
bar_len = 60 | |
filled_len = int(round(bar_len * count / float(total))) | |
bar = '=' * filled_len + '-' * (bar_len - filled_len) | |
sys.stdout.write('[%s] %s/%s\r' % (bar, count+1, total)) | |
sys.stdout.flush() # As suggested by Rom Ruben (see: http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console/27871113#comment50529068_27871113) | |
def _chown(path): | |
s = os.stat(path) | |
new_uid = UID_MAP.get(s.st_uid, s.st_uid) | |
new_gid = GID_MAP.get(s.st_gid, s.st_gid) | |
os.chown(path, new_uid, new_gid) | |
if __name__ == "__main__": | |
if len(sys.argv) <= 1: | |
print("Missing path argument") | |
exit(-1) | |
print("Compute files and subdirectories list") | |
paths = [] | |
for path, subdirs, files in os.walk(sys.argv[1]): | |
for subd in subdirs: | |
paths.append(os.path.join(path,subd)) | |
for name in files: | |
paths.append(os.path.join(path, name)) | |
total_paths = len(paths) | |
print("Found {} paths to chown".format(total_paths)) | |
print("Chowning ...") | |
for i, p in enumerate(paths): | |
_progress(i, total_paths) | |
_chown(p) | |
print("\nDone") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment