root / pykota / trunk / bin / dumpykota @ 1720

Revision 1720, 7.7 kB (checked in by jalet, 20 years ago)

Fix for uninitialized variable

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