<s> import os <EOL> import sys <EOL> import urllib <EOL> import urlparse <EOL> import simplejson as json <EOL> from datetime import datetime as dt <EOL> from stat import * <EOL> from cherrypy import CherryPyWSGIServer <EOL> from wsgiref . headers import Headers <EOL> from git_http_backend import GitHTTPBackendInfoRefs <EOL> from git_http_backend import GitHTTPBackendSmartHTTP <EOL> from git_http_backend import WSGIHandlerSelector <EOL> from git_http_backend import StaticWSGIServer <EOL> from grit . repo import Local <EOL> from grit . repo import is_repo , get_repo_parent <EOL> from grit . server . handler import * <EOL> from grit . exc import * <EOL> from grit . log import log <EOL> from grit . cfg import GRIT_STATIC_DIR <EOL> import time <EOL> import email . utils <EOL> import mimetypes <EOL> mimetypes . add_type ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> mimetypes . add_type ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def make_app ( * args , ** kw ) : <EOL> '''<STR_LIT>''' <EOL> default_options = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:.>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> ] <EOL> args = list ( args ) <EOL> options = dict ( default_options ) <EOL> options . update ( kw ) <EOL> while default_options and args : <EOL> _d = default_options . pop ( <NUM_LIT:0> ) <EOL> _a = args . pop ( <NUM_LIT:0> ) <EOL> options [ _d [ <NUM_LIT:0> ] ] = _a <EOL> options [ '<STR_LIT>' ] = os . path . abspath ( options [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf8>' ) ) <EOL> options [ '<STR_LIT>' ] = options [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf8>' ) <EOL> selector = WSGIHandlerSelector ( ) <EOL> git_inforefs_handler = GitHTTPBackendInfoRefs ( ** options ) <EOL> git_rpc_handler = GitHTTPBackendSmartHTTP ( ** options ) <EOL> static_handler = StaticServer ( ** options ) <EOL> file_handler = FileServer ( ** options ) <EOL> json_handler = JSONServer ( ** options ) <EOL> ui_handler = UIServer ( ** options ) <EOL> if options [ '<STR_LIT>' ] : <EOL> marker_regex = r'<STR_LIT>' + options [ '<STR_LIT>' ] + '<STR_LIT:)>' <EOL> else : <EOL> marker_regex = '<STR_LIT>' <EOL> selector . add ( <EOL> marker_regex + r'<STR_LIT>' , <EOL> GET = git_inforefs_handler , <EOL> HEAD = git_inforefs_handler <EOL> ) <EOL> selector . add ( <EOL> marker_regex + r'<STR_LIT>' , <EOL> POST = git_rpc_handler <EOL> ) <EOL> selector . add ( <EOL> marker_regex + r'<STR_LIT>' , <EOL> GET = static_handler , <EOL> HEAD = static_handler ) <EOL> selector . add ( <EOL> marker_regex + r'<STR_LIT>' , <EOL> GET = file_handler , <EOL> HEAD = file_handler ) <EOL> selector . add ( <EOL> marker_regex + r'<STR_LIT>' , <EOL> GET = ui_handler , <EOL> POST = json_handler , <EOL> HEAD = ui_handler ) <EOL> return selector <EOL> class JSONServer ( StaticWSGIServer ) : <EOL> def error_response ( self , error , environ , start_response ) : <EOL> headerbase = [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) ] <EOL> start_response ( self . canned_collection [ '<STR_LIT>' ] , headerbase ) <EOL> d = { } <EOL> d [ '<STR_LIT:success>' ] = False <EOL> d [ '<STR_LIT>' ] = True <EOL> d [ '<STR_LIT:data>' ] = { '<STR_LIT>' : error } <EOL> _ret = json . dumps ( d ) <EOL> log . debug ( '<STR_LIT>' % _ret ) <EOL> return _ret <EOL> def json_response ( self , data , environ , start_response ) : <EOL> headerbase = [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) ] <EOL> start_response ( self . canned_collection [ '<STR_LIT>' ] , headerbase ) <EOL> d = { } <EOL> d [ '<STR_LIT:success>' ] = True <EOL> d [ '<STR_LIT>' ] = False <EOL> try : <EOL> if type ( data ) == list : <EOL> for item in data : <EOL> if not item . get ( '<STR_LIT:url>' ) : <EOL> item [ '<STR_LIT:url>' ] = os . path . join ( self . url , item . get ( '<STR_LIT:path>' , str ( item ) ) ) <EOL> d [ '<STR_LIT:data>' ] = data <EOL> _ret = json . dumps ( d ) <EOL> except Exception , e : <EOL> return self . error_response ( str ( e ) , environ , start_response ) <EOL> return _ret <EOL> def get_params ( self , environ ) : <EOL> kwargs = { } <EOL> params = urlparse . parse_qs ( environ . get ( '<STR_LIT>' ) . read ( ) ) <EOL> action = params . get ( '<STR_LIT:action>' , [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> xaction = params . get ( '<STR_LIT>' , [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> try : <EOL> del params [ '<STR_LIT:action>' ] <EOL> del params [ '<STR_LIT>' ] <EOL> except : <EOL> pass <EOL> for k , v in params . items ( ) : <EOL> try : <EOL> kwargs [ k ] = eval ( params [ k ] [ <NUM_LIT:0> ] ) <EOL> except Exception , e : <EOL> kwargs [ k ] = params [ k ] [ <NUM_LIT:0> ] <EOL> return action , kwargs <EOL> def __call__ ( self , environ , start_response ) : <EOL> selector_matches = ( environ . get ( '<STR_LIT>' ) or ( [ ] , { } ) ) [ <NUM_LIT:1> ] <EOL> if '<STR_LIT>' in selector_matches : <EOL> path_info = selector_matches [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf8>' ) <EOL> else : <EOL> path_info = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . decode ( '<STR_LIT:utf8>' ) <EOL> scheme = environ . get ( '<STR_LIT>' , '<STR_LIT:http>' ) <EOL> host = environ . get ( '<STR_LIT>' , '<STR_LIT:localhost>' ) . decode ( '<STR_LIT:utf8>' ) <EOL> self . url = '<STR_LIT>' % ( scheme , host , path_info ) <EOL> full_path = os . path . abspath ( os . path . join ( self . content_path , path_info . strip ( '<STR_LIT:/>' ) ) ) <EOL> _pp = os . path . abspath ( self . content_path ) <EOL> cmd , kwargs = self . get_params ( environ ) <EOL> if not full_path . startswith ( _pp ) : <EOL> log . error ( '<STR_LIT>' % full_path ) <EOL> return self . canned_handlers ( environ , start_response , '<STR_LIT>' ) <EOL> if os . path . exists ( full_path ) : <EOL> mtime = os . stat ( full_path ) . st_mtime <EOL> etag , last_modified = str ( mtime ) , email . utils . formatdate ( mtime ) <EOL> else : <EOL> mtime , etag , last_modified = ( None , None , None ) <EOL> headers = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , email . utils . formatdate ( time . time ( ) ) ) , <EOL> ( '<STR_LIT>' , last_modified ) , <EOL> ( '<STR_LIT>' , etag ) <EOL> ] <EOL> fmap = { <EOL> '<STR_LIT>' : handle_read , <EOL> '<STR_LIT>' : handle_branch , <EOL> '<STR_LIT>' : handle_branch , <EOL> '<STR_LIT>' : handle_repos , <EOL> '<STR_LIT>' : handle_items , <EOL> '<STR_LIT>' : handle_versions , <EOL> '<STR_LIT>' : handle_submodules , <EOL> '<STR_LIT>' : handle_addSubmodule , <EOL> '<STR_LIT>' : handle_addVersion , <EOL> '<STR_LIT>' : handle_parent , <EOL> '<STR_LIT>' : handle_upload , <EOL> } <EOL> repo = get_repo_parent ( full_path ) <EOL> if repo is None : <EOL> repo = full_path <EOL> item_path = full_path . split ( str ( repo ) ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> if item_path and cmd != '<STR_LIT>' : <EOL> log . debug ( '<STR_LIT>' % ( full_path , item_path ) ) <EOL> items = repo . items ( path = item_path ) <EOL> if items : <EOL> repo = item = items [ <NUM_LIT:0> ] <EOL> if cmd == '<STR_LIT:data>' : <EOL> data = repo . file ( ) <EOL> return self . package_response ( data , environ , start_response ) <EOL> else : <EOL> func = fmap . get ( cmd , None ) <EOL> if func : <EOL> response = func ( repo , ** kwargs ) <EOL> else : <EOL> response = getattr ( repo , cmd ) ( ** kwargs ) <EOL> return self . json_response ( response , environ , start_response ) <EOL> class StaticServer ( StaticWSGIServer ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( StaticServer , self ) . __init__ ( * args , ** kwargs ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> path_info = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . decode ( '<STR_LIT:utf8>' ) <EOL> self . content_path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' , '<STR_LIT:..>' ) <EOL> return super ( StaticServer , self ) . __call__ ( environ , start_response ) <EOL> class FileServer ( StaticWSGIServer ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( FileServer , self ) . __init__ ( * args , ** kwargs ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> selector_matches = ( environ . get ( '<STR_LIT>' ) or ( [ ] , { } ) ) [ <NUM_LIT:1> ] <EOL> if '<STR_LIT>' in selector_matches : <EOL> path_info = selector_matches [ '<STR_LIT>' ] . decode ( '<STR_LIT:utf8>' ) <EOL> else : <EOL> path_info = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . decode ( '<STR_LIT:utf8>' ) <EOL> full_path = os . path . abspath ( os . path . join ( self . content_path , path_info . strip ( '<STR_LIT:/>' ) ) ) <EOL> repo = get_repo_parent ( full_path ) <EOL> item_path = full_path . split ( str ( repo ) ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> items = repo . items ( path = item_path ) <EOL> if items : <EOL> file_like = items [ <NUM_LIT:0> ] . file ( ) <EOL> else : <EOL> default = os . path . join ( GRIT_STATIC_DIR , os . path . basename ( item_path ) ) <EOL> file_like = open ( default , '<STR_LIT:rb>' ) <EOL> return self . package_response ( file_like , environ , start_response ) <EOL> class UIServer ( StaticWSGIServer ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( UIServer , self ) . __init__ ( * args , ** kwargs ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> full_path = os . path . join ( GRIT_STATIC_DIR , '<STR_LIT>' ) <EOL> mtime = os . stat ( full_path ) . st_mtime <EOL> etag , last_modified = str ( mtime ) , email . utils . formatdate ( mtime ) <EOL> headers = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , email . utils . formatdate ( time . time ( ) ) ) , <EOL> ( '<STR_LIT>' , last_modified ) , <EOL> ( '<STR_LIT>' , etag ) <EOL> ] <EOL> file_like = open ( full_path , '<STR_LIT:rb>' ) <EOL> return self . package_response ( file_like , environ , start_response , headers ) <EOL> class Server ( CherryPyWSGIServer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , base_dir = '<STR_LIT:.>' , port = <NUM_LIT> , uri_marker = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> ip = '<STR_LIT>' <EOL> app = make_app ( <EOL> content_path = base_dir , <EOL> uri_marker = uri_marker , <EOL> performance_settings = { <EOL> '<STR_LIT>' : True <EOL> } <EOL> ) <EOL> super ( Server , self ) . __init__ ( ( ip , int ( port ) ) , app ) </s>
