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 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 | # $Log$ |
---|
26 | # Revision 1.21 2005/01/08 17:03:06 jalet |
---|
27 | # "--format cups" output more resembling CUPS' page_log. |
---|
28 | # Split into a command line tool and a module, to allow easier coding of |
---|
29 | # a CGI interface. |
---|
30 | # |
---|
31 | # Revision 1.20 2005/01/08 11:25:48 jalet |
---|
32 | # Fixed the time zone field in page_log output to make PrintAnalyzer happy |
---|
33 | # |
---|
34 | # Revision 1.19 2005/01/08 10:52:01 jalet |
---|
35 | # Now dumpykota can dump the history into CUPS' page_log format, allowing the |
---|
36 | # use of tools like PrintAnalyzer or phpPrintAnalyzer along with PyKota |
---|
37 | # |
---|
38 | # Revision 1.18 2005/01/07 12:40:53 jalet |
---|
39 | # Fixed field type's name in XML dumps. |
---|
40 | # |
---|
41 | # Revision 1.17 2004/12/21 15:50:00 jalet |
---|
42 | # The dumpykota command now supports extended filtering capabilities with |
---|
43 | # the PostgreSQL backend. LDAP doesn't yet support such possibilities. |
---|
44 | # |
---|
45 | # Revision 1.16 2004/12/21 14:45:31 jalet |
---|
46 | # Prepared dumpykota to accept the new --filter command line option. Some |
---|
47 | # additionnal work needs to be done in the backends though. |
---|
48 | # |
---|
49 | # Revision 1.15 2004/10/12 15:37:00 jalet |
---|
50 | # Now outputs the name of the offending user if a mere mortal tries to use |
---|
51 | # one of these commands !!! |
---|
52 | # |
---|
53 | # Revision 1.14 2004/10/11 22:53:05 jalet |
---|
54 | # Postponed string interpolation to help message's output method |
---|
55 | # |
---|
56 | # Revision 1.13 2004/10/11 12:49:06 jalet |
---|
57 | # Renders help translatable |
---|
58 | # |
---|
59 | # Revision 1.12 2004/10/07 21:14:28 jalet |
---|
60 | # Hopefully final fix for data encoding to and from the database |
---|
61 | # |
---|
62 | # Revision 1.11 2004/10/07 14:35:40 jalet |
---|
63 | # Now edpykota refuses to launch if the user is not a PyKota administrator. |
---|
64 | # dumpykota : now has the same error message than edpykota in this case. |
---|
65 | # |
---|
66 | # Revision 1.10 2004/10/06 10:05:47 jalet |
---|
67 | # Minor changes to allow any PyKota administrator to launch enhanced versions |
---|
68 | # of the commands, and not only the root user. |
---|
69 | # |
---|
70 | # Revision 1.9 2004/10/05 20:08:46 jalet |
---|
71 | # Misleading help message. Thx to Johannes Laemmermann. |
---|
72 | # |
---|
73 | # Revision 1.8 2004/10/05 09:59:19 jalet |
---|
74 | # Restore compatibility with Python 2.1 |
---|
75 | # |
---|
76 | # Revision 1.7 2004/10/04 21:25:29 jalet |
---|
77 | # dumpykota can now output datas in the XML format |
---|
78 | # |
---|
79 | # Revision 1.6 2004/09/15 18:28:41 jalet |
---|
80 | # Updated help for dumpykota |
---|
81 | # |
---|
82 | # Revision 1.5 2004/09/15 07:38:05 jalet |
---|
83 | # Fix for uninitialized variable |
---|
84 | # |
---|
85 | # Revision 1.4 2004/09/15 07:26:19 jalet |
---|
86 | # Data dumps are now ordered by entry creation date if applicable. |
---|
87 | # Now dumpykota exits with a message when there's a broken pipe like |
---|
88 | # in dumpykota --data history | head -3 |
---|
89 | # |
---|
90 | # Revision 1.3 2004/09/15 06:58:25 jalet |
---|
91 | # User groups membership and printer groups membership can now be dumped too |
---|
92 | # |
---|
93 | # Revision 1.2 2004/09/14 22:29:12 jalet |
---|
94 | # First version of dumpykota. Works fine but only with PostgreSQL backend |
---|
95 | # for now. |
---|
96 | # |
---|
97 | # Revision 1.1 2004/07/01 19:22:37 jalet |
---|
98 | # First draft of dumpykota |
---|
99 | # |
---|
100 | # |
---|
101 | # |
---|
102 | |
---|
103 | import sys |
---|
104 | import os |
---|
105 | |
---|
106 | from pykota import version |
---|
107 | from pykota.tool import PyKotaToolError, crashed, N_ |
---|
108 | from pykota.dumper import DumPyKota |
---|
109 | |
---|
110 | __doc__ = N_("""dumpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
111 | |
---|
112 | Dumps PyKota database's content. |
---|
113 | |
---|
114 | command line usage : |
---|
115 | |
---|
116 | dumpykota [options] [filterexpr] |
---|
117 | |
---|
118 | options : |
---|
119 | |
---|
120 | -v | --version Prints dumpykota's version number then exits. |
---|
121 | -h | --help Prints this message then exits. |
---|
122 | |
---|
123 | -d | --data type Dumps 'type' datas. Allowed types are : |
---|
124 | |
---|
125 | - history : dumps the jobs history. |
---|
126 | - users : dumps users. |
---|
127 | - groups : dumps user groups. |
---|
128 | - printers : dump printers. |
---|
129 | - upquotas : dump user quotas. |
---|
130 | - gpquotas : dump user groups quotas. |
---|
131 | - payments : dumps user payments. |
---|
132 | - pmembers : dumps printer groups members. |
---|
133 | - umembers : dumps user groups members. |
---|
134 | |
---|
135 | NB : the -d | --data command line option |
---|
136 | is MANDATORY. |
---|
137 | |
---|
138 | -f | --format fmt Dumps datas in the 'fmt' format. When not specified, |
---|
139 | the format is to dump datas in the csv format (comma |
---|
140 | separated values). All data dumped is between double |
---|
141 | quotes. Allowed formats are : |
---|
142 | |
---|
143 | - csv : separate datas with commas |
---|
144 | - ssv : separate datas with semicolons |
---|
145 | - tsv : separate datas with tabs |
---|
146 | - xml : dump data as XML |
---|
147 | - cups : dump datas in CUPS' page_log format : |
---|
148 | ONLY AVAILABLE WITH --data history |
---|
149 | |
---|
150 | -o | --output fname All datas will be dumped to the file instead of |
---|
151 | to the standard output. The special '-' filename |
---|
152 | is the default value and means stdout. |
---|
153 | WARNING : existing files are truncated ! |
---|
154 | |
---|
155 | Use the filter expressions to extract only parts of the |
---|
156 | datas. Allowed filters are of the form : |
---|
157 | |
---|
158 | key=value |
---|
159 | |
---|
160 | Allowed keys for now are : |
---|
161 | |
---|
162 | username User's name |
---|
163 | groupname Users group's name |
---|
164 | printername Printer's name |
---|
165 | pgroupname Printers group's name |
---|
166 | |
---|
167 | Contrary to other PyKota management tools, wildcard characters are not |
---|
168 | expanded, so you can't use them. |
---|
169 | |
---|
170 | NB : not all keys are allowed for each data type, so the result may be |
---|
171 | empty if you use a key not available for a particular data type. |
---|
172 | |
---|
173 | Examples : |
---|
174 | |
---|
175 | $ dumpykota --data history --format csv >myfile.csv |
---|
176 | |
---|
177 | This dumps the history in a comma separated values file, for possible |
---|
178 | use in a spreadsheet. |
---|
179 | |
---|
180 | $ dumpykota --data users --format xml -o users.xml |
---|
181 | |
---|
182 | Dumps all users datas to the users.xml file. |
---|
183 | |
---|
184 | $ dumpykota --data history printername=HP2100 username=jerome |
---|
185 | |
---|
186 | Dumps the job history for user jerome on printer HP2100 only. |
---|
187 | |
---|
188 | This program is free software; you can redistribute it and/or modify |
---|
189 | it under the terms of the GNU General Public License as published by |
---|
190 | the Free Software Foundation; either version 2 of the License, or |
---|
191 | (at your option) any later version. |
---|
192 | |
---|
193 | This program is distributed in the hope that it will be useful, |
---|
194 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
195 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
196 | GNU General Public License for more details. |
---|
197 | |
---|
198 | You should have received a copy of the GNU General Public License |
---|
199 | along with this program; if not, write to the Free Software |
---|
200 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
201 | |
---|
202 | Please e-mail bugs to: %s""") |
---|
203 | |
---|
204 | if __name__ == "__main__" : |
---|
205 | retcode = 0 |
---|
206 | try : |
---|
207 | defaults = { \ |
---|
208 | "format" : "csv", \ |
---|
209 | "output" : "-", \ |
---|
210 | } |
---|
211 | short_options = "vhd:f:o:" |
---|
212 | long_options = ["help", "version", "data=", "format=", "output="] |
---|
213 | |
---|
214 | # Initializes the command line tool |
---|
215 | dumper = DumPyKota(doc=__doc__) |
---|
216 | |
---|
217 | # parse and checks the command line |
---|
218 | (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
219 | |
---|
220 | # sets long options |
---|
221 | options["help"] = options["h"] or options["help"] |
---|
222 | options["version"] = options["v"] or options["version"] |
---|
223 | options["data"] = options["d"] or options["data"] |
---|
224 | options["format"] = options["f"] or options["format"] or defaults["format"] |
---|
225 | options["output"] = options["o"] or options["output"] or defaults["output"] |
---|
226 | |
---|
227 | if options["help"] : |
---|
228 | dumper.display_usage_and_quit() |
---|
229 | elif options["version"] : |
---|
230 | dumper.display_version_and_quit() |
---|
231 | elif options["data"] is None : |
---|
232 | raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.") |
---|
233 | else : |
---|
234 | retcode = dumper.main(args, options) |
---|
235 | except SystemExit : |
---|
236 | pass |
---|
237 | except : |
---|
238 | try : |
---|
239 | dumper.crashed("dumpykota failed") |
---|
240 | except : |
---|
241 | crashed("dumpykota failed") |
---|
242 | retcode = -1 |
---|
243 | |
---|
244 | try : |
---|
245 | dumper.storage.close() |
---|
246 | except (TypeError, NameError, AttributeError) : |
---|
247 | pass |
---|
248 | |
---|
249 | sys.exit(retcode) |
---|