Packaging sites with Aspen

Tue 05 May 2015 by pj Tagged as aspen software www

As the co-maintainer of Aspen, I've focused mainly on making it easier to use, but have, myself, had few opportunities to build sites using it. One such was granted to me the other day, and I took the time to figure out how to build a python package that used aspen and exported a website created in the package.

So with a file tree like:

README.md
setup.py
myapp
myapp/__main__.py
myapp/__init__.pyc
myapp/wsgi.pyc
myapp/__init__.py
myapp/www
myapp/www/index.spt
myapp/wsgi.py

$ more myapp/wsgi.py

import os.path
from aspen.website import Website

def main():
    website = Website()
    website.www_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'www')
    return website

$ more myapp/main.py

# Called via python -m myapp
import os

if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    from myapp.wsgi import main
    website = main()
    port = int(os.environ.get('PORT', '8080'))
    server = make_server('0.0.0.0', port, website)
    print("Greetings, program! Welcome to port {0}.".format(port))
    server.serve_forever()

$ more setup.py

import os

from setuptools import setup, find_packages

def spt_files(topdir):
    """find .spt files under 'topdir'"""
    found = []
    for root, dirs, files in os.walk(topdir):
        found += [ os.path.join(root, f) for f in files if f.endswith('.spt') ]
    found = [ f[len(topdir):] if f.startswith(topdir) else f for f in found ]
    return found


setup(
        name='myapp',
        version='0.1',
        description='My app description',

        url='https://github.com/pypa/sampleproject',

        # Author details
        author='The Python Packaging Authority',
        author_email='pypa-dev@googlegroups.com',

        # Choose your license
        license='GPLv3',

        # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
        classifiers=[
                    'Development Status :: 3 - Alpha',
                    'Intended Audience :: Developers',
                    'Programming Language :: Python :: 2.7',
        ],
        keywords = 'myapp',
        packages = find_packages(),
        package_data = { '': spt_files('myapp') },
        install_requires = [ 'aspen' ]
)

The key here is the package_data parameter, which pulls in .spt files under the specified directory, making them part of the package.