<s> import os <EOL> import stat <EOL> import urllib <EOL> import dbutils <EOL> import gitutils <EOL> import page . utils <EOL> import htmlutils <EOL> import textutils <EOL> def renderShowTree ( req , db , user ) : <EOL> cursor = db . cursor ( ) <EOL> sha1 = req . getParameter ( "<STR_LIT>" ) <EOL> path = req . getParameter ( "<STR_LIT:path>" , "<STR_LIT:/>" ) <EOL> review_id = req . getParameter ( "<STR_LIT>" , None , filter = int ) <EOL> if path [ <NUM_LIT:0> ] == '<STR_LIT:/>' : <EOL> full_path = path <EOL> if path != "<STR_LIT:/>" : path = path [ <NUM_LIT:1> : ] <EOL> else : <EOL> full_path = "<STR_LIT:/>" + path <EOL> if not path : path = "<STR_LIT:/>" <EOL> if review_id is None : <EOL> review = None <EOL> repository_arg = req . getParameter ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if repository_arg : <EOL> repository = gitutils . Repository . fromParameter ( db , repository_arg ) <EOL> else : <EOL> repository = gitutils . Repository . fromSHA1 ( db , sha1 ) <EOL> else : <EOL> review = dbutils . Review . fromId ( db , review_id ) <EOL> repository = review . repository <EOL> document = htmlutils . Document ( req ) <EOL> html = document . html ( ) <EOL> head = html . head ( ) <EOL> body = html . body ( ) <EOL> extra_links = [ ] <EOL> if review : <EOL> extra_links . append ( ( "<STR_LIT>" % review . id , "<STR_LIT>" ) ) <EOL> page . utils . generateHeader ( body , db , user , extra_links = extra_links ) <EOL> document . addExternalStylesheet ( "<STR_LIT>" ) <EOL> if review : <EOL> document . addInternalScript ( review . getJS ( ) ) <EOL> target = body . div ( "<STR_LIT>" ) <EOL> table = target . table ( "<STR_LIT>" , align = "<STR_LIT>" , cellspacing = <NUM_LIT:0> ) <EOL> table . col ( width = "<STR_LIT>" ) <EOL> table . col ( width = "<STR_LIT>" ) <EOL> table . col ( width = "<STR_LIT>" ) <EOL> thead = table . thead ( ) <EOL> h1 = thead . tr ( ) . td ( '<STR_LIT>' , colspan = <NUM_LIT:3> ) . h1 ( ) <EOL> def make_url ( url_path , path ) : <EOL> params = { "<STR_LIT>" : sha1 , <EOL> "<STR_LIT:path>" : path } <EOL> if review is None : <EOL> params [ "<STR_LIT>" ] = str ( repository . id ) <EOL> else : <EOL> params [ "<STR_LIT>" ] = str ( review . id ) <EOL> return "<STR_LIT>" % ( url_path , urllib . urlencode ( params ) ) <EOL> if path == "<STR_LIT:/>" : <EOL> h1 . text ( "<STR_LIT:/>" ) <EOL> else : <EOL> h1 . a ( "<STR_LIT:root>" , href = make_url ( "<STR_LIT>" , "<STR_LIT:/>" ) ) . text ( "<STR_LIT:root>" ) <EOL> h1 . span ( ) . text ( '<STR_LIT:/>' ) <EOL> components = path . split ( "<STR_LIT:/>" ) <EOL> for index , component in enumerate ( components [ : - <NUM_LIT:1> ] ) : <EOL> h1 . a ( href = make_url ( "<STR_LIT>" , "<STR_LIT:/>" . join ( components [ : index + <NUM_LIT:1> ] ) ) ) . text ( component , escape = True ) <EOL> h1 . span ( ) . text ( '<STR_LIT:/>' ) <EOL> h1 . text ( components [ - <NUM_LIT:1> ] , escape = True ) <EOL> row = thead . tr ( ) <EOL> row . td ( '<STR_LIT>' ) . text ( "<STR_LIT>" ) <EOL> row . td ( '<STR_LIT:name>' ) . text ( "<STR_LIT:Name>" ) <EOL> row . td ( '<STR_LIT:size>' ) . text ( "<STR_LIT>" ) <EOL> tree = gitutils . Tree . fromPath ( gitutils . Commit . fromSHA1 ( db , repository , sha1 ) , full_path ) <EOL> if tree is None : <EOL> raise page . utils . DisplayMessage ( <EOL> title = "<STR_LIT>" , <EOL> body = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( htmlutils . htmlify ( textutils . escape ( full_path ) ) , <EOL> htmlutils . htmlify ( repository . name ) , <EOL> htmlutils . htmlify ( sha1 ) , htmlutils . htmlify ( sha1 [ : <NUM_LIT:8> ] ) ) ) , <EOL> html = True ) <EOL> def compareEntries ( a , b ) : <EOL> if a . type != b . type : <EOL> if a . type == "<STR_LIT>" : return - <NUM_LIT:1> <EOL> else : return <NUM_LIT:1> <EOL> else : <EOL> return cmp ( a . name , b . name ) <EOL> tbody = table . tbody ( ) <EOL> for entry in sorted ( tree , cmp = compareEntries ) : <EOL> if entry . type in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if entry . type == "<STR_LIT>" : <EOL> url_path = "<STR_LIT>" <EOL> else : <EOL> url_path = "<STR_LIT>" <EOL> url = make_url ( url_path , os . path . join ( path , entry . name ) ) <EOL> else : <EOL> url = None <EOL> row = tbody . tr ( entry . type ) <EOL> row . td ( '<STR_LIT>' ) . text ( str ( entry . mode ) ) <EOL> if stat . S_ISLNK ( entry . mode ) : <EOL> cell = row . td ( '<STR_LIT>' , colspan = <NUM_LIT:2> ) <EOL> cell . span ( '<STR_LIT:name>' ) . text ( entry . name , escape = True ) <EOL> cell . text ( '<STR_LIT>' ) <EOL> cell . span ( '<STR_LIT:target>' ) . text ( repository . fetch ( entry . sha1 ) . data ) <EOL> elif entry . type == "<STR_LIT>" : <EOL> row . td ( '<STR_LIT:name>' ) . text ( "<STR_LIT>" % ( entry . name , entry . sha1 ) , escape = True ) <EOL> row . td ( '<STR_LIT:size>' ) . text ( entry . size ) <EOL> else : <EOL> row . td ( '<STR_LIT:name>' ) . a ( href = url ) . text ( entry . name , escape = True ) <EOL> row . td ( '<STR_LIT:size>' ) . text ( entry . size ) <EOL> return document </s>
