<s> from django import template <EOL> from django . conf import settings <EOL> from django . conf . urls . defaults import patterns , url <EOL> from django . core . management import call_command <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponse <EOL> import os <EOL> import socket <EOL> THISDIR = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> TEMPLATE_DIR = os . path . join ( THISDIR , '<STR_LIT>' ) <EOL> MEDIA_DIR = os . path . join ( THISDIR , '<STR_LIT>' ) <EOL> STATIC_DIR = os . path . join ( THISDIR , '<STR_LIT>' ) <EOL> def index ( request ) : <EOL> context = template . RequestContext ( request , { <EOL> '<STR_LIT>' : get_templates ( ) , <EOL> } ) <EOL> tpl = template . Template ( """<STR_LIT>""" ) <EOL> return HttpResponse ( tpl . render ( context ) ) <EOL> urlpatterns = patterns ( '<STR_LIT>' , <EOL> url ( '<STR_LIT>' , index ) , <EOL> url ( '<STR_LIT>' , '<STR_LIT>' , name = '<STR_LIT>' ) , <EOL> url ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : MEDIA_DIR } ) , <EOL> url ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : STATIC_DIR } ) , <EOL> ) <EOL> def get_templates ( ) : <EOL> for root , _ , files in os . walk ( TEMPLATE_DIR ) : <EOL> for filename in files : <EOL> template_name = os . path . normpath ( os . path . join ( os . path . relpath ( root , TEMPLATE_DIR ) , filename ) ) <EOL> url = reverse ( '<STR_LIT>' , args = ( template_name , ) ) <EOL> yield url , template_name <EOL> def get_open_port ( ) : <EOL> port = <NUM_LIT> <EOL> while True : <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> try : <EOL> s . bind ( ( '<STR_LIT:localhost>' , port ) ) <EOL> except socket . error : <EOL> port += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> finally : <EOL> s . close ( ) <EOL> return port <EOL> def run ( public = True , port = None ) : <EOL> settings . configure ( <EOL> ROOT_URLCONF = '<STR_LIT>' , <EOL> DEBUG = True , <EOL> TEMPLATE_DEBUG = True , <EOL> TEMPLATE_DIRS = [ TEMPLATE_DIR ] , <EOL> APPEND_SLASH = False , <EOL> STATIC_ROOT = STATIC_DIR , <EOL> MEDIA_ROOT = MEDIA_DIR , <EOL> STATIC_URL = '<STR_LIT>' , <EOL> MEDIA_URL = '<STR_LIT>' , <EOL> ) <EOL> port = port or get_open_port ( ) <EOL> if public : <EOL> location = '<STR_LIT>' % port <EOL> else : <EOL> location = '<STR_LIT>' % port <EOL> call_command ( '<STR_LIT>' , location ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import argparse <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT:port>' , default = <NUM_LIT:0> , type = int , nargs = '<STR_LIT:?>' ) <EOL> args = parser . parse_args ( ) <EOL> run ( args . public , args . port ) </s>
