Skip to content

Commit 11c9656

Browse files
glenn20dpgeorge
authored andcommitted
tools/mpremote: Support mip install from package.json on local fs.
Add support for `mpremote mip install package.json` where `package.json` is a json file on the local filesystem. Without this, package json files can only be loaded from http, https, github or gitlab URLs. This is useful for testing `package.json` files for pacages in development and for constructing one's own `package.json` files for Python packages which are not yet available for installation using mip. Signed-off-by: Glenn Moloney <[email protected]>
1 parent 752c167 commit 11c9656

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

tools/mpremote/mpremote/mip.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
_PACKAGE_INDEX = "https://micropython.org/pi/v2"
1515

16+
allowed_mip_url_prefixes = ("http://", "https://", "github:", "gitlab:")
17+
1618

1719
# This implements os.makedirs(os.dirname(path))
1820
def _ensure_path_exists(transport, path):
@@ -78,16 +80,25 @@ def _download_file(transport, url, dest):
7880

7981

8082
def _install_json(transport, package_json_url, index, target, version, mpy):
81-
try:
82-
with urllib.request.urlopen(_rewrite_url(package_json_url, version)) as response:
83-
package_json = json.load(response)
84-
except urllib.error.HTTPError as e:
85-
if e.status == 404:
86-
raise CommandError(f"Package not found: {package_json_url}")
87-
else:
88-
raise CommandError(f"Error {e.status} requesting {package_json_url}")
89-
except urllib.error.URLError as e:
90-
raise CommandError(f"{e.reason} requesting {package_json_url}")
83+
if package_json_url.startswith(allowed_mip_url_prefixes):
84+
try:
85+
with urllib.request.urlopen(_rewrite_url(package_json_url, version)) as response:
86+
package_json = json.load(response)
87+
except urllib.error.HTTPError as e:
88+
if e.status == 404:
89+
raise CommandError(f"Package not found: {package_json_url}")
90+
else:
91+
raise CommandError(f"Error {e.status} requesting {package_json_url}")
92+
except urllib.error.URLError as e:
93+
raise CommandError(f"{e.reason} requesting {package_json_url}")
94+
elif package_json_url.endswith(".json"):
95+
try:
96+
with open(package_json_url, "r") as f:
97+
package_json = json.load(f)
98+
except OSError:
99+
raise CommandError(f"Error opening {package_json_url}")
100+
else:
101+
raise CommandError(f"Invalid url for package: {package_json_url}")
91102
for target_path, short_hash in package_json.get("hashes", ()):
92103
fs_target_path = target + "/" + target_path
93104
file_url = f"{index}/file/{short_hash[:2]}/{short_hash}"
@@ -100,12 +111,7 @@ def _install_json(transport, package_json_url, index, target, version, mpy):
100111

101112

102113
def _install_package(transport, package, index, target, version, mpy):
103-
if (
104-
package.startswith("http://")
105-
or package.startswith("https://")
106-
or package.startswith("github:")
107-
or package.startswith("gitlab:")
108-
):
114+
if package.startswith(allowed_mip_url_prefixes):
109115
if package.endswith(".py") or package.endswith(".mpy"):
110116
print(f"Downloading {package} to {target}")
111117
_download_file(
@@ -118,6 +124,8 @@ def _install_package(transport, package, index, target, version, mpy):
118124
package += "/"
119125
package += "package.json"
120126
print(f"Installing {package} to {target}")
127+
elif package.endswith(".json"):
128+
pass
121129
else:
122130
if not version:
123131
version = "latest"

0 commit comments

Comments
 (0)