Example #1
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)
Example #2
0
def add_mam_torrent(torrent_id, instance=None, location=None, mam_client=None,
                          add_to_client=True):
    mam_torrent = MAMTorrent.get_or_create(mam_client, torrent_id)

    if not instance:
        instance = ReplicaSet.get_myanonamouse_master().get_preferred_instance()
    if not location:
        location = DownloadLocation.get_myanonamouse_preferred()

    with LockModelTables(MAMTransTorrent):
        try:
            MAMTransTorrent.objects.get(info_hash=mam_torrent.info_hash)
            raise TorrentAlreadyAddedException(u'Already added.')
        except MAMTransTorrent.DoesNotExist:
            pass

        download_dir = os.path.join(location.path, unicode(mam_torrent.id))

        def create_b_torrent():
            new_b_torrent = MAMTransTorrent(
                instance=instance,
                location=location,
                mam_torrent=mam_torrent,
                info_hash=mam_torrent.info_hash,
            )
            new_b_torrent.save()
            return new_b_torrent

        if add_to_client:
            with transaction.atomic():
                b_torrent = create_b_torrent()

                t_torrent = instance.client.add_torrent(
                    base64.b64encode(mam_torrent.torrent_file),
                    download_dir=download_dir,
                    paused=False
                )
                t_torrent = instance.client.get_torrent(
                    t_torrent.id, arguments=MAMTransTorrent.sync_t_arguments)

                if not os.path.exists(download_dir):
                    os.mkdir(download_dir)
                if not os.stat(download_dir).st_mode & 0777 == 0777:
                    os.chmod(download_dir, 0777)

                norm_t_torrent(t_torrent)
                b_torrent.sync_t_torrent(t_torrent)
        else:
Example #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)