def parse_alpine_installed_db(location): """ Yield AlpinePackage objects from an installed database file at `location` or None. Typically found at '/lib/apk/db/installed' in an Alpine installation. Note: http://uk.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz are also in the same format as an installed database. """ if not path.exists(location): return with open(location, 'rb') as f: installed = as_unicode(f.read()) if installed: # each paragrpah is seprated by LFLF packages = (p for p in re.split('\n\n', installed) if p) for pkg in packages: try: fields = email.message_from_string(pkg) except UnicodeEncodeError: fields = email.message_from_string(pkg.encode('utf-8')) fields = [( n.strip(), v.strip(), ) for n, v in fields.items()] yield build_package(fields)
def parse_rpm_xmlish(location, detect_licenses=False): """ Yield RpmPackage(s) from a RPM XML'ish file at `location`. This is a file created with rpm and the xml query option. """ if not location or not os.path.exists(location): return # there are smetimes weird encodings. We avoid issues there with open(location, 'rb') as f: rpms = as_unicode(f.read()) # The XML'ish format is in fact multiple XML documents, one for each package # wrapped in an <rpmHeader> root element. So we add a global root element # to make this a valid XML document. for rpm_raw_tags in collect_rpms(rpms): tags = collect_tags(rpm_raw_tags) pkg = build_package(tags) if detect_licenses: pkg.license_expression = pkg.compute_normalized_license() yield pkg
def parse_rpm_xmlish(location, datasource_id, package_type): """ Yield PackageData built from an RPM XML'ish file at ``location``. This is a file created with the rpm CLI with the xml query option. """ if not location or not os.path.exists(location): return # there are smetimes weird encodings. We avoid issues there with open(location, 'rb') as f: rpms = as_unicode(f.read()) # The XML'ish format is in fact multiple XML documents, one for each package # wrapped in an <rpmHeader> root element. So we add a global root element # to make this a valid XML document. for rpm_raw_tags in collect_rpms(rpms): tags = collect_tags(rpm_raw_tags) yield build_package( rpm_tags=tags, datasource_id=datasource_id, package_type=package_type, )
def test_as_unicode_from_unicode_replaces_null_bytes_with_space(self): test = '\x00is designed to give them, \x00BEFORE the\x00\x00\x00\x00\x00\x00' result = as_unicode(test) expected = ' is designed to give them, BEFORE the ' assert expected == result
def test_as_unicode_converts_bytes_to_unicode(self): test_line = ' // as defined in https://tools.ietf.org/html/rfc2821#section-4.1.2.'.encode( ) result = as_unicode(test_line) assert type(result) == str