Pillow/winbuild/fetch.py

45 lines
1.2 KiB
Python
Raw Normal View History

2015-06-20 06:43:14 +03:00
import os
2016-08-29 14:53:48 +03:00
import sys
2015-06-20 06:43:14 +03:00
import urllib.parse
import urllib.request
2019-10-28 15:11:13 +03:00
from config import libs
def fetch(url):
2019-10-28 15:11:13 +03:00
depends_filename = None
for lib in libs.values():
if lib["url"] == url:
depends_filename = lib["filename"]
break
if depends_filename and os.path.exists(depends_filename):
return depends_filename
2019-06-13 18:54:57 +03:00
name = urllib.parse.urlsplit(url)[2].split("/")[-1]
if not os.path.exists(name):
2019-10-28 15:11:13 +03:00
def retrieve(request_url):
print("Fetching", request_url)
try:
return urllib.request.urlopen(request_url)
except urllib.error.URLError:
return urllib.request.urlopen(request_url)
2019-03-12 12:44:00 +03:00
try:
2019-10-28 15:11:13 +03:00
r = retrieve(url)
except urllib.error.HTTPError:
if depends_filename:
r = retrieve(
"https://github.com/python-pillow/pillow-depends/raw/master/"
+ depends_filename
)
name = depends_filename
2019-03-12 12:44:00 +03:00
content = r.read()
2019-06-13 18:54:57 +03:00
with open(name, "wb") as fd:
fd.write(content)
return name
2018-03-03 12:54:00 +03:00
2019-06-13 18:54:57 +03:00
if __name__ == "__main__":
fetch(sys.argv[1])