Exemplo n.º 1
0
def init_sync_instance_db(instance):
    mam_torrents = instance.get_mam_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(MAMTransTorrent.sync_t_arguments)

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if c_hash not in mam_torrents:
                try:
                    mam_torrent = MAMTorrent.objects.get(info_hash=c_hash)
                    d_location = DownloadLocation.get_by_full_path(t_torrent.downloadDir)

                    mam_torrent = manage_mam.add_mam_torrent(
                        mam_torrent.id,
                        instance,
                        d_location,
                        add_to_client=False
                    )
                    mam_torrents[mam_torrent.info_hash] = mam_torrent
                except MAMTorrent.DoesNotExist:
                    raise Exception(u'Could not find hash {0} for name {1} in '
                                    u'DB during initial sync.'
                                    .format(c_hash, t_torrent.name))

            mam_torrent = mam_torrents[c_hash]
            mam_torrent.sync_t_torrent(t_torrent)
Exemplo n.º 2
0
def init_sync_instance_db(instance):
    mam_torrents = instance.get_mam_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(
        MAMTransTorrent.sync_t_arguments)

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if c_hash not in mam_torrents:
                try:
                    mam_torrent = MAMTorrent.objects.get(info_hash=c_hash)
                    d_location = DownloadLocation.get_by_full_path(
                        t_torrent.downloadDir)

                    mam_torrent = manage_mam.add_mam_torrent(
                        mam_torrent.id,
                        instance,
                        d_location,
                        add_to_client=False)
                    mam_torrents[mam_torrent.info_hash] = mam_torrent
                except MAMTorrent.DoesNotExist:
                    raise Exception(u'Could not find hash {0} for name {1} in '
                                    u'DB during initial sync.'.format(
                                        c_hash, t_torrent.name))

            mam_torrent = mam_torrents[c_hash]
            mam_torrent.sync_t_torrent(t_torrent)
Exemplo n.º 3
0
def sync_instance_db(instance):
    mam_torrents = instance.get_mam_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(
        MAMTransTorrent.sync_t_arguments)

    for c_hash, mam_torrent in mam_torrents.items():
        if c_hash not in t_torrents:
            mam_torrent_path = mam_torrent.path.encode('utf-8')

            messages = []
            with transaction.atomic():
                mam_torrent.delete()
                del mam_torrents[c_hash]

                if instance.replica_set.is_master:
                    if os.path.exists(mam_torrent_path):
                        files = os.listdir(mam_torrent_path)
                        if len(files):
                            messages.append(
                                u'There are other files so leaving in place.')
                        else:
                            messages.append(
                                u'No other files. Deleting directory.')
                            os.rmdir(mam_torrent_path)
                    else:
                        messages.append(u'Path does not exist.')

            LogEntry.add(
                None, u'action',
                u'MAM torrent {0} deleted from instance {1}. {2}'.format(
                    mam_torrent, instance, ' '.join(messages)))

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if c_hash not in mam_torrents:
                torrent_id = int(os.path.basename(t_torrent.downloadDir))
                w_torrent = MAMTorrent.get_or_create(None, torrent_id)
                d_location = DownloadLocation.get_by_full_path(
                    t_torrent.downloadDir)
                m_torrent = manage_mam.add_mam_torrent(w_torrent.id, instance,
                                                       d_location, None, False)
                mam_torrents[m_torrent.info_hash] = m_torrent
                LogEntry.add(
                    None, u'action',
                    u'MAM torrent {0} appeared in instance {1}.'.format(
                        t_torrent.name, instance))
            else:
                mam_torrent = mam_torrents[c_hash]
                mam_torrent.sync_t_torrent(t_torrent)
