Exemplo n.º 1
0
 def test_poll_failed(self):
     msg = Message('file-viewer:%s' % self.file_viewer)
     msg.save('I like cheese.')
     res = self.client.get(self.poll_url())
     assert res.status_code == 200
     data = json.loads(res.content)
     assert not data['status']
     assert data['msg'] == ['I like cheese.']
Exemplo n.º 2
0
 def test_poll_failed(self):
     msg = Message('file-viewer:%s' % self.file_viewer)
     msg.save('I like cheese.')
     res = self.client.get(self.poll_url())
     eq_(res.status_code, 200)
     data = json.loads(res.content)
     eq_(data['status'], False)
     eq_(data['msg'], ['I like cheese.'])
Exemplo n.º 3
0
 def test_poll_failed(self):
     msg = Message('file-viewer:%s' % self.file_viewer)
     msg.save('I like cheese.')
     res = self.client.get(self.poll_url())
     assert res.status_code == 200
     data = json.loads(res.content)
     assert not data['status']
     assert data['msg'] == ['I like cheese.']
Exemplo n.º 4
0
 def test_poll_failed(self):
     msg = Message('file-viewer:%s' % self.file_viewer)
     msg.save('I like cheese.')
     res = self.client.get(self.poll_url())
     eq_(res.status_code, 200)
     data = json.loads(res.content)
     eq_(data['status'], False)
     eq_(data['msg'], ['I like cheese.'])
Exemplo n.º 5
0
    def test_locked(self):
        self.viewer.src = get_file('dictionary-test.xpi')

        # Lock was successfully attained
        assert self.viewer.extract()

        msg = Message(self.viewer._cache_key(LOCKED))
        msg.save(True)

        # Not extracting, the viewer is locked, lock could not be attained
        assert not self.viewer.extract()
Exemplo n.º 6
0
    def test_locked(self):
        self.viewer.src = get_file('dictionary-test.xpi')

        # Lock was successfully attained
        assert self.viewer.extract()

        msg = Message(self.viewer._cache_key(LOCKED))
        msg.save(True)

        # Not extracting, the viewer is locked, lock could not be attained
        assert not self.viewer.extract()
Exemplo n.º 7
0
def compare_poll(request, diff):
    msgs = []
    for f in (diff.left, diff.right):
        m = Message('file-viewer:%s' % f).get(delete=True)
        if m:
            msgs.append(m)
    return {'status': diff.is_extracted(), 'msg': msgs}
Exemplo n.º 8
0
def extract_file(viewer, **kw):
    # This message is for end users so they'll see a nice error.
    msg = Message("file-viewer:%s" % viewer)
    msg.delete()
    # This flag is so that we can signal when the extraction is completed.
    flag = Message(viewer._extraction_cache_key())
    task_log.debug("[1@%s] Unzipping %s for file viewer." % (extract_file.rate_limit, viewer))

    try:
        flag.save("extracting")  # Set the flag to a truthy value.
        viewer.extract()
    except Exception, err:
        if settings.DEBUG:
            msg.save(_("There was an error accessing file %s. %s.") % (viewer, err))
        else:
            msg.save(_("There was an error accessing file %s.") % viewer)
        task_log.error("[1@%s] Error unzipping: %s" % (extract_file.rate_limit, err))
Exemplo n.º 9
0
    def lock(self):
        """Lock the file-viewer.

        If locked, no one is allowed to run any extraction in `self.src` to
        avoid files / directories get messed up.

        There is a potential race-condition between getting and setting the
        lock.

        TODO: This should be re-implemented once we're on Django 1.9+ which
        has support for get_or_set.

        :return: `True` if the lock was attained,
                 `False` if there is an already existing lock.
        """
        cache_key = self._cache_key(LOCKED)
        msg = Message(cache_key)

        if msg.get():
            # Already locked
            yield False
        else:
            # Not yet locked, save flag and delete on exit.
            msg.save(True, time=LOCKED_TIMEOUT)
            try:
                yield True
            finally:
                msg.delete()
Exemplo n.º 10
0
    def test_extract_file_locked_message(self):
        self.viewer.src = get_file('dictionary-test.xpi')
        assert not self.viewer.is_extracted()

        msg = Message(self.viewer._cache_key(LOCKED))
        msg.save(True)

        msg = extract_file(self.viewer)
        assert str(msg.get()).startswith(u'File viewer is locked')
        msg.delete()
