#!/usr/bin/env python """ $URL$ $Id$ A simple script to generate a QP application skeleton using DurusPublisher. """ import os from datetime import datetime from qp.lib.site import Site svn_vars = '''$URL$\n$Id$''' readme_contents = '''This is %(app_name)s. QUICK START ----------- %(app_help)s .. vim: set ft=rst: ''' changes_contents = '''Version 0.1: %(create_date)s %(app_name)s application created. .. vim: set ft=rst: ''' app_help = """To start and stop %(app_name)s issue the commands:: qp %(app_name)s start (or use qp -u %(app_name)s) qp %(app_name)s stop (or use qp -d %(app_name)s) For more qp command line options:: qp -h To view the running application, visit:: http://localhost:%(add_http)d/ """ init_file_contents = '''""" %(SVN)s """ from qpy.compile import compile_qpy_files compile_qpy_files(__path__[0])''' slash_contents = '''""" %(SVN)s """ from qp.fill.directory import Directory from qp.pub.common import get_site, header, footer from qp.pub.publish import DurusPublisher class SitePublisher (DurusPublisher): configuration = dict( durus_address=('localhost', %(add_durus)d), http_address=('', %(add_http)d), as_https_address=('localhost', %(add_as_https)d), https_address=('', %(add_https)d), ) def display_exceptions(self): return True class SiteDirectory(Directory): def get_exports(self): yield ('', 'index', 'Home', 'The Home page of this site.') def index [html] (self): title = get_site().get_name() header(title) '
It worked!
' 'The ' title ' application lives at %(basedir)s.
' footer() ''' skeleton = [ # (dir, needs an __init__.py file) ('', True), # main driver (slash.(qpy|py)) and objects ('bin', False), # scripts, if any ('doc', False), ('test', False), # sancho tests for objects ('ui', True), # ui components ('ui/test', False), # tests for ui components ('var', False), # durus db, log, and pid files ] files = { 'README': readme_contents, 'CHANGES': changes_contents, 'slash.qpy': slash_contents, } def make_dirs(basepath): for directory, init_required in skeleton: new_path = os.path.join(basepath, directory) os.makedirs(new_path) if init_required: init_file = open(os.path.join(new_path, '__init__.py'), 'w') init_file.write(init_file_contents % dict(SVN=svn_vars)) init_file.close() def make_files(basepath, context): for file_name, content in files.items(): assert file_name[0] != '/' new_file = open(os.path.join(basepath, file_name), 'w') new_file.write(content % context) new_file.close() def make_ports(http_port): http = int(http_port) if not (http >= 8000 and http <= 8999): raise SystemExit, ('Choose an HTTP port between 8000 and 8999. ' '\nPorts can be changed later in slash.qpy.\nTerminating.') ports = ( http - 1000, http, http + 1000, http + 2000) in_use = check_ports(ports) if in_use: print '\n\nWarning, some ports are also specified by other QP applications.' print ' %s\n\n' % in_use return ports def check_ports(ports): """(ports : tuple) -> [(string, int),] Determines if any of the ports are currently in use by other applications If you reuse port numbers, this will be annoying to you. """ in_use = [] sites = Site.get_sites() for name, site in sites.items(): site_ports = [address[1] for item, address in site.get_configuration().items() if item.endswith('_address')] for port in ports: if port in site_ports: in_use.append((name, port)) return in_use def check_basedir(basedir): # no support for $QP_SITES at present. if os.path.exists(basedir): raise SystemExit, '%s path already exists! Terminating.' % basedir if not 'qp_sites' in basedir: raise SystemExit, '%s path not in ../qp_sites. Terminating.' % basedir head, tail = os.path.split(basedir) if not head.endswith('qp_sites'): raise SystemExit, ('%s is an invalid path. ' '\nQP apps should live immediately under qp_sites/ - ' '\nTerminating.' % basedir) return basedir, tail if __name__ == '__main__': import sys if len(sys.argv) != 3: raise SystemExit, 'usage: %s