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