Exemplo n.º 1
0
 def _unlock(self, response):
     if response and response.session_file and response.session_locked:
         try:
             portalocker.unlock(response.session_file)
             response.session_locked = False
         except:  # this should never happen but happens in Windows
             pass
Exemplo n.º 2
0
 def _unlock(self, response):
     if response and response.session_file and response.session_locked:
         try:
             portalocker.unlock(response.session_file)
             response.session_locked = False
         except:  # this should never happen but happens in Windows
             pass
Exemplo n.º 3
0
def write_hosts_deny(denied_hosts):
    f = open(deny_file, 'w')
    portalocker.lock(f, portalocker.LOCK_EX)
    for key, val in denied_hosts.items():
        if time.time()-val[1] < expiration_failed_logins:
            line = '%s %s %s\n' % (key, val[0], val[1])
            f.write(line)
    portalocker.unlock(f)
    f.close()
Exemplo n.º 4
0
def write_hosts_deny(denied_hosts):
    f = open(deny_file, 'w')
    portalocker.lock(f, portalocker.LOCK_EX)
    for key, val in denied_hosts.items():
        if time.time() - val[1] < expiration_failed_logins:
            line = '%s %s %s\n' % (key, val[0], val[1])
            f.write(line)
    portalocker.unlock(f)
    f.close()
Exemplo n.º 5
0
 def release(self):
     """
     this function writes into cron.msater the time when cron job 
     was completed
     """
     if not self.master.closed:
         portalocker.lock(self.master,portalocker.LOCK_EX)        
         logging.debug('WEB2PY CRON: Releasing cron lock')
         self.master.seek(0)
         (start, stop) =  cPickle.load(self.master)
         if start == self.now: # if this is my lock
             self.master.seek(0)
             cPickle.dump((self.now,time.time()),self.master)
         portalocker.unlock(self.master)
         self.master.close()
 def release(self):
     """
     this function writes into cron.master the time when cron job
     was completed
     """
     if not self.master.closed:
         portalocker.lock(self.master, portalocker.LOCK_EX)
         logger.debug('WEB2PY CRON: Releasing cron lock')
         self.master.seek(0)
         (start, stop) = cPickle.load(self.master)
         if start == self.now:  # if this is my lock
             self.master.seek(0)
             cPickle.dump((self.now, time.time()), self.master)
         portalocker.unlock(self.master)
         self.master.close()
Exemplo n.º 7
0
def read_hosts_deny():
    import datetime
    hosts = {}
    if os.path.exists(deny_file):
        hosts = {}
        f = open(deny_file, 'r')
        portalocker.lock(f, portalocker.LOCK_SH)
        for line in f.readlines():
            if not line.strip() or line.startswith('#'):
                continue
            fields = line.strip().split()
            if len(fields) > 2:
                hosts[fields[0].strip()] = ( # ip
                    int(fields[1].strip()),  # n attemps
                    int(fields[2].strip())   # last attempts
                    )
        portalocker.unlock(f)
        f.close()
    return hosts
Exemplo n.º 8
0
def read_hosts_deny():
    import datetime
    hosts = {}
    if os.path.exists(deny_file):
        hosts = {}
        f = open(deny_file, 'r')
        portalocker.lock(f, portalocker.LOCK_SH)
        for line in f.readlines():
            if not line.strip() or line.startswith('#'):
                continue
            fields = line.strip().split()
            if len(fields) > 2:
                hosts[fields[0].strip()] = (  # ip
                    int(fields[1].strip()),  # n attemps
                    int(fields[2].strip())   # last attempts
                    )
        portalocker.unlock(f)
        f.close()
    return hosts
    def acquire(self, startup=False):
        """
        returns the time when the lock is acquired or
        None if cron already running

        lock is implemented by writing a pickle (start, stop) in cron.master
        start is time when cron job starts and stop is time when cron completed
        stop == 0 if job started but did not yet complete
        if a cron job started within less than 60 seconds, acquire returns None
        if a cron job started before 60 seconds and did not stop,
        a warning is issue "Stale cron.master detected"
        """
        if portalocker.LOCK_EX == None:
            logger.warning('WEB2PY CRON: Disabled because no file locking')
            return None
        self.master = open(self.path, 'rb+')
        try:
            ret = None
            portalocker.lock(self.master, portalocker.LOCK_EX)
            try:
                (start, stop) = cPickle.load(self.master)
            except:
                (start, stop) = (0, 1)
            if startup or self.now - start > 59.99:
                ret = self.now
                if not stop:
                    # this happens if previous cron job longer than 1 minute
                    logger.warning('WEB2PY CRON: Stale cron.master detected')
                logger.debug('WEB2PY CRON: Acquiring lock')
                self.master.seek(0)
                cPickle.dump((self.now, 0), self.master)
        finally:
            portalocker.unlock(self.master)
        if not ret:
            # do this so no need to release
            self.master.close()
        return ret
