<commit_before>
__author__ = 'alexisgallepe'

import time
import PeersManager
import PeerSeeker
import PiecesManager
import Torrent
import Tracker
import logging
import Queue
import os
import urllib


class Runner(object):
    def __init__(self, torrent_path, file_store, http_timeout=10):
        newpeersQueue = Queue.Queue()
        self.torrent = Torrent.Torrent(torrent_path, file_store)
        self.http_timeout = 10
        self.file_store = file_store
        self.tracker = Tracker.Tracker(self.torrent,newpeersQueue)

        self.peerSeeker = PeerSeeker.PeerSeeker(newpeersQueue, self.torrent)
        self.piecesManager = PiecesManager.PiecesManager(self.torrent)
        self.peersManager = PeersManager.PeersManager(self.torrent,self.piecesManager)

        self.peersManager.start()
        logging.info("Peers-manager Started")

        self.peerSeeker.start()
        logging.info("Peer-seeker Started")

        self.piecesManager.start()
        logging.info("Pieces-manager Started")

    def start(self):
        old=0
        while not self.piecesManager.arePiecesCompleted() and self.http_timeout > 0.0:
            if len(self.peersManager.unchokedPeers) > 0:
                for piece in self.piecesManager.pieces:
                    if not piece.finished:
                        pieceIndex = piece.pieceIndex

                        peer = self.peersManager.getUnchokedPeer(pieceIndex)
                        if not peer:
                            continue

                        data = self.piecesManager.pieces[pieceIndex].getEmptyBlock()

                        if data:
                            index, offset, length = data
                            self.peersManager.requestNewPiece(peer,index, offset, length)

                        piece.isComplete()

                ##########################
                        for block in piece.blocks:
                            if ( int(time.time()) - block[3] ) > 8 and block[0] == "Pending" :
                                block[0] = "Free"
                                block[3] = 0

                b=0
                for i in range(self.piecesManager.numberOfPieces):
                    for j in range(self.piecesManager.pieces[i].num_blocks):
                        if self.piecesManager.pieces[i].blocks[j][0]=="Full":
                            b+=len(self.piecesManager.pieces[i].blocks[j][2])

                if b == old:
                    continue

                old = b
                print "Number of peers: ",len(self.peersManager.unchokedPeers)," Completed: ",float((float(b) / self.torrent.totalLength)*100),"%"
                print self.peersManager.unchokedPeers[0].ip
               ##########################
            time.sleep(0.1)
            self.http_timeout -= .1
        print "No Peers Found -- Trying HTTP Backup"

        urls_to_try = self.torrent.torrentFile.get('url-list', [])
        if len(urls_to_try) > 0:
            print "Downloading from backup URL: " + urls_to_try[0]
            urllib.urlretrieve(urls_to_try[0], self.file_store + self.torrent.torrentFile.get('info', {}).get('name', ''))
        else:
            print "No Backup URL Present"

        # gotta kill the threads
        self.peerSeeker.requestStop()
        self.peersManager.requestStop()<commit_msg>cleaning up code<commit_after>
__author__ = 'alexisgallepe'

import time
import PeersManager
import PeerSeeker
import PiecesManager
import Torrent
import Tracker
import logging
import Queue
import os
import urllib


class Runner(object):
    def __init__(self, torrent_path, file_store, http_timeout=10):
        newpeersQueue = Queue.Queue()
        self.torrent = Torrent.Torrent(torrent_path, file_store)
        self.http_timeout = 10
        self.file_store = file_store
        self.tracker = Tracker.Tracker(self.torrent,newpeersQueue)

        self.peerSeeker = PeerSeeker.PeerSeeker(newpeersQueue, self.torrent)
        self.piecesManager = PiecesManager.PiecesManager(self.torrent)
        self.peersManager = PeersManager.PeersManager(self.torrent,self.piecesManager)

        self.peersManager.start()
        logging.info("Peers-manager Started")

        self.peerSeeker.start()
        logging.info("Peer-seeker Started")

        self.piecesManager.start()
        logging.info("Pieces-manager Started")

    def start(self):
        old=0
        while not self.piecesManager.arePiecesCompleted() and self.http_timeout > 0.0:
            if len(self.peersManager.unchokedPeers) > 0:
                for piece in self.piecesManager.pieces:
                    if not piece.finished:
                        pieceIndex = piece.pieceIndex

                        peer = self.peersManager.getUnchokedPeer(pieceIndex)
                        if not peer:
                            continue

                        data = self.piecesManager.pieces[pieceIndex].getEmptyBlock()

                        if data:
                            index, offset, length = data
                            self.peersManager.requestNewPiece(peer,index, offset, length)

                        piece.isComplete()

                ##########################
                        for block in piece.blocks:
                            if ( int(time.time()) - block[3] ) > 8 and block[0] == "Pending" :
                                block[0] = "Free"
                                block[3] = 0

                b=0
                for i in range(self.piecesManager.numberOfPieces):
                    for j in range(self.piecesManager.pieces[i].num_blocks):
                        if self.piecesManager.pieces[i].blocks[j][0]=="Full":
                            b+=len(self.piecesManager.pieces[i].blocks[j][2])

                if b == old:
                    continue

                old = b
                print "Number of peers: ",len(self.peersManager.unchokedPeers)," Completed: ",float((float(b) / self.torrent.totalLength)*100),"%"
                print self.peersManager.unchokedPeers[0].ip
               ##########################
            else:
                time.sleep(0.1)
                self.http_timeout -= .1
        print "No Peers Found -- Trying HTTP Backup"

        if self.http_timeout <= 0.0:
            urls_to_try = self.torrent.torrentFile.get('url-list', [])
            if len(urls_to_try) > 0:
                print "Downloading from backup URL: " + urls_to_try[0]
                if self.torrent.torrentFile.get('info', {}).get('files') is not None:
                    for f in self.torrent.torrentFile.get('info', {}).get('files'):
                        urllib.urlretrieve(urls_to_try[0], self.torrent.torrentFile.get('info', {}).get('name', '') + "/" + f['path'][0])
                else:
                    urllib.urlretrieve(urls_to_try[0], self.file_store + self.torrent.torrentFile.get('info', {}).get('name', ''))
            else:
                print "No Backup URL Present"

        # gotta kill the threads
        self.peerSeeker.requestStop()
        self.peersManager.requestStop()
{'fraction_non_alphanumeric': 0.11500423848544787, 'fraction_numerical': 0.008194405199208816, 'mean_word_length': 1.7804399057344855, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5337987', 'n_tokens_mistral': 2093, 'n_tokens_neox': 1936, 'n_words': 375}