1 | #! /usr/bin/env python |
---|
2 | # |
---|
3 | # PyKota |
---|
4 | # |
---|
5 | # PyKota : Print Quotas for CUPS and LPRng |
---|
6 | # |
---|
7 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
8 | # This program is free software; you can redistribute it and/or modify |
---|
9 | # it under the terms of the GNU General Public License as published by |
---|
10 | # the Free Software Foundation; either version 2 of the License, or |
---|
11 | # (at your option) any later version. |
---|
12 | # |
---|
13 | # This program is distributed in the hope that it will be useful, |
---|
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | # GNU General Public License for more details. |
---|
17 | # |
---|
18 | # You should have received a copy of the GNU General Public License |
---|
19 | # along with this program; if not, write to the Free Software |
---|
20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
21 | # |
---|
22 | # $Id$ |
---|
23 | # |
---|
24 | # $Log$ |
---|
25 | # Revision 1.16 2003/06/06 20:49:15 jalet |
---|
26 | # Very latest schema. UNTESTED. |
---|
27 | # |
---|
28 | # Revision 1.15 2003/05/17 16:32:30 jalet |
---|
29 | # Also outputs the original import error message. |
---|
30 | # |
---|
31 | # Revision 1.14 2003/05/17 16:31:38 jalet |
---|
32 | # Dies gracefully if DistUtils is not present. |
---|
33 | # |
---|
34 | # Revision 1.13 2003/04/29 18:37:54 jalet |
---|
35 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
36 | # |
---|
37 | # Revision 1.12 2003/04/23 22:13:56 jalet |
---|
38 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
39 | # |
---|
40 | # Revision 1.11 2003/04/17 13:49:29 jalet |
---|
41 | # Typo |
---|
42 | # |
---|
43 | # Revision 1.10 2003/04/17 13:48:39 jalet |
---|
44 | # Better help |
---|
45 | # |
---|
46 | # Revision 1.9 2003/04/17 13:47:28 jalet |
---|
47 | # Help added during installation. |
---|
48 | # |
---|
49 | # Revision 1.8 2003/04/15 17:49:29 jalet |
---|
50 | # Installation script now checks the presence of Netatalk |
---|
51 | # |
---|
52 | # Revision 1.7 2003/04/03 20:03:37 jalet |
---|
53 | # Installation script now allows to install the sample configuration file. |
---|
54 | # |
---|
55 | # Revision 1.6 2003/03/29 13:45:26 jalet |
---|
56 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
57 | # Two README files were added. |
---|
58 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
59 | # |
---|
60 | # Revision 1.5 2003/03/29 13:08:28 jalet |
---|
61 | # Configuration is now expected to be found in /etc/pykota.conf instead of |
---|
62 | # in /etc/cups/pykota.conf |
---|
63 | # Installation script can move old config files to the new location if needed. |
---|
64 | # Better error handling if configuration file is absent. |
---|
65 | # |
---|
66 | # Revision 1.4 2003/03/29 09:47:00 jalet |
---|
67 | # More powerful installation script. |
---|
68 | # |
---|
69 | # Revision 1.3 2003/03/26 17:48:36 jalet |
---|
70 | # First shot at trying to detect the availability of the needed software |
---|
71 | # during the installation. |
---|
72 | # |
---|
73 | # Revision 1.2 2003/03/09 16:49:04 jalet |
---|
74 | # The installation script installs the man pages too now. |
---|
75 | # |
---|
76 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
77 | # Initial import into CVS |
---|
78 | # |
---|
79 | # |
---|
80 | # |
---|
81 | |
---|
82 | import sys |
---|
83 | import glob |
---|
84 | import os |
---|
85 | import shutil |
---|
86 | import ConfigParser |
---|
87 | try : |
---|
88 | from distutils.core import setup |
---|
89 | except ImportError, msg : |
---|
90 | sys.stderr.write("%s\n" % msg) |
---|
91 | sys.stderr.write("You need the DistUtils Python module.\nunder Debian, you may have to install the python-dev package.\nOf course, YMMV.\n") |
---|
92 | sys.exit(-1) |
---|
93 | |
---|
94 | sys.path.insert(0, "pykota") |
---|
95 | from pykota.version import __version__, __doc__ |
---|
96 | |
---|
97 | ACTION_CONTINUE = 0 |
---|
98 | ACTION_ABORT = 1 |
---|
99 | |
---|
100 | def checkModule(module) : |
---|
101 | """Checks if a Python module is available or not.""" |
---|
102 | try : |
---|
103 | exec "import %s" % module |
---|
104 | except ImportError : |
---|
105 | return 0 |
---|
106 | else : |
---|
107 | return 1 |
---|
108 | |
---|
109 | def checkCommand(command) : |
---|
110 | """Checks if a command is available or not.""" |
---|
111 | input = os.popen("type %s 2>/dev/null" % command) |
---|
112 | result = input.read().strip() |
---|
113 | input.close() |
---|
114 | return result |
---|
115 | |
---|
116 | def checkWithPrompt(prompt, module=None, command=None, helper=None) : |
---|
117 | """Tells the user what will be checked, and asks him what to do if something is absent.""" |
---|
118 | sys.stdout.write("Checking for %s availability : " % prompt) |
---|
119 | sys.stdout.flush() |
---|
120 | if command is not None : |
---|
121 | result = checkCommand(command) |
---|
122 | elif module is not None : |
---|
123 | result = checkModule(module) |
---|
124 | if result : |
---|
125 | sys.stdout.write("OK\n") |
---|
126 | return ACTION_CONTINUE |
---|
127 | else : |
---|
128 | sys.stdout.write("NO.\n") |
---|
129 | sys.stderr.write("ERROR : %s not available !\n" % prompt) |
---|
130 | if helper is not None : |
---|
131 | sys.stdout.write("%s\n" % helper) |
---|
132 | sys.stdout.write("You may continue safely if you don't need this functionnality.\n") |
---|
133 | answer = raw_input("%s is missing. Do you want to continue anyway (y/N) ? " % prompt) |
---|
134 | if answer[0:1].upper() == 'Y' : |
---|
135 | return ACTION_CONTINUE |
---|
136 | else : |
---|
137 | return ACTION_ABORT |
---|
138 | |
---|
139 | if "install" in sys.argv : |
---|
140 | # checks if Python version is correct, we need >= 2.1 |
---|
141 | if not (sys.version > "2.1") : |
---|
142 | sys.stderr.write("PyKota needs at least Python v2.1 !\nYour version seems to be older than that, please update.\nAborted !\n") |
---|
143 | sys.exit(-1) |
---|
144 | |
---|
145 | # checks if a configuration file is present in the old location |
---|
146 | if os.path.isfile("/etc/cups/pykota.conf") : |
---|
147 | if not os.path.isfile("/etc/pykota.conf") : |
---|
148 | sys.stdout.write("From version 1.02 on, PyKota expects to find its configuration\nfile in /etc instead of /etc/cups.\n") |
---|
149 | sys.stdout.write("It seems that you've got a configuration file in the old location,\nso it will not be used anymore,\nand there's no configuration file in the new location.\n") |
---|
150 | answer = raw_input("Do you want to move /etc/cups/pykota.conf to /etc/pykota.conf (y/N) ? ") |
---|
151 | if answer[0:1].upper() == 'Y' : |
---|
152 | try : |
---|
153 | os.rename("/etc/cups/pykota.conf", "/etc/pykota.conf") |
---|
154 | except OSError : |
---|
155 | sys.stderr.write("ERROR : An error occured while moving /etc/cups/pykota.conf to /etc/pykota.conf\nAborted !\n") |
---|
156 | sys.exit(-1) |
---|
157 | else : |
---|
158 | sys.stderr.write("WARNING : Configuration file /etc/cups/pykota.conf won't be used ! Move it to /etc instead.\n") |
---|
159 | sys.stderr.write("PyKota installation will continue anyway, but the software won't run until you put a proper configuration file in /etc\n") |
---|
160 | else : |
---|
161 | sys.stderr.write("WARNING : Configuration file /etc/cups/pykota.conf will not be used !\nThe file /etc/pykota.conf will be used instead.\n") |
---|
162 | elif not os.path.isfile("/etc/pykota.conf") : |
---|
163 | # no configuration file, first installation it seems. |
---|
164 | if os.path.isfile("conf/pykota.conf.sample") : |
---|
165 | answer = raw_input("Do you want to install conf/pykota.conf.sample as /etc/pykota.conf (y/N) ? ") |
---|
166 | if answer[0:1].upper() == 'Y' : |
---|
167 | try : |
---|
168 | shutil.copy("conf/pykota.conf.sample", "/etc/pykota.conf") |
---|
169 | except IOError : |
---|
170 | sys.stderr.write("WARNING : Problem while installing /etc/pykota.conf, please do it manually.\n") |
---|
171 | else : |
---|
172 | sys.stdout.write("Configuration file /etc/pykota.conf installed.\nDon't forget to adapt /etc/pykota.conf to your needs.\n") |
---|
173 | else : |
---|
174 | sys.stderr.write("WARNING : PyKota won't run without a configuration file !\n") |
---|
175 | else : |
---|
176 | # Configuration file already exists. Check if this is an old version or not |
---|
177 | # if the 'method: lazy' line is present, then the configuration file |
---|
178 | # has to be updated. |
---|
179 | oldconf = ConfigParser.ConfigParser() |
---|
180 | oldconf.read(["/etc/pykota.conf"]) |
---|
181 | try : |
---|
182 | if oldconf.get("global", "method", raw=1).lower().strip() == "lazy" : |
---|
183 | sys.stdout.write("You have got an OLD PyKota configuration file !\n") |
---|
184 | sys.stdout.write("The 'method' statement IS NOT SUPPORTED ANYMORE\nand was replaced with the 'accounter' statement.\n") |
---|
185 | sys.stdout.write("You have to manually set an 'accounter' statement,\neither globally or for each printer.\n") |
---|
186 | sys.stdout.write("Please read the sample configuration file conf/pykota.conf.sample\n") |
---|
187 | sys.stdout.write("to learn how to MANUALLY apply the modifications needed,\nafter the installation is done.\n") |
---|
188 | sys.stdout.write("If you don't do this, then PyKota will stop working !\n") |
---|
189 | answer = raw_input("Please, press ENTER when you'll have read the above paragraph.") |
---|
190 | except ConfigParser.NoOptionError : |
---|
191 | # New configuration file, OK |
---|
192 | pass |
---|
193 | |
---|
194 | # checks if some needed Python modules are there or not. |
---|
195 | modulestocheck = [ ("PygreSQL", "pg", "PygreSQL is mandatory if you want to use PostgreSQL as the quota storage backend."), |
---|
196 | ("mxDateTime", "mx.DateTime", "eGenix' mxDateTime is mandatory for PyKota to work."), |
---|
197 | ("Python-LDAP", "ldap", "Python-LDAP is mandatory if you plan to use an LDAP\ndirectory as the quota storage backend.") |
---|
198 | ] |
---|
199 | commandstocheck = [("SNMP Tools", "snmpget", "SNMP Tools are needed if you want to use SNMP enabled printers."), ("Netatalk", "pap", "Netatalk is needed if you want to use AppleTalk enabled printers.")] |
---|
200 | for (name, module, helper) in modulestocheck : |
---|
201 | action = checkWithPrompt(name, module=module, helper=helper) |
---|
202 | if action == ACTION_ABORT : |
---|
203 | sys.stderr.write("Aborted !\n") |
---|
204 | sys.exit(-1) |
---|
205 | |
---|
206 | # checks if some software are there or not. |
---|
207 | for (name, command, helper) in commandstocheck : |
---|
208 | action = checkWithPrompt(name, command=command, helper=helper) |
---|
209 | if action == ACTION_ABORT : |
---|
210 | sys.stderr.write("Aborted !\n") |
---|
211 | sys.exit(-1) |
---|
212 | |
---|
213 | data_files = [] |
---|
214 | mofiles = glob.glob(os.sep.join(["po", "*", "*.mo"])) |
---|
215 | for mofile in mofiles : |
---|
216 | lang = mofile.split(os.sep)[1] |
---|
217 | directory = os.sep.join(["share", "locale", lang, "LC_MESSAGES"]) |
---|
218 | data_files.append((directory, [ mofile ])) |
---|
219 | |
---|
220 | directory = os.sep.join(["share", "man", "man1"]) |
---|
221 | manpages = glob.glob(os.sep.join(["man", "*.1"])) |
---|
222 | data_files.append((directory, manpages)) |
---|
223 | |
---|
224 | setup(name = "pykota", version = __version__, |
---|
225 | license = "GNU GPL", |
---|
226 | description = __doc__, |
---|
227 | author = "Jerome Alet", |
---|
228 | author_email = "alet@librelogiciel.com", |
---|
229 | url = "http://www.librelogiciel.com/software/", |
---|
230 | packages = [ "pykota", "pykota.storages", "pykota.requesters", "pykota.loggers", "pykota.accounters" ], |
---|
231 | scripts = [ "bin/pykota", "bin/edpykota", "bin/repykota", "bin/warnpykota" ], |
---|
232 | data_files = data_files) |
---|