Exemplo n.º 10
0
 def acquire(self,startup=False):
     """
     returns the time when the lock is acquired or 
     None if cron already runing
     
     lock is implemnted by writing a pickle (start, stop) in cron.master
     start is time when cron job starts and stop is time when cron completed
     stop == 0 if job started but did not yet complete
     if a cron job started within less than 60 secods, acquire returns None
     if a cron job started before 60 seconds and did not stop, 
     a warning is issue "Stale cron.master detected"
     """
     if portalocker.LOCK_EX == None:
         logging.warning('WEB2PY CRON: Disabled because no file locking')
         return None
     self.master = open(self.path,'rb+')        
     try:
         ret = None
         portalocker.lock(self.master,portalocker.LOCK_EX)
         try:
             (start, stop) =  cPickle.load(self.master)
         except:
             (start, stop) = (0, 1)
         if startup or self.now - start >= 60:
             ret = self.now
             if not stop:
                 # this happens if previous cron job longer than 1 minute
                 logging.warning('WEB2PY CRON: Stale cron.master detected')
             logging.debug('WEB2PY CRON: Acquiring lock')
             self.master.seek(0)
             cPickle.dump((self.now,0),self.master)
     finally:
         portalocker.unlock(self.master)
     if not ret:
         # do this so no need to release
         self.master.close()
     return ret
def ccache():
    form = FORM(
        P(TAG.BUTTON("Clear CACHE?", _type="submit", _name="yes", _value="yes")),
        P(TAG.BUTTON("Clear RAM", _type="submit", _name="ram", _value="ram")),
        P(TAG.BUTTON("Clear DISK", _type="submit", _name="disk", _value="disk")),
    )
    
    if form.accepts(request.vars, session):
        clear_ram = False
        clear_disk = False
        session.flash = ""
        if request.vars.yes:
            clear_ram = clear_disk = True
        if request.vars.ram:
            clear_ram = True
        if request.vars.disk:
            clear_disk = True
            
        if clear_ram:
            cache.ram.clear()
            session.flash += "Ram Cleared "
        if clear_disk:
            cache.disk.clear()
            session.flash += "Disk Cleared"
            
        redirect(URL(r=request))
    
    try:
        from guppy import hpy; hp=hpy()
    except:
        hp = False
        
    import shelve, os, copy, time, math
    from gluon import portalocker
    
    ram = {
        'bytes': 0,
        'objects': 0,
        'hits': 0,
        'misses': 0,
        'ratio': 0,
        'oldest': time.time()
    }
    disk = copy.copy(ram)
    total = copy.copy(ram)
    
    for key, value in cache.ram.storage.items():
        if isinstance(value, dict):
            ram['hits'] = value['hit_total'] - value['misses']
            ram['misses'] = value['misses']
            try:
                ram['ratio'] = ram['hits'] * 100 / value['hit_total']
            except:
                ram['ratio'] = 0
        else:
            if hp:
                ram['bytes'] += hp.iso(value[1]).size
                ram['objects'] += hp.iso(value[1]).count
                
                if value[0] < ram['oldest']:
                    ram['oldest'] = value[0]
    
    locker = open(os.path.join(request.folder,
                                        'cache/cache.lock'), 'a')
    portalocker.lock(locker, portalocker.LOCK_EX)
    disk_storage = shelve.open(
        os.path.join(request.folder,
                'cache/cache.shelve'))
    
    for key, value in disk_storage.items():
        if isinstance(value, dict):
            disk['hits'] = value['hit_total'] - value['misses']
            disk['misses'] = value['misses']
            try:
                disk['ratio'] = disk['hits'] * 100 / value['hit_total']
            except:
                disk['ratio'] = 0
        else:
            if hp:
                disk['bytes'] += hp.iso(value[1]).size
                disk['objects'] += hp.iso(value[1]).count
                if value[0] < disk['oldest']:
                    disk['oldest'] = value[0]
        
    portalocker.unlock(locker)
    locker.close()
    disk_storage.close()        
    
    total['bytes'] = ram['bytes'] + disk['bytes']
    total['objects'] = ram['objects'] + disk['objects']
    total['hits'] = ram['hits'] + disk['hits']
    total['misses'] = ram['misses'] + disk['misses']
    total['ratio'] = (ram['ratio'] + disk['ratio']) / 2
    if disk['oldest'] < ram['oldest']:
        total['oldest'] = disk['oldest']
    else:
        total['oldest'] = ram['oldest']
    
    def GetInHMS(seconds):
        hours = math.floor(seconds / 3600)
        seconds -= hours * 3600
        minutes = math.floor(seconds / 60)
        seconds -= minutes * 60
        seconds = math.floor(seconds)
        
        return (hours, minutes, seconds)

    ram['oldest'] = GetInHMS(time.time() - ram['oldest'])
    disk['oldest'] = GetInHMS(time.time() - disk['oldest'])
    total['oldest'] = GetInHMS(time.time() - total['oldest'])
    
    return dict(form=form, total=total,
                ram=ram, disk=disk)
