Changeset 14161 for trunk


Ignore:
Timestamp:
05/04/12 16:14:01 (12 years ago)
Author:
ramonb
Message:

sara_cmt/settings.py:

  • some semi-intelligent config file checks, warnings and hint
  • check if config file may be outdated
  • print some suggestions to resolve them
  • see #9
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sara_cmt/sara_cmt/settings.py

    r14158 r14161  
    11
    2 import os, sys, ConfigParser, site
     2import os, os.path, sys, ConfigParser, site, string, time
    33
    44from socket import gethostbyname_ex
    55
     6# Path's customizable through virtualenv
     7sample_configfile = '%s/etc/cmt/cmt.conf.sample' % site.sys.prefix
    68configfile = '%s/etc/cmt/cmt.conf' % site.sys.prefix
     9
     10def count_configlines( filename ):
     11
     12        line_count      = 0
     13        cfg_fp          = open( filename )
     14
     15        for line in cfg_fp.readlines():
     16
     17                line = line.strip()
     18
     19                if len( line ) == 0:
     20                        continue
     21
     22                # RB: ConfigParser considers lines starting with # or ; as comments
     23                if line[0] == '#' or line[0] == ';':
     24                        continue
     25
     26                line_count += 1
     27
     28        cfg_fp.close()
     29
     30        return line_count
     31
     32if not os.path.exists( configfile ):
     33
     34        print 'Unable to find confilefile: %s' %configfile
     35
     36        if os.path.exists( sample_configfile ):
     37
     38                print 'Please modify the sample config file: %s to reflect your settings' %sample_configfile
     39                print 'and then rename it to: %s' %configfile
     40
     41        else:
     42
     43                print 'Also no sample config file was found: %s' %sample_configfile
     44                print 'Something is terribly wrong here ;)'
     45
     46        print ''
     47        print 'Fatal: Giving up and exiting now..'
     48
     49        sys.exit(1)
     50
     51# We are still here: both configfile AND sample_configfile found
     52if exists( sample_configfile ):
     53
     54        # Is the sample configfile newer?
     55        if os.path.getmtime( sample_configfile ) > os.path.getmtime( configfile ):
     56
     57                # Well this is weird, but not fatal
     58                print 'Warning: sample config file(%s) is newer than original config(%s)' %(configfile, sample_configfile)
     59
     60                # Does the sample config file contain more options?
     61                if count_configlines( sample_configfile ) > count_configlines( configfile ):
     62
     63                        print 'Warning: sample config file contains MORE OPTIONS than original config!'
     64                        print ''
     65                        print 'This happens for example if you upgraded CMT and the new release incorporates new configuration options!'
     66                        print ''
     67                        print 'Please update your original config(%s) to incorporate the new config options from sample config(%s)' %( configfile, sample_configfile )
     68                        print ''
     69
     70                else:
     71                        # Config is good but mtime is older, weird.. Just print hint
     72                        print 'Hint: Remove sample config file(%s) to get rid of this warning..' %sample_configfile
     73
     74                # Give them some time to think about warnings and generally annoy them just enough to fix it
     75                time.sleep(2)
     76
     77                # Moving right along; print empty line for cosmetic reasons
     78                print ''
    779
    880config = ConfigParser.RawConfigParser()
     
    1082
    1183try:
    12         DATABASE_USER = config.get('database', 'USER')
    13         DATABASE_PASSWORD = config.get('database', 'PASSWORD')
    14         DATABASE_HOST = config.get('database', 'HOST')
    15         DATABASE_ENGINE = config.get('database', 'ENGINE')
    16         DATABASE_NAME = config.get('database', 'NAME')
     84        DATABASE_USER           = config.get('database', 'USER')
     85        DATABASE_PASSWORD       = config.get('database', 'PASSWORD')
     86        DATABASE_HOST           = config.get('database', 'HOST')
     87        DATABASE_ENGINE         = config.get('database', 'ENGINE')
     88        DATABASE_NAME           = config.get('database', 'NAME')
    1789
    1890except (ConfigParser.NoOptionError, ConfigParser.NoSectionError), details:
    1991
    2092        print 'Config file error: %s' %str(details)
     93        print ''
     94        print 'Giving up and exiting now..'
    2195        sys.exit(1)
    2296
     
    37111try:
    38112        gethostbyname_ex( DATABASE_HOST )
     113
    39114except socket.gaierror, details:
     115
    40116        print 'Unable to resolve database host: %s' %DATABASE_HOST
    41         print 'Exiting.'
     117        print ''
     118        print 'Giving up and exiting now..'
    42119        sys.exit(1)
    43120
Note: See TracChangeset for help on using the changeset viewer.