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.7 2004/10/04 21:25:29 jalet |
---|
27 | # dumpykota can now output datas in the XML format |
---|
28 | # |
---|
29 | # Revision 1.6 2004/09/15 18:28:41 jalet |
---|
30 | # Updated help for dumpykota |
---|
31 | # |
---|
32 | # Revision 1.5 2004/09/15 07:38:05 jalet |
---|
33 | # Fix for uninitialized variable |
---|
34 | # |
---|
35 | # Revision 1.4 2004/09/15 07:26:19 jalet |
---|
36 | # Data dumps are now ordered by entry creation date if applicable. |
---|
37 | # Now dumpykota exits with a message when there's a broken pipe like |
---|
38 | # in dumpykota --data history | head -3 |
---|
39 | # |
---|
40 | # Revision 1.3 2004/09/15 06:58:25 jalet |
---|
41 | # User groups membership and printer groups membership can now be dumped too |
---|
42 | # |
---|
43 | # Revision 1.2 2004/09/14 22:29:12 jalet |
---|
44 | # First version of dumpykota. Works fine but only with PostgreSQL backend |
---|
45 | # for now. |
---|
46 | # |
---|
47 | # Revision 1.1 2004/07/01 19:22:37 jalet |
---|
48 | # First draft of dumpykota |
---|
49 | # |
---|
50 | # |
---|
51 | # |
---|
52 | |
---|
53 | import sys |
---|
54 | import os |
---|
55 | import pwd |
---|
56 | |
---|
57 | try : |
---|
58 | import jaxml |
---|
59 | except ImportError : |
---|
60 | sys.stderr.write("The jaxml Python module is not installed. XML output is disabled.\n") |
---|
61 | sys.stderr.write("Download jaxml from http://www.librelogiciel.com/software/ or from your Debian archive of choice\n") |
---|
62 | hasJAXML = 0 |
---|
63 | else : |
---|
64 | hasJAXML = 1 |
---|
65 | |
---|
66 | from pykota import version |
---|
67 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed |
---|
68 | from pykota.config import PyKotaConfigError |
---|
69 | from pykota.storage import PyKotaStorageError |
---|
70 | |
---|
71 | __doc__ = """dumpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
72 | |
---|
73 | Dumps PyKota database's content. |
---|
74 | |
---|
75 | command line usage : |
---|
76 | |
---|
77 | dumpykota [options] |
---|
78 | |
---|
79 | options : |
---|
80 | |
---|
81 | -v | --version Prints repykota's version number then exits. |
---|
82 | -h | --help Prints this message then exits. |
---|
83 | |
---|
84 | -d | --data type Dumps 'type' datas. Allowed types are : |
---|
85 | |
---|
86 | - history : dumps the jobs history. |
---|
87 | - users : dumps users. |
---|
88 | - groups : dumps user groups. |
---|
89 | - printers : dump printers. |
---|
90 | - upquotas : dump user quotas. |
---|
91 | - gpquotas : dump user groups quotas. |
---|
92 | - payments : dumps user payments. |
---|
93 | - pmembers : dumps printer groups members. |
---|
94 | - umembers : dumps user groups members. |
---|
95 | |
---|
96 | NB : the -d | --data command line option |
---|
97 | is MANDATORY. |
---|
98 | |
---|
99 | -f | --format fmt Dumps datas in the 'fmt' format. When not specified, |
---|
100 | the format is to dump datas in the csv format (comma |
---|
101 | separated values). All data dumped is between double |
---|
102 | quotes. Allowed formats are : |
---|
103 | |
---|
104 | - csv : separate datas with commas |
---|
105 | - ssv : separate datas with semicolons |
---|
106 | - tsv : separate datas with tabs |
---|
107 | - xml : dump data as XML |
---|
108 | |
---|
109 | -o | --output fname All datas will be dumped to the file instead of |
---|
110 | to the standard output. The special '-' filename |
---|
111 | is the default value and means stdout. |
---|
112 | WARNING : existing files are truncated ! |
---|
113 | |
---|
114 | Examples : |
---|
115 | |
---|
116 | $ dumpykota --data history --format csv >myfile.csv |
---|
117 | |
---|
118 | This dumps the history in a comma separated values file, for possible |
---|
119 | use in a spreadsheet. |
---|
120 | |
---|
121 | $ dumpykota --data users --format xml -o users.xml |
---|
122 | |
---|
123 | Dumps all users datas to the users.xml file. |
---|
124 | |
---|
125 | This program is free software; you can redistribute it and/or modify |
---|
126 | it under the terms of the GNU General Public License as published by |
---|
127 | the Free Software Foundation; either version 2 of the License, or |
---|
128 | (at your option) any later version. |
---|
129 | |
---|
130 | This program is distributed in the hope that it will be useful, |
---|
131 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
132 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
133 | GNU General Public License for more details. |
---|
134 | |
---|
135 | You should have received a copy of the GNU General Public License |
---|
136 | along with this program; if not, write to the Free Software |
---|
137 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
138 | |
---|
139 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
140 | |
---|
141 | class DumPyKota(PyKotaTool) : |
---|
142 | """A class for dumpykota.""" |
---|
143 | def main(self, arguments, options) : |
---|
144 | """Print Quota Data Dumper.""" |
---|
145 | datatype = options["data"] |
---|
146 | if datatype not in [ "history", |
---|
147 | "users", |
---|
148 | "groups", |
---|
149 | "printers", |
---|
150 | "upquotas", |
---|
151 | "gpquotas", |
---|
152 | "payments", |
---|
153 | "pmembers", |
---|
154 | "umembers", |
---|
155 | ] : |
---|
156 | raise PyKotaToolError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype |
---|
157 | |
---|
158 | format = options["format"] |
---|
159 | if format not in [ "csv", |
---|
160 | "ssv", |
---|
161 | "tsv", |
---|
162 | "xml", |
---|
163 | ] : |
---|
164 | raise PyKotaToolError, _("Invalid modifier [%s] for --format command line option, see help.") % datatype |
---|
165 | |
---|
166 | if (format == "xml") and not hasJAXML : |
---|
167 | raise PyKotaToolError, _("XML output is disabled because the jaxml module is not available.") |
---|
168 | |
---|
169 | entries = getattr(self.storage, "extract%s" % datatype.title())() |
---|
170 | if entries : |
---|
171 | mustclose = 0 |
---|
172 | if options["output"].strip() == "-" : |
---|
173 | self.outfile = sys.stdout |
---|
174 | else : |
---|
175 | self.outfile = open(options["output"], "w") |
---|
176 | mustclose = 1 |
---|
177 | |
---|
178 | retcode = getattr(self, "dump%s" % format.title())(entries, datatype) |
---|
179 | |
---|
180 | if mustclose : |
---|
181 | self.outfile.close() |
---|
182 | |
---|
183 | return retcode |
---|
184 | return 0 |
---|
185 | |
---|
186 | def dumpWithSeparator(self, separator, entries) : |
---|
187 | """Dumps datas with a separator.""" |
---|
188 | for entry in entries : |
---|
189 | line = separator.join([ '"%s"' % field for field in entry ]) |
---|
190 | try : |
---|
191 | self.outfile.write("%s\n" % line) |
---|
192 | except IOError, msg : |
---|
193 | sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg)) |
---|
194 | return -1 |
---|
195 | return 0 |
---|
196 | |
---|
197 | def dumpCsv(self, entries, dummy) : |
---|
198 | """Dumps datas with a comma as the separator.""" |
---|
199 | return self.dumpWithSeparator(",", entries) |
---|
200 | |
---|
201 | def dumpSsv(self, entries, dummy) : |
---|
202 | """Dumps datas with a comma as the separator.""" |
---|
203 | return self.dumpWithSeparator(";", entries) |
---|
204 | |
---|
205 | def dumpTsv(self, entries, dummy) : |
---|
206 | """Dumps datas with a comma as the separator.""" |
---|
207 | return self.dumpWithSeparator("\t", entries) |
---|
208 | |
---|
209 | def dumpXml(self, entries, datatype) : |
---|
210 | """Dumps datas as XML.""" |
---|
211 | x = jaxml.XML_document(encoding="UTF-8") |
---|
212 | x.pykota(version=version.__version__) |
---|
213 | x.dump(storage=self.config.getStorageBackend()["storagebackend"], type=datatype) |
---|
214 | headers = entries[0] |
---|
215 | for entry in entries[1:] : |
---|
216 | x._push() |
---|
217 | x.entry() |
---|
218 | for i in range(len(entry)) : |
---|
219 | value = entry[i] |
---|
220 | strvalue = str(value).decode(self.getCharset()).encode("UTF-8") |
---|
221 | x.attribute(strvalue, type=type(value).__name__, name=headers[i]) |
---|
222 | x._pop() |
---|
223 | x._output(self.outfile) |
---|
224 | |
---|
225 | if __name__ == "__main__" : |
---|
226 | retcode = 0 |
---|
227 | try : |
---|
228 | defaults = { \ |
---|
229 | "format" : "csv", \ |
---|
230 | "output" : "-", \ |
---|
231 | } |
---|
232 | short_options = "vhd:f:o:" |
---|
233 | long_options = ["help", "version", "data=", "format=", "output="] |
---|
234 | |
---|
235 | # Initializes the command line tool |
---|
236 | dumper = DumPyKota(doc=__doc__) |
---|
237 | |
---|
238 | # parse and checks the command line |
---|
239 | (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
240 | |
---|
241 | # sets long options |
---|
242 | options["help"] = options["h"] or options["help"] |
---|
243 | options["version"] = options["v"] or options["version"] |
---|
244 | options["data"] = options["d"] or options["data"] |
---|
245 | options["format"] = options["f"] or options["format"] or defaults["format"] |
---|
246 | options["output"] = options["o"] or options["output"] or defaults["output"] |
---|
247 | |
---|
248 | if options["help"] : |
---|
249 | dumper.display_usage_and_quit() |
---|
250 | elif options["version"] : |
---|
251 | dumper.display_version_and_quit() |
---|
252 | elif options["data"] is None : |
---|
253 | raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.") |
---|
254 | else : |
---|
255 | if args : |
---|
256 | raise PyKotaToolError, _("Too many arguments, see help.") |
---|
257 | retcode = dumper.main(args, options) |
---|
258 | except SystemExit : |
---|
259 | pass |
---|
260 | except : |
---|
261 | try : |
---|
262 | dumper.crashed("dumpykota failed") |
---|
263 | except : |
---|
264 | crashed("dumpykota failed") |
---|
265 | retcode = -1 |
---|
266 | |
---|
267 | try : |
---|
268 | dumper.storage.close() |
---|
269 | except (TypeError, NameError, AttributeError) : |
---|
270 | pass |
---|
271 | |
---|
272 | sys.exit(retcode) |
---|