Triber: added the modified version of cmdlinedl.py.
authorAdriana Draghici <adriana008@gmail.com>
Sun, 15 Nov 2009 00:28:52 +0000 (00:28 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Sun, 15 Nov 2009 00:28:52 +0000 (00:28 +0000)
tribler-mod/cmdlinedl.py [new file with mode: 0644]
tribler-mod/scrips/README

diff --git a/tribler-mod/cmdlinedl.py b/tribler-mod/cmdlinedl.py
new file mode 100644 (file)
index 0000000..6356cc3
--- /dev/null
@@ -0,0 +1,138 @@
+from time import localtime, strftime
+
+# Written by Arno Bakker 
+# see LICENSE.txt for license information
+#
+# Razvan Deaconescu, 2008:
+#       * corrected problem when running in background
+#       * added usage and print_version functions
+#       * uses getopt for command line argument parsing
+
+import sys
+import shutil
+import time
+import tempfile
+import random
+import os
+import getopt
+from traceback import print_exc
+
+from Tribler.Core.API import *
+from Tribler.Core.BitTornado.__init__ import version, report_email
+
+
+def usage():
+    print "Usage: python cmdlinedl.py [options] torrent_file"
+    print "Options:"
+    print "\t--port <port>"
+    print "\t-p <port>\t\tuse <port> to listen for connections"
+    print "\t\t\t\t(default is random value)"
+    print "\t--output <output-dir>"
+    print "\t-o <output-dir>\t\tuse <output-dir for storing downloaded data"
+    print "\t\t\t\t(default is current directory)"
+    print "\t--version"
+    print "\t-v\t\t\tprint version and exit"
+    print "\t--help"
+    print "\t-h\t\t\tprint this help screen"
+    print
+    print "Report bugs to <" + report_email + ">"
+
+def print_version():
+    print version, "<" + report_email + ">"
+
+def state_callback(ds):
+    d = ds.get_download()
+#    print >>sys.stderr, strftime("%d %b %Y %H:%M:%S", localtime())," ",`d.get_def().get_name()`,dlstatus_strings[ds.get_status()],ds.get_progress(),"%",ds.get_error(),"up",ds.get_current_speed(UPLOAD),"down",ds.get_current_speed(DOWNLOAD)
+    print >>sys.stderr, strftime("%d %b %Y %H:%M:%S", localtime())," ", '%s %s %5.2f%% %s up %8.2fKB/s down %8.2fKB/s eta %d peers %d' % \
+            (d.get_def().get_name(), \
+            dlstatus_strings[ds.get_status()], \
+            ds.get_progress() * 100, \
+            ds.get_error(), \
+            ds.get_current_speed(UPLOAD), \
+            ds.get_current_speed(DOWNLOAD), \
+            ds.get_eta(), \
+            ds.get_num_peers())
+
+    return (1.0, False)
+
+def main():
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], "hvo:p:", ["help", "version", "output-dir", "port"])
+    except getopt.GetoptError, err:
+        print str(err)
+        usage()
+        sys.exit(2)
+
+    # init to default values
+    output_dir = os.getcwd()
+    port = random.randint(10000, 65535)
+
+    for o, a in opts:
+        if o in ("-h", "--help"):
+            usage()
+            sys.exit(0)
+        elif o in ("-o", "--output-dir"):
+            output_dir = a
+        elif o in ("-p", "--port"):
+            port = int(a)
+        elif o in ("-v", "--version"):
+            print_version()
+            sys.exit(0)
+        else:
+            assert False, "unhandled option"
+
+    if len(args) == 0:
+        usage()
+        sys.exit(2)
+
+    if len(args) > 1:
+        print "Too many arguments"
+        usage()
+        sys.exit(2)
+    torrent_file = args[0]
+
+    print "Press Ctrl-C to stop the download"
+
+    # setup session
+    sscfg = SessionStartupConfig()
+    statedir = tempfile.mkdtemp()
+    sscfg.set_state_dir(statedir)
+    sscfg.set_listen_port(port)
+    sscfg.set_megacache(False)
+    sscfg.set_overlay(False)
+    sscfg.set_dialback(True)
+    sscfg.set_internal_tracker(False)
+    
+    s = Session(sscfg)
+
+    # setup and start download
+    dscfg = DownloadStartupConfig()
+    dscfg.set_dest_dir(output_dir);
+
+    tdef = TorrentDef.load(torrent_file)
+    d = s.start_download(tdef, dscfg)
+    d.set_state_callback(state_callback, getpeerlist=False)
+   
+    #
+    # loop while waiting for CTRL-C (or any other signal/interrupt)
+    #
+    # - cannot use sys.stdin.read() - it means busy waiting when running
+    #   the process in background
+    # - cannot use condition variable - that don't listen to KeyboardInterrupt
+    #
+    # time.sleep(sys.maxint) has "issues" on 64bit architectures; divide it
+    # by some value (2048) to solve problem
+    #
+    try:
+        while True:
+            time.sleep(sys.maxint/2048)
+    except:
+        print_exc()
+
+    s.shutdown()
+    time.sleep(3)
+    shutil.rmtree(statedir)
+
+
+if __name__ == "__main__":
+    main()
index ee3a331..5854c2a 100644 (file)
@@ -29,4 +29,6 @@ Exemplu:
 Fisierele modified_files_* contin pe fiecare linie calea catre un fisier ce trebuie modificat.
 Liniile care nu contin cel putin un "/" sunt ignorate.
 
+======= 
 
+Fisierul cmdlinedl.py este modificat pt a afisa eta si numarul de peers.