mercurial/hgweb/common.py
changeset 2356 2db831b33e8f
parent 2355 eb08fb4d41e1
child 2391 d351a3be3371
equal deleted inserted replaced
2355:eb08fb4d41e1 2356:2db831b33e8f
       
     1 # hgweb.py - web interface to a mercurial repository
       
     2 #
       
     3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
       
     4 # Copyright 2005 Matt Mackall <mpm@selenic.com>
       
     5 #
       
     6 # This software may be used and distributed according to the terms
       
     7 # of the GNU General Public License, incorporated herein by reference.
       
     8 
       
     9 import os, mimetypes
       
    10 import os.path
       
    11 
       
    12 def get_mtime(repo_path):
       
    13     hg_path = os.path.join(repo_path, ".hg")
       
    14     cl_path = os.path.join(hg_path, "00changelog.i")
       
    15     if os.path.exists(os.path.join(cl_path)):
       
    16         return os.stat(cl_path).st_mtime
       
    17     else:
       
    18         return os.stat(hg_path).st_mtime
       
    19 
       
    20 def staticfile(directory, fname):
       
    21     """return a file inside directory with guessed content-type header
       
    22 
       
    23     fname always uses '/' as directory separator and isn't allowed to
       
    24     contain unusual path components.
       
    25     Content-type is guessed using the mimetypes module.
       
    26     Return an empty string if fname is illegal or file not found.
       
    27 
       
    28     """
       
    29     parts = fname.split('/')
       
    30     path = directory
       
    31     for part in parts:
       
    32         if (part in ('', os.curdir, os.pardir) or
       
    33             os.sep in part or os.altsep is not None and os.altsep in part):
       
    34             return ""
       
    35         path = os.path.join(path, part)
       
    36     try:
       
    37         os.stat(path)
       
    38         ct = mimetypes.guess_type(path)[0] or "text/plain"
       
    39         return "Content-type: %s\n\n%s" % (ct, file(path).read())
       
    40     except (TypeError, OSError):
       
    41         # illegal fname or unreadable file
       
    42         return ""