예제 #1
0
    def runTest(self):
        """ Tests the Asset SQLObject. """
        fake_plugin = Plugin(name='fake_plugin', activated='notfound')
        asset_channel = PluginChannel(name='Asset Channel',
                                      plugin=fake_plugin,
                                      subscription_right='public')

        a1 = Asset(plugin_channel=asset_channel,
                   user=None,
                   filename='path_test',
                   extension='.txt')
        last_ref_a1 = a1.last_reference
        time.sleep(1)
        assert a1.path == os.path.join('static', 'storage',
                                       str(asset_channel.id),
                                       str(a1.id) + '.txt')
        assert a1.last_reference > last_ref_a1

        a2 = Asset(plugin_channel=asset_channel,
                   user=None,
                   filename='path_test')
        assert a2.path == os.path.join('static', 'storage',
                                       str(asset_channel.id), str(a2.id))

        a3 = Asset(plugin_channel=asset_channel,
                   user=None,
                   filename='cache_test',
                   in_flight=True)
        a3_path = os.path.join('static', 'storage', str(asset_channel.id),
                               str(a3.id))
        assert a3.path is None
        assert a3._get_path(force=True) == a3_path
        a3.in_flight = False
        assert a3.path == a3_path

        a4 = Asset(plugin_channel=asset_channel,
                   user=None,
                   filename='test_write',
                   extension='.txt')
        a4_path = os.path.join(get_root_path(), 'static', 'storage',
                               str(asset_channel.id),
                               str(a4.id) + '.txt')
        a4_content = 'a4 file content'.encode()
        a4.write_to_asset_file(a4_content)
        with open(a4_path, 'rb') as f:
            assert f.read() == a4_content
        a4.destroySelf()
        assert not os.path.exists(a4_path)
예제 #2
0
 def cache_file_at_url(self, url):
     """ Caches the remote file or retrieves its cached version if one exists. Returns None if an error occurs. """
     filename, extension = os.path.splitext(url)
     cache_asset_hash = hash(str(self.channel_id) + filename)
     asset = Asset.selectBy(plugin_channel=self.channel_id,
                            filename=filename,
                            is_cached=True).getOne(None)
     if asset is None:
         if CacheManager._get_lock(cache_asset_hash, blocking=False):
             try:
                 asset = Asset(plugin_channel=self.channel_id,
                               filename=filename,
                               user=None,
                               extension=extension if extension is not None
                               and len(extension) > 0 else None,
                               is_cached=True)
                 self.download_manager.enqueue_asset(asset)
             except (URLError, OSError):
                 logger.warning(
                     'Exception encountered when attempting to cache file at url %s',
                     url,
                     exc_info=True)
                 CacheManager._release_lock(cache_asset_hash)
                 return None
             CacheManager._release_lock(cache_asset_hash)
         else:
             CacheManager._get_lock(cache_asset_hash, blocking=True)
             CacheManager._release_lock(cache_asset_hash)
             asset = Asset.selectBy(plugin_channel=self.channel_id,
                                    filename=filename,
                                    is_cached=True).getOne(None)
     return asset
예제 #3
0
 def runTest(self):
     """ Tests the cache mecanism. """
     u = User(username='******',
              fullname='testasset test',
              email='*****@*****.**',
              super_admin=True,
              disabled=False)
     PluginChannel(name='testasset2',
                   plugin=Plugin(name='cache_plugin', activated='no'),
                   subscription_right='restricted')
     c = PluginChannel.selectBy(name="testasset2").getOne()
     a = Asset(plugin_channel=c, user=u)
     self.testApp.get('/cache/' + str(a.id), status=303)
     try:
         Asset.delete(a.id)
     except SQLObjectNotFound:
         pass
     finally:
         User.delete(u.id)
         PluginChannel.delete(c.id)