rpm: support generating compressed patches
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 7 Jun 2012 14:20:21 +0000 (17:20 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 7 Jan 2014 14:21:29 +0000 (16:21 +0200)
This patch adds the support to generate compressed patches. User can
define the patch file size limit after which patches gzipped.

Re-writes the write_patch() function(s) and now uses the same-and-only
write_patch() for buildpackage-rpm and pq-rpm.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp-rpm.conf
gbp/config.py
gbp/rpm/__init__.py
gbp/scripts/buildpackage_rpm.py
gbp/scripts/pq_rpm.py

index 15529f14b71b4ed5bff87e8568390a69bf83b8c4..2e31a6859672fcbc5c58a94acdb2fc17ba67a0b5 100644 (file)
@@ -24,6 +24,8 @@
 #packaging-dir=rpm
 # Spec file to be used
 #spec-file = gbp.spec
+# Compress auto-generated patches
+#patch-export-compress=100k
 # Export patches with numbering in filenames
 #patch-numbers = False
 
index c301c20f92bc8a14467c42000b8a7e7afea6ef0d..de45c237a82e50602e766d6bea5a776d0938028e 100644 (file)
@@ -550,6 +550,7 @@ class GbpOptionParserRpm(GbpOptionParser):
                        'rpmbuild-srpmdir'       : 'SRPMS',
                        'rpmbuild-buildrootdir'  : 'BUILDROOT',
                        'patch-export'           : 'False',
+                       'patch-export-compress'  : '0',
                        'pristine-tarball-name'  : 'auto',
                      } )
 
@@ -569,6 +570,8 @@ class GbpOptionParserRpm(GbpOptionParser):
                         "build with untracked files in the source tree, default is '%(ignore-untracked)s'",
                    'patch-export':
                         "Create patches between upstream and export-treeish, default is '%(patch-export)s'",
+                   'patch-export-compress':
+                        "Compress (auto-generated) patches larger than given number of bytes, 0 never compresses, default is '%(patch-export-compress)s'",
                    'pristine-tarball-name':
                         "Filename to record to pristine-tar, set to 'auto' to not mangle the file name, default is '%(pristine-tarball-name)s'",
                  } )
index 73934b663708cd192b7d954dc44e8a8f863f48ba..5b5bbc2bf71e71dd7618ab8a8b9429fed4940223 100644 (file)
@@ -538,4 +538,33 @@ def guess_spec_repo(repo, branch, packaging_dir):
     raise NoSpecError, "Searching spec from other branch not implemented yet"
 
 
+def string_to_int(val_str):
+    """
+    Convert string of possible unit identifier to int.
+
+    @param val_str: value to be converted
+    @type val_str: C{str}
+    @return: value as integer
+    @rtype: C{int}
+
+    >>> string_to_int("1234")
+    1234
+    >>> string_to_int("123k")
+    125952
+    >>> string_to_int("1234K")
+    1263616
+    >>> string_to_int("1M")
+    1048576
+    """
+    units = {'k': 1024,
+             'm': 1024**2,
+             'g': 1024**3,
+             't': 1024**4}
+
+    if val_str[-1].lower() in units:
+        return int(val_str[:-1]) * units[val_str[-1].lower()]
+    else:
+        return int(val_str)
+
+
 # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·:
index 9adea212d5099a0e14fd02ce000155a37ff1fe4c..520697a200f2b57099fe3cc0df54262140db149e 100755 (executable)
@@ -26,6 +26,7 @@ import tempfile
 import shutil
 import re
 import datetime
+import gzip
 
 import gbp.rpm as rpm
 from gbp.rpm.policy import RpmPkgPolicy
@@ -355,9 +356,12 @@ def parse_args(argv, prefix):
     export_group.add_option("--git-export-only", action="store_true", dest="export_only", default=False,
                       help="only export packaging files, don't build")
     export_group.add_boolean_config_file_option("patch-export", dest="patch_export")
+    export_group.add_config_file_option("patch-export-compress", dest="patch_export_compress")
     export_group.add_boolean_config_file_option(option_name="patch-numbers", dest="patch_numbers")
     options, args = parser.parse_args(args)
 
+    options.patch_export_compress = rpm.string_to_int(options.patch_export_compress)
+
     gbp.log.setup(options.color, options.verbose)
     if options.retag:
         if not options.tag and not options.tag_only:
index 0a21a1d593c7c9214bbaaecea3185f75e0c0b37b..796ea34b3ec097499a07cd41ee468aded054514a 100755 (executable)
@@ -25,6 +25,7 @@ import sys
 import tempfile
 import re
 import gzip
+import subprocess
 from gbp.config import (GbpOptionParserRpm, GbpOptionGroup)
 from gbp.rpm.git import (GitRepositoryError, RpmGitRepository)
 from gbp.git import GitModifier
@@ -34,7 +35,7 @@ from gbp.errors import GbpError
 import gbp.log
 from gbp.patch_series import (PatchSeries, Patch)
 from gbp.pkg import parse_archive_filename
-from gbp.rpm import (SpecFile, guess_spec)
+from gbp.rpm import (SpecFile, guess_spec, string_to_int)
 from gbp.scripts.common.pq import (is_pq_branch, pq_branch_name, pq_branch_base,
                                    parse_gbp_commands, format_patch,
                                    format_diff,
@@ -42,6 +43,23 @@ from gbp.scripts.common.pq import (is_pq_branch, pq_branch_name, pq_branch_base,
                                    apply_and_commit_patch, drop_pq)
 
 
+def compress_patches(patches, compress_size=0):
+    """
+    Rename and/or compress patches
+    """
+    ret_patches = []
+    for patch in patches:
+        # Compress if patch file is larger than "threshold" value
+        suffix = ''
+        if compress_size and os.path.getsize(patch) > compress_size:
+            gbp.log.debug("Compressing %s" % os.path.basename(patch))
+            subprocess.Popen(['gzip', '-n', patch]).communicate()
+            suffix = '.gz'
+
+        ret_patches.append(os.path.basename(patch) + suffix)
+    return ret_patches
+
+
 def generate_patches(repo, start, end, outdir, options):
     """
     Generate patch files from git
@@ -85,6 +103,9 @@ def generate_patches(repo, start, end, outdir, options):
         if patch_fn:
             patches.append(patch_fn)
 
+    # Compress
+    patches = compress_patches(patches, options.patch_export_compress)
+
     return patches, commands
 
 
@@ -365,9 +386,12 @@ def main(argv):
     parser.add_option("--export-rev", action="store", dest="export_rev", default="",
                       help="Export patches from treeish object TREEISH instead "
                            "of head of patch-queue branch", metavar="TREEISH")
+    parser.add_config_file_option("patch-export-compress",
+                                  dest="patch_export_compress")
 
     (options, args) = parser.parse_args(argv)
     gbp.log.setup(options.color, options.verbose)
+    options.patch_export_compress = string_to_int(options.patch_export_compress)
 
     if len(args) < 2:
         gbp.log.err("No action given.")