def update(ids_list, localname):
    localid = open("/etc/%s" % localname).readlines()
    dirty = False
    for id_name, ids in ids_list.items():
        # For each elem in ids.tables
        for idx in xrange(len(localid)):
            # For each elem in /etc/(passwd|group)
            pwd = localid[idx].split(':')

            if 3 < len(pwd):
                print "Invalid line [%s]" % localid[idx][:-1]
                continue

            if pwd[0] != id_name:
                # User defined in ids.tables not present in /etc/
                continue

            if pwd[1] != "x":
                print "Warning: %s has a password, removing it"
                pwd[1] = 'x'
                localid[idx] = ":".join(pwd)
                dirty = True

            wanted = "%s:x:%s:%s" % (id_name, ids[0], ids[1])
            if ids[1]:
                wanted += ':'
            # Check for uid/gid mismatch
            if localid[idx].startswith(wanted):
                if "DEBUG" in os.environ:
                    print "Debug: %s: %s is correct (%s == %s)" % (
                        localname, id_name, wanted, localid[idx][:-1])
                continue
            print "Info: %s: %s is not correct (%s != %s)" % (
                localname, id_name, wanted, localid[idx][:-1])
            if ids[1]:
                update_uid(pwd[2], ids[0])
                pwd[3] = ids[1]
            else:
                update_gid(pwd[2], ids[0])
            pwd[2] = ids[0]
            localid[idx] = ":".join(pwd)
            dirty = True

    if dirty:
        open("/etc/%s.orig" % localname, "w").write(
            open("/etc/%s" % localname).read())
        open("/etc/%s" % localname, "w").write("".join(localid))
        print "Info: Updated /etc/%s (original copied to /etc/%s.org" % (
            localname, localname)
dataset/ETHPy150Open redhat-cip/software-factory/config/scripts/sf-update-uid-gid.py/update/swapped operands of `<`