comparison mxtool/mx.py @ 4188:148fa38782e8

Downloads are attempted via Java first since it shows a progress counter.
author Doug Simon <doug.simon@oracle.com>
date Tue, 03 Jan 2012 15:49:22 +0100
parents 71a2cd79c375
children a2caa019ba3a
comparison
equal deleted inserted replaced
4187:ac5c2bdfcca2 4188:148fa38782e8
708 """ 708 """
709 d = dirname(path) 709 d = dirname(path)
710 if d != '' and not exists(d): 710 if d != '' and not exists(d):
711 os.makedirs(d) 711 os.makedirs(d)
712 712
713 # Try it with the Java tool first since it can show a progress counter
714 myDir = dirname(__file__)
715
716 javaSource = join(myDir, 'URLConnectionDownload.java')
717 javaClass = join(myDir, 'URLConnectionDownload.class')
718 if not exists(javaClass) or getmtime(javaClass) < getmtime(javaSource):
719 subprocess.check_call([java().javac, '-d', myDir, javaSource])
720 if run([java().java, '-cp', myDir, 'URLConnectionDownload', path] + urls) == 0:
721 return
722
713 def url_open(url): 723 def url_open(url):
714 userAgent = 'Mozilla/5.0 (compatible)' 724 userAgent = 'Mozilla/5.0 (compatible)'
715 headers = { 'User-Agent' : userAgent } 725 headers = { 'User-Agent' : userAgent }
716 req = urllib2.Request(url, headers=headers) 726 req = urllib2.Request(url, headers=headers)
717 return urllib2.urlopen(req); 727 return urllib2.urlopen(req);
742 except IOError as e: 752 except IOError as e:
743 log('Error reading from ' + url + ': ' + str(e)) 753 log('Error reading from ' + url + ': ' + str(e))
744 except zipfile.BadZipfile as e: 754 except zipfile.BadZipfile as e:
745 log('Error in zip file downloaded from ' + url + ': ' + str(e)) 755 log('Error in zip file downloaded from ' + url + ': ' + str(e))
746 756
747 # now try it with Java - urllib2 does not handle meta refreshes which are used by Sourceforge 757 abort('Could not download to ' + path + ' from any of the following URLs:\n\n ' +
748 myDir = dirname(__file__) 758 '\n '.join(urls) + '\n\nPlease use a web browser to do the download manually')
749 759
750 javaSource = join(myDir, 'URLConnectionDownload.java')
751 javaClass = join(myDir, 'URLConnectionDownload.class')
752 if not exists(javaClass) or getmtime(javaClass) < getmtime(javaSource):
753 subprocess.check_call([java().javac, '-d', myDir, javaSource])
754 if run([java().java, '-cp', myDir, 'URLConnectionDownload', path] + urls) != 0:
755 abort('Could not download to ' + path + ' from any of the following URLs:\n\n ' +
756 '\n '.join(urls) + '\n\nPlease use a web browser to do the download manually')
757
758 def update_file(path, content): 760 def update_file(path, content):
759 """ 761 """
760 Updates a file with some given content if the content differs from what's in 762 Updates a file with some given content if the content differs from what's in
761 the file already. The return value indicates if the file was updated. 763 the file already. The return value indicates if the file was updated.
762 """ 764 """