root / pykota / trunk / bin / dumpykota @ 2240

Revision 2218, 6.6 kB (checked in by jerome, 20 years ago)

The data dumper now supports filtering by hostname
or billing code.

  • 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, 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) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres
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                         
59                       NB : the -d | --data command line option   
60                       is MANDATORY.
61 
62  -f | --format fmt    Dumps datas in the 'fmt' format. When not specified,
63                       the format is to dump datas in the csv format (comma
64                       separated values). All data dumped is between double
65                       quotes. Allowed formats are :
66                       
67                         - csv : separate datas with commas
68                         - ssv : separate datas with semicolons
69                         - tsv : separate datas with tabs
70                         - xml : dump data as XML
71                         - cups : dump datas in CUPS' page_log format :
72                                  ONLY AVAILABLE WITH --data history
73                         
74  -o | --output fname  All datas will be dumped to the file instead of
75                       to the standard output. The special '-' filename
76                       is the default value and means stdout.
77                       WARNING : existing files are truncated !
78                       
79  Use the filter expressions to extract only parts of the
80  datas. Allowed filters are of the form :
81               
82         key=value
83                         
84  Allowed keys for now are : 
85                       
86         username       User's name
87         groupname      Users group's name
88         printername    Printer's name
89         pgroupname     Printers group's name
90         hostname       Client's hostname
91         billingcode    Job's billing code
92         
93  Contrary to other PyKota management tools, wildcard characters are not
94  expanded, so you can't use them.
95 
96  NB : not all keys are allowed for each data type, so the result may be
97  empty if you use a key not available for a particular data type.
98 
99Examples :
100
101  $ dumpykota --data history --format csv >myfile.csv
102 
103  This dumps the history in a comma separated values file, for possible
104  use in a spreadsheet.
105 
106  $ dumpykota --data users --format xml -o users.xml
107 
108  Dumps all users datas to the users.xml file.
109 
110  $ dumpykota --data history printername=HP2100 username=jerome
111 
112  Dumps the job history for user jerome on printer HP2100 only.
113 
114This program is free software; you can redistribute it and/or modify
115it under the terms of the GNU General Public License as published by
116the Free Software Foundation; either version 2 of the License, or
117(at your option) any later version.
118
119This program is distributed in the hope that it will be useful,
120but WITHOUT ANY WARRANTY; without even the implied warranty of
121MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
122GNU General Public License for more details.
123
124You should have received a copy of the GNU General Public License
125along with this program; if not, write to the Free Software
126Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
127
128Please e-mail bugs to: %s""")
129       
130if __name__ == "__main__" : 
131    retcode = 0
132    try :
133        defaults = { \
134                     "format" : "csv", \
135                     "output" : "-", \
136                   }
137        short_options = "vhd:f:o:"
138        long_options = ["help", "version", "data=", "format=", "output="]
139       
140        # Initializes the command line tool
141        dumper = DumPyKota(doc=__doc__)
142        dumper.deferredInit()
143       
144        # parse and checks the command line
145        (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
146       
147        # sets long options
148        options["help"] = options["h"] or options["help"]
149        options["version"] = options["v"] or options["version"]
150        options["data"] = options["d"] or options["data"]
151        options["format"] = options["f"] or options["format"] or defaults["format"]
152        options["output"] = options["o"] or options["output"] or defaults["output"]
153       
154        if options["help"] :
155            dumper.display_usage_and_quit()
156        elif options["version"] :
157            dumper.display_version_and_quit()
158        elif options["data"] is None :   
159            raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.")
160        else :
161            retcode = dumper.main(args, options)
162    except KeyboardInterrupt :       
163        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
164    except SystemExit :       
165        pass
166    except :
167        try :
168            dumper.crashed("dumpykota failed")
169        except :   
170            crashed("dumpykota failed")
171        retcode = -1
172
173    try :
174        dumper.storage.close()
175    except (TypeError, NameError, AttributeError) :   
176        pass
177       
178    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.