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