Changeset 3433

Show
Ignore:
Timestamp:
10/05/08 22:33:00 (15 years ago)
Author:
jerome
Message:

Now pkturnkey supports new style command line handling code.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pkturnkey

    r3429 r3433  
    2121# 
    2222# 
     23 
     24"""A tool that can be used to fill PyKota's database from system accounts, and detect the best accouting settings for the existing printers.""" 
    2325 
    2426import sys 
     
    3234 
    3335import pykota.appinit 
    34 from pykota.utils import * 
    35  
     36from pykota.utils import run 
     37from pykota.commandline import PyKotaOptionParser 
    3638from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    3739from pykota.tool import Tool 
    38  
    39 __doc__ = N_("""pkturnkey v%(__version__)s (c) %(__years__)s %(__author__)s 
    40  
    41 A turn key tool for PyKota. When launched, this command will initialize 
    42 PyKota's database with all existing print queues and some or all users. 
    43 For now, no prices or limits are set, so printing is fully accounted 
    44 for, but not limited. That's why you'll probably want to also use 
    45 edpykota once the database has been initialized. 
    46  
    47 command line usage : 
    48  
    49   pkturnkey [options] [printqueues names] 
    50  
    51 options : 
    52  
    53   -v | --version       Prints pkturnkey version number then exits. 
    54   -h | --help          Prints this message then exits. 
    55  
    56   -c | --doconf        Give hints about what to put into pykota.conf 
    57  
    58   -d | --dousers       Manages users accounts as well. 
    59  
    60   -D | --dogroups      Manages users groups as well. 
    61                        Implies -d | --dousers. 
    62  
    63   -e | --emptygroups   Includes empty groups. 
    64  
    65   -f | --force         Modifies the database instead of printing what 
    66                        it would do. 
    67  
    68   -u | --uidmin uid    Only adds users whose uid is greater than or equal to 
    69                        uid. You can pass an username there as well, and its 
    70                        uid will be used automatically. 
    71                        If not set, 0 will be used automatically. 
    72                        Implies -d | --dousers. 
    73  
    74   -U | --uidmax uid    Only adds users whose uid is lesser than or equal to 
    75                        uid. You can pass an username there as well, and its 
    76                        uid will be used automatically. 
    77                        If not set, a large value will be used automatically. 
    78                        Implies -d | --dousers. 
    79  
    80   -g | --gidmin gid    Only adds groups whose gid is greater than or equal to 
    81                        gid. You can pass a groupname there as well, and its 
    82                        gid will be used automatically. 
    83                        If not set, 0 will be used automatically. 
    84                        Implies -D | --dogroups. 
    85  
    86   -G | --gidmax gid    Only adds groups whose gid is lesser than or equal to 
    87                        gid. You can pass a groupname there as well, and its 
    88                        gid will be used automatically. 
    89                        If not set, a large value will be used automatically. 
    90                        Implies -D | --dogroups. 
    91  
    92 examples : 
    93  
    94   $ pkturnkey --dousers --uidmin jerome 
    95  
    96   Will simulate the initialization of PyKota's database will all existing 
    97   printers and print accounts for all users whose uid is greater than 
    98   or equal to jerome's one. Won't manage any users group. 
    99  
    100   To REALLY initialize the database instead of simulating it, please 
    101   use the -f | --force command line switch. 
    102  
    103   You can limit the initialization to only a subset of the existing 
    104   printers, by passing their names at the end of the command line. 
    105 """) 
    10640 
    10741class PKTurnKey(Tool) : 
     
    13165        """Returns a list of users whose uids are between uidmin and uidmax.""" 
    13266        self.printInfo("Extracting all users whose uid is between %s and %s." % (uidmin, uidmax)) 
    133         return [(entry[0], entry[3]) for entry in pwd.getpwall() if uidmin <= entry[2] <= uidmax] 
     67        return [(entry.pw_name, entry.pw_gid) for entry in pwd.getpwall() if uidmin <= entry.pw_uid <= uidmax] 
    13468 
    13569    def listGroups(self, gidmin, gidmax, users) : 
    13670        """Returns a list of groups whose gids are between gidmin and gidmax.""" 
    13771        self.printInfo("Extracting all groups whose gid is between %s and %s." % (gidmin, gidmax)) 
    138         groups = [(entry[0], entry[2], entry[3]) for entry in grp.getgrall() if gidmin <= entry[2] <= gidmax] 
     72        groups = [(entry.gr_name, entry.gr_gid, entry.gr_mem) for entry in grp.getgrall() if gidmin <= entry.gr_gid <= gidmax] 
    13973        gidusers = {} 
    14074        usersgid = {} 
     
    236170            except ImportError : 
    237171                logerr("pysnmp doesn't seem to be installed. SNMP checks will be ignored !\n") 
    238                 return 0 
     172                return False 
    239173        else : 
    240174            hasV4 = True 
     
    292226                                raise "No SNMP !" 
    293227                            else : 
    294                                 self.SNMPOK = 1 
    295                                 return 1 
    296  
    297         self.SNMPOK = 0 
     228                                self.SNMPOK = True 
     229                                return True 
     230 
     231        self.SNMPOK = False 
    298232        try : 
    299233            retrieveSNMPValues(hostname, community) 
    300234        except : 
    301             self.SNMPOK = 0 
     235            self.SNMPOK = False 
    302236        return self.SNMPOK 
    303237 
     
    307241            raise "Timeout !" 
    308242 
    309         pjlsupport = 0 
     243        pjlsupport = False 
    310244        signal.signal(signal.SIGALRM, alarmHandler) 
    311245        signal.alarm(2) # wait at most 2 seconds 
     
    320254            pass 
    321255        else : 
    322             pjlsupport = 1 
     256            pjlsupport = True 
    323257        s.close() 
    324258        signal.alarm(0) 
     
    388322        self.adminOnly() 
    389323 
     324        if options.uidmin or options.uidmax : 
     325            if not options.dousers : 
     326                self.printInfo(_("The --uidmin or --uidmax command line option implies --dousers as well."), "warn") 
     327            options.dousers = True 
     328 
     329        if options.gidmin or options.gidmax : 
     330            if not options.dogroups : 
     331                self.printInfo(_("The --gidmin or --gidmax command line option implies --dogroups as well."), "warn") 
     332            options.dogroups = True 
     333 
     334        if options.dogroups : 
     335            if not options.dousers : 
     336                self.printInfo(_("The --dogroups command line option implies --dousers as well."), "warn") 
     337            options.dousers = True 
     338 
    390339        if not names : 
    391             names = ["*"] 
     340            names = [u"*"] 
    392341 
    393342        self.printInfo(_("Please be patient...")) 
    394         dryrun = not options["force"] 
     343        dryrun = not options.force 
    395344        if dryrun : 
    396345            self.printInfo(_("Don't worry, the database WILL NOT BE MODIFIED.")) 
     
    398347            self.printInfo(_("Please WORRY NOW, the database WILL BE MODIFIED.")) 
    399348 
    400         if options["dousers"] : 
    401             if not options["uidmin"] : 
     349        if options.dousers : 
     350            if not options.uidmin : 
    402351                self.printInfo(_("System users will have a print account as well !"), "warn") 
    403352                uidmin = 0 
    404353            else : 
    405354                try : 
    406                     uidmin = int(options["uidmin"]) 
     355                    uidmin = int(options.uidmin) 
    407356                except : 
    408357                    try : 
    409                         uidmin = pwd.getpwnam(options["uidmin"])[2] 
     358                        uidmin = pwd.getpwnam(options.uidmin).pw_uid 
    410359                    except KeyError, msg : 
    411360                        raise PyKotaCommandLineError, _("Unknown username %s : %s") \ 
    412                                                    % (options["uidmin"], msg) 
    413  
    414             if not options["uidmax"] : 
     361                                                   % (options.uidmin, msg) 
     362 
     363            if not options.uidmax : 
    415364                uidmax = sys.maxint 
    416365            else : 
    417366                try : 
    418                     uidmax = int(options["uidmax"]) 
     367                    uidmax = int(options.uidmax) 
    419368                except : 
    420369                    try : 
    421                         uidmax = pwd.getpwnam(options["uidmax"])[2] 
     370                        uidmax = pwd.getpwnam(options.uidmax).pw_uid 
    422371                    except KeyError, msg : 
    423372                        raise PyKotaCommandLineError, _("Unknown username %s : %s") \ 
    424                                                    % (options["uidmax"], msg) 
     373                                                   % (options.uidmax, msg) 
    425374 
    426375            if uidmin > uidmax : 
     
    430379            users = [] 
    431380 
    432         if options["dogroups"] : 
    433             if not options["gidmin"] : 
     381        if options.dogroups : 
     382            if not options.gidmin : 
    434383                self.printInfo(_("System groups will have a print account as well !"), "warn") 
    435384                gidmin = 0 
    436385            else : 
    437386                try : 
    438                     gidmin = int(options["gidmin"]) 
     387                    gidmin = int(options.gidmin) 
    439388                except : 
    440389                    try : 
    441                         gidmin = grp.getgrnam(options["gidmin"])[2] 
     390                        gidmin = grp.getgrnam(options.gidmin).gr_gid 
    442391                    except KeyError, msg : 
    443392                        raise PyKotaCommandLineError, _("Unknown groupname %s : %s") \ 
    444                                                    % (options["gidmin"], msg) 
    445  
    446             if not options["gidmax"] : 
     393                                                   % (options.gidmin, msg) 
     394 
     395            if not options.gidmax : 
    447396                gidmax = sys.maxint 
    448397            else : 
    449398                try : 
    450                     gidmax = int(options["gidmax"]) 
     399                    gidmax = int(options.gidmax) 
    451400                except : 
    452401                    try : 
    453                         gidmax = grp.getgrnam(options["gidmax"])[2] 
     402                        gidmax = grp.getgrnam(options.gidmax).gr_gid 
    454403                    except KeyError, msg : 
    455404                        raise PyKotaCommandLineError, _("Unknown groupname %s : %s") \ 
    456                                                    % (options["gidmax"], msg) 
     405                                                   % (options.gidmax, msg) 
    457406 
    458407            if gidmin > gidmax : 
    459408                (gidmin, gidmax) = (gidmax, gidmin) 
    460409            groups = self.listGroups(gidmin, gidmax, users) 
    461             if not options["emptygroups"] : 
     410            if not options.emptygroups : 
    462411                for (groupname, members) in groups.items() : 
    463412                    if not members : 
     
    477426            self.printInfo(_("Database initialized !")) 
    478427 
    479         if options["doconf"] : 
     428        if options.doconf : 
    480429            self.hintConfig(printers) 
    481430 
    482431 
    483432if __name__ == "__main__" : 
    484     retcode = 0 
    485     try : 
    486         short_options = "hvdDefu:U:g:G:c" 
    487         long_options = ["help", "version", "dousers", "dogroups", \ 
    488                         "emptygroups", "force", "uidmin=", "uidmax=", \ 
    489                         "gidmin=", "gidmax=", "doconf"] 
    490  
    491         # Initializes the command line tool 
    492         manager = PKTurnKey(doc=__doc__) 
    493         manager.deferredInit() 
    494  
    495         # parse and checks the command line 
    496         (options, args) = manager.parseCommandline(sys.argv[1:], \ 
    497                                                    short_options, \ 
    498                                                    long_options, \ 
    499                                                    allownothing=1) 
    500  
    501         # sets long options 
    502         options["help"] = options["h"] or options["help"] 
    503         options["version"] = options["v"] or options["version"] 
    504         options["dousers"] = options["d"] or options["dousers"] 
    505         options["dogroups"] = options["D"] or options["dogroups"] 
    506         options["emptygroups"] = options["e"] or options["emptygroups"] 
    507         options["force"] = options["f"] or options["force"] 
    508         options["uidmin"] = options["u"] or options["uidmin"] 
    509         options["uidmax"] = options["U"] or options["uidmax"] 
    510         options["gidmin"] = options["g"] or options["gidmin"] 
    511         options["gidmax"] = options["G"] or options["gidmax"] 
    512         options["doconf"] = options["c"] or options["doconf"] 
    513  
    514         if options["uidmin"] or options["uidmax"] : 
    515             if not options["dousers"] : 
    516                 manager.printInfo(_("The --uidmin or --uidmax command line option implies --dousers as well."), "warn") 
    517             options["dousers"] = 1 
    518  
    519         if options["gidmin"] or options["gidmax"] : 
    520             if not options["dogroups"] : 
    521                 manager.printInfo(_("The --gidmin or --gidmax command line option implies --dogroups as well."), "warn") 
    522             options["dogroups"] = 1 
    523  
    524         if options["dogroups"] : 
    525             if not options["dousers"] : 
    526                 manager.printInfo(_("The --dogroups command line option implies --dousers as well."), "warn") 
    527             options["dousers"] = 1 
    528  
    529         if options["help"] : 
    530             manager.display_usage_and_quit() 
    531         elif options["version"] : 
    532             manager.display_version_and_quit() 
    533         else : 
    534             retcode = manager.main(args, options) 
    535     except KeyboardInterrupt : 
    536         logerr("\nInterrupted with Ctrl+C !\n") 
    537         retcode = -3 
    538     except PyKotaCommandLineError, msg : 
    539         logerr("%s : %s\n" % (sys.argv[0], msg)) 
    540         retcode = -2 
    541     except SystemExit : 
    542         pass 
    543     except : 
    544         try : 
    545             manager.crashed("pkturnkey failed") 
    546         except : 
    547             crashed("pkturnkey failed") 
    548         retcode = -1 
    549  
    550     try : 
    551         manager.storage.close() 
    552     except (TypeError, NameError, AttributeError) : 
    553         pass 
    554  
    555     sys.exit(retcode) 
     433    parser = PyKotaOptionParser(description=_("A turn key tool for PyKota. When launched, this command will initialize PyKota's database with all existing print queues and some or all users. For now, no prices or limits are set, so printing is fully accounted for, but not limited. That's why you'll probably want to also use edpykota once the database has been initialized."), 
     434                                usage="pkturnkey [options] printer1 printer2 ... printerN") 
     435    parser.add_option("-c", "--doconf", 
     436                            action="store_true", 
     437                            dest="doconf", 
     438                            help=_("Try to autodetect the best print accounting settings for existing CUPS printers. All printers must be switched ON beforehand.")) 
     439    parser.add_option("-d", "--dousers", 
     440                            action="store_true", 
     441                            dest="dousers", 
     442                            help=_("Create accounts for users, and allocate print quota entries for them.")) 
     443    parser.add_option("-D", "--dogroups", 
     444                            action="store_true", 
     445                            dest="dogroups", 
     446                            help=_("Create accounts for users groups, and allocate print quota entries for them.")) 
     447    parser.add_option("-e", "--emptygroups", 
     448                            action="store_true", 
     449                            dest="emptygroups", 
     450                            help=_("Also include groups which don't have any member.")) 
     451    parser.add_option("-f", "--force", 
     452                            action="store_true", 
     453                            dest="force", 
     454                            help=_("Modifies PyKota's database content for real, instead of faking it (for safety reasons).")) 
     455    parser.add_option("-u", "--uidmin", 
     456                            dest="uidmin", 
     457                            help=_("Only include users whose uid is greater than or equal to this parameter. If you pass an username instead, his uid will be used automatically.")) 
     458    parser.add_option("-U", "--uidmax", 
     459                            dest="uidmax", 
     460                            help=_("Only include users whose uid is lesser than or equal to this parameter. If you pass an username instead, his uid will be used automatically.")) 
     461    parser.add_option("-g", "--gidmin", 
     462                            dest="gidmin", 
     463                            help=_("Only include users groups whose gid is greater than or equal to this parameter. If you pass a groupname instead, its gid will be used automatically.")) 
     464    parser.add_option("-G", "--gidmax", 
     465                            dest="gidmax", 
     466                            help=_("Only include users groups whose gid is lesser than or equal to this parameter. If you pass a groupname instead, its gid will be used automatically.")) 
     467 
     468    parser.add_example("--dousers --uidmin jerome HPLASER1 HPLASER2", 
     469                       _("Would simulate the creation in PyKota's database of the printing accounts for all users whose uid is greater than or equal to 'jerome''s. Each of them would be given a print quota entry on printers 'HPLASER1' and 'HPLASER2'.")) 
     470    parser.add_example("--force --dousers --uidmin jerome HPLASER1 HPLASER2", 
     471                       _("Would do the same as the example above, but for real. Please take great care when using the --force command line option.")) 
     472    parser.add_example("--doconf", 
     473                       _("Would try to automatically detect the best print accounting settings for all active printers, and generate some lines for you to add into your pykota.conf")) 
     474    run(parser, PKTurnKey)