root / pykota / trunk / bin / dumpykota @ 1719

Revision 1719, 7.5 kB (checked in by jalet, 20 years ago)

Data dumps are now ordered by entry creation date if applicable.
Now dumpykota exits with a message when there's a broken pipe like
in dumpykota --data history | head -3

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