misc: add download_bytes() and deprecate mode param for download_text()

Passing 'mode' assumes use of open(), but callers don't care about
implementation, just if the returned object is text or bytes
This commit is contained in:
Dan Streetman 2021-07-13 13:18:55 -04:00
parent 243a728d6c
commit d8df8cc869
2 changed files with 20 additions and 11 deletions

View File

@ -53,7 +53,7 @@ from ubuntutools.lp.udtexceptions import (PackageNotFoundException,
PocketDoesNotExistError,
InvalidDistroValueError)
from ubuntutools.misc import (download,
download_text,
download_bytes,
verify_file_checksum,
verify_file_checksums,
DownloadError)
@ -179,7 +179,7 @@ class SourcePackage(ABC):
# If provided a dscfile, process it now to set our source and version
if self._dsc_source:
self._dsc = Dsc(download_text(self._dsc_source, mode='rb'))
self._dsc = Dsc(download_bytes(self._dsc_source))
self.source = self._dsc['Source']
self._version = Version(self._dsc['Version'])
self._check_dsc_signature()

View File

@ -339,20 +339,29 @@ def download(src, dst, size=0):
size / 1024.0 / 1024))
def download_text(src, mode='r'):
""" return the text content of a downloaded file
def _download_text(src, binary):
with tempfile.TemporaryDirectory() as d:
dst = os.path.join(d, 'dst')
download(src, dst)
with open(dst, mode='rb' if binary else 'r') as f:
return f.read()
def download_text(src, mode=None):
""" Return the text content of a downloaded file
src: str
Source to copy from (file path or url)
mode: str
Mode to use with open()
Deprecated, ignored unless a string that contains 'b'
Raises the same exceptions as download()
Returns text (or binary, if mode includes 'b') content of downloaded file
Returns text content of downloaded file
"""
with tempfile.TemporaryDirectory() as d:
dst = os.path.join(d, 'dst')
download(src, dst)
with open(dst, mode=mode) as f:
return f.read()
return _download_text(src, binary='b' in (mode or ''))
def download_bytes(src):
""" Same as download_text() but returns bytes """
return _download_text(src, binary=True)