Skip to content

Commit d4c0704

Browse files
committed
tools/upip: Upgrade to 1.2.
Memory optimizations and error handling improvements.
1 parent 12ea065 commit d4c0704

1 file changed

Lines changed: 44 additions & 35 deletions

File tree

tools/upip.py

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def expandhome(s):
104104
warn_ussl = True
105105
def url_open(url):
106106
global warn_ussl
107+
108+
if debug:
109+
print(url)
110+
107111
proto, _, host, urlpath = url.split('/', 3)
108112
try:
109113
ai = usocket.getaddrinfo(host, 443)
@@ -113,41 +117,43 @@ def url_open(url):
113117
addr = ai[0][4]
114118

115119
s = usocket.socket(ai[0][0])
116-
#print("Connect address:", addr)
117-
s.connect(addr)
118-
119-
if proto == "https:":
120-
s = ussl.wrap_socket(s)
121-
if warn_ussl:
122-
print("Warning: %s SSL certificate is not validated" % host)
123-
warn_ussl = False
124-
125-
# MicroPython rawsocket module supports file interface directly
126-
s.write("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (urlpath, host))
127-
l = s.readline()
128-
protover, status, msg = l.split(None, 2)
129-
if status != b"200":
130-
s.close()
131-
exc = ValueError(status)
132-
if status == b"404":
133-
fatal("Package not found", exc)
134-
fatal("Unexpected error querying for package", exc)
135-
while 1:
120+
try:
121+
#print("Connect address:", addr)
122+
s.connect(addr)
123+
124+
if proto == "https:":
125+
s = ussl.wrap_socket(s)
126+
if warn_ussl:
127+
print("Warning: %s SSL certificate is not validated" % host)
128+
warn_ussl = False
129+
130+
# MicroPython rawsocket module supports file interface directly
131+
s.write("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (urlpath, host))
136132
l = s.readline()
137-
if not l:
138-
s.close()
139-
fatal("Unexpected EOF in HTTP headers", ValueError())
140-
if l == b'\r\n':
141-
break
133+
protover, status, msg = l.split(None, 2)
134+
if status != b"200":
135+
if status == b"404" or status == b"301":
136+
raise NotFoundError("Package not found")
137+
raise ValueError(status)
138+
while 1:
139+
l = s.readline()
140+
if not l:
141+
raise ValueError("Unexpected EOF in HTTP headers")
142+
if l == b'\r\n':
143+
break
144+
except Exception as e:
145+
s.close()
146+
raise e
142147

143148
return s
144149

145150

146151
def get_pkg_metadata(name):
147152
f = url_open("https://pypi.python.org/pypi/%s/json" % name)
148-
s = f.read()
149-
f.close()
150-
return json.loads(s)
153+
try:
154+
return json.load(f)
155+
finally:
156+
f.close()
151157

152158

153159
def fatal(msg, exc=None):
@@ -168,10 +174,12 @@ def install_pkg(pkg_spec, install_path):
168174
print("Installing %s %s from %s" % (pkg_spec, latest_ver, package_url))
169175
package_fname = op_basename(package_url)
170176
f1 = url_open(package_url)
171-
f2 = uzlib.DecompIO(f1, gzdict_sz)
172-
f3 = tarfile.TarFile(fileobj=f2)
173-
meta = install_tar(f3, install_path)
174-
f1.close()
177+
try:
178+
f2 = uzlib.DecompIO(f1, gzdict_sz)
179+
f3 = tarfile.TarFile(fileobj=f2)
180+
meta = install_tar(f3, install_path)
181+
finally:
182+
f1.close()
175183
del f3
176184
del f2
177185
gc.collect()
@@ -208,9 +216,10 @@ def install(to_install, install_path=None):
208216
if deps:
209217
deps = deps.decode("utf-8").split("\n")
210218
to_install.extend(deps)
211-
except NotFoundError:
212-
print("Error: cannot find '%s' package (or server error), packages may be partially installed" \
213-
% pkg_spec, file=sys.stderr)
219+
except Exception as e:
220+
print("Error installing '{}': {}, packages may be partially installed".format(
221+
pkg_spec, e),
222+
file=sys.stderr)
214223

215224
def get_install_path():
216225
global install_path

0 commit comments

Comments
 (0)