home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / btlaunchmany.bittorrent < prev    next >
Encoding:
Text File  |  2007-02-19  |  8.9 KB  |  231 lines

  1. #! /usr/bin/python
  2.  
  3. # Written by Michael Janssen (jamuraa at base0 dot net)
  4. # originally heavily borrowed code from btlaunchmany.py by Bram Cohen
  5. # and btdownloadcurses.py written by Henry 'Pi' James
  6. # now not so much.
  7. # fmttime and fmtsize stolen from btdownloadcurses. 
  8. # see LICENSE.txt for license information
  9.  
  10. from BitTorrent.download import download
  11. from BitTorrent.fmt import fmttime, fmtsize
  12. from threading import Thread, Event, Lock
  13. from os import listdir
  14. from os.path import abspath, join, exists, getsize
  15. from sys import argv, stdout, exit
  16. from time import sleep
  17. import traceback
  18.  
  19. def dummy(*args, **kwargs):
  20.     pass
  21.  
  22. threads = {}
  23. ext = '.torrent'
  24. print 'btlaunchmany starting..'
  25. filecheck = Lock()
  26.  
  27. def dropdir_mainloop(d, params):
  28.     deadfiles = []
  29.     global threads, status
  30.     while 1:
  31.         files = listdir(d)
  32.         # new files
  33.         for file in files: 
  34.             if file[-len(ext):] == ext:
  35.                 if file not in threads.keys() + deadfiles:
  36.                     threads[file] = {'kill': Event(), 'try': 1}
  37.                     print 'New torrent: %s' % file
  38.                     stdout.flush()
  39.                     threads[file]['thread'] = Thread(target = StatusUpdater(join(d, file), params, file).download, name = file)
  40.                     threads[file]['thread'].start()
  41.         # files with multiple tries
  42.         for file, threadinfo in threads.items():
  43.             if threadinfo.get('timeout') == 0:
  44.                 # Zero seconds left, try and start the thing again.
  45.                 threadinfo['try'] = threadinfo['try'] + 1
  46.                 threadinfo['thread'] = Thread(target = StatusUpdater(join(d, file), params, file).download, name = file)
  47.                 threadinfo['thread'].start()
  48.                 threadinfo['timeout'] = -1
  49.             elif threadinfo.get('timeout') > 0: 
  50.                 # Decrement our counter by 1
  51.                 threadinfo['timeout'] = threadinfo['timeout'] - 1
  52.             elif not threadinfo['thread'].isAlive():
  53.                 # died without permission
  54.                 # if it was checking the file, it isn't anymore.
  55.                 if threadinfo.get('checking', None):
  56.                     filecheck.release()
  57.                 if threadinfo.get('try') == 6: 
  58.                     # Died on the sixth try? You're dead.
  59.                     deadfiles.append(file)
  60.                     print '%s died 6 times, added to dead list' % fil
  61.                     stdout.flush()
  62.                     del threads[file]
  63.                 else:
  64.                     del threadinfo['thread']
  65.                     threadinfo['timeout'] = 10
  66.             # dealing with files that dissapear
  67.             if file not in files:
  68.                 print 'Torrent file dissapeared, killing %s' % file
  69.                 stdout.flush()
  70.                 if threadinfo.get('timeout', -1) == -1:
  71.                     threadinfo['kill'].set()
  72.                     threadinfo['thread'].join()
  73.                 # if this thread was filechecking, open it up
  74.                 if threadinfo.get('checking', None): 
  75.                     filecheck.release()
  76.                 del threads[file]
  77.         for file in deadfiles:
  78.             # if the file dissapears, remove it from our dead list
  79.             if file not in files: 
  80.                 deadfiles.remove(file)
  81.         sleep(1)
  82.  
  83. def display_thread(displaykiller):
  84.     interval = 1.0
  85.     global threads, status
  86.     while 1:
  87.         # display file info
  88.         if (displaykiller.isSet()): 
  89.             break
  90.         totalup = 0
  91.         totaldown = 0
  92.         totaluptotal = 0.0
  93.         totaldowntotal = 0.0
  94.         tdis = threads.items()
  95.         tdis.sort()
  96.         for file, threadinfo in tdis: 
  97.             uprate = threadinfo.get('uprate', 0)
  98.             downrate = threadinfo.get('downrate', 0)
  99.             uptxt = fmtsize(uprate, padded = 0)
  100.             downtxt = fmtsize(downrate, padded = 0)
  101.             uptotal = threadinfo.get('uptotal', 0.0)
  102.             downtotal = threadinfo.get('downtotal', 0.0)
  103.             uptotaltxt = fmtsize(uptotal, baseunit = 2, padded = 0)
  104.             downtotaltxt = fmtsize(downtotal, baseunit = 2, padded = 0)
  105.             filename = threadinfo.get('savefile', file)
  106.             if threadinfo.get('timeout', 0) > 0:
  107.                 trys = threadinfo.get('try', 1)
  108.                 timeout = threadinfo.get('timeout')
  109.                 print '%s: try %d died, retry in %d' % (filename, trys, timeout)
  110.             else:
  111.                 status = threadinfo.get('status','')
  112.                 print '%s: Spd: %s/s:%s/s Tot: %s:%s [%s]' % (filename, uptxt, downtxt, uptotaltxt, downtotaltxt, status)
  113.             totalup += uprate
  114.             totaldown += downrate
  115.             totaluptotal += uptotal
  116.             totaldowntotal += downtotal
  117.         # display totals line
  118.         totaluptxt = fmtsize(totalup, padded = 0)
  119.         totaldowntxt = fmtsize(totaldown, padded = 0)
  120.         totaluptotaltxt = fmtsize(totaluptotal, baseunit = 2, padded = 0)
  121.         totaldowntotaltxt = fmtsize(totaldowntotal, baseunit = 2, padded = 0)
  122.         print 'All: Spd: %s/s:%s/s Tot: %s:%s' % (totaluptxt, totaldowntxt, totaluptotaltxt, totaldowntotaltxt)
  123.         print
  124.         stdout.flush()
  125.         sleep(interval)
  126.  
  127. class StatusUpdater:
  128.     def __init__(self, file, params, name):
  129.         self.file = file
  130.         self.params = params
  131.         self.name = name
  132.         self.myinfo = threads[name]
  133.         self.done = 0
  134.         self.checking = 0
  135.         self.activity = 'starting'
  136.         self.display()
  137.         self.myinfo['errors'] = []
  138.  
  139.     def download(self): 
  140.         download(self.params + ['--responsefile', self.file], self.choose, self.display, self.finished, self.err, self.myinfo['kill'], 80)
  141.         print 'Torrent %s stopped' % self.file
  142.         stdout.flush()
  143.  
  144.     def finished(self): 
  145.         self.done = 1
  146.         self.myinfo['done'] = 1
  147.         self.activity = 'complete'
  148.         self.display({'fractionDone' : 1, 'downRate' : 0})
  149.  
  150.     def err(self, msg): 
  151.         self.myinfo['errors'].append(msg)
  152.         self.display()
  153.  
  154.     def failed(self): 
  155.         self.activity = 'failed' 
  156.         self.display() 
  157.  
  158.     def choose(self, default, size, saveas, dir):
  159.         global filecheck
  160.         self.myinfo['downfile'] = default
  161.         self.myinfo['filesize'] = fmtsize(size)
  162.         if saveas == '': 
  163.             saveas = default
  164.         # it asks me where I want to save it before checking the file.. 
  165.         if exists(self.file[:-len(ext)]) and (getsize(self.file[:-len(ext)]) > 0):
  166.             # file will get checked
  167.             while (not filecheck.acquire(0) and not self.myinfo['kill'].isSet()):
  168.                 self.myinfo['status'] = 'disk wait'
  169.                 sleep(0.1)
  170.             if not self.myinfo['kill'].isSet():
  171.                 self.checking = 1
  172.                 self.myinfo['checking'] = 1
  173.         self.myinfo['savefile'] = self.file[:-len(ext)]
  174.         return self.file[:-len(ext)]
  175.     
  176.     def display(self, dict = {}):
  177.         fractionDone = dict.get('fractionDone', None)
  178.         timeEst = dict.get('timeEst', None)
  179.         activity = dict.get('activity', None) 
  180.         global status
  181.         if activity is not None and not self.done: 
  182.             if activity == 'checking existing file':
  183.                 self.activity = 'disk check'
  184.             elif activity == 'connecting to peers':
  185.                 self.activity = 'connecting'
  186.             else:
  187.                 self.activity = activity
  188.         elif timeEst is not None: 
  189.             self.activity = fmttime(timeEst, 1)
  190.         if fractionDone is not None: 
  191.             self.myinfo['status'] = '%s %.0f%%' % (self.activity, fractionDone * 100)
  192.         else:
  193.             self.myinfo['status'] = self.activity
  194.         if self.activity != 'checking existing file' and self.checking:
  195.             # we finished checking our files. 
  196.             filecheck.release()
  197.             self.checking = 0
  198.             self.myinfo['checking'] = 0
  199.         if dict.has_key('upRate'):
  200.             self.myinfo['uprate'] = dict['upRate']
  201.         if dict.has_key('downRate'):
  202.             self.myinfo['downrate'] = dict['downRate']
  203.         if dict.has_key('upTotal'):
  204.             self.myinfo['uptotal'] = dict['upTotal']
  205.         if dict.has_key('downTotal'):
  206.             self.myinfo['downtotal'] = dict['downTotal']
  207.  
  208. if __name__ == '__main__':
  209.     if (len(argv) < 2):
  210.         print """Usage: btlaunchmany.py <directory> <global options>
  211.   <directory> - directory to look for .torrent files (non-recursive)
  212.   <global options> - options to be applied to all torrents (see btdownloadheadless.py)
  213. """
  214.         exit(-1)
  215.     try:
  216.         displaykiller = Event()
  217.         displaythread = Thread(target = display_thread, name = 'display', args = [displaykiller])
  218.         displaythread.start()
  219.         dropdir_mainloop(argv[1], argv[2:])
  220.     except KeyboardInterrupt: 
  221.         print '^C caught! Killing torrents..'
  222.         for file, threadinfo in threads.items(): 
  223.             status = 'Killing torrent %s' % file
  224.             threadinfo['kill'].set() 
  225.             threadinfo['thread'].join() 
  226.             del threads[file]
  227.         displaykiller.set()
  228.         displaythread.join()
  229.     except:
  230.         traceback.print_exc()
  231.