Exemplo n.º 11
0
def extract_file(file_id, **kw):
    # This message is for end users so they'll see a nice error.
    viewer = FileViewer(File.objects.get(pk=file_id))
    msg = Message('file-viewer:%s' % viewer)
    msg.delete()
    # This flag is so that we can signal when the extraction is completed.
    flag = Message(viewer._extraction_cache_key())
    task_log.debug('[1@%s] Unzipping %s for file viewer.' % (
        extract_file.rate_limit, viewer))

    try:
        flag.save('extracting')  # Set the flag to a truthy value.
        viewer.extract()
    except Exception, err:
        if settings.DEBUG:
            msg.save(_('There was an error accessing file %s. %s.')
                     % (viewer, err))
        else:
            msg.save(_('There was an error accessing file %s.') % viewer)
        task_log.error('[1@%s] Error unzipping: %s' % (extract_file.rate_limit,
                                                       err))
Exemplo n.º 12
0
def extract_file(viewer, **kw):
    # This message is for end users so they'll see a nice error.
    msg = Message('file-viewer:%s' % viewer)
    msg.delete()
    task_log.debug('Unzipping %s for file viewer.' % viewer)

    try:
        lock_attained = viewer.extract()

        if not lock_attained:
            info_msg = _(
                'File viewer is locked, extraction for %s could be '
                'in progress. Please try again in approximately 5 minutes.' %
                viewer)
            msg.save(info_msg)
    except Exception, err:
        error_message = _('There was an error accessing file %s.') % viewer

        if settings.DEBUG:
            msg.save(error_message + ' ' + err)
        else:
            msg.save(error_message)
        task_log.error('Error unzipping: %s' % err)
Exemplo n.º 13
0
    def test_extract_file_locked_message(self):
        self.viewer.src = get_file('dictionary-test.xpi')
        assert not self.viewer.is_extracted()

        msg = Message(self.viewer._cache_key(LOCKED))
        msg.save(True)

        msg = extract_file(self.viewer)
        assert str(msg.get()).startswith(u'File viewer is locked')
        msg.delete()
Exemplo n.º 14
0
def extract_file(viewer, **kw):
    # This message is for end users so they'll see a nice error.
    msg = Message('file-viewer:%s' % viewer)
    msg.delete()
    task_log.debug('Unzipping %s for file viewer.' % viewer)

    try:
        lock_attained = viewer.extract()

        if not lock_attained:
            info_msg = _(
                'File viewer is locked, extraction for %s could be '
                'in progress. Please try again in approximately 5 minutes.'
                % viewer)
            msg.save(info_msg)
    except Exception, err:
        error_message = _('There was an error accessing file %s.') % viewer

        if settings.DEBUG:
            msg.save(error_message + ' ' + err)
        else:
            msg.save(error_message)
        task_log.error('Error unzipping: %s' % err)
Exemplo n.º 15
0
def poll(request, viewer):
    return {
        'status': viewer.is_extracted(),
        'msg': [Message('file-viewer:%s' % viewer).get(delete=True)]
    }
Exemplo n.º 16
0
def extract_file(file_id, **kw):
    # This message is for end users so they'll see a nice error.
    viewer = FileViewer(File.objects.get(pk=file_id))
    msg = Message('file-viewer:%s' % viewer)
    msg.delete()
    # This flag is so that we can signal when the extraction is completed.
    flag = Message(viewer._extraction_cache_key())
    task_log.debug('[1@%s] Unzipping %s for file viewer.' %
                   (extract_file.rate_limit, viewer))

    try:
        flag.save('extracting')  # Set the flag to a truthy value.
        viewer.extract()
    except Exception, err:
        if settings.DEBUG:
            msg.save(
                _('There was an error accessing file %s. %s.') % (viewer, err))
        else:
            msg.save(_('There was an error accessing file %s.') % viewer)
        task_log.error('[1@%s] Error unzipping: %s' %
                       (extract_file.rate_limit, err))
Exemplo n.º 17
0
 def is_extracted(self):
     """If the file has been extracted or not."""
     return (os.path.exists(self.dest)
             and not Message(self._extraction_cache_key()).get())
Exemplo n.º 18
0
 def is_extracted(self):
     """If the file has been extracted or not."""
     return (private_storage.exists(
         os.path.join(self.dest, 'manifest.webapp'))
             and not Message(self._extraction_cache_key()).get())