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