Exemplo n.º 4
0
def sync_instance_db(instance):
    mam_torrents = instance.get_mam_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(MAMTransTorrent.sync_t_arguments)

    for c_hash, mam_torrent in mam_torrents.items():
        if c_hash not in t_torrents:
            mam_torrent_path = mam_torrent.path.encode('utf-8')

            messages = []
            with transaction.atomic():
                mam_torrent.delete()
                del mam_torrents[c_hash]

                if instance.replica_set.is_master:
                    if os.path.exists(mam_torrent_path):
                        files = os.listdir(mam_torrent_path)
                        if len(files):
                            messages.append(u'There are other files so leaving in place.')
                        else:
                            messages.append(u'No other files. Deleting directory.')
                            os.rmdir(mam_torrent_path)
                    else:
                        messages.append(u'Path does not exist.')

            LogEntry.add(None, u'action',
                         u'MAM torrent {0} deleted from instance {1}. {2}'
                         .format(mam_torrent, instance, ' '.join(messages)))

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if c_hash not in mam_torrents:
                torrent_id = int(os.path.basename(t_torrent.downloadDir))
                w_torrent = MAMTorrent.get_or_create(None, torrent_id)
                d_location = DownloadLocation.get_by_full_path(t_torrent.downloadDir)
                m_torrent = manage_mam.add_mam_torrent(w_torrent.id, instance,
                                                       d_location, None, False)
                mam_torrents[m_torrent.info_hash] = m_torrent
                LogEntry.add(None, u'action',
                             u'MAM torrent {0} appeared in instance {1}.'
                             .format(t_torrent.name, instance))
            else:
                mam_torrent = mam_torrents[c_hash]
                mam_torrent.sync_t_torrent(t_torrent)
Exemplo n.º 5
0
def add_torrent(request, torrent_id):
    mam_client = MAMClient.get()
    try:
        m_torrent = manage_mam.add_mam_torrent(torrent_id,
                                               mam_client=mam_client)
        mam_torrent = m_torrent.mam_torrent
        LogEntry.add(request.user, u'action',
                     u'Added {0} to {1}'.format(m_torrent, m_torrent.instance))
        return {
            'success': True,
            'title': mam_torrent.title,
        }
    except TorrentAlreadyAddedException:
        mam_torrent = None
        try:
            mam_torrent = MAMTorrent.objects.get(id=torrent_id)
        except MAMTorrent.DoesNotExist:
            pass
        LogEntry.add(
            request.user, u'info',
            u'Tried adding MyAnonaMouse torrent_id={0}, already added.'.format(
                torrent_id))
        return {
            'success':
            False,
            'error_code':
            u'already_added',
            'error':
            u'Already added.',
            'title': (mam_torrent.title
                      if mam_torrent else '<<< Unable to find torrent >>>'),
        }
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(
            request.user, u'error',
            u'Tried adding MyAnonaMouse torrent_id={0}. Error: {1}'.format(
                torrent_id, unicode(ex)), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb,
        }
Exemplo n.º 6
0
def add_torrent(request, torrent_id):
    mam_client = MAMClient.get()
    try:
        m_torrent = manage_mam.add_mam_torrent(torrent_id, mam_client=mam_client)
        mam_torrent = m_torrent.mam_torrent
        LogEntry.add(request.user, u'action', u'Added {0} to {1}'
                     .format(m_torrent, m_torrent.instance))
        return {
            'success': True,
            'title': mam_torrent.title,
        }
    except TorrentAlreadyAddedException:
        mam_torrent = None
        try:
            mam_torrent = MAMTorrent.objects.get(id=torrent_id)
        except MAMTorrent.DoesNotExist:
            pass
        LogEntry.add(request.user, u'info',
                     u'Tried adding MyAnonaMouse torrent_id={0}, already added.'.format(torrent_id))
        return {
            'success': False,
            'error_code': u'already_added',
            'error': u'Already added.',
            'title': (mam_torrent.title if mam_torrent
                      else '<<< Unable to find torrent >>>'),
        }
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error',
                     u'Tried adding MyAnonaMouse torrent_id={0}. Error: {1}'
                     .format(torrent_id, unicode(ex)), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb,
        }