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