本文整理汇总了Python中apt.debfile.DebPackage.data_content方法的典型用法代码示例。如果您正苦于以下问题:Python DebPackage.data_content方法的具体用法?Python DebPackage.data_content怎么用?Python DebPackage.data_content使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apt.debfile.DebPackage
的用法示例。
在下文中一共展示了DebPackage.data_content方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_changelog
# 需要导入模块: from apt.debfile import DebPackage [as 别名]
# 或者: from apt.debfile.DebPackage import data_content [as 别名]
#.........这里部分代码省略.........
else:
source_is_cache = uri.startswith(self.apt_cache_path)
if _DEBUG: print("Using cached package:" if source_is_cache else
"Downloading: %s (%.2f MB)" % (uri, r.length / self.MB))
try:
if not source_is_cache:
# download stream to temporary file
tmpFile = tempfile.NamedTemporaryFile(prefix="apt-changelog-")
if self.interactive and r.length:
# download chunks with progress indicator
recv_length = 0
blocks = 60
for data in r.iter_content(chunk_size=16384):
recv_length += len(data)
tmpFile.write(data)
recv_pct = recv_length / r.length
recv_blocks = int(blocks * recv_pct)
print("\r[%(progress)s%(spacer)s] %(percentage).1f%%" %
{
"progress": "=" * recv_blocks,
"spacer": " " * (blocks - recv_blocks),
"percentage": recv_pct * 100
}, end="", flush=True)
# clear progress bar when done
print("\r" + " " * (blocks + 10), end="\r", flush=True)
else:
# no content-length or non-interactive, download in one go
# up to the configured max_download_size, ask only when
# exceeded
r.raw.decode_content = True
size = 0
size_exceeded = False
while True:
buf = r.raw.read(16*1024)
if not size_exceeded:
size += len(buf)
if size > self.max_download_size:
if not self.user_confirm(self.max_download_size_msg_unknown):
r.close()
tmpFile.close()
return ""
else:
size_exceeded = True
if not buf:
break
tmpFile.write(buf)
r.close()
tmpFile.seek(0)
if uri.endswith(".deb"):
# process .deb file
if source_is_cache:
f = uri
else:
f = tmpFile.name
# We could copy the downloaded .deb files to the apt
# cache here but then we'd need to run the script elevated:
# shutil.copy(f, self.apt_cache_path + os.path.basename(uri))
deb = DebPackage(f)
changelog_file = self.get_changelog_from_filelist(deb.filelist)
if changelog_file:
changelog = deb.data_content(changelog_file)
if changelog.startswith('Automatically decompressed:'):
changelog = changelog[29:]
else:
raise ValueError('Malformed Debian package')
elif uri.endswith(".diff.gz"):
# Ubuntu partner repo has .diff.gz files,
# we can extract a changelog from that
data = gzip.open(tmpFile.name, "r").read().decode('utf-8')
additions = data.split("+++")
for addition in additions:
lines = addition.split("\n")
if "/debian/changelog" in lines[0]:
for line in lines[2:]:
if line.startswith("+"):
changelog += "%s\n" % line[1:]
else:
break
if not changelog:
raise ValueError('No changelog in .diff.gz')
else:
# process .tar.xz file
with tarfile.open(fileobj=tmpFile, mode="r:xz") as tar:
changelog_file = self.get_changelog_from_filelist(
[s.name for s in tar.getmembers() if s.type in (b"0", b"2")])
if changelog_file:
changelog = tar.extractfile(changelog_file).read().decode()
else:
raise ValueError('No changelog in source package')
except Exception as e:
_generic_exception_handler(e)
self.exit_on_fail(520)
if 'tmpFile' in vars():
try:
tmpFile.close()
except Exception as e:
_generic_exception_handler(e)
# ALL DONE
return changelog