root / pykota / trunk / bin / dumpykota @ 3093

Revision 3093, 6.7 kB (checked in by jerome, 18 years ago)

Now the data dumper can use jobid= as a filter.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[1583]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3
4# PyKota Print Quota Data Dumper
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
[2622]8# (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com>
[1583]9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
[2303]21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[1583]22#
23# $Id$
24#
[2028]25#
[1583]26
27import sys
28
[2829]29from pykota.tool import PyKotaCommandLineError, crashed, N_
[2016]30from pykota.dumper import DumPyKota
[1583]31
[2344]32__doc__ = N_("""dumpykota v%(__version__)s (c) %(__years__)s %(__author__)s
[1583]33
34Dumps PyKota database's content.
35
36command line usage :
37
[1991]38  dumpykota [options] [filterexpr]
[1583]39
40options :
41
[1779]42  -v | --version       Prints dumpykota's version number then exits.
[1583]43  -h | --help          Prints this message then exits.
44 
[1717]45  -d | --data type     Dumps 'type' datas. Allowed types are :
[1583]46                       
47                         - history : dumps the jobs history.
48                         - users : dumps users.
49                         - groups : dumps user groups.
50                         - printers : dump printers.
[1724]51                         - upquotas : dump user quotas.
52                         - gpquotas : dump user groups quotas.
[1717]53                         - payments : dumps user payments.
[1718]54                         - pmembers : dumps printer groups members.
55                         - umembers : dumps user groups members.
[2342]56                         - billingcodes : dumps billing codes.
[2600]57                         - all : dumps all PyKota datas. The output format
58                                 is always XML in this case.
[1717]59                         
60                       NB : the -d | --data command line option   
61                       is MANDATORY.
[1583]62 
63  -f | --format fmt    Dumps datas in the 'fmt' format. When not specified,
64                       the format is to dump datas in the csv format (comma
65                       separated values). All data dumped is between double
66                       quotes. Allowed formats are :
67                       
68                         - csv : separate datas with commas
69                         - ssv : separate datas with semicolons
70                         - tsv : separate datas with tabs
[2600]71                         - xml : dump data as XML
[2013]72                         - cups : dump datas in CUPS' page_log format :
73                                  ONLY AVAILABLE WITH --data history
[1771]74                         
75  -o | --output fname  All datas will be dumped to the file instead of
76                       to the standard output. The special '-' filename
77                       is the default value and means stdout.
78                       WARNING : existing files are truncated !
[2282]79
80  -s | --sum           Summarize the selected datas.
81                           ONLY AVAILABLE WITH --data history or payments
82
[1991]83  Use the filter expressions to extract only parts of the
84  datas. Allowed filters are of the form :
85               
86         key=value
87                         
88  Allowed keys for now are : 
89                       
90         username       User's name
91         groupname      Users group's name
92         printername    Printer's name
93         pgroupname     Printers group's name
[2218]94         hostname       Client's hostname
[3093]95         jobid          Job's Id
[2218]96         billingcode    Job's billing code
[2266]97         start          Job's date of printing
98         end            Job's date of printing
[1991]99         
100  Contrary to other PyKota management tools, wildcard characters are not
101  expanded, so you can't use them.
[1583]102 
[1991]103  NB : not all keys are allowed for each data type, so the result may be
104  empty if you use a key not available for a particular data type.
105 
[1771]106Examples :
107
108  $ dumpykota --data history --format csv >myfile.csv
109 
110  This dumps the history in a comma separated values file, for possible
111  use in a spreadsheet.
112 
113  $ dumpykota --data users --format xml -o users.xml
114 
115  Dumps all users datas to the users.xml file.
116 
[1991]117  $ dumpykota --data history printername=HP2100 username=jerome
[1990]118 
[1991]119  Dumps the job history for user jerome on printer HP2100 only.
[1990]120 
[2266]121  $ dumpykota --data history start=200503 end=20050730234615
122 
123  Dumps all jobs printed between March 1st 2005 at midnight and
124  July 30th 2005 at 23 hours 46 minutes and 15 secondes included.
[2344]125""")
[1583]126       
127if __name__ == "__main__" : 
[1720]128    retcode = 0
[1583]129    try :
130        defaults = { \
[1717]131                     "format" : "csv", \
[1771]132                     "output" : "-", \
[1583]133                   }
[2282]134        short_options = "vhd:f:o:s"
135        long_options = ["help", "version", "data=", "format=", "output=", "sum"]
[1583]136       
137        # Initializes the command line tool
138        dumper = DumPyKota(doc=__doc__)
[2210]139        dumper.deferredInit()
[1583]140       
141        # parse and checks the command line
142        (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
143       
144        # sets long options
145        options["help"] = options["h"] or options["help"]
146        options["version"] = options["v"] or options["version"]
[1717]147        options["data"] = options["d"] or options["data"]
148        options["format"] = options["f"] or options["format"] or defaults["format"]
[1771]149        options["output"] = options["o"] or options["output"] or defaults["output"]
[2282]150        options["sum"] = options["s"] or options["sum"]
[1583]151       
152        if options["help"] :
153            dumper.display_usage_and_quit()
154        elif options["version"] :
155            dumper.display_version_and_quit()
[1717]156        elif options["data"] is None :   
[2512]157            raise PyKotaCommandLineError, _("The -d | --data command line option is mandatory, see help.")
[1583]158        else :
159            retcode = dumper.main(args, options)
[2216]160    except KeyboardInterrupt :       
161        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
[2609]162        retcode = -3
[2512]163    except PyKotaCommandLineError, msg :   
164        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
[2609]165        retcode = -2
[1583]166    except SystemExit :       
167        pass
168    except :
169        try :
[1717]170            dumper.crashed("dumpykota failed")
[1583]171        except :   
[1720]172            crashed("dumpykota failed")
[1583]173        retcode = -1
174
175    try :
176        dumper.storage.close()
177    except (TypeError, NameError, AttributeError) :   
178        pass
179       
180    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.