[Tutor] where to put configuration files

Paul Tremblay phthenry@earthlink.net
Sun Jun 8 15:17:01 2003


On Sun, Jun 08, 2003 at 11:55:00AM -0700, Sean 'Shaleh' Perry wrote:
> 
> since you referred to /etc I assumed (yes I know ...) you were only referring 
> to a Unix like operating system.

Yes, I guess I wasn't clear.

> 
> If you truly want to be portable here we go .....
> 
> a) easy case, store all of the program in one base dir as suggested.  Expected 
> behavior on Windows and I believe OS X.
> 
> b) however, this is the WRONG behavior under a Unix like OS.  /usr is often 
> mounted read-only so your config once written could never be changed.  It 
> also prevents people from packaging the software and integrating it with the 
> rest of the OS (say for instance Debian, RedHat, NetBSD).
> 
> Therefore as Kirk mentions the best situation is to have the location of th 
> config defined as a variable and allow the installation procedure to indicate 
> where the config ends up.

I included my configuartion script below. Is what you mean? The user
runs python configuration.py before python setup.py build. 

> 
> Note this same discussion is relevant for logs, data and just about anything 
> else you write.
> 
> 
> Now that I have put everyone to sleep, back to your regularly scheduled 
> emails.

Not me! Configuration files is always a bit of a problem for me.
Everything else in the setup seems relativley straight forward.


# /usr/bin/env python 

import sys, os
import options_trem

"""

The configuration script gets the target from the command line. It changest the setup.py and the actual script so that they have the right target.

"""

def configure():
    target = get_target()
    change_setup(target)
    change_script(target)

def get_target():
    """
    This functions uses a module I wrote to parse options. If no options are
    determined on the command line, the function returnst the default
    /etc/nest_docutis

    """
    options_dict = {
        'target':     [1, 't'],
    }
    options_obj = options_trem.ParseOptions(sys.argv, 
            options_dict)
    opt_dict, args = options_obj.parse_options()
    if opt_dict == 0:
        sys.stdout.write('Will use the default configuration of /etc/nest_docutils\n')
        return '/etc/docutils_nest'
    target = opt_dict.get('target')
    if not target:
        return '/etc/docutils_nest'
    return target

def change_setup(target):
    """

    Chage the setup.py file to reflect the target

    """
    read_obj = open('setup.py', 'r')
    write_obj = open('temp', 'w')
    line = 1
    while line:
        line = read_obj.readline()
        index = line.find('data_files=')
        if index > -1:
            write_obj.write('data_files = [("%s", ["data/configure.xml"])],\n' % target)
        else:
            write_obj.write(line)
    read_obj.close()
    write_obj.close()
    read_obj = open('temp', 'r')
    write_obj = open('setup.py', 'w')
    line = 1
    while line:
        line = read_obj.readline()
        write_obj.write(line)
    read_obj.close()
    write_obj.close()


def change_script(target):

    """

    Changet the script to reflect the right target

    """
    read_obj = open('nest_inline/nest_docutils.py', 'r')
    write_obj = open('temp', 'w')
    line = 1
    while line:
        line = read_obj.readline()
        index = line.find('$configure$')
        if index > -1:
            write_obj.write("ext_location = '%s' #  $configure$" % \
                    target)
        else:
            write_obj.write(line)
    read_obj.close()
    write_obj.close()
    read_obj = open('temp', 'r')
    write_obj = open('docutils_nest/nest_docutils.py', 'w')
    line = 1
    while line:
        line = read_obj.readline()
        write_obj.write(line)
    read_obj.close()
    write_obj.close()

if __name__ == '__main__':
    configure()

-- 

************************
*Paul Tremblay         *
*phthenry@earthlink.net*
************************