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$ |
---|
26 | # Revision 1.3 2004/09/15 06:58:25 jalet |
---|
27 | # User groups membership and printer groups membership can now be dumped too |
---|
28 | # |
---|
29 | # Revision 1.2 2004/09/14 22:29:12 jalet |
---|
30 | # First version of dumpykota. Works fine but only with PostgreSQL backend |
---|
31 | # for now. |
---|
32 | # |
---|
33 | # Revision 1.1 2004/07/01 19:22:37 jalet |
---|
34 | # First draft of dumpykota |
---|
35 | # |
---|
36 | # |
---|
37 | # |
---|
38 | |
---|
39 | import sys |
---|
40 | import os |
---|
41 | import pwd |
---|
42 | |
---|
43 | from pykota import version |
---|
44 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
45 | from pykota.config import PyKotaConfigError |
---|
46 | from pykota.storage import PyKotaStorageError |
---|
47 | |
---|
48 | __doc__ = """dumpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
49 | |
---|
50 | Dumps PyKota database's content. |
---|
51 | |
---|
52 | command line usage : |
---|
53 | |
---|
54 | dumpykota [options] |
---|
55 | |
---|
56 | options : |
---|
57 | |
---|
58 | -v | --version Prints repykota's version number then exits. |
---|
59 | -h | --help Prints this message then exits. |
---|
60 | |
---|
61 | -d | --data type Dumps 'type' datas. Allowed types are : |
---|
62 | |
---|
63 | - history : dumps the jobs history. |
---|
64 | - users : dumps users. |
---|
65 | - groups : dumps user groups. |
---|
66 | - printers : dump printers. |
---|
67 | - uquotas : dump user quotas. |
---|
68 | - gquotas : dump user groups quotas. |
---|
69 | - payments : dumps user payments. |
---|
70 | - pmembers : dumps printer groups members. |
---|
71 | - umembers : dumps user groups members. |
---|
72 | |
---|
73 | NB : the -d | --data command line option |
---|
74 | is MANDATORY. |
---|
75 | |
---|
76 | -f | --format fmt Dumps datas in the 'fmt' format. When not specified, |
---|
77 | the format is to dump datas in the csv format (comma |
---|
78 | separated values). All data dumped is between double |
---|
79 | quotes. Allowed formats are : |
---|
80 | |
---|
81 | - csv : separate datas with commas |
---|
82 | - ssv : separate datas with semicolons |
---|
83 | - tsv : separate datas with tabs |
---|
84 | |
---|
85 | This program is free software; you can redistribute it and/or modify |
---|
86 | it under the terms of the GNU General Public License as published by |
---|
87 | the Free Software Foundation; either version 2 of the License, or |
---|
88 | (at your option) any later version. |
---|
89 | |
---|
90 | This program is distributed in the hope that it will be useful, |
---|
91 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
92 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
93 | GNU General Public License for more details. |
---|
94 | |
---|
95 | You should have received a copy of the GNU General Public License |
---|
96 | along with this program; if not, write to the Free Software |
---|
97 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
98 | |
---|
99 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
100 | |
---|
101 | class DumPyKota(PyKotaTool) : |
---|
102 | """A class for dumpykota.""" |
---|
103 | def main(self, arguments, options) : |
---|
104 | """Print Quota Data Dumper.""" |
---|
105 | datatype = options["data"] |
---|
106 | if datatype not in [ "history", |
---|
107 | "users", |
---|
108 | "groups", |
---|
109 | "printers", |
---|
110 | "upquotas", |
---|
111 | "gpquotas", |
---|
112 | "payments", |
---|
113 | "pmembers", |
---|
114 | "umembers", |
---|
115 | ] : |
---|
116 | raise PyKotaToolError, _("Invalid modifier [%s] for --data command line option, see help.") % datatype |
---|
117 | |
---|
118 | format = options["format"] |
---|
119 | if format not in [ "csv", |
---|
120 | "ssv", |
---|
121 | "tsv", |
---|
122 | ] : |
---|
123 | raise PyKotaToolError, _("Invalid modifier [%s] for --format command line option, see help.") % datatype |
---|
124 | |
---|
125 | entries = getattr(self.storage, "extract%s" % datatype.title())() |
---|
126 | if entries is not None : |
---|
127 | getattr(self, "dump%s" % format.title())(entries) |
---|
128 | return 0 |
---|
129 | |
---|
130 | def dumpWithSeparator(self, separator, entries) : |
---|
131 | """Dumps datas with a separator.""" |
---|
132 | for entry in entries : |
---|
133 | line = separator.join([ '"%s"' % field for field in entry ]) |
---|
134 | print line |
---|
135 | |
---|
136 | def dumpCsv(self, entries) : |
---|
137 | """Dumps datas with a comma as the separator.""" |
---|
138 | self.dumpWithSeparator(",", entries) |
---|
139 | |
---|
140 | def dumpSsv(self, entries) : |
---|
141 | """Dumps datas with a comma as the separator.""" |
---|
142 | self.dumpWithSeparator(";", entries) |
---|
143 | |
---|
144 | def dumpTsv(self, entries) : |
---|
145 | """Dumps datas with a comma as the separator.""" |
---|
146 | self.dumpWithSeparator("\t", entries) |
---|
147 | |
---|
148 | if __name__ == "__main__" : |
---|
149 | try : |
---|
150 | defaults = { \ |
---|
151 | "format" : "csv", \ |
---|
152 | } |
---|
153 | short_options = "vhd:f:" |
---|
154 | long_options = ["help", "version", "data=", "format="] |
---|
155 | |
---|
156 | # Initializes the command line tool |
---|
157 | dumper = DumPyKota(doc=__doc__) |
---|
158 | |
---|
159 | # parse and checks the command line |
---|
160 | (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
161 | |
---|
162 | # sets long options |
---|
163 | options["help"] = options["h"] or options["help"] |
---|
164 | options["version"] = options["v"] or options["version"] |
---|
165 | options["data"] = options["d"] or options["data"] |
---|
166 | options["format"] = options["f"] or options["format"] or defaults["format"] |
---|
167 | |
---|
168 | if options["help"] : |
---|
169 | dumper.display_usage_and_quit() |
---|
170 | elif options["version"] : |
---|
171 | dumper.display_version_and_quit() |
---|
172 | elif options["data"] is None : |
---|
173 | raise PyKotaToolError, _("The -d | --data command line option is mandatory, see help.") |
---|
174 | else : |
---|
175 | if args : |
---|
176 | raise PyKotaToolError, _("Too many arguments, see help.") |
---|
177 | retcode = dumper.main(args, options) |
---|
178 | except SystemExit : |
---|
179 | pass |
---|
180 | except : |
---|
181 | try : |
---|
182 | dumper.crashed("dumpykota failed") |
---|
183 | except : |
---|
184 | pass |
---|
185 | retcode = -1 |
---|
186 | |
---|
187 | try : |
---|
188 | dumper.storage.close() |
---|
189 | except (TypeError, NameError, AttributeError) : |
---|
190 | pass |
---|
191 | |
---|
192 | sys.exit(retcode) |
---|