[1583] | 1 | #! /usr/bin/env python |
---|
[3260] | 2 | # -*- coding: UTF-8 -*- |
---|
[1583] | 3 | # |
---|
[3260] | 4 | # PyKota : Print Quotas for CUPS |
---|
[1583] | 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 7 | # This program is free software: you can redistribute it and/or modify |
---|
[1583] | 8 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
[1583] | 10 | # (at your option) any later version. |
---|
[3260] | 11 | # |
---|
[1583] | 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[1583] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
[2028] | 22 | # |
---|
[1583] | 23 | |
---|
| 24 | import sys |
---|
| 25 | |
---|
[2829] | 26 | from pykota.tool import PyKotaCommandLineError, crashed, N_ |
---|
[2016] | 27 | from pykota.dumper import DumPyKota |
---|
[1583] | 28 | |
---|
[2344] | 29 | __doc__ = N_("""dumpykota v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[1583] | 30 | |
---|
| 31 | Dumps PyKota database's content. |
---|
| 32 | |
---|
| 33 | command line usage : |
---|
| 34 | |
---|
[1991] | 35 | dumpykota [options] [filterexpr] |
---|
[1583] | 36 | |
---|
| 37 | options : |
---|
| 38 | |
---|
[1779] | 39 | -v | --version Prints dumpykota's version number then exits. |
---|
[1583] | 40 | -h | --help Prints this message then exits. |
---|
| 41 | |
---|
[1717] | 42 | -d | --data type Dumps 'type' datas. Allowed types are : |
---|
[1583] | 43 | |
---|
| 44 | - history : dumps the jobs history. |
---|
| 45 | - users : dumps users. |
---|
| 46 | - groups : dumps user groups. |
---|
| 47 | - printers : dump printers. |
---|
[1724] | 48 | - upquotas : dump user quotas. |
---|
| 49 | - gpquotas : dump user groups quotas. |
---|
[1717] | 50 | - payments : dumps user payments. |
---|
[1718] | 51 | - pmembers : dumps printer groups members. |
---|
| 52 | - umembers : dumps user groups members. |
---|
[2342] | 53 | - billingcodes : dumps billing codes. |
---|
[2600] | 54 | - all : dumps all PyKota datas. The output format |
---|
| 55 | is always XML in this case. |
---|
[1717] | 56 | |
---|
| 57 | NB : the -d | --data command line option |
---|
| 58 | is MANDATORY. |
---|
[1583] | 59 | |
---|
| 60 | -f | --format fmt Dumps datas in the 'fmt' format. When not specified, |
---|
| 61 | the format is to dump datas in the csv format (comma |
---|
| 62 | separated values). All data dumped is between double |
---|
| 63 | quotes. Allowed formats are : |
---|
| 64 | |
---|
| 65 | - csv : separate datas with commas |
---|
| 66 | - ssv : separate datas with semicolons |
---|
| 67 | - tsv : separate datas with tabs |
---|
[2600] | 68 | - xml : dump data as XML |
---|
[2013] | 69 | - cups : dump datas in CUPS' page_log format : |
---|
| 70 | ONLY AVAILABLE WITH --data history |
---|
[1771] | 71 | |
---|
| 72 | -o | --output fname All datas will be dumped to the file instead of |
---|
| 73 | to the standard output. The special '-' filename |
---|
| 74 | is the default value and means stdout. |
---|
| 75 | WARNING : existing files are truncated ! |
---|
[2282] | 76 | |
---|
[3165] | 77 | -O | --orderby exp Change the ordering or result. 'exp' is a comma |
---|
| 78 | separated list of ordering statements, for example |
---|
| 79 | '--orderby +username,-printername'. Not all expression |
---|
| 80 | values are meaningful, so using this command line |
---|
| 81 | switch is not recommanded if you don't know the |
---|
| 82 | exact layout of PyKota's database schema. |
---|
| 83 | |
---|
[2282] | 84 | -s | --sum Summarize the selected datas. |
---|
| 85 | ONLY AVAILABLE WITH --data history or payments |
---|
| 86 | |
---|
[1991] | 87 | Use the filter expressions to extract only parts of the |
---|
| 88 | datas. Allowed filters are of the form : |
---|
| 89 | |
---|
| 90 | key=value |
---|
| 91 | |
---|
| 92 | Allowed keys for now are : |
---|
| 93 | |
---|
| 94 | username User's name |
---|
| 95 | groupname Users group's name |
---|
| 96 | printername Printer's name |
---|
| 97 | pgroupname Printers group's name |
---|
[2218] | 98 | hostname Client's hostname |
---|
[3093] | 99 | jobid Job's Id |
---|
[2218] | 100 | billingcode Job's billing code |
---|
[2266] | 101 | start Job's date of printing |
---|
| 102 | end Job's date of printing |
---|
[1991] | 103 | |
---|
[3094] | 104 | Dates formatting with 'start' and 'end' filter keys : |
---|
| 105 | |
---|
| 106 | YYYY : year boundaries |
---|
| 107 | YYYYMM : month boundaries |
---|
| 108 | YYYYMMDD : day boundaries |
---|
| 109 | YYYYMMDDhh : hour boundaries |
---|
| 110 | YYYYMMDDhhmm : minute boundaries |
---|
| 111 | YYYYMMDDhhmmss : second boundaries |
---|
| 112 | yesterday[+-NbDays] : yesterday more or less N days (e.g. : yesterday-15) |
---|
| 113 | today[+-NbDays] : today more or less N days (e.g. : today-15) |
---|
| 114 | tomorrow[+-NbDays] : tomorrow more or less N days (e.g. : tomorrow-15) |
---|
| 115 | now[+-NbDays] : now more or less N days (e.g. now-15) |
---|
| 116 | |
---|
| 117 | 'now' and 'today' are not exactly the same since today represents the first |
---|
| 118 | or last second of the day depending on if it's used in a start= or end= |
---|
| 119 | date expression. The utility to be able to specify dates in the future is |
---|
| 120 | a question which remains to be answered :-) |
---|
| 121 | |
---|
[1991] | 122 | Contrary to other PyKota management tools, wildcard characters are not |
---|
| 123 | expanded, so you can't use them. |
---|
[1583] | 124 | |
---|
[1991] | 125 | NB : not all keys are allowed for each data type, so the result may be |
---|
| 126 | empty if you use a key not available for a particular data type. |
---|
| 127 | |
---|
[1771] | 128 | Examples : |
---|
| 129 | |
---|
| 130 | $ dumpykota --data history --format csv >myfile.csv |
---|
| 131 | |
---|
| 132 | This dumps the history in a comma separated values file, for possible |
---|
| 133 | use in a spreadsheet. |
---|
| 134 | |
---|
| 135 | $ dumpykota --data users --format xml -o users.xml |
---|
| 136 | |
---|
| 137 | Dumps all users datas to the users.xml file. |
---|
| 138 | |
---|
[1991] | 139 | $ dumpykota --data history printername=HP2100 username=jerome |
---|
[1990] | 140 | |
---|
[1991] | 141 | Dumps the job history for user jerome on printer HP2100 only. |
---|
[1990] | 142 | |
---|
[2266] | 143 | $ dumpykota --data history start=200503 end=20050730234615 |
---|
| 144 | |
---|
| 145 | Dumps all jobs printed between March 1st 2005 at midnight and |
---|
| 146 | July 30th 2005 at 23 hours 46 minutes and 15 secondes included. |
---|
[2344] | 147 | """) |
---|
[1583] | 148 | |
---|
| 149 | if __name__ == "__main__" : |
---|
[1720] | 150 | retcode = 0 |
---|
[1583] | 151 | try : |
---|
| 152 | defaults = { \ |
---|
[1717] | 153 | "format" : "csv", \ |
---|
[1771] | 154 | "output" : "-", \ |
---|
[1583] | 155 | } |
---|
[3165] | 156 | short_options = "vhd:f:o:sO:" |
---|
| 157 | long_options = ["help", "version", "data=", "format=", "output=", "sum", "orderby="] |
---|
[1583] | 158 | |
---|
| 159 | # Initializes the command line tool |
---|
| 160 | dumper = DumPyKota(doc=__doc__) |
---|
[2210] | 161 | dumper.deferredInit() |
---|
[1583] | 162 | |
---|
| 163 | # parse and checks the command line |
---|
| 164 | (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
| 165 | |
---|
| 166 | # sets long options |
---|
| 167 | options["help"] = options["h"] or options["help"] |
---|
| 168 | options["version"] = options["v"] or options["version"] |
---|
[1717] | 169 | options["data"] = options["d"] or options["data"] |
---|
| 170 | options["format"] = options["f"] or options["format"] or defaults["format"] |
---|
[1771] | 171 | options["output"] = options["o"] or options["output"] or defaults["output"] |
---|
[2282] | 172 | options["sum"] = options["s"] or options["sum"] |
---|
[3165] | 173 | options["orderby"] = options["O"] or options["orderby"] |
---|
[1583] | 174 | |
---|
| 175 | if options["help"] : |
---|
| 176 | dumper.display_usage_and_quit() |
---|
| 177 | elif options["version"] : |
---|
| 178 | dumper.display_version_and_quit() |
---|
[1717] | 179 | elif options["data"] is None : |
---|
[2512] | 180 | raise PyKotaCommandLineError, _("The -d | --data command line option is mandatory, see help.") |
---|
[1583] | 181 | else : |
---|
| 182 | retcode = dumper.main(args, options) |
---|
[2216] | 183 | except KeyboardInterrupt : |
---|
| 184 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 185 | retcode = -3 |
---|
[2512] | 186 | except PyKotaCommandLineError, msg : |
---|
| 187 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 188 | retcode = -2 |
---|
[1583] | 189 | except SystemExit : |
---|
| 190 | pass |
---|
| 191 | except : |
---|
| 192 | try : |
---|
[1717] | 193 | dumper.crashed("dumpykota failed") |
---|
[1583] | 194 | except : |
---|
[1720] | 195 | crashed("dumpykota failed") |
---|
[1583] | 196 | retcode = -1 |
---|
| 197 | |
---|
| 198 | try : |
---|
| 199 | dumper.storage.close() |
---|
| 200 | except (TypeError, NameError, AttributeError) : |
---|
| 201 | pass |
---|
| 202 | |
---|
| 203 | sys.exit(retcode) |
---|