From aac2db9aafbaaa7cea82be5683b69833859200f5 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 10 Feb 2017 20:18:05 +0300 Subject: tools/upip: Update to 1.1.5. Better and more user-friendly error handling. --- tools/upip.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'tools/upip.py') diff --git a/tools/upip.py b/tools/upip.py index db18a7427..0070fd619 100644 --- a/tools/upip.py +++ b/tools/upip.py @@ -105,7 +105,10 @@ warn_ussl = True def url_open(url): global warn_ussl proto, _, host, urlpath = url.split('/', 3) - ai = usocket.getaddrinfo(host, 443) + try: + ai = usocket.getaddrinfo(host, 443) + except OSError as e: + fatal("Unable to resolve %s (no Internet?)" % host, e) #print("Address infos:", ai) addr = ai[0][4] @@ -124,13 +127,16 @@ def url_open(url): l = s.readline() protover, status, msg = l.split(None, 2) if status != b"200": + s.close() + exc = ValueError(status) if status == b"404": - print("Package not found") - raise ValueError(status) + fatal("Package not found", exc) + fatal("Unexpected error querying for package", exc) while 1: l = s.readline() if not l: - raise ValueError("Unexpected EOF") + s.close() + fatal("Unexpected EOF in HTTP headers", ValueError()) if l == b'\r\n': break @@ -144,8 +150,10 @@ def get_pkg_metadata(name): return json.loads(s) -def fatal(msg): - print(msg) +def fatal(msg, exc=None): + print("Error:", msg) + if exc and debug: + raise exc sys.exit(1) def install_pkg(pkg_spec, install_path): -- cgit v1.2.3 From e81f46940e005e07502e3952f5b0feb23037235b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 1 May 2017 00:03:45 +0300 Subject: tools/upip: Upgrade to 1.1.6, supports commented lines in requirements.txt. --- tools/upip.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tools/upip.py') diff --git a/tools/upip.py b/tools/upip.py index 0070fd619..a156340b2 100644 --- a/tools/upip.py +++ b/tools/upip.py @@ -275,6 +275,8 @@ def main(): l = f.readline() if not l: break + if l[0] == "#": + continue to_install.append(l.rstrip()) elif opt == "--debug": debug = True -- cgit v1.2.3 From d4c070415a3ec8ea06ab99b48ecf6976b7c93289 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 5 May 2017 13:12:19 +0300 Subject: tools/upip: Upgrade to 1.2. Memory optimizations and error handling improvements. --- tools/upip.py | 79 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 35 deletions(-) (limited to 'tools/upip.py') diff --git a/tools/upip.py b/tools/upip.py index a156340b2..7b85c718f 100644 --- a/tools/upip.py +++ b/tools/upip.py @@ -104,6 +104,10 @@ import usocket warn_ussl = True def url_open(url): global warn_ussl + + if debug: + print(url) + proto, _, host, urlpath = url.split('/', 3) try: ai = usocket.getaddrinfo(host, 443) @@ -113,41 +117,43 @@ def url_open(url): addr = ai[0][4] s = usocket.socket(ai[0][0]) - #print("Connect address:", addr) - s.connect(addr) - - if proto == "https:": - s = ussl.wrap_socket(s) - if warn_ussl: - print("Warning: %s SSL certificate is not validated" % host) - warn_ussl = False - - # MicroPython rawsocket module supports file interface directly - s.write("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (urlpath, host)) - l = s.readline() - protover, status, msg = l.split(None, 2) - if status != b"200": - s.close() - exc = ValueError(status) - if status == b"404": - fatal("Package not found", exc) - fatal("Unexpected error querying for package", exc) - while 1: + try: + #print("Connect address:", addr) + s.connect(addr) + + if proto == "https:": + s = ussl.wrap_socket(s) + if warn_ussl: + print("Warning: %s SSL certificate is not validated" % host) + warn_ussl = False + + # MicroPython rawsocket module supports file interface directly + s.write("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (urlpath, host)) l = s.readline() - if not l: - s.close() - fatal("Unexpected EOF in HTTP headers", ValueError()) - if l == b'\r\n': - break + protover, status, msg = l.split(None, 2) + if status != b"200": + if status == b"404" or status == b"301": + raise NotFoundError("Package not found") + raise ValueError(status) + while 1: + l = s.readline() + if not l: + raise ValueError("Unexpected EOF in HTTP headers") + if l == b'\r\n': + break + except Exception as e: + s.close() + raise e return s def get_pkg_metadata(name): f = url_open("https://pypi.python.org/pypi/%s/json" % name) - s = f.read() - f.close() - return json.loads(s) + try: + return json.load(f) + finally: + f.close() def fatal(msg, exc=None): @@ -168,10 +174,12 @@ def install_pkg(pkg_spec, install_path): print("Installing %s %s from %s" % (pkg_spec, latest_ver, package_url)) package_fname = op_basename(package_url) f1 = url_open(package_url) - f2 = uzlib.DecompIO(f1, gzdict_sz) - f3 = tarfile.TarFile(fileobj=f2) - meta = install_tar(f3, install_path) - f1.close() + try: + f2 = uzlib.DecompIO(f1, gzdict_sz) + f3 = tarfile.TarFile(fileobj=f2) + meta = install_tar(f3, install_path) + finally: + f1.close() del f3 del f2 gc.collect() @@ -208,9 +216,10 @@ def install(to_install, install_path=None): if deps: deps = deps.decode("utf-8").split("\n") to_install.extend(deps) - except NotFoundError: - print("Error: cannot find '%s' package (or server error), packages may be partially installed" \ - % pkg_spec, file=sys.stderr) + except Exception as e: + print("Error installing '{}': {}, packages may be partially installed".format( + pkg_spec, e), + file=sys.stderr) def get_install_path(): global install_path -- cgit v1.2.3