Changeset 3348

Show
Ignore:
Timestamp:
02/24/08 23:22:08 (17 years ago)
Author:
jerome
Message:

repykota now works new style.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/repykota

    r3295 r3348  
    2222# 
    2323 
     24"""Minimalist print accounting reports for PyKota.""" 
     25 
    2426import sys 
    2527import os 
     
    2931 
    3032import pykota.appinit 
    31 from pykota.utils import * 
    32  
    33 from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     33from pykota.utils import run 
     34from pykota.commandline import PyKotaOptionParser 
     35from pykota.errors import PyKotaCommandLineError 
    3436from pykota.tool import PyKotaTool 
    3537from pykota import reporter 
    36  
    37 __doc__ = N_("""repykota v%(__version__)s (c) %(__years__)s %(__author__)s 
    38  
    39 Generates print quota reports. 
    40  
    41 command line usage : 
    42  
    43   repykota [options]  
    44  
    45 options : 
    46  
    47   -v | --version       Prints repykota's version number then exits. 
    48   -h | --help          Prints this message then exits. 
    49    
    50   -u | --users         Generates a report on users quota, this is  
    51                        the default. 
    52    
    53   -g | --groups        Generates a report on group quota instead of users. 
    54    
    55   -i | --ingroups g1[,g2...]  Only lists users who are members of these 
    56                               groups. Reserved to PyKota Administrators. 
    57    
    58   -P | --printer p     Report quotas on this printer only. Actually p can 
    59                        use wildcards characters to select only 
    60                        some printers. The default value is *, meaning 
    61                        all printers. 
    62                        You can specify several names or wildcards,  
    63                        by separating them with commas. 
    64    
    65 examples :                               
    66  
    67   $ repykota --printer lp 
    68    
    69   This will print the quota status for all users who use the lp printer. 
    70  
    71   $ repykota  
    72    
    73   This will print the quota status for all users on all printers. 
    74    
    75   $ repykota --printer "laser*,*pson" jerome "jo*" 
    76    
    77   This will print the quota status for user jerome and all users 
    78   whose name begins with "jo" on all printers which name begins 
    79   with "laser" or ends with "pson". 
    80    
    81   If launched by an user who is not a PyKota administrator, additionnal 
    82   arguments representing users or groups names are ignored, and only the 
    83   current user/group is reported. 
    84 """) 
    8538         
    8639class RePyKota(PyKotaTool) :         
     
    8841    def main(self, ugnames, options) : 
    8942        """Print Quota reports generator.""" 
     43        if options.ingroups and options.groups : 
     44            raise PyKotaCommandLineError, _("Incompatible options, see help.") 
     45             
    9046        if self.config.isAdmin : 
    9147            # PyKota administrator 
     
    9450                ugnames = [ "*" ] 
    9551                 
    96             if options["ingroups"] : 
    97                 groupsnames = options["ingroups"].split(",") 
     52            if options.ingroups : 
     53                groupsnames = options.ingroups.split(",") 
    9854                groups = [self.storage.getGroup(gname) for gname in groupsnames] 
    9955                members = {} 
     
    10763        else :         
    10864            # reports only the current user 
    109             if options["ingroups"] : 
     65            if options.ingroups : 
    11066                raise PyKotaCommandLineError, _("Option --ingroups is reserved to PyKota Administrators.") 
    11167                 
    11268            username = pwd.getpwuid(os.geteuid())[0] 
    113             if options["groups"] : 
     69            if options.groups : 
    11470                user = self.storage.getUser(username) 
    11571                if user.Exists : 
     
    12076                ugnames = [ username ] 
    12177         
    122         printers = self.storage.getMatchingPrinters(options["printer"]) 
     78        printers = self.storage.getMatchingPrinters(options.printer) 
    12379        if not printers : 
    124             raise PyKotaCommandLineError, _("There's no printer matching %s") % options["printer"] 
     80            raise PyKotaCommandLineError, _("There's no printer matching %s") % options.printer 
    12581             
    126         self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0)     
     82        self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, options.groups) 
    12783        print self.reportingtool.generateReport() 
    12884                     
    12985if __name__ == "__main__" :  
    130     retcode = 0 
    131     try : 
    132         defaults = { \ 
    133                      "printer" : "*", \ 
    134                    } 
    135         short_options = "vhugi:P:" 
    136         long_options = ["help", "version", "users", "groups", "ingroups=", "printer="] 
    137          
    138         # Initializes the command line tool 
    139         reportTool = RePyKota(doc=__doc__) 
    140         reportTool.deferredInit() 
    141          
    142         # parse and checks the command line 
    143         (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) 
    144          
    145         # sets long options 
    146         options["help"] = options["h"] or options["help"] 
    147         options["version"] = options["v"] or options["version"] 
    148         options["users"] = options["u"] or options["users"] 
    149         options["groups"] = options["g"] or options["groups"] 
    150         options["printer"] = options["P"] or options["printer"] or defaults["printer"] 
    151         options["ingroups"] = options["i"] or options["ingroups"] 
    152          
    153         if options["help"] : 
    154             reportTool.display_usage_and_quit() 
    155         elif options["version"] : 
    156             reportTool.display_version_and_quit() 
    157         elif (options["users"] or options["ingroups"]) and options["groups"] : 
    158             raise PyKotaCommandLineError, _("incompatible options, see help.") 
    159         else : 
    160             retcode = reportTool.main(args, options) 
    161     except KeyboardInterrupt :         
    162         logerr("\nInterrupted with Ctrl+C !\n") 
    163         retcode = -3 
    164     except PyKotaCommandLineError, msg :     
    165         logerr("%s : %s\n" % (sys.argv[0], msg)) 
    166         retcode = -2 
    167     except SystemExit :         
    168         pass 
    169     except : 
    170         try : 
    171             reportTool.crashed("repykota failed") 
    172         except :     
    173             crashed("repykota failed") 
    174         retcode = -1 
    175  
    176     try : 
    177         reportTool.storage.close() 
    178     except (TypeError, NameError, AttributeError) :     
    179         pass 
    180          
    181     sys.exit(retcode)     
     86    parser = PyKotaOptionParser(description=_("Minimalist print accounting reports for PyKota. If not launched by a PyKota administrator, additionnal arguments representing users or groups names are ignored, limiting the scope of the reports to the current user."), 
     87                                usage="repykota [options] [usernames|groupnames]") 
     88    parser.add_option("-g", "--groups", 
     89                            action="store_true", 
     90                            dest="groups", 
     91                            help=_("Generate group print quota reports instead of user print quota reports.")) 
     92    parser.add_option("-i", "--ingroups", 
     93                            dest="ingroups", 
     94                            help=_("Only reports users who are members of the specified groups. This option is reserved to PyKota administrators.")) 
     95    parser.add_option("-P", "--printer", 
     96                            dest="printer", 
     97                            default="*", 
     98                            help=_("Reports for this printer only. You can specify several printer names by separating them with commas. The default value is '%default', which means all printers.")) 
     99                             
     100    parser.add_example('', 
     101                       _("This would generate a report for all users on all printers.")) 
     102    parser.add_example('--printer HP2100', 
     103                       _("This would generate a report for all users who print to printer 'HP2100'.")) 
     104    parser.add_example('--printer "laser*,*pson" jerome "jo*"', 
     105                       _("This would generate a report for all users named 'jerome' or whose name begins with 'jo', on all printers which name begins with 'laser' or ends with 'pson'.")) 
     106                        
     107    run(parser, RePyKota)