Skip to content

Commit 244b9e2

Browse files
committed
ota.update: Breaking change: Change handling of auth args for requests.
Drop support for `username` and `password` arguments in OTA methods. Instead, any additional keyword arguments will be passed as keyword arguments to `requests.get()`, eg: - `ota.update.from_file( "http://nas.local/micropython.bin", auth=("username", "password"))`.
1 parent 252bec1 commit 244b9e2

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

README.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ convenience functions which use `OTA` to perform simple OTA firmware updates:
166166
`from_file()` and `from_json()`.
167167

168168
- function `ota.update.from_file(url: str, sha="", length=0, verify=True,
169-
verbose=True, reboot=True, username="", password="")`
169+
verbose=True, reboot=True, **request_args)`
170170

171171
Read a micropython firmware from url and write it to the next ota partition.
172172
sha and length are used to validate the data written to the partition.
@@ -180,11 +180,16 @@ convenience functions which use `OTA` to perform simple OTA firmware updates:
180180
- `verbose=True` (optional) prints out verbose information of what it is doing
181181
- `reboot=True` (optional) Performs a `machine.hard_reset()` 10 seconds after
182182
a successful OTA update.
183-
- 'username' (optional) Username for http authentication.
184-
- 'password' (optional) Password for http authentication.
183+
- `request_args`: any additional keyword arguments will be passed as
184+
keyword arguments to `requests.get()`, eg:
185+
- `ota.update.from_file("http://nas.local/micropython.bin",
186+
auth=("username", "password"))`.
187+
188+
**Note:** The `username` and `password` arguments have been deprecated in
189+
favour of `request-args`.
185190

186191
- function `ota.update.from_json(url: str, sha="", length=0, verify=True,
187-
verbose=True, reboot=True, username="", password="")`
192+
verbose=True, reboot=True, **request_args)`
188193

189194
Read a JSON file from **url** (must end in ".json") containing the **url**,
190195
**sha** and **length** of the firmware file. Then, read the firmware file and
@@ -228,7 +233,7 @@ updates.
228233
- **sha** and **length** may instead be provided as arguments to some
229234
methods below.
230235

231-
- Method: `OTA.from_firmware_file(url: str, sha="", length=0) -> int`
236+
- Method: `OTA.from_firmware_file(url: str, sha="", length=0, **request_args) -> int`
232237

233238
- Read a micropython firmware from **url** and write it to the next **ota**
234239
partition. **sha** and **length** are used to validate the data written to
@@ -237,8 +242,10 @@ updates.
237242
- `url` is a http[s] url or a filename on the device
238243
- `sha` (optional) is the expected sha256sum of the firmware file
239244
- `length` (optional) is the expected length of the firmware file (in bytes)
245+
- `request_args`: any additional keyword arguments will be passed as
246+
keyword arguments to `requests.get()`.
240247

241-
- Method: `OTA.from_json(url: str) -> int`
248+
- Method: `OTA.from_json(url: str, **request_args) -> int`
242249

243250
- Read a JSON file from **url** (must end in ".json") containing the **url**,
244251
**sha** and **length** of the firmware file. Then, read the firmware file
@@ -298,28 +305,26 @@ updates.
298305
#### Examples
299306

300307
```py
301-
from ota.update import OTA
308+
import ota.update
302309

303310
# Write firmware from a url provided in a JSON file
304-
with OTA() as ota:
305-
ota.from_json("http://nas.local/micropython/micropython.json")
311+
ota.update.from_json("http://nas.local/micropython/micropython.json")
306312

307313
# Write firmware from a url or filename and reboot if successful and verified
308-
with OTA(reboot=True) as ota:
309-
ota.from_firmware_file(
310-
"http://nas.local/micropython/micropython.bin",
311-
sha="7920d527d578e90ce074b23f9050ffe4ebbd8809b79da0b81493f6ba721d110e",
312-
length=1558512)
314+
ota.update.from_firmware_file(
315+
"http://nas.local/micropython/micropython.bin",
316+
sha="7920d527d578e90ce074b23f9050ffe4ebbd8809b79da0b81493f6ba721d110e",
317+
length=1558512)
313318

314319
# Write firmware from an open stream:
315-
with OTA() as ota:
320+
with ota.update.OTA() as ota:
316321
with open("/sdcard/micropython.bin", "rb") as f:
317322
ota.from_stream(f)
318323

319324
# Read a firmware file from a serial uart
320325
remaining = 1558512
321326
sha = "7920d527d578e90ce074b23f9050ffe4ebbd8809b79da0b81493f6ba721d110e"
322-
with OTA(length=remaining, sha=sha) as ota:
327+
with ota.update.OTA(length=remaining, sha=sha) as ota:
323328
data = memoryview(bytearray(1024))
324329
gc.collect()
325330
while remaining > 0:
@@ -328,7 +333,7 @@ with OTA(length=remaining, sha=sha) as ota:
328333
remaining -= n
329334

330335
# Used without the "with" statement - must call close() explicitly
331-
ota = OTA()
336+
ota = ota.update.OTA()
332337
ota.from_json("http://nas.local/micropython/micropython.json")
333338
ota.close()
334339
```