Exemplo n.º 12
0
def ccache():
    form = FORM(
        P(TAG.BUTTON("Clear CACHE?", _type="submit", _name="yes", _value="yes")),
        P(TAG.BUTTON("Clear RAM", _type="submit", _name="ram", _value="ram")),
        P(TAG.BUTTON("Clear DISK", _type="submit", _name="disk", _value="disk")),
    )

    if form.accepts(request.vars, session):
        clear_ram = False
        clear_disk = False
        session.flash = ""
        if request.vars.yes:
            clear_ram = clear_disk = True
        if request.vars.ram:
            clear_ram = True
        if request.vars.disk:
            clear_disk = True

        if clear_ram:
            cache.ram.clear()
            session.flash += "Ram Cleared "
        if clear_disk:
            cache.disk.clear()
            session.flash += "Disk Cleared"

        redirect(URL(r=request))

    try:
        from guppy import hpy
        hp = hpy()
    except ImportError:
        hp = False

    import shelve
    import os
    import copy
    import time
    import math
    from gluon import portalocker

    ram = {
        'entries': 0,
        'bytes': 0,
        'objects': 0,
        'hits': 0,
        'misses': 0,
        'ratio': 0,
        'oldest': time.time(),
        'keys': []
    }
    disk = copy.copy(ram)
    total = copy.copy(ram)
    disk['keys'] = []
    total['keys'] = []

    def GetInHMS(seconds):
        hours = math.floor(seconds / 3600)
        seconds -= hours * 3600
        minutes = math.floor(seconds / 60)
        seconds -= minutes * 60
        seconds = math.floor(seconds)

        return (hours, minutes, seconds)

    for key, value in cache.ram.storage.items():
        if isinstance(value, dict):
            ram['hits'] = value['hit_total'] - value['misses']
            ram['misses'] = value['misses']
            try:
                ram['ratio'] = ram['hits'] * 100 / value['hit_total']
            except (KeyError, ZeroDivisionError):
                ram['ratio'] = 0
        else:
            if hp:
                ram['bytes'] += hp.iso(value[1]).size
                ram['objects'] += hp.iso(value[1]).count
            ram['entries'] += 1
            if value[0] < ram['oldest']:
                ram['oldest'] = value[0]
            ram['keys'].append((key, GetInHMS(time.time() - value[0])))

    locker = open(os.path.join(request.folder,
                                        'cache/cache.lock'), 'a')
    portalocker.lock(locker, portalocker.LOCK_EX)
    disk_storage = shelve.open(os.path.join(request.folder, 'cache/cache.shelve'))
    try:
        for key, value in disk_storage.items():
            if isinstance(value, dict):
                disk['hits'] = value['hit_total'] - value['misses']
                disk['misses'] = value['misses']
                try:
                    disk['ratio'] = disk['hits'] * 100 / value['hit_total']
                except (KeyError, ZeroDivisionError):
                    disk['ratio'] = 0
            else:
                if hp:
                    disk['bytes'] += hp.iso(value[1]).size
                    disk['objects'] += hp.iso(value[1]).count
                disk['entries'] += 1
                if value[0] < disk['oldest']:
                    disk['oldest'] = value[0]
                disk['keys'].append((key, GetInHMS(time.time() - value[0])))

    finally:
        portalocker.unlock(locker)
        locker.close()
        disk_storage.close()

    total['entries'] = ram['entries'] + disk['entries']
    total['bytes'] = ram['bytes'] + disk['bytes']
    total['objects'] = ram['objects'] + disk['objects']
    total['hits'] = ram['hits'] + disk['hits']
    total['misses'] = ram['misses'] + disk['misses']
    total['keys'] = ram['keys'] + disk['keys']
    try:
        total['ratio'] = total['hits'] * 100 / (total['hits'] + total['misses'])
    except (KeyError, ZeroDivisionError):
        total['ratio'] = 0

    if disk['oldest'] < ram['oldest']:
        total['oldest'] = disk['oldest']
    else:
        total['oldest'] = ram['oldest']

    ram['oldest'] = GetInHMS(time.time() - ram['oldest'])
    disk['oldest'] = GetInHMS(time.time() - disk['oldest'])
    total['oldest'] = GetInHMS(time.time() - total['oldest'])

    def key_table(keys):
        return TABLE(
            TR(TD(B('Key')), TD(B('Time in Cache (h:m:s)'))),
            *[TR(TD(k[0]), TD('%02d:%02d:%02d' % k[1])) for k in keys],
            **dict(_class='cache-keys',
                   _style="border-collapse: separate; border-spacing: .5em;"))

    ram['keys'] = key_table(ram['keys'])
    disk['keys'] = key_table(disk['keys'])
    total['keys'] = key_table(total['keys'])

    return dict(form=form, total=total,
                ram=ram, disk=disk, object_stats=hp != False)
