comparison mxtool/mx.py @ 15925:674d4065e9fb

mxtool: remove python downloader
author Bernhard Urban <bernhard.urban@jku.at>
date Tue, 27 May 2014 13:16:12 +0200
parents 2022366b513c
children dea42a47850e
comparison
equal deleted inserted replaced
15924:48b85f37e03b 15925:674d4065e9fb
31 Version 1.x supports a single suite of projects. 31 Version 1.x supports a single suite of projects.
32 32
33 Full documentation can be found at https://wiki.openjdk.java.net/display/Graal/The+mx+Tool 33 Full documentation can be found at https://wiki.openjdk.java.net/display/Graal/The+mx+Tool
34 """ 34 """
35 35
36 import sys, os, errno, time, subprocess, shlex, types, urllib2, contextlib, StringIO, zipfile, signal, xml.sax.saxutils, tempfile, fnmatch 36 import sys, os, errno, time, subprocess, shlex, types, StringIO, zipfile, signal, xml.sax.saxutils, tempfile, fnmatch
37 import multiprocessing 37 import multiprocessing
38 import textwrap 38 import textwrap
39 import socket 39 import socket
40 import tarfile 40 import tarfile
41 import hashlib 41 import hashlib
1884 """ 1884 """
1885 d = dirname(path) 1885 d = dirname(path)
1886 if d != '' and not exists(d): 1886 if d != '' and not exists(d):
1887 os.makedirs(d) 1887 os.makedirs(d)
1888 1888
1889 1889 assert not path.endswith(os.sep)
1890 if not path.endswith(os.sep): 1890
1891 # Try it with the Java tool first since it can show a progress counter 1891 myDir = dirname(__file__)
1892 myDir = dirname(__file__) 1892 javaSource = join(myDir, 'URLConnectionDownload.java')
1893 javaSource = join(myDir, 'URLConnectionDownload.java') 1893 javaClass = join(myDir, 'URLConnectionDownload.class')
1894 javaClass = join(myDir, 'URLConnectionDownload.class') 1894 if not exists(javaClass) or getmtime(javaClass) < getmtime(javaSource):
1895 if not exists(javaClass) or getmtime(javaClass) < getmtime(javaSource): 1895 subprocess.check_call([java().javac, '-d', myDir, javaSource])
1896 subprocess.check_call([java().javac, '-d', myDir, javaSource]) 1896 verbose = []
1897 verbose = [] 1897 if sys.stderr.isatty():
1898 if sys.stderr.isatty(): 1898 verbose.append("-v")
1899 verbose.append("-v") 1899 if run([java().java, '-cp', myDir, 'URLConnectionDownload', path] + verbose + urls, nonZeroIsFatal=False) == 0:
1900 if run([java().java, '-cp', myDir, 'URLConnectionDownload', path] + verbose + urls, nonZeroIsFatal=False) == 0: 1900 return
1901 return
1902
1903 def url_open(url):
1904 userAgent = 'Mozilla/5.0 (compatible)'
1905 headers = {'User-Agent' : userAgent}
1906 req = urllib2.Request(url, headers=headers)
1907 return urllib2.urlopen(req)
1908
1909 for url in urls:
1910 try:
1911 if verbose:
1912 log('Downloading ' + url + ' to ' + path)
1913 if url.startswith('zip:') or url.startswith('jar:'):
1914 i = url.find('!/')
1915 if i == -1:
1916 abort('Zip or jar URL does not contain "!/": ' + url)
1917 url, _, entry = url[len('zip:'):].partition('!/')
1918 with contextlib.closing(url_open(url)) as f:
1919 data = f.read()
1920 zipdata = StringIO.StringIO(f.read())
1921
1922 zf = zipfile.ZipFile(zipdata, 'r')
1923 data = zf.read(entry)
1924 with open(path, 'wb') as f:
1925 f.write(data)
1926 else:
1927 with contextlib.closing(url_open(url)) as f:
1928 data = f.read()
1929 if path.endswith(os.sep):
1930 # Scrape directory listing for relative URLs
1931 hrefs = re.findall(r' href="([^"]*)"', data)
1932 if len(hrefs) != 0:
1933 for href in hrefs:
1934 if not '/' in href:
1935 download(join(path, href), [url + href], verbose)
1936 else:
1937 log('no locals hrefs scraped from ' + url)
1938 else:
1939 with open(path, 'wb') as f:
1940 f.write(data)
1941 return
1942 except IOError as e:
1943 log('Error reading from ' + url + ': ' + str(e))
1944 except zipfile.BadZipfile as e:
1945 log('Error in zip file downloaded from ' + url + ': ' + str(e))
1946 1901
1947 abort('Could not download to ' + path + ' from any of the following URLs:\n\n ' + 1902 abort('Could not download to ' + path + ' from any of the following URLs:\n\n ' +
1948 '\n '.join(urls) + '\n\nPlease use a web browser to do the download manually') 1903 '\n '.join(urls) + '\n\nPlease use a web browser to do the download manually')
1949 1904
1950 def update_file(path, content): 1905 def update_file(path, content):