[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 | # |
---|
[2028] | 8 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
[1583] | 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 |
---|
[2303] | 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1583] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
[2028] | 25 | # |
---|
[1583] | 26 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | |
---|
| 30 | from pykota import version |
---|
[2512] | 31 | from pykota.tool import PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
[2016] | 32 | from pykota.dumper import DumPyKota |
---|
[1583] | 33 | |
---|
[2344] | 34 | __doc__ = N_("""dumpykota v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[1583] | 35 | |
---|
| 36 | Dumps PyKota database's content. |
---|
| 37 | |
---|
| 38 | command line usage : |
---|
| 39 | |
---|
[1991] | 40 | dumpykota [options] [filterexpr] |
---|
[1583] | 41 | |
---|
| 42 | options : |
---|
| 43 | |
---|
[1779] | 44 | -v | --version Prints dumpykota's version number then exits. |
---|
[1583] | 45 | -h | --help Prints this message then exits. |
---|
| 46 | |
---|
[1717] | 47 | -d | --data type Dumps 'type' datas. Allowed types are : |
---|
[1583] | 48 | |
---|
| 49 | - history : dumps the jobs history. |
---|
| 50 | - users : dumps users. |
---|
| 51 | - groups : dumps user groups. |
---|
| 52 | - printers : dump printers. |
---|
[1724] | 53 | - upquotas : dump user quotas. |
---|
| 54 | - gpquotas : dump user groups quotas. |
---|
[1717] | 55 | - payments : dumps user payments. |
---|
[1718] | 56 | - pmembers : dumps printer groups members. |
---|
| 57 | - umembers : dumps user groups members. |
---|
[2342] | 58 | - billingcodes : dumps billing codes. |
---|
[2600] | 59 | - all : dumps all PyKota datas. The output format |
---|
| 60 | is always XML in this case. |
---|
[1717] | 61 | |
---|
| 62 | NB : the -d | --data command line option |
---|
| 63 | is MANDATORY. |
---|
[1583] | 64 | |
---|
| 65 | -f | --format fmt Dumps datas in the 'fmt' format. When not specified, |
---|
| 66 | the format is to dump datas in the csv format (comma |
---|
| 67 | separated values). All data dumped is between double |
---|
| 68 | quotes. Allowed formats are : |
---|
| 69 | |
---|
| 70 | - csv : separate datas with commas |
---|
| 71 | - ssv : separate datas with semicolons |
---|
| 72 | - tsv : separate datas with tabs |
---|
[2600] | 73 | - xml : dump data as XML |
---|
[2013] | 74 | - cups : dump datas in CUPS' page_log format : |
---|
| 75 | ONLY AVAILABLE WITH --data history |
---|
[1771] | 76 | |
---|
| 77 | -o | --output fname All datas will be dumped to the file instead of |
---|
| 78 | to the standard output. The special '-' filename |
---|
| 79 | is the default value and means stdout. |
---|
| 80 | WARNING : existing files are truncated ! |
---|
[2282] | 81 | |
---|
| 82 | -s | --sum Summarize the selected datas. |
---|
| 83 | ONLY AVAILABLE WITH --data history or payments |
---|
| 84 | |
---|
[1991] | 85 | Use the filter expressions to extract only parts of the |
---|
| 86 | datas. Allowed filters are of the form : |
---|
| 87 | |
---|
| 88 | key=value |
---|
| 89 | |
---|
| 90 | Allowed keys for now are : |
---|
| 91 | |
---|
| 92 | username User's name |
---|
| 93 | groupname Users group's name |
---|
| 94 | printername Printer's name |
---|
| 95 | pgroupname Printers group's name |
---|
[2218] | 96 | hostname Client's hostname |
---|
| 97 | billingcode Job's billing code |
---|
[2266] | 98 | start Job's date of printing |
---|
| 99 | end Job's date of printing |
---|
[1991] | 100 | |
---|
| 101 | Contrary to other PyKota management tools, wildcard characters are not |
---|
| 102 | expanded, so you can't use them. |
---|
[1583] | 103 | |
---|
[1991] | 104 | NB : not all keys are allowed for each data type, so the result may be |
---|
| 105 | empty if you use a key not available for a particular data type. |
---|
| 106 | |
---|
[1771] | 107 | Examples : |
---|
| 108 | |
---|
| 109 | $ dumpykota --data history --format csv >myfile.csv |
---|
| 110 | |
---|
| 111 | This dumps the history in a comma separated values file, for possible |
---|
| 112 | use in a spreadsheet. |
---|
| 113 | |
---|
| 114 | $ dumpykota --data users --format xml -o users.xml |
---|
| 115 | |
---|
| 116 | Dumps all users datas to the users.xml file. |
---|
| 117 | |
---|
[1991] | 118 | $ dumpykota --data history printername=HP2100 username=jerome |
---|
[1990] | 119 | |
---|
[1991] | 120 | Dumps the job history for user jerome on printer HP2100 only. |
---|
[1990] | 121 | |
---|
[2266] | 122 | $ dumpykota --data history start=200503 end=20050730234615 |
---|
| 123 | |
---|
| 124 | Dumps all jobs printed between March 1st 2005 at midnight and |
---|
| 125 | July 30th 2005 at 23 hours 46 minutes and 15 secondes included. |
---|
[2344] | 126 | """) |
---|
[1583] | 127 | |
---|
| 128 | if __name__ == "__main__" : |
---|
[1720] | 129 | retcode = 0 |
---|
[1583] | 130 | try : |
---|
| 131 | defaults = { \ |
---|
[1717] | 132 | "format" : "csv", \ |
---|
[1771] | 133 | "output" : "-", \ |
---|
[1583] | 134 | } |
---|
[2282] | 135 | short_options = "vhd:f:o:s" |
---|
| 136 | long_options = ["help", "version", "data=", "format=", "output=", "sum"] |
---|
[1583] | 137 | |
---|
| 138 | # Initializes the command line tool |
---|
| 139 | dumper = DumPyKota(doc=__doc__) |
---|
[2210] | 140 | dumper.deferredInit() |
---|
[1583] | 141 | |
---|
| 142 | # parse and checks the command line |
---|
| 143 | (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 144 | |
---|
| 145 | # sets long options |
---|
| 146 | options["help"] = options["h"] or options["help"] |
---|
| 147 | options["version"] = options["v"] or options["version"] |
---|
[1717] | 148 | options["data"] = options["d"] or options["data"] |
---|
| 149 | options["format"] = options["f"] or options["format"] or defaults["format"] |
---|
[1771] | 150 | options["output"] = options["o"] or options["output"] or defaults["output"] |
---|
[2282] | 151 | options["sum"] = options["s"] or options["sum"] |
---|
[1583] | 152 | |
---|
| 153 | if options["help"] : |
---|
| 154 | dumper.display_usage_and_quit() |
---|
| 155 | elif options["version"] : |
---|
| 156 | dumper.display_version_and_quit() |
---|
[1717] | 157 | elif options["data"] is None : |
---|
[2512] | 158 | raise PyKotaCommandLineError, _("The -d | --data command line option is mandatory, see help.") |
---|
[1583] | 159 | else : |
---|
| 160 | retcode = dumper.main(args, options) |
---|
[2216] | 161 | except KeyboardInterrupt : |
---|
| 162 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 163 | retcode = -3 |
---|
[2512] | 164 | except PyKotaCommandLineError, msg : |
---|
| 165 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 166 | retcode = -2 |
---|
[1583] | 167 | except SystemExit : |
---|
| 168 | pass |
---|
| 169 | except : |
---|
| 170 | try : |
---|
[1717] | 171 | dumper.crashed("dumpykota failed") |
---|
[1583] | 172 | except : |
---|
[1720] | 173 | crashed("dumpykota failed") |
---|
[1583] | 174 | retcode = -1 |
---|
| 175 | |
---|
| 176 | try : |
---|
| 177 | dumper.storage.close() |
---|
| 178 | except (TypeError, NameError, AttributeError) : |
---|
| 179 | pass |
---|
| 180 | |
---|
| 181 | sys.exit(retcode) |
---|