Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bytes and strings from RPM (#1764642) #23

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 25 additions & 5 deletions meh/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,17 @@ def get_package_and_component(file_=sys.argv[0]):
except StopIteration:
raise RPMinfoError("Cannot get package and component for file "+
"{0}".format(file_))
pkg_info = PackageInfo(header["name"].decode("utf-8"), header["version"].decode("utf-8"),
header["release"].decode("utf-8"),
u"%d" % header["epoch"] if header["epoch"] else u"0",
header["arch"].decode("utf-8"))

pkg_info = PackageInfo(
decode_bytes(header["name"]),
decode_bytes(header["version"]),
decode_bytes(header["release"]),
decode_bytes(header["epoch"]) if header["epoch"] else "0",
decode_bytes(header["arch"])
)

# cuts the name from the NVR format: foo-blah-2.8.8-2.fc17.src.rpm
srpm_name = header["sourcerpm"].decode("utf-8")
srpm_name = decode_bytes(header["sourcerpm"])
name_end = len(srpm_name)
try:
name_end = srpm_name.rindex('-')
Expand All @@ -174,6 +178,22 @@ def get_package_and_component(file_=sys.argv[0]):

return (pkg_info, component)

def decode_bytes(data):
"""Decode the given bytes.

Return the given string or a string decoded from the given bytes.

:param data: bytes or a string
:return: a string
"""
if isinstance(data, str):
return data

if isinstance(data, bytes):
return data.decode('utf-8')

raise ValueError("Unsupported type '{}'.".format(type(data).__name__))

def get_release_version():
"""Returns release version (according to RELEASE_NAME_FILE)"""
try:
Expand Down