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