| 70 | Implies -d | --dousers. |
| 71 | |
| 72 | -g | --gidmin gid Only adds groups whose gid is greater than or equal to |
| 73 | gid. You can pass a groupname there as well, and its |
| 74 | gid will be used automatically. |
| 75 | If not set, 0 will be used automatically. |
| 76 | Implies -D | --dogroups. |
| 77 | |
| 78 | -G | --gidmax gid Only adds groups whose gid is lesser than or equal to |
| 79 | gid. You can pass a groupname there as well, and its |
| 80 | gid will be used automatically. |
| 81 | If not set, a large value will be used automatically. |
| 82 | Implies -D | --dogroups. |
92 | | return [entry[0] for entry in pwd.getpwall() if uidmin <= entry[2] <= uidmax] |
| 115 | return [(entry[0], entry[3]) for entry in pwd.getpwall() if uidmin <= entry[2] <= uidmax] |
| 116 | |
| 117 | def listGroups(self, gidmin, gidmax, users) : |
| 118 | """Returns a list of groups whose gids are between gidmin and gidmax.""" |
| 119 | self.printInfo("Extracting all groups whose gid is between %s and %s." % (gidmin, gidmax)) |
| 120 | groups = [(entry[0], entry[2], entry[3]) for entry in grp.getgrall() if gidmin <= entry[2] <= gidmax] |
| 121 | gidusers = {} |
| 122 | usersgid = {} |
| 123 | for u in users : |
| 124 | gidusers.setdefault(u[1], []).append(u[0]) |
| 125 | usersgid.setdefault(u[0], []).append(u[1]) |
| 126 | |
| 127 | membership = {} |
| 128 | for g in range(len(groups)) : |
| 129 | (gname, gid, members) = groups[g] |
| 130 | newmembers = {} |
| 131 | for m in members : |
| 132 | newmembers[m] = m |
| 133 | try : |
| 134 | usernames = gidusers[gid] |
| 135 | except KeyError : |
| 136 | pass |
| 137 | else : |
| 138 | for username in usernames : |
| 139 | if not newmembers.has_key(username) : |
| 140 | newmembers[username] = username |
| 141 | for member in newmembers.keys() : |
| 142 | if not usersgid.has_key(member) : |
| 143 | del newmembers[member] |
| 144 | membership[gname] = newmembers.keys() |
| 145 | return membership |
| 167 | |
| 168 | def createGroups(self, groups, dryrun=0) : |
| 169 | """Creates all groups in PyKota's database.""" |
| 170 | if groups : |
| 171 | commands = ["edpykota --groups --add --noquota %s" % " ".join(['"%s"' % g for g in groups.keys()])] |
| 172 | revmembership = {} |
| 173 | for (groupname, usernames) in groups.items() : |
| 174 | for username in usernames : |
| 175 | revmembership.setdefault(username, []).append(groupname) |
| 176 | for (username, groupnames) in revmembership.items() : |
| 177 | commands.append('edpykota --ingroups %s "%s"' % (",".join(['"%s"' % g for g in groupnames]), username)) |
| 178 | for command in commands : |
| 179 | self.runCommand(command, dryrun) |
129 | | self.printInfo("Please WORRY NOW, the database WILL BE MODIFIED.") |
130 | | |
131 | | if not options["uidmin"] : |
132 | | self.printInfo(_("System users will have a print account as well !"), "warn") |
133 | | uidmin = 0 |
| 195 | self.printInfo(_("Please WORRY NOW, the database WILL BE MODIFIED.")) |
| 196 | |
| 197 | if options["dousers"] : |
| 198 | if not options["uidmin"] : |
| 199 | self.printInfo(_("System users will have a print account as well !"), "warn") |
| 200 | uidmin = 0 |
| 201 | else : |
| 202 | try : |
| 203 | uidmin = int(options["uidmin"]) |
| 204 | except : |
| 205 | try : |
| 206 | uidmin = pwd.getpwnam(options["uidmin"])[2] |
| 207 | except KeyError, msg : |
| 208 | raise PyKotaToolError, _("Unknown username %s : %s") % (options["uidmin"], msg) |
| 209 | |
| 210 | if not options["uidmax"] : |
| 211 | uidmax = sys.maxint |
| 212 | else : |
| 213 | try : |
| 214 | uidmax = int(options["uidmax"]) |
| 215 | except : |
| 216 | try : |
| 217 | uidmax = pwd.getpwnam(options["uidmax"])[2] |
| 218 | except KeyError, msg : |
| 219 | raise PyKotaToolError, _("Unknown username %s : %s") % (options["uidmax"], msg) |
| 220 | |
| 221 | if uidmin > uidmax : |
| 222 | (uidmin, uidmax) = (uidmax, uidmin) |
| 223 | users = self.listUsers(uidmin, uidmax) |
139 | | uidmin = pwd.getpwnam(options["uidmin"])[2] |
140 | | except KeyError, msg : |
141 | | raise PyKotaToolError, _("Unknown username %s : %s") % (options["uidmin"], msg) |
142 | | |
143 | | if not options["uidmax"] : |
144 | | uidmax = sys.maxint |
| 233 | gidmin = int(options["gidmin"]) |
| 234 | except : |
| 235 | try : |
| 236 | gidmin = grp.getgrnam(options["gidmin"])[2] |
| 237 | except KeyError, msg : |
| 238 | raise PyKotaToolError, _("Unknown groupname %s : %s") % (options["gidmin"], msg) |
| 239 | |
| 240 | if not options["gidmax"] : |
| 241 | gidmax = sys.maxint |
| 242 | else : |
| 243 | try : |
| 244 | gidmax = int(options["gidmax"]) |
| 245 | except : |
| 246 | try : |
| 247 | gidmax = grp.getgrnam(options["gidmax"])[2] |
| 248 | except KeyError, msg : |
| 249 | raise PyKotaToolError, _("Unknown groupname %s : %s") % (options["gidmax"], msg) |
| 250 | |
| 251 | if gidmin > gidmax : |
| 252 | (gidmin, gidmax) = (gidmax, gidmin) |
| 253 | groups = self.listGroups(gidmin, gidmax, users) |
| 254 | if not options["emptygroups"] : |
| 255 | for (groupname, members) in groups.items() : |
| 256 | if not members : |
| 257 | del groups[groupname] |
172 | | short_options = "hvfu:U:" |
173 | | long_options = ["help", "version", "force", "uidmin=", "uidmax="] |
| 276 | short_options = "hvdDefu:U:g:G:" |
| 277 | long_options = ["help", "version", "dousers", "dogroups", \ |
| 278 | "emptygroups", "force", "uidmin=", "uidmax=", \ |
| 279 | "gidmin=", "gidmax="] |
191 | | |
| 300 | options["gidmin"] = options["g"] or options["gidmin"] |
| 301 | options["gidmax"] = options["G"] or options["gidmax"] |
| 302 | |
| 303 | if options["uidmin"] or options["uidmax"] : |
| 304 | if not options["dousers"] : |
| 305 | manager.printInfo(_("The --uidmin or --uidmax command line option implies --dousers as well."), "warn") |
| 306 | options["dousers"] = 1 |
| 307 | |
| 308 | if options["gidmin"] or options["gidmax"] : |
| 309 | if not options["dogroups"] : |
| 310 | manager.printInfo(_("The --gidmin or --gidmax command line option implies --dogroups as well."), "warn") |
| 311 | options["dogroups"] = 1 |
| 312 | |
| 313 | if options["dogroups"] : |
| 314 | if not options["dousers"] : |
| 315 | manager.printInfo(_("The --dogroups command line option implies --dousers as well."), "warn") |
| 316 | options["dousers"] = 1 |
| 317 | |