Exemplo n.º 13
0
def ccache():
    form = FORM(
        P(
            TAG.BUTTON(T("Clear CACHE?"),
                       _type="submit",
                       _name="yes",
                       _value="yes")),
        P(TAG.BUTTON(T("Clear RAM"), _type="submit", _name="ram",
                     _value="ram")),
        P(
            TAG.BUTTON(T("Clear DISK"),
                       _type="submit",
                       _name="disk",
                       _value="disk")),
    )

    if form.accepts(request.vars, session):
        clear_ram = False
        clear_disk = False
        session.flash = ""
        if request.vars.yes:
            clear_ram = clear_disk = True
        if request.vars.ram:
            clear_ram = True
        if request.vars.disk:
            clear_disk = True

        if clear_ram:
            cache.ram.clear()
            session.flash += T("Ram Cleared")
        if clear_disk:
            cache.disk.clear()
            session.flash += T("Disk Cleared")

        redirect(URL(r=request))

    try:
        from guppy import hpy
        hp = hpy()
    except ImportError:
        hp = False

    import shelve, os, copy, time, math
    from gluon import portalocker

    ram = {
        'entries': 0,
        'bytes': 0,
        'objects': 0,
        'hits': 0,
        'misses': 0,
        'ratio': 0,
        'oldest': time.time(),
        'keys': []
    }
    disk = copy.copy(ram)
    total = copy.copy(ram)
    disk['keys'] = []
    total['keys'] = []

    def GetInHMS(seconds):
        hours = math.floor(seconds / 3600)
        seconds -= hours * 3600
        minutes = math.floor(seconds / 60)
        seconds -= minutes * 60
        seconds = math.floor(seconds)

        return (hours, minutes, seconds)

    for key, value in cache.ram.storage.items():
        if isinstance(value, dict):
            ram['hits'] = value['hit_total'] - value['misses']
            ram['misses'] = value['misses']
            try:
                ram['ratio'] = ram['hits'] * 100 / value['hit_total']
            except (KeyError, ZeroDivisionError):
                ram['ratio'] = 0
        else:
            if hp:
                ram['bytes'] += hp.iso(value[1]).size
                ram['objects'] += hp.iso(value[1]).count
            ram['entries'] += 1
            if value[0] < ram['oldest']:
                ram['oldest'] = value[0]
            ram['keys'].append((key, GetInHMS(time.time() - value[0])))

    locker = open(os.path.join(request.folder, 'cache/cache.lock'), 'a')
    portalocker.lock(locker, portalocker.LOCK_EX)
    disk_storage = shelve.open(
        os.path.join(request.folder, 'cache/cache.shelve'))
    try:
        for key, value in disk_storage.items():
            if isinstance(value, dict):
                disk['hits'] = value['hit_total'] - value['misses']
                disk['misses'] = value['misses']
                try:
                    disk['ratio'] = disk['hits'] * 100 / value['hit_total']
                except (KeyError, ZeroDivisionError):
                    disk['ratio'] = 0
            else:
                if hp:
                    disk['bytes'] += hp.iso(value[1]).size
                    disk['objects'] += hp.iso(value[1]).count
                disk['entries'] += 1
                if value[0] < disk['oldest']:
                    disk['oldest'] = value[0]
                disk['keys'].append((key, GetInHMS(time.time() - value[0])))

    finally:
        portalocker.unlock(locker)
        locker.close()
        disk_storage.close()

    total['entries'] = ram['entries'] + disk['entries']
    total['bytes'] = ram['bytes'] + disk['bytes']
    total['objects'] = ram['objects'] + disk['objects']
    total['hits'] = ram['hits'] + disk['hits']
    total['misses'] = ram['misses'] + disk['misses']
    total['keys'] = ram['keys'] + disk['keys']
    try:
        total['ratio'] = total['hits'] * 100 / (total['hits'] +
                                                total['misses'])
    except (KeyError, ZeroDivisionError):
        total['ratio'] = 0

    if disk['oldest'] < ram['oldest']:
        total['oldest'] = disk['oldest']
    else:
        total['oldest'] = ram['oldest']

    ram['oldest'] = GetInHMS(time.time() - ram['oldest'])
    disk['oldest'] = GetInHMS(time.time() - disk['oldest'])
    total['oldest'] = GetInHMS(time.time() - total['oldest'])

    def key_table(keys):
        return TABLE(
            TR(TD(B(T('Key'))), TD(B(T('Time in Cache (h:m:s)')))),
            *[TR(TD(k[0]), TD('%02d:%02d:%02d' % k[1])) for k in keys],
            **dict(_class='cache-keys',
                   _style="border-collapse: separate; border-spacing: .5em;"))

    ram['keys'] = key_table(ram['keys'])
    disk['keys'] = key_table(disk['keys'])
    total['keys'] = key_table(total['keys'])

    return dict(form=form,
                total=total,
                ram=ram,
                disk=disk,
                object_stats=hp != False)
