Source

lafs-rpg / configure.py

#! /usr/bin/env python

import sys
import os
import optparse

import configure


USAGE = """
{script_name} --public-host PUBLIC_HOST --front-page URL_PATH [ --lafs-host LAFS_HOST ] [ --lafs-port LAFS_PORT ]

Generate configuration files for nginx to serve as a Tahoe-LAFS public
web gateway.  This does not install the generated files or otherwise
modify the host system.

Read ./README to understand the consequences of this technology.

Read ./INSTALL to understand the functional context of this script.
""".format(script_name = sys.argv[0])


class DEFAULTS (object):
    # This class is just a namespace; do not instantiate.
    LAFS_HOST = '127.0.0.1'
    LAFS_PORT = 3456


def main(args = sys.argv[1:]):
    opts = parse_args(args)

    params = vars(opts)

    srcdir = os.path.dirname(configure.__file__)
    templatesdir = os.path.join(srcdir, 'templates')
    builddir = os.path.join(srcdir, 'build')

    build_templates_dir(params, templatesdir, builddir)


def parse_args(args):
    p = optparse.OptionParser(usage=USAGE)

    # Required params:
    p.add_option('--public-host',
                 type='str',
                 dest='PUBLIC_HOST',
                 help='The public host for redirection URLs. No default. Example: mysite.com')
    p.add_option('--front-page',
                 type='str',
                 dest='FRONT_PAGE',
                 help='The relative path to redirect to on / requests. Must begin with /. No default. Example: /uri/URI:DIR2-RO:.../index.html')

    p.add_option('--lafs-host',
                 type='str',
                 dest='LAFS_HOST',
                 default=DEFAULTS.LAFS_HOST,
                 help='The host of the Tahoe-LAFS gateway webapi. Default: {0}'.format(DEFAULTS.LAFS_HOST))
    p.add_option('--lafs-port',
                 type='int',
                 dest='LAFS_PORT',
                 default=DEFAULTS.LAFS_PORT,
                 help='The port of the Tahoe-LAFS gateway webapi. Default: {0}'.format(DEFAULTS.LAFS_PORT))

    opts, args = p.parse_args(args)

    if args:
        p.error('Unexpected args: {0!r}'.format(args))

    if opts.PUBLIC_HOST is None:
        p.error('Required option --https-root-url is missing.')

    if opts.FRONT_PAGE is None:
        p.error('Required option --front-page is missing.')

    if not opts.FRONT_PAGE.startswith('/'):
        p.error("The --front-page URL path must begin with '/'.")

    return opts


def build_templates(params, template, outpath):
    if os.path.isdir(template):
        build_templates_dir(params, template, outpath)
    else:
        assert os.path.isfile(template)
        build_template_file(params, template, outpath)


def build_templates_dir(params, template, outpath):
    try:
        os.mkdir(outpath)
    except os.error, e:
        if e.strerror != 'File exists':
            raise

    for name in os.listdir(template):
        subtemplate = os.path.join(template, name)
        suboutpath = os.path.join(outpath, name)

        build_templates(params, subtemplate, suboutpath)


def build_template_file(params, template, outpath):
    print 'Processing {0!r}.'.format(template)
    with file(template, 'r') as f:
        tmplsrc = f.read()

    output = tmplsrc.format(**params)

    with file(outpath, 'w') as f:
        f.write(output)
        

if __name__ == '__main__':
    main()