#! /usr/bin/env python # -*- coding: ISO-8859-15 -*- # PyKota Print Quota Data Dumper # # PyKota - Print Quotas for CUPS and LPRng # # (c) 2003, 2004, 2005 Jerome Alet # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. # # $Id$ # # import sys import os from pykota import version from pykota.tool import PyKotaToolError, crashed, N_ from pykota.dumper import DumPyKota __doc__ = N_("""dumpykota v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres Dumps PyKota database's content. command line usage : dumpykota [options] [filterexpr] options : -v | --version Prints dumpykota's version number then exits. -h | --help Prints this message then exits. -d | --data type Dumps 'type' datas. Allowed types are : - history : dumps the jobs history. - users : dumps users. - groups : dumps user groups. - printers : dump printers. - upquotas : dump user quotas. - gpquotas : dump user groups quotas. - payments : dumps user payments. - pmembers : dumps printer groups members. - umembers : dumps user groups members. NB : the -d | --data command line option is MANDATORY. -f | --format fmt Dumps datas in the 'fmt' format. When not specified, the format is to dump datas in the csv format (comma separated values). All data dumped is between double quotes. Allowed formats are : - csv : separate datas with commas - ssv : separate datas with semicolons - tsv : separate datas with tabs - xml : dump data as XML - cups : dump datas in CUPS' page_log format : ONLY AVAILABLE WITH --data history -o | --output fname All datas will be dumped to the file instead of to the standard output. The special '-' filename is the default value and means stdout. WARNING : existing files are truncated ! Use the filter expressions to extract only parts of the datas. Allowed filters are of the form : key=value Allowed keys for now are : username User's name groupname Users group's name printername Printer's name pgroupname Printers group's name Contrary to other PyKota management tools, wildcard characters are not expanded, so you can't use them. NB : not all keys are allowed for each data type, so the result may be empty if you use a key not available for a particular data type. Examples : $ dumpykota --data history --format csv >myfile.csv This dumps the history in a comma separated values file, for possible use in a spreadsheet. $ dumpykota --data users --format xml -o users.xml Dumps all users datas to the users.xml file. $ dumpykota --data history printername=HP2100 username=jerome Dumps the job history for user jerome on printer HP2100 only. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Please e-mail bugs to: %s""") if __name__ == "__main__" : retcode = 0 try : defaults = { \ "format" : "csv", \ "output" : "-", \ } short_options = "vhd:f:o:" long_options = ["help", "version", "data=", "format=", "output="] # Initializes the command line tool dumper = DumPyKota(doc=__doc__) # parse and checks the command line (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) # sets long options options["help"] = options["h"] or options["help"] options["version"] = options["v"] or options["version"] options["data"] = options["d"] or options["data"] options["format"] = options["f"] or options["format"] or defaults["format"] options["output"] = options["o"] or options["output"] or defaults["output"] if options["help"] : dumper.display_usage_and_quit() elif options["version"] : dumper.display_version_and_quit() elif options["data"] is None : raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.") else : retcode = dumper.main(args, options) except SystemExit : pass except : try : dumper.crashed("dumpykota failed") except : crashed("dumpykota failed") retcode = -1 try : dumper.storage.close() except (TypeError, NameError, AttributeError) : pass sys.exit(retcode)