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.1 2004/07/01 19:22:37 jalet |
---|
27 | # First draft of dumpykota |
---|
28 | # |
---|
29 | # |
---|
30 | # |
---|
31 | |
---|
32 | import sys |
---|
33 | import os |
---|
34 | import pwd |
---|
35 | |
---|
36 | from pykota import version |
---|
37 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
38 | from pykota.config import PyKotaConfigError |
---|
39 | from pykota.storage import PyKotaStorageError |
---|
40 | |
---|
41 | __doc__ = """dumpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
42 | |
---|
43 | Dumps PyKota database's content. |
---|
44 | |
---|
45 | command line usage : |
---|
46 | |
---|
47 | dumpykota [options] |
---|
48 | |
---|
49 | options : |
---|
50 | |
---|
51 | -v | --version Prints repykota's version number then exits. |
---|
52 | -h | --help Prints this message then exits. |
---|
53 | |
---|
54 | -d | --data type Dumps 'type' datas. When not specified, the |
---|
55 | default is to dump the jobs history data. |
---|
56 | Allowed types are : |
---|
57 | |
---|
58 | - history : dumps the jobs history. |
---|
59 | - users : dumps users. |
---|
60 | - groups : dumps user groups. |
---|
61 | - printers : dump printers. |
---|
62 | - uquotas : dump user quotas. |
---|
63 | - gquotas : dump user groups quotas. |
---|
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 |
---|
73 | |
---|
74 | -p | --printer p Only dumps datas concerning printer 'p'. |
---|
75 | Actually 'p' can use wildcards characters to select |
---|
76 | only some printers. The default value is *, meaning |
---|
77 | all printers. |
---|
78 | You can specify several names or wildcards, |
---|
79 | by separating them with commas. |
---|
80 | |
---|
81 | -u | --user u Only dumps datas concerning user 'u'. |
---|
82 | Actually 'u' can use wildcards characters to select |
---|
83 | only some users. The default value is *, meaning |
---|
84 | all users. |
---|
85 | You can specify several names or wildcards, |
---|
86 | by separating them with commas. |
---|
87 | |
---|
88 | -g | --group g Only dumps datas concerning group 'g'. |
---|
89 | Actually 'g' can use wildcards characters to select |
---|
90 | only some groups. The default value is *, meaning |
---|
91 | all groups. |
---|
92 | You can specify several names or wildcards, |
---|
93 | by separating them with commas. |
---|
94 | |
---|
95 | If launched by a non-root user, additionnal arguments representing |
---|
96 | users or groups names are ignored, and only the current user/group |
---|
97 | is reported. |
---|
98 | |
---|
99 | This program is free software; you can redistribute it and/or modify |
---|
100 | it under the terms of the GNU General Public License as published by |
---|
101 | the Free Software Foundation; either version 2 of the License, or |
---|
102 | (at your option) any later version. |
---|
103 | |
---|
104 | This program is distributed in the hope that it will be useful, |
---|
105 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
106 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
107 | GNU General Public License for more details. |
---|
108 | |
---|
109 | You should have received a copy of the GNU General Public License |
---|
110 | along with this program; if not, write to the Free Software |
---|
111 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
112 | |
---|
113 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
114 | |
---|
115 | class DumPyKota(PyKotaTool) : |
---|
116 | """A class for dumpykota.""" |
---|
117 | def main(self, ugnames, options) : |
---|
118 | """Print Quota Data Dumper.""" |
---|
119 | uid = os.geteuid() |
---|
120 | if not uid : |
---|
121 | # root user |
---|
122 | if not ugnames : |
---|
123 | # no username, means all usernames |
---|
124 | ugnames = [ "*" ] |
---|
125 | else : |
---|
126 | # not the root user |
---|
127 | # reports only the current user |
---|
128 | username = pwd.getpwuid(uid)[0] |
---|
129 | if options["data"] == "groups" : |
---|
130 | user = self.storage.getUser(username) |
---|
131 | if user.Exists : |
---|
132 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
133 | else : |
---|
134 | ugnames = [ ] |
---|
135 | else : |
---|
136 | ugnames = [ username ] |
---|
137 | |
---|
138 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
139 | if not printers : |
---|
140 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
141 | |
---|
142 | # TODO : insert code here ! |
---|
143 | raise PyKotaToolError, "Not implemented" |
---|
144 | |
---|
145 | if __name__ == "__main__" : |
---|
146 | try : |
---|
147 | defaults = { \ |
---|
148 | "printer" : "*", \ |
---|
149 | } |
---|
150 | short_options = "vhd:f:p:u:g:" |
---|
151 | long_options = ["help", "version", "data=", "format=", "printer=", "user=", "group="] |
---|
152 | |
---|
153 | # Initializes the command line tool |
---|
154 | dumper = DumPyKota(doc=__doc__) |
---|
155 | |
---|
156 | # parse and checks the command line |
---|
157 | (options, args) = dumper.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
158 | |
---|
159 | # sets long options |
---|
160 | options["help"] = options["h"] or options["help"] |
---|
161 | options["version"] = options["v"] or options["version"] |
---|
162 | options["users"] = options["u"] or options["users"] |
---|
163 | options["groups"] = options["g"] or options["groups"] |
---|
164 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
165 | |
---|
166 | if options["help"] : |
---|
167 | dumper.display_usage_and_quit() |
---|
168 | elif options["version"] : |
---|
169 | dumper.display_version_and_quit() |
---|
170 | elif options["users"] and options["groups"] : |
---|
171 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
172 | else : |
---|
173 | retcode = dumper.main(args, options) |
---|
174 | except SystemExit : |
---|
175 | pass |
---|
176 | except : |
---|
177 | try : |
---|
178 | dumper.crashed("repykota failed") |
---|
179 | except : |
---|
180 | pass |
---|
181 | retcode = -1 |
---|
182 | |
---|
183 | try : |
---|
184 | dumper.storage.close() |
---|
185 | except (TypeError, NameError, AttributeError) : |
---|
186 | pass |
---|
187 | |
---|
188 | sys.exit(retcode) |
---|