[1583] | 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$ |
---|
[2015] | 26 | # Revision 1.20 2005/01/08 11:25:48 jalet |
---|
| 27 | # Fixed the time zone field in page_log output to make PrintAnalyzer happy |
---|
| 28 | # |
---|
[2013] | 29 | # Revision 1.19 2005/01/08 10:52:01 jalet |
---|
| 30 | # Now dumpykota can dump the history into CUPS' page_log format, allowing the |
---|
| 31 | # use of tools like PrintAnalyzer or phpPrintAnalyzer along with PyKota |
---|
| 32 | # |
---|
[2010] | 33 | # Revision 1.18 2005/01/07 12:40:53 jalet |
---|
| 34 | # Fixed field type's name in XML dumps. |
---|
| 35 | # |
---|
[1991] | 36 | # Revision 1.17 2004/12/21 15:50:00 jalet |
---|
| 37 | # The dumpykota command now supports extended filtering capabilities with |
---|
| 38 | # the PostgreSQL backend. LDAP doesn't yet support such possibilities. |
---|
| 39 | # |
---|
[1990] | 40 | # Revision 1.16 2004/12/21 14:45:31 jalet |
---|
| 41 | # Prepared dumpykota to accept the new --filter command line option. Some |
---|
| 42 | # additionnal work needs to be done in the backends though. |
---|
| 43 | # |
---|
[1809] | 44 | # Revision 1.15 2004/10/12 15:37:00 jalet |
---|
| 45 | # Now outputs the name of the offending user if a mere mortal tries to use |
---|
| 46 | # one of these commands !!! |
---|
| 47 | # |
---|
[1803] | 48 | # Revision 1.14 2004/10/11 22:53:05 jalet |
---|
| 49 | # Postponed string interpolation to help message's output method |
---|
| 50 | # |
---|
[1796] | 51 | # Revision 1.13 2004/10/11 12:49:06 jalet |
---|
| 52 | # Renders help translatable |
---|
| 53 | # |
---|
[1790] | 54 | # Revision 1.12 2004/10/07 21:14:28 jalet |
---|
| 55 | # Hopefully final fix for data encoding to and from the database |
---|
| 56 | # |
---|
[1789] | 57 | # Revision 1.11 2004/10/07 14:35:40 jalet |
---|
| 58 | # Now edpykota refuses to launch if the user is not a PyKota administrator. |
---|
| 59 | # dumpykota : now has the same error message than edpykota in this case. |
---|
| 60 | # |
---|
[1785] | 61 | # Revision 1.10 2004/10/06 10:05:47 jalet |
---|
| 62 | # Minor changes to allow any PyKota administrator to launch enhanced versions |
---|
| 63 | # of the commands, and not only the root user. |
---|
| 64 | # |
---|
[1779] | 65 | # Revision 1.9 2004/10/05 20:08:46 jalet |
---|
| 66 | # Misleading help message. Thx to Johannes Laemmermann. |
---|
| 67 | # |
---|
[1777] | 68 | # Revision 1.8 2004/10/05 09:59:19 jalet |
---|
| 69 | # Restore compatibility with Python 2.1 |
---|
| 70 | # |
---|
[1771] | 71 | # Revision 1.7 2004/10/04 21:25:29 jalet |
---|
| 72 | # dumpykota can now output datas in the XML format |
---|
| 73 | # |
---|
[1724] | 74 | # Revision 1.6 2004/09/15 18:28:41 jalet |
---|
| 75 | # Updated help for dumpykota |
---|
| 76 | # |
---|
[1720] | 77 | # Revision 1.5 2004/09/15 07:38:05 jalet |
---|
| 78 | # Fix for uninitialized variable |
---|
| 79 | # |
---|
[1719] | 80 | # Revision 1.4 2004/09/15 07:26:19 jalet |
---|
| 81 | # Data dumps are now ordered by entry creation date if applicable. |
---|
| 82 | # Now dumpykota exits with a message when there's a broken pipe like |
---|
| 83 | # in dumpykota --data history | head -3 |
---|
| 84 | # |
---|
[1718] | 85 | # Revision 1.3 2004/09/15 06:58:25 jalet |
---|
| 86 | # User groups membership and printer groups membership can now be dumped too |
---|
| 87 | # |
---|
[1717] | 88 | # Revision 1.2 2004/09/14 22:29:12 jalet |
---|
| 89 | # First version of dumpykota. Works fine but only with PostgreSQL backend |
---|
| 90 | # for now. |
---|
| 91 | # |
---|
[1583] | 92 | # Revision 1.1 2004/07/01 19:22:37 jalet |
---|
| 93 | # First draft of dumpykota |
---|
| 94 | # |
---|
| 95 | # |
---|
| 96 | # |
---|
| 97 | |
---|
| 98 | import sys |
---|
| 99 | import os |
---|
| 100 | import pwd |
---|
[2013] | 101 | from mx import DateTime |
---|
[1583] | 102 | |
---|
[1771] | 103 | try : |
---|
| 104 | import jaxml |
---|
| 105 | except ImportError : |
---|
| 106 | sys.stderr.write("The jaxml Python module is not installed. XML output is disabled.\n") |
---|
| 107 | sys.stderr.write("Download jaxml from http://www.librelogiciel.com/software/ or from your Debian archive of choice\n") |
---|
| 108 | hasJAXML = 0 |
---|
| 109 | else : |
---|
| 110 | hasJAXML = 1 |
---|
| 111 | |
---|
[1583] | 112 | from pykota import version |
---|
[1796] | 113 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[1583] | 114 | from pykota.config import PyKotaConfigError |
---|
| 115 | from pykota.storage import PyKotaStorageError |
---|
| 116 | |
---|
[1796] | 117 | __doc__ = N_("""dumpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
[1583] | 118 | |
---|
| 119 | Dumps PyKota database's content. |
---|
| 120 | |
---|
| 121 | command line usage : |
---|
| 122 | |
---|
[1991] | 123 | dumpykota [options] [filterexpr] |
---|
[1583] | 124 | |
---|
| 125 | options : |
---|
| 126 | |
---|
[1779] | 127 | -v | --version Prints dumpykota's version number then exits. |
---|
[1583] | 128 | -h | --help Prints this message then exits. |
---|
| 129 | |
---|
[1717] | 130 | -d | --data type Dumps 'type' datas. Allowed types are : |
---|
[1583] | 131 | |
---|
| 132 | - history : dumps the jobs history. |
---|
| 133 | - users : dumps users. |
---|
| 134 | - groups : dumps user groups. |
---|
| 135 | - printers : dump printers. |
---|
[1724] | 136 | - upquotas : dump user quotas. |
---|
| 137 | - gpquotas : dump user groups quotas. |
---|
[1717] | 138 | - payments : dumps user payments. |
---|
[1718] | 139 | - pmembers : dumps printer groups members. |
---|
| 140 | - umembers : dumps user groups members. |
---|
[1717] | 141 | |
---|
| 142 | NB : the -d | --data command line option |
---|
| 143 | is MANDATORY. |
---|
[1583] | 144 | |
---|
| 145 | -f | --format fmt Dumps datas in the 'fmt' format. When not specified, |
---|
| 146 | the format is to dump datas in the csv format (comma |
---|
| 147 | separated values). All data dumped is between double |
---|
| 148 | quotes. Allowed formats are : |
---|
| 149 | |
---|
| 150 | - csv : separate datas with commas |
---|
| 151 | - ssv : separate datas with semicolons |
---|
| 152 | - tsv : separate datas with tabs |
---|
[1771] | 153 | - xml : dump data as XML |
---|
[2013] | 154 | - cups : dump datas in CUPS' page_log format : |
---|
| 155 | ONLY AVAILABLE WITH --data history |
---|
[1771] | 156 | |
---|
| 157 | -o | --output fname All datas will be dumped to the file instead of |
---|
| 158 | to the standard output. The special '-' filename |
---|
| 159 | is the default value and means stdout. |
---|
| 160 | WARNING : existing files are truncated ! |
---|
[1991] | 161 | |
---|
| 162 | Use the filter expressions to extract only parts of the |
---|
| 163 | datas. Allowed filters are of the form : |
---|
| 164 | |
---|
| 165 | key=value |
---|
| 166 | |
---|
| 167 | Allowed keys for now are : |
---|
| 168 | |
---|
| 169 | username User's name |
---|
| 170 | groupname Users group's name |
---|
| 171 | printername Printer's name |
---|
| 172 | pgroupname Printers group's name |
---|
| 173 | |
---|
| 174 | Contrary to other PyKota management tools, wildcard characters are not |
---|
| 175 | expanded, so you can't use them. |
---|
[1583] | 176 | |
---|
[1991] | 177 | NB : not all keys are allowed for each data type, so the result may be |
---|
| 178 | empty if you use a key not available for a particular data type. |
---|
| 179 | |
---|
[1771] | 180 | Examples : |
---|
| 181 | |
---|
| 182 | $ dumpykota --data history --format csv >myfile.csv |
---|
| 183 | |
---|
| 184 | This dumps the history in a comma separated values file, for possible |
---|
| 185 | use in a spreadsheet. |
---|
| 186 | |
---|
| 187 | $ dumpykota --data users --format xml -o users.xml |
---|
| 188 | |
---|
| 189 | Dumps all users datas to the users.xml file. |
---|
| 190 | |
---|
[1991] | 191 | $ dumpykota --data history printername=HP2100 username=jerome |
---|
[1990] | 192 | |
---|
[1991] | 193 | Dumps the job history for user jerome on printer HP2100 only. |
---|
[1990] | 194 | |
---|
[1583] | 195 | This program is free software; you can redistribute it and/or modify |
---|
| 196 | it under the terms of the GNU General Public License as published by |
---|
| 197 | the Free Software Foundation; either version 2 of the License, or |
---|
| 198 | (at your option) any later version. |
---|
| 199 | |
---|
| 200 | This program is distributed in the hope that it will be useful, |
---|
| 201 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 202 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 203 | GNU General Public License for more details. |
---|
| 204 | |
---|
| 205 | You should have received a copy of the GNU General Public License |
---|
| 206 | along with this program; if not, write to the Free Software |
---|
| 207 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 208 | |
---|
[1803] | 209 | Please e-mail bugs to: %s""") |
---|
[1583] | 210 | |
---|
| 211 | class DumPyKota(PyKotaTool) : |
---|
| 212 | """A class for dumpykota.""" |
---|
[1717] | 213 | def main(self, arguments, options) : |
---|
[1583] | 214 | """Print Quota Data Dumper.""" |
---|
[1785] | 215 | if not self.config.isAdmin : |
---|
[1809] | 216 | raise PyKotaToolError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
[1785] | 217 | |
---|
[1990] | 218 | extractonly = {} |
---|
[1991] | 219 | for filterexp in arguments : |
---|
| 220 | if filterexp.strip() : |
---|
| 221 | try : |
---|
| 222 | (filterkey, filtervalue) = [part.strip() for part in filterexp.split("=")] |
---|
| 223 | if filterkey not in [ "username", |
---|
| 224 | "groupname", |
---|
| 225 | "printername", |
---|
| 226 | "pgroupname", |
---|
| 227 | ] : |
---|
| 228 | raise ValueError |
---|
| 229 | except ValueError : |
---|
| 230 | raise PyKotaToolError, _("Invalid value [%s] for --filter command line option, see help.") % filterexp |
---|
| 231 | else : |
---|
| 232 | extractonly.update({ filterkey : filtervalue }) |
---|
[1990] | 233 | |
---|
[1717] | 234 | datatype = options["data"] |
---|
| 235 | if datatype not in [ "history", |
---|
| 236 | "users", |
---|
| 237 | "groups", |
---|
| 238 | "printers", |
---|
| 239 | "upquotas", |
---|
| 240 | "gpquotas", |
---|
| 241 | "payments", |
---|
[1718] | 242 | "pmembers", |
---|
| 243 | "umembers", |
---|
[1717] | 244 | ] : |
---|
| 245 | raise PyKotaToolError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype |
---|
[1583] | 246 | |
---|
[1717] | 247 | format = options["format"] |
---|
[2013] | 248 | if (format not in [ "csv", |
---|
[1717] | 249 | "ssv", |
---|
| 250 | "tsv", |
---|
[1771] | 251 | "xml", |
---|
[2013] | 252 | "cups", |
---|
| 253 | ]) \ |
---|
| 254 | or ((format == "cups") and (datatype != "history")) : |
---|
| 255 | raise PyKotaToolError, _("Invalid modifier [%s] for --format command line option, see help.") % format |
---|
[1717] | 256 | |
---|
[1771] | 257 | if (format == "xml") and not hasJAXML : |
---|
| 258 | raise PyKotaToolError, _("XML output is disabled because the jaxml module is not available.") |
---|
| 259 | |
---|
[1990] | 260 | entries = getattr(self.storage, "extract%s" % datatype.title())(extractonly) |
---|
[1771] | 261 | if entries : |
---|
| 262 | mustclose = 0 |
---|
| 263 | if options["output"].strip() == "-" : |
---|
| 264 | self.outfile = sys.stdout |
---|
| 265 | else : |
---|
| 266 | self.outfile = open(options["output"], "w") |
---|
| 267 | mustclose = 1 |
---|
| 268 | |
---|
| 269 | retcode = getattr(self, "dump%s" % format.title())(entries, datatype) |
---|
| 270 | |
---|
| 271 | if mustclose : |
---|
| 272 | self.outfile.close() |
---|
| 273 | |
---|
| 274 | return retcode |
---|
[1717] | 275 | return 0 |
---|
| 276 | |
---|
| 277 | def dumpWithSeparator(self, separator, entries) : |
---|
| 278 | """Dumps datas with a separator.""" |
---|
| 279 | for entry in entries : |
---|
| 280 | line = separator.join([ '"%s"' % field for field in entry ]) |
---|
[1719] | 281 | try : |
---|
[1771] | 282 | self.outfile.write("%s\n" % line) |
---|
[1719] | 283 | except IOError, msg : |
---|
| 284 | sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg)) |
---|
| 285 | return -1 |
---|
| 286 | return 0 |
---|
[1717] | 287 | |
---|
[1771] | 288 | def dumpCsv(self, entries, dummy) : |
---|
[1717] | 289 | """Dumps datas with a comma as the separator.""" |
---|
[1719] | 290 | return self.dumpWithSeparator(",", entries) |
---|
[1717] | 291 | |
---|
[1771] | 292 | def dumpSsv(self, entries, dummy) : |
---|
[1717] | 293 | """Dumps datas with a comma as the separator.""" |
---|
[1719] | 294 | return self.dumpWithSeparator(";", entries) |
---|
[1717] | 295 | |
---|
[1771] | 296 | def dumpTsv(self, entries, dummy) : |
---|
[1717] | 297 | """Dumps datas with a comma as the separator.""" |
---|
[1719] | 298 | return self.dumpWithSeparator("\t", entries) |
---|
[1771] | 299 | |
---|
[2013] | 300 | def dumpCups(self, entries, dummy) : |
---|
| 301 | """Dumps history datas as CUPS' page_log format.""" |
---|
| 302 | fieldnames = entries[0] |
---|
| 303 | fields = {} |
---|
| 304 | for i in range(len(fieldnames)) : |
---|
| 305 | fields[fieldnames[i]] = i |
---|
| 306 | sortindex = fields["jobdate"] |
---|
| 307 | entries = entries[1:] |
---|
| 308 | entries.sort(lambda m,n : cmp(m[sortindex], n[sortindex])) |
---|
| 309 | for entry in entries[1:] : |
---|
| 310 | printername = entry[fields["printername"]] |
---|
| 311 | username = entry[fields["username"]] |
---|
| 312 | jobid = entry[fields["jobid"]] |
---|
[2015] | 313 | jobdate = DateTime.ISO.ParseDateTime(entry[fields["jobdate"]]) |
---|
| 314 | gmtoffset = jobdate.gmtoffset() |
---|
| 315 | jobdate = "%s %+03i00" % (jobdate.strftime("%d/%b/%Y:%H:%M:%S"), gmtoffset.hour) |
---|
[2013] | 316 | jobsize = entry[fields["jobsize"]] |
---|
| 317 | copies = entry[fields["copies"]] |
---|
| 318 | hostname = entry[fields["hostname"]] |
---|
| 319 | self.outfile.write("%s %s %s [%s] %s %s - %s\n" % (printername, username, jobid, jobdate, jobsize, copies, hostname)) |
---|
| 320 | |
---|
[1771] | 321 | def dumpXml(self, entries, datatype) : |
---|
| 322 | """Dumps datas as XML.""" |
---|
| 323 | x = jaxml.XML_document(encoding="UTF-8") |
---|
[1803] | 324 | x.pykota(version=version.__version__, author=version.__author__) |
---|
[1771] | 325 | x.dump(storage=self.config.getStorageBackend()["storagebackend"], type=datatype) |
---|
| 326 | headers = entries[0] |
---|
| 327 | for entry in entries[1:] : |
---|
| 328 | x._push() |
---|
| 329 | x.entry() |
---|
| 330 | for i in range(len(entry)) : |
---|
[1790] | 331 | value = str(entry[i]) |
---|
[2010] | 332 | typval = type(entry[i]).__name__ |
---|
[1790] | 333 | try : |
---|
| 334 | value = unicode(value, self.getCharset()).encode("UTF-8") |
---|
| 335 | except UnicodeError : |
---|
| 336 | pass |
---|
[2010] | 337 | x.attribute(value, type=typval, name=headers[i]) |
---|
[1771] | 338 | x._pop() |
---|
| 339 | x._output(self.outfile) |
---|
[1717] | 340 | |
---|
[1583] | 341 | if __name__ == "__main__" : |
---|
[1720] | 342 | retcode = 0 |
---|
[1583] | 343 | try : |
---|
| 344 | defaults = { \ |
---|
[1717] | 345 | "format" : "csv", \ |
---|
[1771] | 346 | "output" : "-", \ |
---|
[1583] | 347 | } |
---|
[1991] | 348 | short_options = "vhd:f:o:" |
---|
| 349 | long_options = ["help", "version", "data=", "format=", "output="] |
---|
[1583] | 350 | |
---|
| 351 | # Initializes the command line tool |
---|
| 352 | dumper = DumPyKota(doc=__doc__) |
---|
| 353 | |
---|
| 354 | # parse and checks the command line |
---|
| 355 | (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 356 | |
---|
| 357 | # sets long options |
---|
| 358 | options["help"] = options["h"] or options["help"] |
---|
| 359 | options["version"] = options["v"] or options["version"] |
---|
[1717] | 360 | options["data"] = options["d"] or options["data"] |
---|
| 361 | options["format"] = options["f"] or options["format"] or defaults["format"] |
---|
[1771] | 362 | options["output"] = options["o"] or options["output"] or defaults["output"] |
---|
[1583] | 363 | |
---|
| 364 | if options["help"] : |
---|
| 365 | dumper.display_usage_and_quit() |
---|
| 366 | elif options["version"] : |
---|
| 367 | dumper.display_version_and_quit() |
---|
[1717] | 368 | elif options["data"] is None : |
---|
| 369 | raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.") |
---|
[1583] | 370 | else : |
---|
| 371 | retcode = dumper.main(args, options) |
---|
| 372 | except SystemExit : |
---|
| 373 | pass |
---|
| 374 | except : |
---|
| 375 | try : |
---|
[1717] | 376 | dumper.crashed("dumpykota failed") |
---|
[1583] | 377 | except : |
---|
[1720] | 378 | crashed("dumpykota failed") |
---|
[1583] | 379 | retcode = -1 |
---|
| 380 | |
---|
| 381 | try : |
---|
| 382 | dumper.storage.close() |
---|
| 383 | except (TypeError, NameError, AttributeError) : |
---|
| 384 | pass |
---|
| 385 | |
---|
| 386 | sys.exit(retcode) |
---|