Exemplo n.º 14
0
def ccache():
    form = FORM(
        P(TAG.BUTTON("Clear CACHE?", _type="submit", _name="yes", _value="yes")),
        P(TAG.BUTTON("Clear RAM", _type="submit", _name="ram", _value="ram")),
        P(TAG.BUTTON("Clear DISK", _type="submit", _name="disk", _value="disk")),
    )

    if form.accepts(request.vars, session):
        clear_ram = False
        clear_disk = False
        session.flash = ""
        if request.vars.yes:
            clear_ram = clear_disk = True
        if request.vars.ram:
            clear_ram = True
        if request.vars.disk:
            clear_disk = True

        if clear_ram:
            cache.ram.clear()
            session.flash += "Ram Cleared "
        if clear_disk:
            cache.disk.clear()
            session.flash += "Disk Cleared"

        redirect(URL(r=request))

    try:
        from guppy import hpy; hp=hpy()
    except ImportError:
        hp = False

    import shelve, os, copy, time, math
    from gluon import portalocker

    ram = {
        'bytes': 0,
        'objects': 0,
        'hits': 0,
        'misses': 0,
        'ratio': 0,
        'oldest': time.time()
    }
    disk = copy.copy(ram)
    total = copy.copy(ram)

    for key, value in cache.ram.storage.items():
        if isinstance(value, dict):
            ram['hits'] = value['hit_total'] - value['misses']
            ram['misses'] = value['misses']
            try:
                ram['ratio'] = ram['hits'] * 100 / value['hit_total']
            except (KeyError, ZeroDivisionError):
                ram['ratio'] = 0
        else:
            if hp:
                ram['bytes'] += hp.iso(value[1]).size
                ram['objects'] += hp.iso(value[1]).count

                if value[0] < ram['oldest']:
                    ram['oldest'] = value[0]

    locker = open(os.path.join(request.folder,
                                        'cache/cache.lock'), 'a')
    portalocker.lock(locker, portalocker.LOCK_EX)
    disk_storage = shelve.open(os.path.join(request.folder, 'cache/cache.shelve'))
    try:
        for key, value in disk_storage.items():
            if isinstance(value, dict):
                disk['hits'] = value['hit_total'] - value['misses']
                disk['misses'] = value['misses']
                try:
                    disk['ratio'] = disk['hits'] * 100 / value['hit_total']
                except (KeyError, ZeroDivisionError):
                    disk['ratio'] = 0
            else:
                if hp:
                    disk['bytes'] += hp.iso(value[1]).size
                    disk['objects'] += hp.iso(value[1]).count
                    if value[0] < disk['oldest']:
                        disk['oldest'] = value[0]
    finally:
        portalocker.unlock(locker)
        locker.close()
        disk_storage.close()

    total['bytes'] = ram['bytes'] + disk['bytes']
    total['objects'] = ram['objects'] + disk['objects']
    total['hits'] = ram['hits'] + disk['hits']
    total['misses'] = ram['misses'] + disk['misses']
    try:
        total['ratio'] = total['hits'] * 100 / (total['hits'] + total['misses'])
    except (KeyError, ZeroDivisionError):
        total['ratio'] = 0

    if disk['oldest'] < ram['oldest']:
        total['oldest'] = disk['oldest']
    else:
        total['oldest'] = ram['oldest']

    def GetInHMS(seconds):
        hours = math.floor(seconds / 3600)
        seconds -= hours * 3600
        minutes = math.floor(seconds / 60)
        seconds -= minutes * 60
        seconds = math.floor(seconds)

        return (hours, minutes, seconds)

    ram['oldest'] = GetInHMS(time.time() - ram['oldest'])
    disk['oldest'] = GetInHMS(time.time() - disk['oldest'])
    total['oldest'] = GetInHMS(time.time() - total['oldest'])

    return dict(form=form, total=total,
                ram=ram, disk=disk)
