Changeset 51

Show
Ignore:
Timestamp:
07/24/07 13:32:44 (1 year ago)
Author:
edsuom
Message:

APIDocResource is now a standard simpleserver.http.resource

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/Twisted-Goodies/trunk/misc/var_www_foss.tellectual.com_index.py

    r49 r51  
    2424""" 
    2525 
    26 import tempfile, shutil 
     26import os.path 
    2727from textwrap import TextWrapper 
    28 import os.path as ospath 
    29 from twisted.internet import defer, utils 
    30 from twisted.web2 import static, resource 
     28from twisted.web2 import static 
    3129from twisted.python.log import msg as log 
    3230 
    33 from twisted_goodies.simpleserver.http.external import * 
     31from twisted_goodies.simpleserver.http.resources import * 
    3432 
    3533 
     
    7472 
    7573 
    76 class APIDocResource(resource.Resource): 
    77     """ 
    78     """ 
    79     def __init__(self, vhostPath, projectURLProto, projectRepoProto): 
    80         self.vhostPath = vhostPath 
    81         for name in ('projectURLProto', 'projectRepoProto'): 
    82             string = locals()[name] 
    83             if "%s" not in string: 
    84                 raise ValueError("Invalid string prototype '%s'" % string) 
    85             setattr(self, name, string) 
    86  
    87     def _executable(self, name): 
    88         result = procutils.which(name)[0] 
    89         if not result: 
    90             raise ImportError("Can't locate %s executable" % name) 
    91  
    92     def locateChild(self, request, segments): 
    93         """ 
    94         """ 
    95         def ready(null): 
    96             urlPath = "/api/%s/%s" % (projectName, apiFile) 
    97             newURL = (request.scheme, request.host, urlPath, '', '', '') 
    98             return resource.RedirectResource(*newURL), () 
    99          
    100         projectName = segments[0] 
    101         packageName = projectName.lower().replace("-", "_") 
    102         if len(segments) > 1: 
    103             apiFile = segments[1] 
    104         else: 
    105             apiFile = "%s.html" % packageName 
    106         d = defer.maybeDeferred( 
    107             self.ensureDocsPresent, projectName, packageName, apiFile) 
    108         d.addCallback(ready) 
    109         return d 
    110  
    111     def ensureDocsPresent(self, projectName, packageName, apiFile): 
    112         """ 
    113         """ 
    114         filePath = ospath.join(self.vhostPath, 'api', projectName, apiFile) 
    115         if False and not ospath.exists(filePath): 
    116             tmpDir = tempfile.mkdtemp() 
    117             d = self.svn(tmpDir, projectName) 
    118             d.addCallback(lambda _: self.pydoctor(tmpDir, projectName)) 
    119             d.addCallback(lambda _: shutil.rmtree(tmpDir)) 
    120             return d 
    121  
    122     def svn(self, tmpDir, projectName): 
    123         """ 
    124         """ 
    125         repo = self.projectRepoProto % projectName 
    126         args = [ 
    127             "export", "-q", "--force", 
    128             "%s/%s/trunk" % (repo, projectName)] 
    129         return util.getProcessValue( 
    130             self._executable('svn'), args=args, path=tmpDir) 
    131  
    132     def pydoctor(self, tmpDir, vhostPath, projectName): 
    133         """ 
    134         """ 
    135         url = self.projectURLProto % projectName 
    136         args = [ 
    137             "--add-package=%s" % packageName, 
    138             "--project-name=%s" % projectName, 
    139             "--make-html", 
    140             "--html-output=%s/api/%s" % (self.vhostPath, projectName), 
    141             "--project-url=%s%s" % (url, projectName)] 
    142         return util.getProcessValue( 
    143             self._executable('pydoctor'), args=args, path=tmpDir) 
    144  
    145  
    14674class Resource(StanResource): 
    14775    """ 
     
    178106        env = { 
    179107            'mod_python.subprocess_env':\ 
    180             {'PYTHON_EGG_CACHE': ospath.join(TRAC_DIR, 'python-eggs')}} 
     108            {'PYTHON_EGG_CACHE': os.path.join(TRAC_DIR, 'python-eggs')}} 
    181109        self.tr = TracResource(TRAC_DIR, env) 
    182110 
     
    184112        """ 
    185113        """ 
    186         staticPath = ospath.join(self.vhostPath, *pathParts) 
    187         if ospath.exists(staticPath): 
     114        staticPath = os.path.join(self.vhostPath, *pathParts) 
     115        if os.path.exists(staticPath): 
    188116            return static.File(staticPath) 
    189117 
     
    238166        if segments[0] == 'trac': 
    239167            return self.tracChild(request, segments[1:]) 
    240         return static.File(ospath.join(self.vhostPath, *segments)), () 
     168        return static.File(os.path.join(self.vhostPath, *segments)), () 
    241169 
    242170    def tracChild(self, request, segments):