[120357] branches/gsoc14-pip2port/tester.py

gaurav at macports.org gaurav at macports.org
Sat May 24 00:33:59 PDT 2014


Revision: 120357
          https://trac.macports.org/changeset/120357
Author:   gaurav at macports.org
Date:     2014-05-24 00:33:59 -0700 (Sat, 24 May 2014)
Log Message:
-----------
Added progressbar for fetching

Modified Paths:
--------------
    branches/gsoc14-pip2port/tester.py

Modified: branches/gsoc14-pip2port/tester.py
===================================================================
--- branches/gsoc14-pip2port/tester.py	2014-05-24 06:17:16 UTC (rev 120356)
+++ branches/gsoc14-pip2port/tester.py	2014-05-24 07:33:59 UTC (rev 120357)
@@ -1,11 +1,13 @@
 # -*- coding: utf-8 -*-
 #! /usr/bin/env python
 
-import urllib
+import urllib2
+#import urllib
 import hashlib
 import argparse
 import sys
 import os
+from progressbar import *
 try:
     import xmlrpclib
 except ImportError:
@@ -53,18 +55,40 @@
 def fetch(pkg_name,url):
     checksum_md5 = url.split('#')[-1].split('=')[-1]
     parent_dir = './sources/'
-    src_dir = parent_dir +pkg_name+"/"
+    src_dir = parent_dir + "/"+pkg_name+"/"
     if not os.path.exists(parent_dir):
         os.makedirs(parent_dir)
         if not os.path.exists(src_dir):
             os.makedirs(src_dir)
 
     file_name = src_dir + url.split('/')[-1].split('#')[0]
-    urllib.urlretrieve(url,file_name)
+
+#    urllib.urlretrieve(url,file_name)
+    u = urllib2.urlopen(url)
+    f = open(file_name,'wb')
+    meta = u.info()
+    file_size = int(meta.getheaders("Content-Length")[0])
+
+    widgets = ['Fetching: ', Percentage(), ' ', Bar(marker=RotatingMarker(),left='[',right=']'), ' ', ETA(), ' ', FileTransferSpeed()]
+    pbar = ProgressBar(widgets=widgets, maxval=int(file_size))
+    pbar.start()
+
+    file_size_dl = 0
+    block_sz = 8192
+    while True:
+        buffer = u.read(block_sz)
+        if not buffer:
+            break
+
+        file_size_dl += len(buffer)
+        f.write(buffer)
+        pbar.update(file_size_dl)
+
+    pbar.finish()
+    print
+    f.close()
+
     checksum_md5_calc = hashlib.md5(open(file_name).read()).hexdigest()
-    print "HASHES"
-    print checksum_md5_calc
-    print checksum_md5
     if str(checksum_md5_calc) == str(checksum_md5):
         print 'Successfully fetched\n'
     else:
@@ -127,7 +151,7 @@
         return
 
     if options.action == 'fetch':
-        print options,"\n"
+#        print options,"\n"
         if options.package_name == None:
             if options.package_url == None:
                 parser.error("No package name and url specified")
@@ -136,7 +160,7 @@
         elif options.package_url == None:
             parser.error("No url specified")
         else:
-            print options
+#            print options
             fetch(options.package_name,options.package_url)
         return
     else:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/macports-changes/attachments/20140524/3d52608a/attachment.html>


More information about the macports-changes mailing list