Exemplo n.º 15
0
def system():
    import os
    form = FORM(
        P(TAG.BUTTON("Clear Sessions?", _type="submit", _id = "sessions", _name="sessions", _value="sessions")),
        P(TAG.BUTTON("Clear Errors?", _type="submit", _id = "errors", _name="errors", _value="errors")),
        P(TAG.BUTTON("Clear RAM Cache", _type="submit", _id = "ram", _name="ram", _value="ram")),
        P(TAG.BUTTON("Clear Disk Cache", _type="submit", _id = "disk", _name="disk", _value="disk")),
        P(TAG.BUTTON("Clear All Cache?", _type="submit", _id = "cache", _name="cache", _value="cache"))
    )
    
    if form.accepts(request.vars, session):
        if request.vars.sessions:
	    sessions_path = os.path.join(request.folder, 'sessions')
	    for file_path in os.listdir(sessions_path):
		session_path = os.path.join(request.folder, 'sessions', file_path)
                os.unlink(session_path)
    	    redirect(URL(a=request.application, c='default', f='index'))

        if request.vars.errors:
	    errors_path = os.path.join(request.folder, 'errors')
	    for file_path in os.listdir(errors_path):
		error_path = os.path.join(request.folder, 'errors', file_path)
                os.unlink(error_path)

        clear_ram = False
        clear_disk = False

        if request.vars.cache:
            clear_ram = clear_disk = True
        if request.vars.ram:
            clear_ram = True
        if request.vars.disk:
            clear_disk = True
            
        if clear_ram:
            cache.ram.clear()
        if clear_disk:
            cache.disk.clear()
	    
    try:
        from guppy import hpy; hp=hpy()
    except ImportError:
        hp = False
        
    import shelve, os, copy, time, math
    from gluon import portalocker
    
    ram = {
        'bytes': 0,
        'objects': 0,
        'hits': 0,
        'misses': 0,
        'ratio': 0,
        'oldest': time.time()
    }
    disk = copy.copy(ram)
    total = copy.copy(ram)
    
    for key, value in cache.ram.storage.items():
        if isinstance(value, dict):
            ram['hits'] = value['hit_total'] - value['misses']
            ram['misses'] = value['misses']
            try:
                ram['ratio'] = ram['hits'] * 100 / value['hit_total']
            except (KeyError, ZeroDivisionError):
                ram['ratio'] = 0
        else:
            if hp:
                ram['bytes'] += hp.iso(value[1]).size
                ram['objects'] += hp.iso(value[1]).count
                
                if value[0] < ram['oldest']:
                    ram['oldest'] = value[0]
    
    locker = open(os.path.join(request.folder,
                                        'cache/cache.lock'), 'a')
    portalocker.lock(locker, portalocker.LOCK_EX)
    disk_storage = shelve.open(
        os.path.join(request.folder,
                'cache/cache.shelve'))
    
    for key, value in disk_storage.items():
        if isinstance(value, dict):
            disk['hits'] = value['hit_total'] - value['misses']
            disk['misses'] = value['misses']
            try:
                disk['ratio'] = disk['hits'] * 100 / value['hit_total']
            except (KeyError, ZeroDivisionError):
                disk['ratio'] = 0
        else:
            if hp:
                disk['bytes'] += hp.iso(value[1]).size
                disk['objects'] += hp.iso(value[1]).count
                if value[0] < disk['oldest']:
                    disk['oldest'] = value[0]
        
    portalocker.unlock(locker)
    locker.close()
    disk_storage.close()        
    
    total['bytes'] = ram['bytes'] + disk['bytes']
    total['objects'] = ram['objects'] + disk['objects']
    total['hits'] = ram['hits'] + disk['hits']
    total['misses'] = ram['misses'] + disk['misses']
    try:
        total['ratio'] = total['hits'] * 100 / (total['hits'] + total['misses'])
    except (KeyError, ZeroDivisionError):
        total['ratio'] = 0
    
    if disk['oldest'] < ram['oldest']:
        total['oldest'] = disk['oldest']
    else:
        total['oldest'] = ram['oldest']
    
    def GetInHMS(seconds):
        hours = math.floor(seconds / 3600)
        seconds -= hours * 3600
        minutes = math.floor(seconds / 60)
        seconds -= minutes * 60
        seconds = math.floor(seconds)
        
        return (hours, minutes, seconds)

    ram['oldest'] = GetInHMS(time.time() - ram['oldest'])
    disk['oldest'] = GetInHMS(time.time() - disk['oldest'])
    total['oldest'] = GetInHMS(time.time() - total['oldest'])
    
    return dict(form=form, total=total, ram=ram, disk=disk)
