Esempio n. 1
0
 def eval(self, x, label):
     try:
         return safe.eval_safe(x)
     except SyntaxError:
         pass
     except NameError:
         pass
     raise ValidationError(
         self.description,
         '%s: %s.' % (_(label), _('invalid syntax "%s" for integer') % x))
Esempio n. 2
0
 def eval(self, x, label):
     try:
         return safe.eval_safe(x)
     except SyntaxError:
         pass
     except NameError:
         pass
     raise ValidationError(self.description,
         '%s: %s.' % (_(label),
             _('invalid syntax "%s" for integer') % x))
Esempio n. 3
0
def shell_cache(args, cache='', key=None, validate=None, **options):
    """Runs a shell command and captures the output. It uses a caching system
    so that cached results don't need to run a subprocess anymore. The results
    are cached by sys.platform

    :param args: the command to be executed in the shell
    :type args: tuple of strings
    :param cache: the filename of the cache file
    :type cache: string
    :param validate: a validate (eg mtime) to validate the cache result
    :returns: stdout and stdout
    :rtype: typle of strings

    >>> shell('echo world', shell=True)
    ('world\\n', '')
    """
    # Initialize cache_dict
    args = tuple(args)
    cache_dict = {}
    if key is None:
        key = args
    # Try to load from cache file
    if os.path.isfile(cache):
        f = open(cache, 'rb')
        source = f.read()
        f.close()
        try:
            cache_dict = safe.eval_safe(source)
        except SyntaxError:
            pass
    # Initialize result
    result = None
    # Is it cached already?
    if key in cache_dict:
        x = cache_dict[key]
        if sys.platform in x:
            x = x[sys.platform]
            if validate:
                if validate == x['validate']:
                    result = x
            else:
                result = x
    if result is None:
        # Add to cache
        result = {'validate': validate}
        result['stdout'], result['stderr'] = shell(args, **options)
        if not key in cache_dict:
            cache_dict[key] = {}
        cache_dict[key][sys.platform] = result
        # Save to cache
        ensure_path(os.path.dirname(cache))
        f = open(cache, 'wb')
        f.write(unicode(cache_dict))
        f.close()
    return result['stdout'], result['stderr']