root / pykota / trunk / bin / dumpykota @ 3260

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

Changed license to GNU GPL v3 or later.
Changed Python source encoding from ISO-8859-15 to UTF-8 (only ASCII
was used anyway).

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