Exemplo n.º 16
0
                    disk['hits'] = value['hit_total'] - value['misses']
                    disk['misses'] = value['misses']
                    try:
                        disk['ratio'] = disk['hits'] * 100 / value['hit_total']
                    except (KeyError, ZeroDivisionError):
                        disk['ratio'] = 0
                else:
                    if hp:
                        disk['bytes'] += hp.iso(value[1]).size
                        disk['objects'] += hp.iso(value[1]).count
                    disk['entries'] += 1
                    if value[0] < disk['oldest']:
                        disk['oldest'] = value[0]
                    disk['keys'].append((key, GetInHMS(time.time() - value[0])))
        finally:
            portalocker.unlock(locker)
            locker.close()
            disk_storage.close()

        total['entries'] = ram['entries'] + disk['entries']
        total['bytes'] = ram['bytes'] + disk['bytes']
        total['objects'] = ram['objects'] + disk['objects']
        total['hits'] = ram['hits'] + disk['hits']
        total['misses'] = ram['misses'] + disk['misses']
        total['keys'] = ram['keys'] + disk['keys']
        try:
            total['ratio'] = total['hits'] * 100 / (total['hits'] +
                                                total['misses'])
        except (KeyError, ZeroDivisionError):
            total['ratio'] = 0
Exemplo n.º 17
0
def unlock(locker):
    portalocker.unlock(locker)
    locker.close()
