root / pykota / trunk / bin / dumpykota @ 1728

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

Updated help for dumpykota

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