Changeset 3322

Show
Ignore:
Timestamp:
02/02/08 16:31:22 (16 years ago)
Author:
jerome
Message:

autopykota was converted to new style.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/autopykota

    r3295 r3322  
    2828import pykota.appinit 
    2929from pykota.utils import * 
    30  
    31 from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     30from pykota.commandline import PyKotaOptionParser 
     31from pykota.errors import PyKotaToolError 
    3232from pykota.tool import PyKotaTool 
    3333 
    34 __doc__ = N_("""autopykota v%(__version__)s (c) %(__years__)s %(__author__)s 
    35  
    36 A tool to automate user account creation and initial balance setting. 
     34"""A tool to automate user account creation and initial balance setting. 
    3735 
    3836THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA ! 
    3937 
    40 command line usage : 
    41  
    42   THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART 
    43   OF AN external policy IN pykota.conf 
    44    
    45   autopykota { -i | --initbalance value }  
    46  
    47 options : 
    48  
    49   -v | --version       Prints autopykota's version number then exits. 
    50   -h | --help          Prints this message then exits. 
    51    
    52   -i | --initbalance b Sets the user's account initial balance value to b. 
    53                        If the user already exists, actual balance is left 
    54                        unmodified. If unset, the default value is 0. 
    55  
    56   -e | --email addr    Sets the user's e-mail address. 
    57    
    58 """) 
     38THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART 
     39OF AN external policy IN pykota.conf 
     40""" 
    5941 
    6042class AutoPyKota(PyKotaTool) : 
     
    6749            raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error." 
    6850        else : 
     51            username = username.decode(self.charset) 
     52            printername = printername.decode(self.charset) 
    6953            printer = self.storage.getPrinter(printername) 
    7054            if not printer.Exists : 
    71                 self.logdebug("Creating printer %s which doesn't exist yet." % printername) 
    72                 os.system('pkprinters --add --description "printer created from autopykota" "%s"' % printername) 
     55                self.logdebug("Creating printer %s which doesn't exist yet." \ 
     56                                        % printername) 
     57                os.system('pkprinters --add --description "%s" "%s"' \ 
     58                           % (_("printer created with autopykota").encode(self.charset, "replace"), 
     59                              printername.encode(self.charset))) 
    7360                printer = self.storage.getPrinterFromBackend(printername) 
    7461                if printer.Exists : 
     
    8370            if not user.Exists : 
    8471                self.logdebug("Creating user %s which doesn't exist yet." % username) 
    85                 if (options["email"] is None) : 
    86                     os.system('pkusers --add --limitby balance --balance "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], username)) 
     72                if (options.email is None) : 
     73                    os.system('pkusers --add --limitby balance --balance "%s" --description "%s" "%s"' \ 
     74                                       % (options.initbalance,  
     75                                          _("user created with autopykota").encode(self.charset, "replace"), 
     76                                          username.encode(self.charset))) 
    8777                else : 
    88                     os.system('pkusers --add --limitby balance --balance "%s" --email "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], options["email"], username)) 
     78                    os.system('pkusers --add --limitby balance --balance "%s" --email "%s" --description "%s" "%s"' \ 
     79                                       % (options.initbalance,  
     80                                          options.email.encode(self.charset),  
     81                                          _("user created with autopykota").encode(self.charset, "replace"), 
     82                                          username.encode(self.charset))) 
    8983                     
    9084                user = self.storage.getUserFromBackend(username) 
     
    9791                userpquota = self.storage.getUserPQuota(user, printer) 
    9892                if not userpquota.Exists : 
    99                     self.logdebug("Creating a print quota entry for user %s on printers %s" % (username, printernames)) 
    100                     os.system('edpykota --add --printer "%s" "%s"' % (','.join(printernames), username)) 
    101                     userpquota = self.storage.getUserPQuotaFromBackend(user, printer) 
     93                    self.logdebug("Creating a print quota entry for user %s on printers %s" \ 
     94                                        % (username, printernames)) 
     95                    os.system('edpykota --add --printer "%s" "%s"' \ 
     96                                % (','.join(printernames).encode(self.charset),  
     97                                   username.encode(self.charset))) 
     98                    userpquota = self.storage.getUserPQuotaFromBackend(user,  
     99                                                                       printer) 
    102100                    if userpquota.Exists : 
    103                         self.logdebug("User %s's print quota entry on printer %s created successfully." % (username, printername)) 
     101                        self.logdebug("User %s's print quota entry on printer %s created successfully." \ 
     102                                            % (username, printername)) 
    104103                        return 0 
    105104                    else :     
    106                         self.logdebug("Impossible to create user %s's print quota entry on printer %s." % (username, printername)) 
     105                        self.logdebug("Impossible to create user %s's print quota entry on printer %s." \ 
     106                                            % (username, printername)) 
    107107                        return -1 
    108108                else : 
    109                     self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." % (username, printername)) 
     109                    self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." \ 
     110                                        % (username, printername)) 
    110111                    return 0 
    111112            else :         
     
    113114                 
    114115if __name__ == "__main__" :     
    115     retcode = 0 
    116     try : 
    117         defaults = { \ 
    118                      "initbalance" : 0.0 
    119                    } 
    120         short_options = "vhi:e:" 
    121         long_options = ["help", "version", "initbalance=", "email="] 
    122          
    123         # Initializes the command line tool 
    124         automat = AutoPyKota(doc=__doc__) 
    125         automat.deferredInit() 
    126          
    127         # parse and checks the command line 
    128         (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options) 
    129          
    130         # sets long options 
    131         options["help"] = options["h"] or options["help"] 
    132         options["version"] = options["v"] or options["version"] 
    133         options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"] 
    134         options["email"] = options["e"] or options["email"] 
    135          
    136         if options["help"] : 
    137             automat.display_usage_and_quit() 
    138         elif options["version"] : 
    139             automat.display_version_and_quit() 
    140         elif args :     
    141             raise PyKotaCommandLineError, "autopykota doesn't accept non option arguments !" 
    142         else : 
    143             retcode = automat.main(args, options) 
    144     except KeyboardInterrupt :         
    145         logerr("\nInterrupted with Ctrl+C !\n") 
    146         retcode = -3 
    147     except PyKotaCommandLineError, msg :     
    148         logerr("%s : %s\n" % (sys.argv[0], msg)) 
    149         retcode = -2 
    150     except SystemExit :         
    151         pass 
    152     except : 
    153         try : 
    154             automat.crashed("autopykota failed") 
    155         except :     
    156             crashed("autopykota failed") 
    157         retcode = -1 
    158  
    159     try : 
    160         automat.storage.close() 
    161     except (TypeError, NameError, AttributeError) :     
    162         pass 
    163          
    164     sys.exit(retcode)     
     116    parser = PyKotaOptionParser(description=_("A tool to automate user account creation and initial balance setting. THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART OF AN external policy IN pykota.conf, AND MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA !"), 
     117                                usage="autopykota { -i | --initbalance value } [options]") 
     118    parser.add_option("-i", "--initbalance", 
     119                            type="float", 
     120                            dest="initbalance", 
     121                            default=0.0, 
     122                            help=_("Sets the user's initial account balance value. If the user already exists, actual balance is left unmodified. If unset, the default value is %default")) 
     123    parser.add_option("-e", "--email", 
     124                            dest="email", 
     125                            help=_("Sets the user's email address")) 
     126                             
     127    parser.add_example('--email="@example.com" --initbalance=10.0', 
     128                       _("This would set the current user's email address to $PYKOTAUSERNAME@example.com, and would set the initial value of his account balance to 10.0 credits.")) 
     129                        
     130    run(parser, AutoPyKota)