Exemplo n.º 18
0
def ccache():
    cache.ram.initialize()
    cache.disk.initialize()

    form = FORM(
        P(TAG.BUTTON(T("Clear CACHE?"), _type="submit", _name="yes", _value="yes")),
        P(TAG.BUTTON(T("Clear RAM"), _type="submit", _name="ram", _value="ram")),
        P(TAG.BUTTON(T("Clear DISK"), _type="submit", _name="disk", _value="disk")),
    )

    if form.accepts(request.vars, session):
        clear_ram = False
        clear_disk = False
        session.flash = ""
        if request.vars.yes:
            clear_ram = clear_disk = True
        if request.vars.ram:
            clear_ram = True
        if request.vars.disk:
            clear_disk = True

        if clear_ram:
            cache.ram.clear()
            session.flash += T("Ram Cleared")
        if clear_disk:
            cache.disk.clear()
            session.flash += T("Disk Cleared")

        redirect(URL(r=request))

    try:
        from guppy import hpy

        hp = hpy()
    except ImportError:
        hp = False

    import shelve
    import os
    import copy
    import time
    import math
    from gluon import portalocker

    ram = {
        "entries": 0,
        "bytes": 0,
        "objects": 0,
        "hits": 0,
        "misses": 0,
        "ratio": 0,
        "oldest": time.time(),
        "keys": [],
    }
    disk = copy.copy(ram)
    total = copy.copy(ram)
    disk["keys"] = []
    total["keys"] = []

    def GetInHMS(seconds):
        hours = math.floor(seconds / 3600)
        seconds -= hours * 3600
        minutes = math.floor(seconds / 60)
        seconds -= minutes * 60
        seconds = math.floor(seconds)

        return (hours, minutes, seconds)

    for key, value in cache.ram.storage.iteritems():
        if isinstance(value, dict):
            ram["hits"] = value["hit_total"] - value["misses"]
            ram["misses"] = value["misses"]
            try:
                ram["ratio"] = ram["hits"] * 100 / value["hit_total"]
            except (KeyError, ZeroDivisionError):
                ram["ratio"] = 0
        else:
            if hp:
                ram["bytes"] += hp.iso(value[1]).size
                ram["objects"] += hp.iso(value[1]).count
            ram["entries"] += 1
            if value[0] < ram["oldest"]:
                ram["oldest"] = value[0]
            ram["keys"].append((key, GetInHMS(time.time() - value[0])))
    folder = os.path.join(request.folder, "cache")
    if not os.path.exists(folder):
        os.mkdir(folder)
    locker = open(os.path.join(folder, "cache.lock"), "a")
    portalocker.lock(locker, portalocker.LOCK_EX)
    disk_storage = shelve.open(os.path.join(folder, "cache.shelve"))
    try:
        for key, value in disk_storage.items():
            if isinstance(value, dict):
                disk["hits"] = value["hit_total"] - value["misses"]
                disk["misses"] = value["misses"]
                try:
                    disk["ratio"] = disk["hits"] * 100 / value["hit_total"]
                except (KeyError, ZeroDivisionError):
                    disk["ratio"] = 0
            else:
                if hp:
                    disk["bytes"] += hp.iso(value[1]).size
                    disk["objects"] += hp.iso(value[1]).count
                disk["entries"] += 1
                if value[0] < disk["oldest"]:
                    disk["oldest"] = value[0]
                disk["keys"].append((key, GetInHMS(time.time() - value[0])))

    finally:
        portalocker.unlock(locker)
        locker.close()
        disk_storage.close()

    total["entries"] = ram["entries"] + disk["entries"]
    total["bytes"] = ram["bytes"] + disk["bytes"]
    total["objects"] = ram["objects"] + disk["objects"]
    total["hits"] = ram["hits"] + disk["hits"]
    total["misses"] = ram["misses"] + disk["misses"]
    total["keys"] = ram["keys"] + disk["keys"]
    try:
        total["ratio"] = total["hits"] * 100 / (total["hits"] + total["misses"])
    except (KeyError, ZeroDivisionError):
        total["ratio"] = 0

    if disk["oldest"] < ram["oldest"]:
        total["oldest"] = disk["oldest"]
    else:
        total["oldest"] = ram["oldest"]

    ram["oldest"] = GetInHMS(time.time() - ram["oldest"])
    disk["oldest"] = GetInHMS(time.time() - disk["oldest"])
    total["oldest"] = GetInHMS(time.time() - total["oldest"])

    def key_table(keys):
        return TABLE(
            TR(TD(B(T("Key"))), TD(B(T("Time in Cache (h:m:s)")))),
            *[TR(TD(k[0]), TD("%02d:%02d:%02d" % k[1])) for k in keys],
            **dict(_class="cache-keys", _style="border-collapse: separate; border-spacing: .5em;")
        )

    ram["keys"] = key_table(ram["keys"])
    disk["keys"] = key_table(disk["keys"])
    total["keys"] = key_table(total["keys"])

    return dict(form=form, total=total, ram=ram, disk=disk, object_stats=hp != False)