Changeset 235


Ignore:
Timestamp:
Nov 12, 2007, 3:53:45 AM (11 years ago)
Author:
ecprice
Message:

Use the compiled Cheetah templates.

This means that you really need to run make after any change to the
template, though.

Location:
trunk/web/templates
Files:
1 added
2 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/web/templates/Makefile

    r205 r235  
    1 TEMPLATES=functions.tmpl skeleton.tmpl
     1TEMPLATES=$(wildcard *.tmpl)
    22OUTPUTS=$(TEMPLATES:.tmpl=.py)
    33
    44all: ${OUTPUTS}
    55
    6 ${OUTPUTS}:${TEMPLATES}
    7         cheetah compile $^
     6%.py: %.tmpl
     7        cheetah compile $<
     8
     9#${OUTPUTS}:${TEMPLATES}
     10#       cheetah compile $^
    811
    912clean:
  • trunk/web/templates/main.py

    r228 r235  
    3636sys.path.append('/home/ecprice/.local/lib/python2.5/site-packages')
    3737
     38import templates
    3839from Cheetah.Template import Template
    3940from sipb_xen_database import Machine, CDROM, ctx, connect
     
    4142from webcommon import InvalidInput, CodeError, g
    4243import controls
     44
     45class Checkpoint:
     46    def __init__(self):
     47        self.start_time = time.time()
     48        self.checkpoints = []
     49
     50    def checkpoint(self, s):
     51        self.checkpoints.append((s, time.time()))
     52
     53    def __str__(self):
     54        return ('Timing info:\n%s\n' %
     55                '\n'.join(['%s: %s' % (d, t - self.start_time) for
     56                           (d, t) in self.checkpoints]))
     57
     58checkpoint = Checkpoint()
     59
    4360
    4461def helppopup(subj):
     
    99116    d = dict(op=op, user=user, errorMessage=str(err),
    100117             stderr=emsg)
    101     return Template(file='error.tmpl', searchList=[d])
     118    return templates.error(searchList=[d])
    102119
    103120def invalidInput(op, user, fields, err, emsg):
     
    106123             err_value=str(err.err_value), stderr=emsg,
    107124             errorMessage=str(err))
    108     return Template(file='invalid.tmpl', searchList=[d])
     125    return templates.invalid(searchList=[d])
    109126
    110127def hasVnc(status):
     
    164181    else:
    165182        d['new_machine'] = parsed_fields['name']
    166     return Template(file='list.tmpl', searchList=[d])
     183    return templates.list(searchList=[d])
    167184
    168185
     
    170187    machines = [m for m in Machine.select()
    171188                if validation.haveAccess(user, m)]   
     189    checkpoint.checkpoint('Got my machines')
    172190    on = {}
    173191    has_vnc = {}
    174192    on = g.uptimes
     193    checkpoint.checkpoint('Got uptimes')
    175194    for m in machines:
    176195        m.uptime = g.uptimes.get(m)
     
    183202    max_memory = validation.maxMemory(user)
    184203    max_disk = validation.maxDisk(user)
     204    checkpoint.checkpoint('Got max mem/disk')
    185205    defaults = Defaults(max_memory=max_memory,
    186206                        max_disk=max_disk,
    187207                        owner=user,
    188208                        cdrom='gutsy-i386')
     209    checkpoint.checkpoint('Got defaults')
    189210    d = dict(user=user,
    190211             cant_add_vm=validation.cantAddVm(user),
     
    200221def listVms(user, fields):
    201222    """Handler for list requests."""
     223    checkpoint.checkpoint('Getting list dict')
    202224    d = getListDict(user)
    203     return Template(file='list.tmpl', searchList=[d])
     225    checkpoint.checkpoint('Got list dict')
     226    return templates.list(searchList=[d])
    204227           
    205228def vnc(user, fields):
     
    247270             hostname=os.environ.get('SERVER_NAME', 'localhost'),
    248271             authtoken=token)
    249     return Template(file='vnc.tmpl', searchList=[d])
     272    return templates.vnc(searchList=[d])
    250273
    251274def getNicInfo(data_dict, machine):
     
    303326        result = 'Success!'
    304327        if not back:
    305             return Template(file='command.tmpl', searchList=[d])
     328            return templates.command(searchList=[d])
    306329    if back == 'list':
    307330        g.clear() #Changed global state
    308331        d = getListDict(user)
    309332        d['result'] = result
    310         return Template(file='list.tmpl', searchList=[d])
     333        return templates.list(searchList=[d])
    311334    elif back == 'info':
    312335        machine = validation.testMachineId(user, fields.getfirst('machine_id'))
    313336        d = infoDict(user, machine)
    314337        d['result'] = result
    315         return Template(file='info.tmpl', searchList=[d])
    316     else:
    317         raise InvalidInput('back', back, 'Not a known back page.')
     338        return templates.info(searchList=[d])
     339    else:
     340        raise InvalidInput
     341    ('back', back, 'Not a known back page.')
    318342
    319343def modifyDict(user, fields):
     
    384408            setattr(info_dict['defaults'], field, fields.getfirst(field))
    385409    info_dict['result'] = result
    386     return Template(file='info.tmpl', searchList=[info_dict])
     410    return templates.info(searchList=[info_dict])
    387411   
    388412
     
    428452             mapping=help_mapping)
    429453   
    430     return Template(file="help.tmpl", searchList=[d])
     454    return templates.help(searchList=[d])
    431455   
    432456
     
    436460def infoDict(user, machine):
    437461    status = controls.statusInfo(machine)
     462    checkpoint.checkpoint('Getting status info')
    438463    has_vnc = hasVnc(status)
    439464    if status is None:
     
    448473        cpu_time_float = float(main_status.get('cpu_time', 0))
    449474        cputime = datetime.timedelta(seconds=int(cpu_time_float))
     475    checkpoint.checkpoint('Status')
    450476    display_fields = """name uptime memory state cpu_weight on_reboot
    451477     on_poweroff on_crash on_xend_start on_xend_stop bootloader""".split()
     
    498524            pass
    499525            #fields.append((disp, None))
    500     max_mem = validation.maxMemory(user, machine)
     526
     527    checkpoint.checkpoint('Got fields')
     528
     529
     530    max_mem = validation.maxMemory(user, machine, False)
     531    checkpoint.checkpoint('Got mem')
    501532    max_disk = validation.maxDisk(user, machine)
    502533    defaults = Defaults()
     
    504535        setattr(defaults, name, getattr(machine, name))
    505536    defaults.disk = "%0.2f" % (machine.disks[0].size/1024.)
     537    checkpoint.checkpoint('Got defaults')
    506538    d = dict(user=user,
    507539             cdroms=CDROM.select(),
     
    522554    machine = validation.testMachineId(user, fields.getfirst('machine_id'))
    523555    d = infoDict(user, machine)
    524     return Template(file='info.tmpl', searchList=[d])
     556    checkpoint.checkpoint('Got infodict')
     557    return templates.info(searchList=[d])
    525558
    526559mapping = dict(list=listVms,
     
    547580
    548581def main(operation, user, fields):   
     582    start_time = time.time()
    549583    fun = mapping.get(operation, badOperation)
    550584
     
    552586        connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen')
    553587    try:
     588        checkpoint.checkpoint('Before')
    554589        output = fun(u, fields)
     590        checkpoint.checkpoint('After')
    555591
    556592        headers = dict(DEFAULT_HEADERS)
     
    558594            new_headers, output = output
    559595            headers.update(new_headers)
    560 
    561596        e = revertStandardError()
    562597        if e:
    563598            output.addError(e)
    564599        printHeaders(headers)
    565         print output
     600        output_string =  str(output)
     601        checkpoint.checkpoint('output as a string')
     602        print output_string
     603        print '<pre>%s</pre>' % checkpoint
    566604    except Exception, err:
    567605        if not fields.has_key('js'):
     
    586624
    587625if __name__ == '__main__':
    588     start_time = time.time()
    589626    fields = cgi.FieldStorage()
    590627    u = getUser()
     
    601638        operation = 'list'
    602639
    603     main(operation, u, fields)
    604 
     640    #main(operation, u, fields)
     641    import profile
     642    profile.run('main(operation, u, fields)', 'log-'+operation)
     643
  • trunk/web/templates/skeleton.tmpl

    r228 r235  
    3131</head>
    3232<body id="body">
     33
     34#if True
     35<div>
     36<p>We are in the process of modifying the service.  Things likely will not work.</p>
     37</div>
     38#end if
    3339
    3440<div id="err">
  • trunk/web/templates/validation.py

    r234 r235  
    6868
    6969def haveAccess(user, machine):
    70     """Return whether a user has adminstrative access to a machine"""
     70    """Return whether a user has administrative access to a machine"""
    7171    if user == 'moo':
    7272        return True
Note: See TracChangeset for help on using the changeset viewer.