root / pykota / trunk / bin / dumpykota @ 3288

Revision 3288, 7.9 kB (checked in by jerome, 16 years ago)

Moved all exceptions definitions to a dedicated module.

  • 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: UTF-8 -*-
3#
4# PyKota : Print Quotas for CUPS
5#
6# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# $Id$
21#
22#
23
24import sys
25
26from pykota.errors import PyKotaCommandLineError
27from pykota.tool import crashed, N_
28from pykota.dumper import DumPyKota
29
30__doc__ = N_("""dumpykota v%(__version__)s (c) %(__years__)s %(__author__)s
31
32Dumps PyKota database's content.
33
34command line usage :
35
36  dumpykota [options] [filterexpr]
37
38options :
39
40  -v | --version       Prints dumpykota's version number then exits.
41  -h | --help          Prints this message then exits.
42 
43  -d | --data type     Dumps 'type' datas. Allowed types are :
44                       
45                         - history : dumps the jobs history.
46                         - users : dumps users.
47                         - groups : dumps user groups.
48                         - printers : dump printers.
49                         - upquotas : dump user quotas.
50                         - gpquotas : dump user groups quotas.
51                         - payments : dumps user payments.
52                         - pmembers : dumps printer groups members.
53                         - umembers : dumps user groups members.
54                         - billingcodes : dumps billing codes.
55                         - all : dumps all PyKota datas. The output format
56                                 is always XML in this case.
57                         
58                       NB : the -d | --data command line option   
59                       is MANDATORY.
60 
61  -f | --format fmt    Dumps datas in the 'fmt' format. When not specified,
62                       the format is to dump datas in the csv format (comma
63                       separated values). All data dumped is between double
64                       quotes. Allowed formats are :
65                       
66                         - csv : separate datas with commas
67                         - ssv : separate datas with semicolons
68                         - tsv : separate datas with tabs
69                         - xml : dump data as XML
70                         - cups : dump datas in CUPS' page_log format :
71                                  ONLY AVAILABLE WITH --data history
72                         
73  -o | --output fname  All datas will be dumped to the file instead of
74                       to the standard output. The special '-' filename
75                       is the default value and means stdout.
76                       WARNING : existing files are truncated !
77
78  -O | --orderby exp   Change the ordering or result. 'exp' is a comma
79                       separated list of ordering statements, for example
80                       '--orderby +username,-printername'. Not all expression
81                       values are meaningful, so using this command line
82                       switch is not recommanded if you don't know the
83                       exact layout of PyKota's database schema.
84                       
85  -s | --sum           Summarize the selected datas.
86                           ONLY AVAILABLE WITH --data history or payments
87
88  Use the filter expressions to extract only parts of the
89  datas. Allowed filters are of the form :
90               
91         key=value
92                         
93  Allowed keys for now are : 
94                       
95         username       User's name
96         groupname      Users group's name
97         printername    Printer's name
98         pgroupname     Printers group's name
99         hostname       Client's hostname
100         jobid          Job's Id
101         billingcode    Job's billing code
102         start          Job's date of printing
103         end            Job's date of printing
104         
105  Dates formatting with 'start' and 'end' filter keys :
106 
107    YYYY : year boundaries
108    YYYYMM : month boundaries
109    YYYYMMDD : day boundaries
110    YYYYMMDDhh : hour boundaries
111    YYYYMMDDhhmm : minute boundaries
112    YYYYMMDDhhmmss : second boundaries
113    yesterday[+-NbDays] : yesterday more or less N days (e.g. : yesterday-15)
114    today[+-NbDays] : today more or less N days (e.g. : today-15)
115    tomorrow[+-NbDays] : tomorrow more or less N days (e.g. : tomorrow-15)
116    now[+-NbDays] : now more or less N days (e.g. now-15)
117
118  'now' and 'today' are not exactly the same since today represents the first
119  or last second of the day depending on if it's used in a start= or end=
120  date expression. The utility to be able to specify dates in the future is
121  a question which remains to be answered :-)
122 
123  Contrary to other PyKota management tools, wildcard characters are not
124  expanded, so you can't use them.
125 
126  NB : not all keys are allowed for each data type, so the result may be
127  empty if you use a key not available for a particular data type.
128 
129Examples :
130
131  $ dumpykota --data history --format csv >myfile.csv
132 
133  This dumps the history in a comma separated values file, for possible
134  use in a spreadsheet.
135 
136  $ dumpykota --data users --format xml -o users.xml
137 
138  Dumps all users datas to the users.xml file.
139 
140  $ dumpykota --data history printername=HP2100 username=jerome
141 
142  Dumps the job history for user jerome on printer HP2100 only.
143 
144  $ dumpykota --data history start=200503 end=20050730234615
145 
146  Dumps all jobs printed between March 1st 2005 at midnight and
147  July 30th 2005 at 23 hours 46 minutes and 15 secondes included.
148""")
149       
150if __name__ == "__main__" : 
151    retcode = 0
152    try :
153        defaults = { \
154                     "format" : "csv", \
155                     "output" : "-", \
156                   }
157        short_options = "vhd:f:o:sO:"
158        long_options = ["help", "version", "data=", "format=", "output=", "sum", "orderby="]
159       
160        # Initializes the command line tool
161        dumper = DumPyKota(doc=__doc__)
162        dumper.deferredInit()
163       
164        # parse and checks the command line
165        (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
166       
167        # sets long options
168        options["help"] = options["h"] or options["help"]
169        options["version"] = options["v"] or options["version"]
170        options["data"] = options["d"] or options["data"]
171        options["format"] = options["f"] or options["format"] or defaults["format"]
172        options["output"] = options["o"] or options["output"] or defaults["output"]
173        options["sum"] = options["s"] or options["sum"]
174        options["orderby"] = options["O"] or options["orderby"]
175       
176        if options["help"] :
177            dumper.display_usage_and_quit()
178        elif options["version"] :
179            dumper.display_version_and_quit()
180        elif options["data"] is None :   
181            raise PyKotaCommandLineError, _("The -d | --data command line option is mandatory, see help.")
182        else :
183            retcode = dumper.main(args, options)
184    except KeyboardInterrupt :       
185        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
186        retcode = -3
187    except PyKotaCommandLineError, msg :   
188        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
189        retcode = -2
190    except SystemExit :       
191        pass
192    except :
193        try :
194            dumper.crashed("dumpykota failed")
195        except :   
196            crashed("dumpykota failed")
197        retcode = -1
198
199    try :
200        dumper.storage.close()
201    except (TypeError, NameError, AttributeError) :   
202        pass
203       
204    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.