#! /usr/bin/python import os import sys if len(sys.argv) < 6: print "Usage: dlscd_fixid OLD_PASSWD OLD_GROUP NEW_PASSWD NEW_GROUP DIR..." sys.exit(1) old_passwd, old_group, new_passwd, new_group = sys.argv[1:5] dirs = sys.argv[5:] def makeIdDict(path): dict = {} f = open(path) try: for l in f: fields = l.split(':') try: dict[fields[0]] = int(fields[2]) except: pass finally: f.close() return dict def makeTranslationDict(old_dict, new_dict): dict = {} for key in old_dict.keys(): if key in new_dict: old_value = old_dict[key] new_value = new_dict[key] if old_value != new_value: dict[old_value] = new_value return dict def fixId(path, passwd_dict, group_dict): st = os.lstat(path) if st.st_uid in passwd_dict or st.st_gid in group_dict: new_uid = passwd_dict.get(st.st_uid, st.st_uid) new_gid = group_dict.get(st.st_gid, st.st_gid) os.chown(path, new_uid, new_gid) old_passwd_dict = makeIdDict(old_passwd) new_passwd_dict = makeIdDict(new_passwd) old_group_dict = makeIdDict(old_group) new_group_dict = makeIdDict(new_group) passwd_dict = makeTranslationDict(old_passwd_dict, new_passwd_dict) group_dict = makeTranslationDict(old_group_dict, new_group_dict) if len(passwd_dict) == 0 and len(group_dict) == 0: sys.exit(0) for dir in dirs: for dirpath, dirnames, filenames in os.walk(dir): for dirname in dirnames: path = os.path.join(dirpath, dirname) fixId(path, passwd_dict, group_dict) for filename in filenames: path = os.path.join(dirpath, filename) fixId(path, passwd_dict, group_dict) sys.exit(0)