def test_append_existing_binary_file(self): """ The L{append_text_file} function appends contents to an existing file. """ existing_file = self.makeFile("foo bar") append_binary_file(existing_file, b" baz \xe2\x98\x83") self.assertFileContent(existing_file, b"foo bar baz \xe2\x98\x83")
def test_append_binary_no_file(self): """ The L{append_text_file} function creates a new file if one doesn't exist already. """ new_file = os.path.join(self.makeDir(), "new_file") append_binary_file(new_file, b"contents \xe2\x98\x83") self.assertFileContent(new_file, b"contents \xe2\x98\x83")
def _install_deb_file(self, path): """Fake the the given deb file is installed in the system.""" deb_file = open(path) deb = apt_inst.DebFile(deb_file) control = deb.control.extractdata("control") deb_file.close() lines = control.splitlines() lines.insert(1, b"Status: install ok installed") status = b"\n".join(lines) append_binary_file(self.dpkg_status, status + b"\n\n")
def _add_package(self, packages_file, name, architecture="all", version="1.0", description="description", control_fields=None): if control_fields is None: control_fields = {} package_stanza = textwrap.dedent( """ Package: %(name)s Priority: optional Section: misc Installed-Size: 1234 Maintainer: Someone Architecture: %(architecture)s Source: source Version: %(version)s Description: short description %(description)s """ % { "name": name, "version": version, "architecture": architecture, "description": description }).encode("utf-8") # We want to re-order the TagSection, but it requires bytes as input. # As we also want to write a binary file, we have to explicitly pass # the hardly documented `bytes=True` to TagSection as it would be # returned as unicode in Python 3 otherwise. In future versions of # apt_pkg there should be a TagSection.write() which is recommended. package_stanza = apt_pkg.rewrite_section( apt_pkg.TagSection(package_stanza, bytes=True), apt_pkg.REWRITE_PACKAGE_ORDER, list(control_fields.items())) append_binary_file(packages_file, b"\n" + package_stanza + b"\n")