13
13
14
14
_PACKAGE_INDEX = "https://micropython.org/pi/v2"
15
15
16
+ allowed_mip_url_prefixes = ("http://" , "https://" , "github:" , "gitlab:" )
17
+
16
18
17
19
# This implements os.makedirs(os.dirname(path))
18
20
def _ensure_path_exists (transport , path ):
@@ -78,16 +80,25 @@ def _download_file(transport, url, dest):
78
80
79
81
80
82
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 } " )
91
102
for target_path , short_hash in package_json .get ("hashes" , ()):
92
103
fs_target_path = target + "/" + target_path
93
104
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):
100
111
101
112
102
113
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 ):
109
115
if package .endswith (".py" ) or package .endswith (".mpy" ):
110
116
print (f"Downloading { package } to { target } " )
111
117
_download_file (
@@ -118,6 +124,8 @@ def _install_package(transport, package, index, target, version, mpy):
118
124
package += "/"
119
125
package += "package.json"
120
126
print (f"Installing { package } to { target } " )
127
+ elif package .endswith (".json" ):
128
+ pass
121
129
else :
122
130
if not version :
123
131
version = "latest"
0 commit comments