2018-04-23 02:11:27 -04:00
|
|
|
# upip - Package manager for MicroPython
|
2020-06-03 18:40:05 -04:00
|
|
|
|
|
|
|
# SPDX-FileCopyrightText: Copyright (c) 2015-2018 Paul Sokolovsky
|
|
|
|
# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
|
2018-04-23 02:11:27 -04:00
|
|
|
#
|
2020-06-03 18:40:05 -04:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
import sys
|
|
|
|
import gc
|
|
|
|
import uos as os
|
|
|
|
import uerrno as errno
|
|
|
|
import ujson as json
|
|
|
|
import uzlib
|
|
|
|
import upip_utarfile as tarfile
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
debug = False
|
2021-04-22 20:55:39 -04:00
|
|
|
index_urls = ["https://micropython.org/pi", "https://pypi.org/pypi"]
|
2016-11-07 10:13:56 -05:00
|
|
|
install_path = None
|
|
|
|
cleanup_files = []
|
|
|
|
gzdict_sz = 16 + 15
|
|
|
|
|
|
|
|
file_buf = bytearray(512)
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
class NotFoundError(Exception):
|
|
|
|
pass
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
def op_split(path):
|
|
|
|
if path == "":
|
|
|
|
return ("", "")
|
|
|
|
r = path.rsplit("/", 1)
|
|
|
|
if len(r) == 1:
|
|
|
|
return ("", path)
|
|
|
|
head = r[0]
|
|
|
|
if not head:
|
|
|
|
head = "/"
|
|
|
|
return (head, r[1])
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
# Expects *file* name
|
|
|
|
def _makedirs(name, mode=0o777):
|
|
|
|
ret = False
|
|
|
|
s = ""
|
|
|
|
comps = name.rstrip("/").split("/")[:-1]
|
|
|
|
if comps[0] == "":
|
|
|
|
s = "/"
|
|
|
|
for c in comps:
|
|
|
|
if s and s[-1] != "/":
|
|
|
|
s += "/"
|
|
|
|
s += c
|
|
|
|
try:
|
|
|
|
os.mkdir(s)
|
|
|
|
ret = True
|
|
|
|
except OSError as e:
|
2021-04-20 03:11:59 -04:00
|
|
|
if e.errno != errno.EEXIST and e.errno != errno.EISDIR:
|
2021-04-22 20:55:39 -04:00
|
|
|
raise e
|
2016-11-07 10:13:56 -05:00
|
|
|
ret = False
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def save_file(fname, subf):
|
|
|
|
global file_buf
|
|
|
|
with open(fname, "wb") as outf:
|
|
|
|
while True:
|
|
|
|
sz = subf.readinto(file_buf)
|
|
|
|
if not sz:
|
|
|
|
break
|
|
|
|
outf.write(file_buf, sz)
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
def install_tar(f, prefix):
|
|
|
|
meta = {}
|
|
|
|
for info in f:
|
2021-03-15 09:57:36 -04:00
|
|
|
# print(info)
|
2016-11-07 10:13:56 -05:00
|
|
|
fname = info.name
|
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
fname = fname[fname.index("/") + 1 :]
|
2016-11-07 10:13:56 -05:00
|
|
|
except ValueError:
|
|
|
|
fname = ""
|
|
|
|
|
|
|
|
save = True
|
|
|
|
for p in ("setup.", "PKG-INFO", "README"):
|
2021-03-15 09:57:36 -04:00
|
|
|
# print(fname, p)
|
|
|
|
if fname.startswith(p) or ".egg-info" in fname:
|
|
|
|
if fname.endswith("/requires.txt"):
|
|
|
|
meta["deps"] = f.extractfile(info).read()
|
|
|
|
save = False
|
|
|
|
if debug:
|
|
|
|
print("Skipping", fname)
|
|
|
|
break
|
2016-11-07 10:13:56 -05:00
|
|
|
|
|
|
|
if save:
|
|
|
|
outfname = prefix + fname
|
|
|
|
if info.type != tarfile.DIRTYPE:
|
|
|
|
if debug:
|
|
|
|
print("Extracting " + outfname)
|
|
|
|
_makedirs(outfname)
|
|
|
|
subf = f.extractfile(info)
|
|
|
|
save_file(outfname, subf)
|
|
|
|
return meta
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
def expandhome(s):
|
|
|
|
if "~/" in s:
|
|
|
|
h = os.getenv("HOME")
|
|
|
|
s = s.replace("~/", h + "/")
|
|
|
|
return s
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
import ussl
|
|
|
|
import usocket
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
warn_ussl = True
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
def url_open(url):
|
|
|
|
global warn_ussl
|
2017-05-05 06:12:19 -04:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
print(url)
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
proto, _, host, urlpath = url.split("/", 3)
|
2017-02-10 12:18:05 -05:00
|
|
|
try:
|
2020-10-06 16:19:05 -04:00
|
|
|
port = 443
|
|
|
|
if ":" in host:
|
|
|
|
host, port = host.split(":")
|
|
|
|
port = int(port)
|
|
|
|
ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
|
2017-02-10 12:18:05 -05:00
|
|
|
except OSError as e:
|
|
|
|
fatal("Unable to resolve %s (no Internet?)" % host, e)
|
2021-03-15 09:57:36 -04:00
|
|
|
# print("Address infos:", ai)
|
2018-04-23 02:11:27 -04:00
|
|
|
ai = ai[0]
|
2016-11-07 10:13:56 -05:00
|
|
|
|
2018-04-23 02:11:27 -04:00
|
|
|
s = usocket.socket(ai[0], ai[1], ai[2])
|
2017-05-05 06:12:19 -04:00
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
# print("Connect address:", addr)
|
2018-04-23 02:11:27 -04:00
|
|
|
s.connect(ai[-1])
|
2017-05-05 06:12:19 -04:00
|
|
|
|
|
|
|
if proto == "https:":
|
2017-09-29 21:24:11 -04:00
|
|
|
s = ussl.wrap_socket(s, server_hostname=host)
|
2017-05-05 06:12:19 -04:00
|
|
|
if warn_ussl:
|
|
|
|
print("Warning: %s SSL certificate is not validated" % host)
|
|
|
|
warn_ussl = False
|
|
|
|
|
|
|
|
# MicroPython rawsocket module supports file interface directly
|
2020-10-06 16:19:05 -04:00
|
|
|
s.write("GET /%s HTTP/1.0\r\nHost: %s:%s\r\n\r\n" % (urlpath, host, port))
|
2016-11-07 10:13:56 -05:00
|
|
|
l = s.readline()
|
2017-05-05 06:12:19 -04:00
|
|
|
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")
|
2021-03-15 09:57:36 -04:00
|
|
|
if l == b"\r\n":
|
2017-05-05 06:12:19 -04:00
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
s.close()
|
|
|
|
raise e
|
2016-11-07 10:13:56 -05:00
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def get_pkg_metadata(name):
|
2021-04-22 20:55:39 -04:00
|
|
|
for url in index_urls:
|
|
|
|
try:
|
|
|
|
f = url_open("%s/%s/json" % (url, name))
|
|
|
|
except NotFoundError:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
return json.load(f)
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
raise NotFoundError("Package not found")
|
2016-11-07 10:13:56 -05:00
|
|
|
|
|
|
|
|
2017-02-10 12:18:05 -05:00
|
|
|
def fatal(msg, exc=None):
|
|
|
|
print("Error:", msg)
|
|
|
|
if exc and debug:
|
|
|
|
raise exc
|
2016-11-07 10:13:56 -05:00
|
|
|
sys.exit(1)
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
def install_pkg(pkg_spec, install_path):
|
2021-11-26 10:18:11 -05:00
|
|
|
package = pkg_spec.split("==")
|
|
|
|
data = get_pkg_metadata(package[0])
|
2016-11-07 10:13:56 -05:00
|
|
|
|
2021-11-26 10:18:11 -05:00
|
|
|
if len(package) == 1:
|
|
|
|
latest_ver = data["info"]["version"]
|
|
|
|
else:
|
|
|
|
latest_ver = package[1]
|
2016-11-07 10:13:56 -05:00
|
|
|
packages = data["releases"][latest_ver]
|
|
|
|
del data
|
|
|
|
gc.collect()
|
|
|
|
assert len(packages) == 1
|
|
|
|
package_url = packages[0]["url"]
|
|
|
|
print("Installing %s %s from %s" % (pkg_spec, latest_ver, package_url))
|
|
|
|
f1 = url_open(package_url)
|
2017-05-05 06:12:19 -04:00
|
|
|
try:
|
|
|
|
f2 = uzlib.DecompIO(f1, gzdict_sz)
|
|
|
|
f3 = tarfile.TarFile(fileobj=f2)
|
|
|
|
meta = install_tar(f3, install_path)
|
|
|
|
finally:
|
|
|
|
f1.close()
|
2016-11-07 10:13:56 -05:00
|
|
|
del f3
|
|
|
|
del f2
|
|
|
|
gc.collect()
|
|
|
|
return meta
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
def install(to_install, install_path=None):
|
|
|
|
# Calculate gzip dictionary size to use
|
|
|
|
global gzdict_sz
|
|
|
|
sz = gc.mem_free() + gc.mem_alloc()
|
|
|
|
if sz <= 65536:
|
|
|
|
gzdict_sz = 16 + 12
|
|
|
|
|
|
|
|
if install_path is None:
|
|
|
|
install_path = get_install_path()
|
|
|
|
if install_path[-1] != "/":
|
|
|
|
install_path += "/"
|
|
|
|
if not isinstance(to_install, list):
|
|
|
|
to_install = [to_install]
|
|
|
|
print("Installing to: " + install_path)
|
|
|
|
# sets would be perfect here, but don't depend on them
|
|
|
|
installed = []
|
|
|
|
try:
|
|
|
|
while to_install:
|
|
|
|
if debug:
|
|
|
|
print("Queue:", to_install)
|
|
|
|
pkg_spec = to_install.pop(0)
|
|
|
|
if pkg_spec in installed:
|
|
|
|
continue
|
|
|
|
meta = install_pkg(pkg_spec, install_path)
|
|
|
|
installed.append(pkg_spec)
|
|
|
|
if debug:
|
|
|
|
print(meta)
|
|
|
|
deps = meta.get("deps", "").rstrip()
|
|
|
|
if deps:
|
|
|
|
deps = deps.decode("utf-8").split("\n")
|
|
|
|
to_install.extend(deps)
|
2017-05-05 06:12:19 -04:00
|
|
|
except Exception as e:
|
2021-03-15 09:57:36 -04:00
|
|
|
print(
|
|
|
|
"Error installing '{}': {}, packages may be partially installed".format(pkg_spec, e),
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
|
|
|
|
def get_install_path():
|
|
|
|
global install_path
|
|
|
|
if install_path is None:
|
|
|
|
# sys.path[0] is current module's path
|
|
|
|
install_path = sys.path[1]
|
2021-12-21 07:16:51 -05:00
|
|
|
if install_path == ".frozen":
|
|
|
|
install_path = sys.path[2]
|
2016-11-07 10:13:56 -05:00
|
|
|
install_path = expandhome(install_path)
|
|
|
|
return install_path
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
def cleanup():
|
|
|
|
for fname in cleanup_files:
|
|
|
|
try:
|
|
|
|
os.unlink(fname)
|
|
|
|
except OSError:
|
|
|
|
print("Warning: Cannot delete " + fname)
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
def help():
|
2021-03-15 09:57:36 -04:00
|
|
|
print(
|
|
|
|
"""\
|
2016-11-07 10:13:56 -05:00
|
|
|
upip - Simple PyPI package manager for MicroPython
|
|
|
|
Usage: micropython -m upip install [-p <path>] <package>... | -r <requirements.txt>
|
|
|
|
import upip; upip.install(package_or_list, [<path>])
|
|
|
|
|
2021-12-21 07:16:51 -05:00
|
|
|
If <path> isn't given, packages will be installed to sys.path[1], or
|
|
|
|
sys.path[2] if the former is .frozen (path can be set from MICROPYPATH
|
|
|
|
environment variable if supported)."""
|
2021-03-15 09:57:36 -04:00
|
|
|
)
|
2021-12-21 07:16:51 -05:00
|
|
|
print("Default install path:", get_install_path())
|
2021-03-15 09:57:36 -04:00
|
|
|
print(
|
|
|
|
"""\
|
2016-11-07 10:13:56 -05:00
|
|
|
|
|
|
|
Note: only MicroPython packages (usually, named micropython-*) are supported
|
|
|
|
for installation, upip does not support arbitrary code in setup.py.
|
2021-03-15 09:57:36 -04:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2016-11-07 10:13:56 -05:00
|
|
|
|
|
|
|
def main():
|
|
|
|
global debug
|
2021-04-22 20:55:39 -04:00
|
|
|
global index_urls
|
2016-11-07 10:13:56 -05:00
|
|
|
global install_path
|
|
|
|
install_path = None
|
|
|
|
|
|
|
|
if len(sys.argv) < 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
|
|
|
|
help()
|
|
|
|
return
|
|
|
|
|
|
|
|
if sys.argv[1] != "install":
|
|
|
|
fatal("Only 'install' command supported")
|
|
|
|
|
|
|
|
to_install = []
|
|
|
|
|
|
|
|
i = 2
|
|
|
|
while i < len(sys.argv) and sys.argv[i][0] == "-":
|
|
|
|
opt = sys.argv[i]
|
|
|
|
i += 1
|
|
|
|
if opt == "-h" or opt == "--help":
|
|
|
|
help()
|
|
|
|
return
|
|
|
|
elif opt == "-p":
|
|
|
|
install_path = sys.argv[i]
|
|
|
|
i += 1
|
|
|
|
elif opt == "-r":
|
|
|
|
list_file = sys.argv[i]
|
|
|
|
i += 1
|
|
|
|
with open(list_file) as f:
|
|
|
|
while True:
|
|
|
|
l = f.readline()
|
|
|
|
if not l:
|
|
|
|
break
|
2017-04-30 17:03:45 -04:00
|
|
|
if l[0] == "#":
|
|
|
|
continue
|
2016-11-07 10:13:56 -05:00
|
|
|
to_install.append(l.rstrip())
|
2021-04-22 20:55:39 -04:00
|
|
|
elif opt == "-i":
|
|
|
|
index_urls = [sys.argv[i]]
|
|
|
|
i += 1
|
2016-11-07 10:13:56 -05:00
|
|
|
elif opt == "--debug":
|
|
|
|
debug = True
|
|
|
|
else:
|
|
|
|
fatal("Unknown/unsupported option: " + opt)
|
|
|
|
|
|
|
|
to_install.extend(sys.argv[i:])
|
|
|
|
if not to_install:
|
|
|
|
help()
|
|
|
|
return
|
|
|
|
|
|
|
|
install(to_install)
|
|
|
|
|
|
|
|
if not debug:
|
|
|
|
cleanup()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|