root / pykota / trunk / setup.py @ 1091

Revision 1087, 14.2 kB (checked in by jalet, 21 years ago)

Really big modifications wrt new configuration file's location and content.

  • 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#
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.19  2003/07/16 21:53:07  jalet
26# Really big modifications wrt new configuration file's location and content.
27#
28# Revision 1.18  2003/07/03 09:44:00  jalet
29# Now includes the pykotme utility
30#
31# Revision 1.17  2003/06/30 12:46:15  jalet
32# Extracted reporting code.
33#
34# Revision 1.16  2003/06/06 20:49:15  jalet
35# Very latest schema. UNTESTED.
36#
37# Revision 1.15  2003/05/17 16:32:30  jalet
38# Also outputs the original import error message.
39#
40# Revision 1.14  2003/05/17 16:31:38  jalet
41# Dies gracefully if DistUtils is not present.
42#
43# Revision 1.13  2003/04/29 18:37:54  jalet
44# Pluggable accounting methods (actually doesn't support external scripts)
45#
46# Revision 1.12  2003/04/23 22:13:56  jalet
47# Preliminary support for LPRng added BUT STILL UNTESTED.
48#
49# Revision 1.11  2003/04/17 13:49:29  jalet
50# Typo
51#
52# Revision 1.10  2003/04/17 13:48:39  jalet
53# Better help
54#
55# Revision 1.9  2003/04/17 13:47:28  jalet
56# Help added during installation.
57#
58# Revision 1.8  2003/04/15 17:49:29  jalet
59# Installation script now checks the presence of Netatalk
60#
61# Revision 1.7  2003/04/03 20:03:37  jalet
62# Installation script now allows to install the sample configuration file.
63#
64# Revision 1.6  2003/03/29 13:45:26  jalet
65# GPL paragraphs were incorrectly (from memory) copied into the sources.
66# Two README files were added.
67# Upgrade script for PostgreSQL pre 1.01 schema was added.
68#
69# Revision 1.5  2003/03/29 13:08:28  jalet
70# Configuration is now expected to be found in /etc/pykota.conf instead of
71# in /etc/cups/pykota.conf
72# Installation script can move old config files to the new location if needed.
73# Better error handling if configuration file is absent.
74#
75# Revision 1.4  2003/03/29 09:47:00  jalet
76# More powerful installation script.
77#
78# Revision 1.3  2003/03/26 17:48:36  jalet
79# First shot at trying to detect the availability of the needed software
80# during the installation.
81#
82# Revision 1.2  2003/03/09 16:49:04  jalet
83# The installation script installs the man pages too now.
84#
85# Revision 1.1  2003/02/05 21:28:17  jalet
86# Initial import into CVS
87#
88#
89#
90
91import sys
92import glob
93import os
94import shutil
95try :
96    from distutils.core import setup
97except ImportError, msg :   
98    sys.stderr.write("%s\n" % msg)
99    sys.stderr.write("You need the DistUtils Python module.\nunder Debian, you may have to install the python-dev package.\nOf course, YMMV.\n")
100    sys.exit(-1)
101
102sys.path.insert(0, "pykota")
103from pykota.version import __version__, __doc__
104
105ACTION_CONTINUE = 0
106ACTION_ABORT = 1
107
108def checkModule(module) :
109    """Checks if a Python module is available or not."""
110    try :
111        exec "import %s" % module
112    except ImportError :   
113        return 0
114    else :   
115        return 1
116       
117def checkCommand(command) :
118    """Checks if a command is available or not."""
119    input = os.popen("type %s 2>/dev/null" % command)
120    result = input.read().strip()
121    input.close()
122    return result
123   
124def checkWithPrompt(prompt, module=None, command=None, helper=None) :
125    """Tells the user what will be checked, and asks him what to do if something is absent."""
126    sys.stdout.write("Checking for %s availability : " % prompt)
127    sys.stdout.flush()
128    if command is not None :
129        result = checkCommand(command)
130    elif module is not None :   
131        result = checkModule(module)
132    if result :   
133        sys.stdout.write("OK\n")
134        return ACTION_CONTINUE
135    else :   
136        sys.stdout.write("NO.\n")
137        sys.stderr.write("ERROR : %s not available !\n" % prompt)
138        if helper is not None :
139            sys.stdout.write("%s\n" % helper)
140            sys.stdout.write("You may continue safely if you don't need this functionnality.\n")
141        answer = raw_input("%s is missing. Do you want to continue anyway (y/N) ? " % prompt)
142        if answer[0:1].upper() == 'Y' :
143            return ACTION_CONTINUE
144        else :
145            return ACTION_ABORT
146   
147if "install" in sys.argv :
148    # checks if Python version is correct, we need >= 2.1
149    if not (sys.version > "2.1") :
150        sys.stderr.write("PyKota needs at least Python v2.1 !\nYour version seems to be older than that, please update.\nAborted !\n")
151        sys.exit(-1)
152       
153    # checks if a configuration file is present in the new location
154    if not os.path.isfile("/etc/pykota/pykota.conf") :
155        if not os.path.isdir("/etc/pykota") :
156            try :
157                os.mkdir("/etc/pykota")
158            except OSError, msg :   
159                sys.stderr.write("An error occured while creating the /etc/pykota directory.\n%s\n" % msg)
160                sys.exit(-1)
161               
162        if os.path.isfile("/etc/pykota.conf") :
163            # upgrade from pre-1.14 to 1.14 and above
164            sys.stdout.write("From version 1.14 on, PyKota expects to find its configuration\nfile in /etc/pykota/ instead of /etc/\n")
165            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")
166            answer = raw_input("Do you want to move /etc/pykota.conf to /etc/pykota/pykota.conf (y/N) ? ")
167            if answer[0:1].upper() == 'Y' :
168                try :
169                    os.rename("/etc/pykota.conf", "/etc/pykota/pykota.conf")
170                except OSError :   
171                    sys.stderr.write("ERROR : An error occured while moving /etc/pykota.conf to /etc/pykota/pykota.conf\nAborted !\n")
172                    sys.exit(-1)
173                else :   
174                    sys.stdout.write("Configuration file /etc/pykota.conf moved to /etc/pykota/pykota.conf.\n")
175            else :
176                sys.stderr.write("WARNING : Configuration file /etc/pykota.conf won't be used ! Move it to /etc/pykota/ instead.\n")
177                sys.stderr.write("PyKota installation will continue anyway,\nbut the software won't run until you put a proper configuration file in /etc/pykota/\n")
178            dummy = raw_input("Please press ENTER when you have read the message above. ")
179        else :
180            # first installation
181            if os.path.isfile("conf/pykota.conf.sample") :
182                answer = raw_input("Do you want to install\n\tconf/pykota.conf.sample as /etc/pykota/pykota.conf (y/N) ? ")
183                if answer[0:1].upper() == 'Y' :
184                    try :
185                        shutil.copy("conf/pykota.conf.sample", "/etc/pykota/pykota.conf")       
186                        shutil.copy("conf/pykotadmin.conf.sample", "/etc/pykota/pykotadmin.conf")       
187                    except IOError, msg :   
188                        sys.stderr.write("WARNING : Problem while installing sample configuration files in /etc/pykota/, please do it manually.\n%s\n" % msg)
189                    else :   
190                        sys.stdout.write("Configuration file /etc/pykota/pykota.conf and /etc/pykota/pykotadmin.conf installed.\nDon't forget to adapt these files to your needs.\n")
191                else :       
192                    sys.stderr.write("WARNING : PyKota won't run without a configuration file !\n")
193            else :       
194                # Problem ?
195                sys.stderr.write("WARNING : PyKota's sample configuration file cannot be found.\nWhat you have downloaded seems to be incomplete,\nor you are not in the pykota directory.\nPlease double check, and restart the installation procedure.\n")
196            dummy = raw_input("Please press ENTER when you have read the message above. ")
197    else :   
198        # already at 1.14 or above, nothing to be done.
199        pass
200       
201    # Second stage, we will fail if onfiguration is incorrect for security reasons
202    from pykota.config import PyKotaConfig,PyKotaConfigError
203    try :
204        conf = PyKotaConfig("/etc/pykota/")
205    except PyKotaConfigError, msg :   
206        sys.stedrr.write("%s\nINSTALLATION ABORTED !\nPlease restart installation.\n" % msg)
207        sys.exit(-1)
208    else :
209        hasadmin = conf.getGlobalOption("storageadmin", ignore=1)
210        hasadminpw = conf.getGlobalOption("storageadminpw", ignore=1)
211        hasuser = conf.getGlobalOption("storageuser", ignore=1)
212        if hasadmin or hasadminpw : 
213            sys.stderr.write("From version 1.14 on, PyKota expects that /etc/pykota/pykota.conf doesn't contain the Quota Storage Administrator's name and optional password.\n")
214            sys.stderr.write("Please put these in a [global] section in /etc/pykota/pykotadmin.conf\n")
215            sys.stderr.write("Then replace these values with 'storageuser' and 'storageuserpw' in /etc/pykota/pykota.conf\n")
216            sys.stderr.write("These two fields were re-introduced to allow any user to read to his own quota, without allowing them to modify it.\n")
217            sys.stderr.write("You can look at the conf/pykota.conf.sample and conf/pykotadmin.conf.sample files for examples.\n")
218            sys.stderr.write("YOU HAVE TO DO THESE MODIFICATIONS MANUALLY, AND RESTART THE INSTALLATION.\n")
219            sys.stderr.write("INSTALLATION ABORTED FOR SECURITY REASONS.\n")
220            sys.exit(-1)
221        if not hasuser :
222            sys.stderr.write("From version 1.14 on, PyKota expects that /etc/pykota/pykota.conf contains the Quota Storage Normal User's name and optional password.\n")
223            sys.stderr.write("Please put these in a [global] section in /etc/pykota/pykota.conf\n")
224            sys.stderr.write("These fields are respectively named 'storageuser' and 'storageuserpw'.\n")
225            sys.stderr.write("These two fields were re-introduced to allow any user to read to his own quota, without allowing them to modify it.\n")
226            sys.stderr.write("You can look at the conf/pykota.conf.sample and conf/pykotadmin.conf.sample files for examples.\n")
227            sys.stderr.write("YOU HAVE TO DO THESE MODIFICATIONS MANUALLY, AND RESTART THE INSTALLATION.\n")
228            sys.stderr.write("INSTALLATION ABORTED FOR SECURITY REASONS.\n")
229            sys.exit(-1)
230           
231        sb = conf.getStorageBackend()
232        if (sb.get("storageadmin") is None) or (sb.get("storageuser") is None) :
233            sys.stderr.write("From version 1.14 on, PyKota expects that /etc/pykota/pykota.conf contains the Quota Storage Normal User's name and optional password which gives READONLY access to the Print Quota DataBase,")
234            sys.stderr.write("and that /etc/pykota/pykotadmin.conf contains the Quota Storage Administrator's name and optional password which gives READ/WRITE access to the Print Quota DataBase.\n")
235            sys.stderr.write("Your configuration doesn't seem to be OK, please modify your configuration files in /etc/pykota/\n")
236            sys.stderr.write("AND RESTART THE INSTALLATION.\n")
237            sys.stderr.write("INSTALLATION ABORTED FOR SECURITY REASONS.\n")
238            sys.exit(-1)
239       
240    # change files permissions   
241    os.chmod("/etc/pykota/pykota.conf", 0644)
242    os.chmod("/etc/pykota/pykotadmin.conf", 0640)
243   
244    # WARNING MESSAGE   
245    sys.stdout.write("WARNING : IF YOU ARE UPGRADING FROM A PRE-1.14 TO 1.14 OR ABOVE\n")
246    sys.stdout.write("AND USE THE POSTGRESQL BACKEND, THEN YOU HAVE TO MODIFY YOUR\n")
247    sys.stdout.write("DATABASE SCHEMA USING initscripts/postgresql/upgrade-to-1.14.sql\n")
248    sys.stdout.write("PLEASE READ DOCUMENTATION IN initscripts/postgresql/ TO LEARN HOW TO DO.\n")
249    sys.stdout.write("\n\nYOU DON'T HAVE ANYTHING SPECIAL TO DO IF THIS IS YOUR FIRST INSTALLATION.\n\n")
250    dummy = raw_input("Please press ENTER when you have read the message above. ")
251   
252    # checks if some needed Python modules are there or not.
253    modulestocheck = [ ("PygreSQL", "pg", "PygreSQL is mandatory if you want to use PostgreSQL as the quota storage backend."),                                           
254                       ("mxDateTime", "mx.DateTime", "eGenix' mxDateTime is mandatory for PyKota to work."), 
255                       ("Python-LDAP", "ldap", "Python-LDAP is mandatory if you plan to use an LDAP\ndirectory as the quota storage backend.")
256                     ]
257    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.")]
258    for (name, module, helper) in modulestocheck :
259        action = checkWithPrompt(name, module=module, helper=helper)
260        if action == ACTION_ABORT :
261            sys.stderr.write("Aborted !\n")
262            sys.exit(-1)
263           
264    # checks if some software are there or not.
265    for (name, command, helper) in commandstocheck :
266        action = checkWithPrompt(name, command=command, helper=helper)
267        if action == ACTION_ABORT :
268            sys.stderr.write("Aborted !\n")
269            sys.exit(-1)
270           
271data_files = []
272mofiles = glob.glob(os.sep.join(["po", "*", "*.mo"]))
273for mofile in mofiles :
274    lang = mofile.split(os.sep)[1]
275    directory = os.sep.join(["share", "locale", lang, "LC_MESSAGES"])
276    data_files.append((directory, [ mofile ]))
277   
278directory = os.sep.join(["share", "man", "man1"])
279manpages = glob.glob(os.sep.join(["man", "*.1"]))   
280data_files.append((directory, manpages))
281
282setup(name = "pykota", version = __version__,
283      license = "GNU GPL",
284      description = __doc__,
285      author = "Jerome Alet",
286      author_email = "alet@librelogiciel.com",
287      url = "http://www.librelogiciel.com/software/",
288      packages = [ "pykota", "pykota.storages", "pykota.requesters", "pykota.loggers", "pykota.accounters", "pykota.reporters" ],
289      scripts = [ "bin/pykota", "bin/edpykota", "bin/repykota", "bin/warnpykota", "bin/pykotme" ],
290      data_files = data_files)
Note: See TracBrowser for help on using the browser.