Changeset 180 for trunk

Show
Ignore:
Timestamp:
09/13/06 13:37:35 (2 years ago)
Author:
akuchling
Message:

Performance improvements for tree traversal:

  • Remove 'partialbuild' parameter to build() -- it's always False.
    The resulting dead code has been removed.
  • Create path instances only if they'll actually be used.
  • When rebuild directories are specified, only loop over them, not the
    entire input tree.

In very verbose mode, print the time required to generate a page.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/pyramid/build.py

    r177 r180  
    11#!/usr/bin/python 
    22import sys, os 
     3import time 
    34#sys.path.insert(0, os.path.join(sys.prefix, 'lib/pyramid/dependencies')) 
    45from pyramid.core import PYRAMIDSUFFIXES, DOCTYPE, Context 
     
    3536 
    3637def build(data, out, verbose=0, resourcedirs=[], rebuilddirs=None, 
    37           partialbuild=False, constants=None, update=False, 
     38          constants=None, update=False, 
    3839          relativeurls=False, prettify=False, createcache=False, keepgoing=False): 
    3940 
     
    5859 
    5960    # dangerous remove directory contents function 
    60     if partialbuild is False and rebuilddirs is None: 
     61    if rebuilddirs is None: 
    6162        utils.recursiveRemoveDirectoryContents(OUTPUTDIR) 
    6263 
     
    8788    ctx.prettify=prettify 
    8889 
    89     for root, dirs, files in os.walk('.'): 
    90  
    91         # get the path directory 
    92         ctx.path = path(root).abspath() 
    93         ctx.relpath = path(basedir).relpathto(root) 
    94         ctx.relpathparent = path(basedir).relpathto(ctx.path.parent) 
    95  
     90    path_basedir = path(basedir) 
     91 
     92    if rebuilddirs is not None: 
     93        def f (): 
     94            for p in rebuilddirs: 
     95            for t in os.walk(p): 
     96               yield t 
     97    file_generator = f() 
     98    else: 
     99    file_generator = os.walk('.') 
     100    for root, dirs, files in file_generator: 
    96101        # ignore svn folders 
    97102        try: 
     
    99104        except: 
    100105            pass 
     106 
     107        # get the path directory 
     108        ctx.path = path(root).abspath() 
    101109 
    102110        # if the target directory is not available, create it 
     
    110118                targetfile = opjoin(OUTPUTDIR, root, f) 
    111119                shutil.copy(srcfile, targetfile) 
     120 
     121        ctx.relpath = path_basedir.relpathto(root) 
     122 
     123        # check for nobuild 
     124        nobuild = path(root) / 'NOBUILD' 
     125        if nobuild.isfile(): 
     126            utils.copytree(root, opjoin(OUTPUTDIR, root), 
     127                           nocopylist=['.svn']) 
     128            continue 
    112129 
    113130        # create the html from the index.yml fragment 
     
    119136        cachepathparent = outroot.parent /'.cache.dump' 
    120137 
    121  
    122         # check to see if the current directory is in the rebuild dirs list 
    123         matchesRebuildDir = False 
    124         if rebuilddirs is not None: 
    125             for rebuilddir in rebuilddirs: 
    126                 if ctx.relpath.startswith(rebuilddir): 
    127                     matchesRebuildDir = True 
    128                     break 
    129  
    130         # check for nobuild 
    131         nobuild = path(root) / 'NOBUILD' 
    132         if nobuild.isfile(): 
    133             utils.copytree(root, opjoin(OUTPUTDIR, root), 
    134                            nocopylist=['.svn']) 
    135             continue 
    136  
    137  
    138         if os.path.exists(cachepath) and partialbuild: 
    139             ctx = pickle.load(open(cachepath)) 
    140             ctx.partialbuild = True 
     138        # Load cached data if it's available 
     139        ctx.relpathparent = path_basedir.relpathto(ctx.path.parent) 
     140        if os.path.exists(cachepathparent): 
     141            ctx.data = pickle.load(open(cachepathparent)).data 
     142 
     143        if ctx.verbose > 0: 
     144            print '.. %s Built from Source Files' % str(ctx.relpath) 
     145 
     146        ctx.partialbuild = False 
     147 
     148        try: 
     149            s = time.time() 
    141150            html = flat.flatten([DOCTYPE, 
    142                             flatten(ctx.data[ctx.relpath]['index.yml'], ctx)]) 
    143             file(opjoin(OUTPUTDIR, root, 'index.html'), 'w').write(html) 
    144         else: 
    145             if matchesRebuildDir: 
    146                 ctx.data = pickle.load(open(cachepathparent)).data 
    147  
    148             if matchesRebuildDir or rebuilddirs is None: 
    149                 if ctx.verbose > 0: 
    150                     print '.. %s Built from Source Files' % str(ctx.relpath) 
    151  
    152                 ctx.partialbuild = False 
    153  
    154                 try: 
    155                     html = flat.flatten([DOCTYPE, 
    156                             flatten(Y.fragmentConstructor( 
    157                                     opjoin(root, 'index.yml')), ctx)]) 
    158                 except KeyError: 
    159                     print '*'*80 
    160                     print 'An error has occured building the %s page' % ( 
    161                         path(opjoin(OUTPUTDIR, root, 'index.html')).abspath()) 
    162                     print 'It is likely that an item of content is missing' 
    163                     print 'or malformed.' 
    164                     print 'The current data for this page is shown above' 
    165  
    166                     sys.exit() 
    167                 except (KeyboardInterrupt, SystemExit): # roll on Python2.5 
    168                     raise 
    169                 except: 
    170                     print "While building %s:"%(opjoin(OUTPUTDIR, root)) 
    171                     if not keepgoing: 
    172                         raise 
    173                     else: 
    174                         ee, ev, et = sys.exc_info() 
    175                         traceback.print_exception(ee, ev, et, file=sys.stdout) 
    176                         print "keepgoing (-k) specified, continuing" 
    177                         continue 
    178  
    179                 file(opjoin(OUTPUTDIR, root, 'index.html'), 'w').write(html) 
    180                 if createcache: 
    181                     pickle.dump(ctx, open(cachepath, 'w')) 
     151                    flatten(Y.fragmentConstructor( 
     152                            opjoin(root, 'index.yml')), ctx)]) 
     153            if ctx.verbose > 1: 
     154                print 'Generation time:', time.time()-s 
     155        except KeyError: 
     156            print '*'*80 
     157            print 'An error has occurred building the %s page' % ( 
     158                path(opjoin(OUTPUTDIR, root, 'index.html')).abspath()) 
     159            print 'It is likely that an item of content is missing' 
     160            print 'or malformed.' 
     161            print 'The current data for this page is shown above' 
     162 
     163            sys.exit() 
     164        except (KeyboardInterrupt, SystemExit): # roll on Python2.5 
     165            raise 
     166        except: 
     167            print "While building %s:"%(opjoin(OUTPUTDIR, root)) 
     168            if not keepgoing: 
     169                raise 
    182170            else: 
    183                 if ctx.verbose > 1: 
    184                     print '.. %s Rebuilt from Cached Data' % str(ctx.relpath) 
     171                ee, ev, et = sys.exc_info() 
     172                traceback.print_exception(ee, ev, et, file=sys.stdout) 
     173                print "keepgoing (-k) specified, continuing" 
     174                continue 
     175 
     176        file(opjoin(OUTPUTDIR, root, 'index.html'), 'w').write(html) 
     177        if createcache: 
     178            pickle.dump(ctx, open(cachepath, 'w')) 
     179            print 'Writing cache file:', cachepath 
    185180 
    186181