root / pykota / trunk / bin / dumpykota @ 2342

Revision 2342, 7.1 kB (checked in by jerome, 19 years ago)

The pkbcodes command line tool now works fine with the PostgreSQL
backend. The dumpykota command can now dump billing codes too.
Still no code for LDAP though.
NB : a database upgrade is necessary AGAIN !
Severity : no need to play with this until there's LDAP support.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
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#
8# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
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
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22#
23# $Id$
24#
25#
26
27import sys
28import os
29
30from pykota import version
31from pykota.tool import PyKotaToolError, crashed, N_
32from pykota.dumper import DumPyKota
33
34__doc__ = N_("""dumpykota v%s (c) %s %s
35
36Dumps PyKota database's content.
37
38command line usage :
39
40  dumpykota [options] [filterexpr]
41
42options :
43
44  -v | --version       Prints dumpykota's version number then exits.
45  -h | --help          Prints this message then exits.
46 
47  -d | --data type     Dumps 'type' datas. Allowed types are :
48                       
49                         - history : dumps the jobs history.
50                         - users : dumps users.
51                         - groups : dumps user groups.
52                         - printers : dump printers.
53                         - upquotas : dump user quotas.
54                         - gpquotas : dump user groups quotas.
55                         - payments : dumps user payments.
56                         - pmembers : dumps printer groups members.
57                         - umembers : dumps user groups members.
58                         - billingcodes : dumps billing codes.
59                         
60                       NB : the -d | --data command line option   
61                       is MANDATORY.
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
71                         - xml : dump data as XML
72                         - cups : dump datas in CUPS' page_log format :
73                                  ONLY AVAILABLE WITH --data history
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 !
79
80  -s | --sum           Summarize the selected datas.
81                           ONLY AVAILABLE WITH --data history or payments
82
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
94         hostname       Client's hostname
95         billingcode    Job's billing code
96         start          Job's date of printing
97         end            Job's date of printing
98         
99  Contrary to other PyKota management tools, wildcard characters are not
100  expanded, so you can't use them.
101 
102  NB : not all keys are allowed for each data type, so the result may be
103  empty if you use a key not available for a particular data type.
104 
105Examples :
106
107  $ dumpykota --data history --format csv >myfile.csv
108 
109  This dumps the history in a comma separated values file, for possible
110  use in a spreadsheet.
111 
112  $ dumpykota --data users --format xml -o users.xml
113 
114  Dumps all users datas to the users.xml file.
115 
116  $ dumpykota --data history printername=HP2100 username=jerome
117 
118  Dumps the job history for user jerome on printer HP2100 only.
119 
120  $ dumpykota --data history start=200503 end=20050730234615
121 
122  Dumps all jobs printed between March 1st 2005 at midnight and
123  July 30th 2005 at 23 hours 46 minutes and 15 secondes included.
124 
125This program is free software; you can redistribute it and/or modify
126it under the terms of the GNU General Public License as published by
127the Free Software Foundation; either version 2 of the License, or
128(at your option) any later version.
129
130This program is distributed in the hope that it will be useful,
131but WITHOUT ANY WARRANTY; without even the implied warranty of
132MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
133GNU General Public License for more details.
134
135You should have received a copy of the GNU General Public License
136along with this program; if not, write to the Free Software
137Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
138
139Please e-mail bugs to: %s""")
140       
141if __name__ == "__main__" : 
142    retcode = 0
143    try :
144        defaults = { \
145                     "format" : "csv", \
146                     "output" : "-", \
147                   }
148        short_options = "vhd:f:o:s"
149        long_options = ["help", "version", "data=", "format=", "output=", "sum"]
150       
151        # Initializes the command line tool
152        dumper = DumPyKota(doc=__doc__)
153        dumper.deferredInit()
154       
155        # parse and checks the command line
156        (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
157       
158        # sets long options
159        options["help"] = options["h"] or options["help"]
160        options["version"] = options["v"] or options["version"]
161        options["data"] = options["d"] or options["data"]
162        options["format"] = options["f"] or options["format"] or defaults["format"]
163        options["output"] = options["o"] or options["output"] or defaults["output"]
164        options["sum"] = options["s"] or options["sum"]
165       
166        if options["help"] :
167            dumper.display_usage_and_quit()
168        elif options["version"] :
169            dumper.display_version_and_quit()
170        elif options["data"] is None :   
171            raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.")
172        else :
173            retcode = dumper.main(args, options)
174    except KeyboardInterrupt :       
175        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
176    except SystemExit :       
177        pass
178    except :
179        try :
180            dumper.crashed("dumpykota failed")
181        except :   
182            crashed("dumpykota failed")
183        retcode = -1
184
185    try :
186        dumper.storage.close()
187    except (TypeError, NameError, AttributeError) :   
188        pass
189       
190    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.