mip/ota/update.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,18 @@ def __exit__(self, e_t, e_v, e_tr):
2727

2828

2929
# Open a file or a URL and return a File-like object for reading
30-
def open_url(url: str, username=None, password=None) -> io.BufferedReader:
31-
if url.split(":", 1)[0] not in ("http", "https"):
32-
return open(url, "rb")
33-
else:
30+
def open_url(url_or_filename: str, **kw) -> io.BufferedReader:
31+
if url_or_filename.split(":", 1)[0] in ("http", "https"):
3432
import requests
3533

36-
if username and password:
37-
r = requests.get(url, auth = (username, password))
38-
else:
39-
r = requests.get(url)
40-
34+
r = requests.get(url_or_filename, **kw)
4135
code: int = r.status_code
4236
if code != 200:
4337
r.close()
4438
raise ValueError(f"HTTP Error: {code}")
4539
return SocketWrapper(r.raw) # type: ignore
40+
else:
41+
return open(url_or_filename, "rb")
4642

4743

4844
# OTA manages a MicroPython firmware update over-the-air. It checks that there
@@ -110,24 +106,25 @@ def from_stream(self, f: io.BufferedReader, sha: str = "", length: int = 0) -> i
110106
# - url: a filename or a http[s] url for the micropython.bin firmware.
111107
# - sha: the sha256sum of the firmware file
112108
# - length: the length (in bytes) of the firmware file
113-
def from_firmware_file(self, url: str, sha: str = "", length: int = 0, username=None, password=None) -> int:
109+
def from_firmware_file(self, url: str, sha: str = "", length: int = 0, **kw) -> int:
114110
if self.verbose:
115111
print(f"Opening firmware file {url}...")
116-
with open_url(url, username, password) as f:
112+
with open_url(url, **kw) as f:
117113
return self.from_stream(f, sha, length)
118114

119115
# Load a firmware file, the location of which is read from a json file
120116
# containing the url for the firmware file, the sha and length of the file.
121117
# - url: the name of a file or url containing the json.
122-
def from_json(self, url: str) -> int:
118+
# - kw: extra keywords arguments that will be passed to `requests.get()`
119+
def from_json(self, url: str, **kw) -> int:
123120
if not url.endswith(".json"):
124121
raise ValueError("Url does not end with '.json'")
125122
if self.verbose:
126123
print(f"Opening json file {url}...")
127-
with open_url(url) as f:
128-
import json
124+
with open_url(url, **kw) as f:
125+
from json import load
129126

130-
data: dict = json.load(f)
127+
data: dict = load(f)
131128
try:
132129
firmware: str = data["firmware"]
133130
sha: str = data["sha"]
@@ -136,20 +133,20 @@ def from_json(self, url: str) -> int:
136133
# If firmware filename is relative, append to base of url of json file
137134
baseurl, *_ = url.rsplit("/", 1)
138135
firmware = f"{baseurl}/{firmware}"
139-
return self.from_firmware_file(firmware, sha, length)
136+
return self.from_firmware_file(firmware, sha, length, **kw)
140137
except KeyError as err:
141138
print('OTA json must include "firmware", "sha" and "length" keys.')
142139
raise err
143140

144141

145142
# Convenience functions which use the OTA class to perform OTA updates.
146143
def from_file(
147-
url: str, sha="", length=0, verify=True, verbose=True, reboot=True, username=None, password=None
144+
url: str, sha="", length=0, verify=True, verbose=True, reboot=True, **kw
148145
) -> None:
149146
with OTA(verify, verbose, reboot) as ota_update:
150-
ota_update.from_firmware_file(url, sha, length, username, password)
147+
ota_update.from_firmware_file(url, sha, length, **kw)
151148

152149

153-
def from_json(url: str, verify=True, verbose=True, reboot=True, username=None, password=None):
150+
def from_json(url: str, verify=True, verbose=True, reboot=True, **kw) -> None:
154151
with OTA(verify, verbose, reboot) as ota_update:
155-
ota_update.from_json(url, username, password)
152+
ota_update.from_json(url, **kw)

0 commit comments

Comments
 (0)