From 5b6a41d12fa4a5795daca3d016ce2e27a55c8adf Mon Sep 17 00:00:00 2001 From: David Shea Date: Dec 18 2018 14:53:59 +0000 Subject: [PATCH 1/2] Do not reencode binary source file data. If the SourceFile content is already an instance of 'bytes', it does not need further encoding before being written to the filesystem. This works in both Python 2.6+ and Python 3. --- diff --git a/rpmfluff.py b/rpmfluff.py index 96e54fa..911f04a 100644 --- a/rpmfluff.py +++ b/rpmfluff.py @@ -380,7 +380,10 @@ class SourceFile: def _get_dst_file(self, sourcesDir): import codecs dstFileName = os.path.join(sourcesDir, self.sourceName) - dstFile = codecs.open(dstFileName, "wb", self.encoding) + if isinstance(self.content, bytes): + dstFile = open(dstFileName, "wb") + else: + dstFile = codecs.open(dstFileName, "wb", self.encoding) return dstFile def write_file(self, sourcesDir): From a4548a41efdf6d4545c92242cddf89353a59f32f Mon Sep 17 00:00:00 2001 From: David Shea Date: Dec 18 2018 14:53:59 +0000 Subject: [PATCH 2/2] Add a add_manpage function This use case is slightly different from add_simple_payload_file. Since rpmbuild compresses man pages, the source file name and the %files line may not match. --- diff --git a/rpmfluff.py b/rpmfluff.py index 911f04a..1414602 100644 --- a/rpmfluff.py +++ b/rpmfluff.py @@ -476,6 +476,17 @@ defaultChangelogFormat = """* Sun Jul 22 2018 John Doe - %s-% - Initial version """ +sample_man_page = u""".TH FOO "1" "May 2009" "foo 1.00" "User Commands" +.SH NAME +foo \\- Frobnicates the doohickey +.SH SYNOPSIS +.B foo +[\\fIOPTION\\fR]... + +.SH DESCRIPTION +A sample manpage +""" + def get_expected_arch(): # FIXME: do this by directly querying rpm python bindings: evalArch = subprocess.check_output(['rpm', '--eval', '%{_arch}']) @@ -1212,6 +1223,28 @@ class SimpleRpmBuild(RpmBuild): for file in contents: sub.section_files += '/%s/%s/%s\n' % (installPath, internalPath, file.sourceName) + def add_manpage(self, + sourceFileName='foo.1', + sourceFileContent=sample_man_page, + installPath='usr/share/man/man1/foo.1', + createParentDirs=True, + subpackageSuffix=None): + sourceIndex = self.add_source(SourceFile(sourceFileName, sourceFileContent)) + if createParentDirs: + self.create_parent_dirs(installPath) + self.section_install += 'cp %%{SOURCE%i} $RPM_BUILD_ROOT/%s\n' % (sourceIndex, self.escape_path(installPath)) + + # brp-compress will compress all man pages. If the man page is already + # compressed, it will decompress the page and recompress it. + (installBase, installExt) = os.path.splitext(installPath) + if installExt in ('.gz', '.Z', '.bz2', '.xz', '.lzma'): + finalPath = installBase + '.gz' + else: + finalPath = installPath + '.gz' + + sub = self.get_subpackage(subpackageSuffix) + sub.section_files += '/%s\n' % finalPath + self.add_payload_check(finalPath, subpackageSuffix) class YumRepoBuild: """Class for easily creating a yum repo from a collection of RpmBuild instances""" @@ -1842,6 +1875,27 @@ class TestSimpleRpmBuild(unittest.TestCase): srpmHdr = self.rpmbuild.get_built_srpm_header() self.assertEquals(3, srpmHdr[rpm.RPMTAG_EPOCH]) + def test_add_manpage(self): + self.rpmbuild.add_manpage() + self.rpmbuild.make() + + def test_add_compressed_manpage(self): + """Ensuring that adding an already compressed manpage works correctly""" + import zlib + compressedPage = zlib.compress(sample_man_page.encode('ascii')) + self.rpmbuild.add_manpage(sourceFileName='foo.1.gz', + sourceFileContent=compressedPage, + installPath='usr/share/man/man1/foo.1.gz') + self.rpmbuild.make() + + def test_add_differently_compressed_manpage(self): + """Ensuring that a non-gzip compressed manpage is re-compressed""" + import bz2 + compressedPage = bz2.compress(sample_man_page.encode('ascii')) + self.rpmbuild.add_manpage(sourceFileName='foo.1.bz2', + sourceFileContent=compressedPage, + installPath='usr/share/man/man1/foo.1.bz2') + self.rpmbuild.make() class YumRepoBuildTests(unittest.TestCase): def assert_is_dir(self, dirname):