mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-13 08:01:09 +00:00
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:
parent
243a728d6c
commit
d8df8cc869
@ -53,7 +53,7 @@ from ubuntutools.lp.udtexceptions import (PackageNotFoundException,
|
|||||||
PocketDoesNotExistError,
|
PocketDoesNotExistError,
|
||||||
InvalidDistroValueError)
|
InvalidDistroValueError)
|
||||||
from ubuntutools.misc import (download,
|
from ubuntutools.misc import (download,
|
||||||
download_text,
|
download_bytes,
|
||||||
verify_file_checksum,
|
verify_file_checksum,
|
||||||
verify_file_checksums,
|
verify_file_checksums,
|
||||||
DownloadError)
|
DownloadError)
|
||||||
@ -179,7 +179,7 @@ class SourcePackage(ABC):
|
|||||||
|
|
||||||
# If provided a dscfile, process it now to set our source and version
|
# If provided a dscfile, process it now to set our source and version
|
||||||
if self._dsc_source:
|
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.source = self._dsc['Source']
|
||||||
self._version = Version(self._dsc['Version'])
|
self._version = Version(self._dsc['Version'])
|
||||||
self._check_dsc_signature()
|
self._check_dsc_signature()
|
||||||
|
@ -339,20 +339,29 @@ def download(src, dst, size=0):
|
|||||||
size / 1024.0 / 1024))
|
size / 1024.0 / 1024))
|
||||||
|
|
||||||
|
|
||||||
def download_text(src, mode='r'):
|
def _download_text(src, binary):
|
||||||
""" return the text content of a downloaded file
|
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
|
src: str
|
||||||
Source to copy from (file path or url)
|
Source to copy from (file path or url)
|
||||||
mode: str
|
mode: str
|
||||||
Mode to use with open()
|
Deprecated, ignored unless a string that contains 'b'
|
||||||
|
|
||||||
Raises the same exceptions as download()
|
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:
|
return _download_text(src, binary='b' in (mode or ''))
|
||||||
dst = os.path.join(d, 'dst')
|
|
||||||
download(src, dst)
|
|
||||||
with open(dst, mode=mode) as f:
|
def download_bytes(src):
|
||||||
return f.read()
|
""" Same as download_text() but returns bytes """
|
||||||
|
return _download_text(src, binary=True)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user