Changeset 1717

Show
Ignore:
Timestamp:
09/15/04 00:29:13 (20 years ago)
Author:
jalet
Message:

First version of dumpykota. Works fine but only with PostgreSQL backend
for now.

Location:
pykota/trunk
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/dumpykota

    r1583 r1717  
    2424# 
    2525# $Log$ 
     26# Revision 1.2  2004/09/14 22:29:12  jalet 
     27# First version of dumpykota. Works fine but only with PostgreSQL backend 
     28# for now. 
     29# 
    2630# Revision 1.1  2004/07/01 19:22:37  jalet 
    2731# First draft of dumpykota 
     
    5256  -h | --help          Prints this message then exits. 
    5357   
    54   -d | --data type     Dumps 'type' datas. When not specified, the 
    55                        default is to dump the jobs history data. 
    56                        Allowed types are : 
     58  -d | --data type     Dumps 'type' datas. Allowed types are : 
    5759                        
    5860                         - history : dumps the jobs history. 
     
    6264                         - uquotas : dump user quotas. 
    6365                         - gquotas : dump user groups quotas. 
     66                         - payments : dumps user payments. 
     67                          
     68                       NB : the -d | --data command line option    
     69                       is MANDATORY. 
    6470   
    6571  -f | --format fmt    Dumps datas in the 'fmt' format. When not specified, 
     
    7278                         - tsv : separate datas with tabs 
    7379   
    74   -p | --printer p     Only dumps datas concerning printer 'p'. 
    75                        Actually 'p' can use wildcards characters to select 
    76                        only some printers. The default value is *, meaning 
    77                        all printers. 
    78                        You can specify several names or wildcards,  
    79                        by separating them with commas. 
    80                         
    81   -u | --user u        Only dumps datas concerning user 'u'. 
    82                        Actually 'u' can use wildcards characters to select 
    83                        only some users. The default value is *, meaning 
    84                        all users. 
    85                        You can specify several names or wildcards,  
    86                        by separating them with commas. 
    87                         
    88   -g | --group g       Only dumps datas concerning group 'g'. 
    89                        Actually 'g' can use wildcards characters to select 
    90                        only some groups. The default value is *, meaning 
    91                        all groups. 
    92                        You can specify several names or wildcards,  
    93                        by separating them with commas. 
    94    
    95   If launched by a non-root user, additionnal arguments representing 
    96   users or groups names are ignored, and only the current user/group 
    97   is reported. 
    98  
    9980This program is free software; you can redistribute it and/or modify 
    10081it under the terms of the GNU General Public License as published by 
     
    11596class DumPyKota(PyKotaTool) :         
    11697    """A class for dumpykota.""" 
    117     def main(self, ugnames, options) : 
     98    def main(self, arguments, options) : 
    11899        """Print Quota Data Dumper.""" 
    119         uid = os.geteuid() 
    120         if not uid : 
    121             # root user 
    122             if not ugnames : 
    123                 # no username, means all usernames 
    124                 ugnames = [ "*" ] 
    125         else :         
    126             # not the root user 
    127             # reports only the current user 
    128             username = pwd.getpwuid(uid)[0] 
    129             if options["data"] == "groups" : 
    130                 user = self.storage.getUser(username) 
    131                 if user.Exists : 
    132                     ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] 
    133                 else :     
    134                     ugnames = [ ] 
    135             else : 
    136                 ugnames = [ username ] 
     100        datatype = options["data"] 
     101        if datatype not in [ "history", 
     102                             "users", 
     103                             "groups", 
     104                             "printers", 
     105                             "upquotas", 
     106                             "gpquotas", 
     107                             "payments", 
     108                           ] : 
     109            raise PyKotaToolError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype 
     110                     
     111        format = options["format"] 
     112        if format not in [ "csv", 
     113                           "ssv", 
     114                           "tsv", 
     115                         ] : 
     116            raise PyKotaToolError, _("Invalid modifier [%s] for --format command line option, see help.") % datatype 
     117             
     118        entries = getattr(self.storage, "extract%s" % datatype.title())()     
     119        getattr(self, "dump%s" % format.title())(entries) 
     120        return 0 
    137121         
    138         printers = self.storage.getMatchingPrinters(options["printer"]) 
    139         if not printers : 
    140             raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] 
    141              
    142         # TODO : insert code here !     
    143         raise PyKotaToolError, "Not implemented" 
    144                      
     122    def dumpWithSeparator(self, separator, entries) :     
     123        """Dumps datas with a separator.""" 
     124        for entry in entries : 
     125            line = separator.join([ '"%s"' % field for field in entry ]) 
     126            print line 
     127         
     128    def dumpCsv(self, entries) :     
     129        """Dumps datas with a comma as the separator.""" 
     130        self.dumpWithSeparator(",", entries) 
     131                            
     132    def dumpSsv(self, entries) :     
     133        """Dumps datas with a comma as the separator.""" 
     134        self.dumpWithSeparator(";", entries) 
     135                            
     136    def dumpTsv(self, entries) :     
     137        """Dumps datas with a comma as the separator.""" 
     138        self.dumpWithSeparator("\t", entries) 
     139                            
    145140if __name__ == "__main__" :  
    146141    try : 
    147142        defaults = { \ 
    148                      "printer" : "*", \ 
     143                     "format" : "csv", \ 
    149144                   } 
    150         short_options = "vhd:f:p:u:g:" 
    151         long_options = ["help", "version", "data=", "format=", "printer=", "user=", "group="] 
     145        short_options = "vhd:f:" 
     146        long_options = ["help", "version", "data=", "format="] 
    152147         
    153148        # Initializes the command line tool 
     
    160155        options["help"] = options["h"] or options["help"] 
    161156        options["version"] = options["v"] or options["version"] 
    162         options["users"] = options["u"] or options["users"] 
    163         options["groups"] = options["g"] or options["groups"] 
    164         options["printer"] = options["P"] or options["printer"] or defaults["printer"] 
     157        options["data"] = options["d"] or options["data"] 
     158        options["format"] = options["f"] or options["format"] or defaults["format"] 
    165159         
    166160        if options["help"] : 
     
    168162        elif options["version"] : 
    169163            dumper.display_version_and_quit() 
    170         elif options["users"] and options["groups"] :     
    171             raise PyKotaToolError, _("incompatible options, see help.") 
     164        elif options["data"] is None :     
     165            raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.") 
    172166        else : 
     167            if args : 
     168                raise PyKotaToolError, _("Too many arguments, see help.") 
    173169            retcode = dumper.main(args, options) 
    174170    except SystemExit :         
     
    176172    except : 
    177173        try : 
    178             dumper.crashed("repykota failed") 
     174            dumper.crashed("dumpykota failed") 
    179175        except :     
    180176            pass 
  • pykota/trunk/MANIFEST.in

    r1668 r1717  
    1 include README FAQ COPYING LICENSE TODO NEWS CREDITS SECURITY MANIFEST.in clean.sh bin/lprngpykota bin/cupspykota bin/edpykota bin/warnpykota bin/repykota bin/pykotme bin/pykosd bin/pkprinters bin/pkhint bin/pkpgcounter bin/snmpprinterstatus bin/waitprinter.sh bin/papwaitprinter.sh bin/mailandpopup.sh bin/README 
     1include README FAQ COPYING LICENSE TODO NEWS CREDITS SECURITY MANIFEST.in clean.sh bin/dumpykota bin/lprngpykota bin/cupspykota bin/edpykota bin/warnpykota bin/repykota bin/pykotme bin/pykosd bin/pkprinters bin/pkhint bin/pkpgcounter bin/snmpprinterstatus bin/waitprinter.sh bin/papwaitprinter.sh bin/mailandpopup.sh bin/README 
    22recursive-include po README *.sh *.po *.mo *.pot 
    33recursive-include man README *.sh *.1 
  • pykota/trunk/NEWS

    r1713 r1717  
    2222PyKota NEWS : 
    2323 
     24    - 1.20alpha11 :  
     25     
     26        - Generic data dumper 'dumpykota' introduced. 
     27          Works only with PostgreSQL backend for now. 
     28           
    2429    - 1.20alpha10 : 
    2530     
  • pykota/trunk/pykota/storages/pgstorage.py

    r1520 r1717  
    2222# 
    2323# $Log$ 
     24# Revision 1.37  2004/09/14 22:29:12  jalet 
     25# First version of dumpykota. Works fine but only with PostgreSQL backend 
     26# for now. 
     27# 
    2428# Revision 1.36  2004/06/03 23:14:11  jalet 
    2529# Now stores the job's size in bytes in the database. 
     
    196200        self.tool.logdebug("Transaction aborted.") 
    197201         
    198     def doSearch(self, query) : 
    199         """Does a search query.""" 
     202    def doRawSearch(self, query) : 
     203        """Does a raw search query.""" 
    200204        query = query.strip()     
    201205        if not query.endswith(';') :     
     
    207211            raise PyKotaStorageError, msg 
    208212        else :     
    209             if (result is not None) and (result.ntuples() > 0) :  
    210                 return result.dictresult() 
     213            return result 
    211214             
     215    def doSearch(self, query) :         
     216        """Does a search query.""" 
     217        result = self.doRawSearch(query) 
     218        if (result is not None) and (result.ntuples() > 0) :  
     219            return result.dictresult() 
     220         
    212221    def doModify(self, query) : 
    213222        """Does a (possibly multiple) modify query.""" 
  • pykota/trunk/pykota/storages/sql.py

    r1711 r1717  
    2222# 
    2323# $Log$ 
     24# Revision 1.45  2004/09/14 22:29:13  jalet 
     25# First version of dumpykota. Works fine but only with PostgreSQL backend 
     26# for now. 
     27# 
    2428# Revision 1.44  2004/09/10 21:32:54  jalet 
    2529# Small fixes for incomplete entry intialization 
     
    6569 
    6670class SQLStorage : 
     71    def prepareRawResult(self, result) : 
     72        """Prepares a raw result by including the headers.""" 
     73        if result.ntuples() > 0 : 
     74            entries = [result.listfields()] 
     75            entries.extend(result.getresult()) 
     76            return entries 
     77         
     78    def extractPrinters(self) : 
     79        """Extracts all printer records.""" 
     80        result = self.doRawSearch("SELECT * FROM printers") 
     81        return self.prepareRawResult(result) 
     82         
     83    def extractUsers(self) : 
     84        """Extracts all user records.""" 
     85        result = self.doRawSearch("SELECT * FROM users") 
     86        return self.prepareRawResult(result) 
     87         
     88    def extractGroups(self) : 
     89        """Extracts all group records.""" 
     90        result = self.doRawSearch("SELECT * FROM groups") 
     91        return self.prepareRawResult(result) 
     92         
     93    def extractPayments(self) : 
     94        """Extracts all payment records.""" 
     95        result = self.doRawSearch("SELECT username,payments.* FROM users,payments WHERE users.id=payments.userid") 
     96        return self.prepareRawResult(result) 
     97         
     98    def extractUpquotas(self) : 
     99        """Extracts all userpquota records.""" 
     100        result = self.doRawSearch("SELECT users.username,printers.printername,userpquota.* FROM users,printers,userpquota WHERE users.id=userpquota.userid AND printers.id=userpquota.printerid") 
     101        return self.prepareRawResult(result) 
     102         
     103    def extractGpquotas(self) : 
     104        """Extracts all grouppquota records.""" 
     105        result = self.doRawSearch("SELECT groups.groupname,printers.printername,grouppquota.* FROM groups,printers,grouppquota WHERE groups.id=grouppquota.groupid AND printers.id=grouppquota.printerid") 
     106        return self.prepareRawResult(result) 
     107         
     108    def extractHistory(self) : 
     109        """Extracts all jobhistory records.""" 
     110        result = self.doRawSearch("SELECT users.username,printers.printername,jobhistory.* FROM users,printers,jobhistory WHERE users.id=jobhistory.userid AND printers.id=jobhistory.printerid") 
     111        return self.prepareRawResult(result) 
     112         
    67113    def getAllUsersNames(self) :     
    68114        """Extracts all user names.""" 
  • pykota/trunk/pykota/version.py

    r1713 r1717  
    2222# 
    2323 
    24 __version__ = "1.20alpha10_unofficial" 
     24__version__ = "1.20alpha11_unofficial" 
    2525 
    2626__doc__ = """PyKota : a complete Printing Quota Solution for CUPS and LPRng.""" 
  • pykota/trunk/setup.py

    r1658 r1717  
    2424# 
    2525# $Log$ 
     26# Revision 1.50  2004/09/14 22:29:12  jalet 
     27# First version of dumpykota. Works fine but only with PostgreSQL backend 
     28# for now. 
     29# 
    2630# Revision 1.49  2004/07/28 13:00:02  jalet 
    2731# Now takes care of .sxi and .sxc files if any 
     
    473477      url = "http://www.librelogiciel.com/software/", 
    474478      packages = [ "pykota", "pykota.storages", "pykota.loggers", "pykota.accounters", "pykota.reporters" ], 
    475       scripts = [ "bin/pkpgcounter", "bin/snmpprinterstatus", "bin/pykosd", "bin/edpykota", "bin/repykota", "bin/warnpykota", "bin/pykotme", "bin/pkprinters", "bin/pkhint" ], 
     479      scripts = [ "bin/dumpykota", "bin/pkpgcounter", "bin/snmpprinterstatus", "bin/pykosd", "bin/edpykota", "bin/repykota", "bin/warnpykota", "bin/pykotme", "bin/pkprinters", "bin/pkhint" ], 
    476480      data_files = data_files) 
    477481