diff --git a/ubuntutools/archive.py b/ubuntutools/archive.py index 6c6b20b..88cbae6 100644 --- a/ubuntutools/archive.py +++ b/ubuntutools/archive.py @@ -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() diff --git a/ubuntutools/misc.py b/ubuntutools/misc.py index 3ac61b2..7ca2c08 100644 --- a/ubuntutools/misc.py +++ b/ubuntutools/misc.py @@ -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)