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, 3 Mar 2015 08:07:46 +0000 (10:07 +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/scripts/buildpackage_rpm.py
gbp/scripts/pq_rpm.py

index 15529f14b71b4ed5bff87e8568390a69bf83b8c4..622ab02fd38b1a4f8fdcd4d1781eb8a43b119331 100644 (file)
@@ -24,6 +24,8 @@
 #packaging-dir=rpm
 # Spec file to be used
 #spec-file = gbp.spec
+# Compress auto-generated patches
+#patch-compress=100k
 # Export patches with numbering in filenames
 #patch-numbers = False
 
index 2ac0056f91888dbd97c651eef3200045c88754b8..7c486c9a19c1e2e2edde3f2d60b5e43de4786fff 100644 (file)
@@ -619,6 +619,8 @@ class GbpOptionParserRpm(GbpOptionParser):
             'export-specdir'            : 'SPECS',
             'export-sourcedir'          : 'SOURCES',
             'spec-vcs-tag'              : '',
+            'patch-export'              : 'False',
+            'patch-compress'            : '0',
             'patch-import'              : 'True',
             'import-files'              : ['.gbp.conf',
                                            'debian/gbp.conf'],
@@ -661,6 +663,13 @@ class GbpOptionParserRpm(GbpOptionParser):
             'spec-vcs-tag':
                 "Set/update the 'VCS:' tag in the spec file, empty value "
                 "removes the tag entirely, default is '%(spec-vcs-tag)s'",
+            'patch-export':
+                "Create patches between upstream and export-treeish, default "
+                "is '%(patch-export)s'",
+            'patch-compress':
+                "Compress (auto-generated) patches larger than given number of "
+                "bytes, 0 never compresses, default is "
+                "'%(patch-compress)s'",
             'patch-import':
                 "Import patches to the packaging branch, default is "
                 "'%(patch-import)s'",
@@ -668,9 +677,6 @@ class GbpOptionParserRpm(GbpOptionParser):
                 "Comma-separated list of additional file(s) to import from "
                 "packaging branch. These will appear as one monolithic patch "
                 "in the pq/development branch. Default is %(import-files)s",
-            'patch-export':
-                "Create patches between upstream and export-treeish, default "
-                "is '%(patch-export)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 06e4a3ad742c7a02d7d2e4aed13aefa6b9e4dee1..c402d2c1fc05374bd26ea468edcda416ad8c6bec 100755 (executable)
@@ -40,7 +40,7 @@ from gbp.scripts.common.buildpackage import (index_name, wc_names,
                                              git_archive_submodules,
                                              git_archive_single, dump_tree,
                                              write_wc, drop_index)
-from gbp.scripts.pq_rpm import parse_spec
+from gbp.scripts.pq_rpm import parse_spec, update_patch_series
 
 
 def makedir(path):
@@ -228,6 +228,12 @@ def git_archive_build_orig(repo, spec, output_dir, options):
     return upstream_tree
 
 
+def export_patches(repo, spec, export_treeish, options):
+    """Generate patches and update spec file"""
+    upstream_tree = get_upstream_tree(repo, spec, options)
+    update_patch_series(repo, spec, upstream_tree, export_treeish, options)
+
+
 def is_native(repo, options):
     """Determine whether a package is native or non-native"""
     if options.native.is_auto():
@@ -459,6 +465,14 @@ def build_parser(name, prefix=None, git_treeish=None):
     export_group.add_config_file_option(option_name="spec-file",
                     dest="spec_file")
     export_group.add_config_file_option("spec-vcs-tag", dest="spec_vcs_tag")
+    export_group.add_boolean_config_file_option("patch-export",
+                    dest="patch_export")
+    export_group.add_option("--git-patch-export-rev", dest="patch_export_rev",
+                    metavar="TREEISH",
+                    help="Export patches from TREEISH")
+    export_group.add_boolean_config_file_option(option_name="patch-numbers",
+                    dest="patch_numbers")
+    export_group.add_config_file_option("patch-compress", dest="patch_compress")
     return parser
 
 
@@ -486,6 +500,8 @@ def parse_args(argv, prefix, git_treeish=None):
                         (prefix, prefix, prefix))
             return None, None, None
 
+    options.patch_compress = rpm.string_to_int(options.patch_compress)
+
     return options, args, builder_args
 
 
@@ -553,6 +569,14 @@ def main(argv):
             # Setup builder opts
             setup_builder(options, builder_args)
 
+            # Generate patches, if requested
+            if options.patch_export and not is_native(repo, options):
+                if options.patch_export_rev:
+                    patch_tree = get_tree(repo, options.patch_export_rev)
+                else:
+                    patch_tree = tree
+                export_patches(repo, spec, patch_tree, options)
+
             # Prepare final export dirs
             export_dir = makedir(options.export_dir)
             source_dir = makedir(os.path.join(export_dir,
index 655b4b57eefaac412edd768c7769478b9450f238..77df2ca32faed1003dfdd2da65fbd18e344e3690 100755 (executable)
@@ -25,6 +25,7 @@ import gzip
 import os
 import re
 import shutil
+import subprocess
 import sys
 
 import gbp.log
@@ -37,7 +38,7 @@ from gbp.errors import GbpError
 from gbp.patch_series import PatchSeries, Patch
 from gbp.pkg import parse_archive_filename
 from gbp.rpm import (SpecFile, NoSpecError, guess_spec, guess_spec_repo,
-                     spec_from_repo)
+                     spec_from_repo, 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,
             apply_and_commit_patch, drop_pq)
@@ -54,6 +55,24 @@ def is_ancestor(repo, parent, child):
         merge_base = None
     return merge_base == parent_sha1
 
+
+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
@@ -122,6 +141,9 @@ def generate_patches(repo, start, end, outdir, options):
         if patch_fn:
             patches.append(patch_fn)
 
+    # Compress
+    patches = compress_patches(patches, options.patch_compress)
+
     return patches, commands
 
 
@@ -473,6 +495,8 @@ switch         Switch to patch-queue branch and vice versa.""")
     parser.add_config_file_option(option_name="import-files",
             dest="import_files", type="string", action="callback",
             callback=optparse_split_cb)
+    parser.add_config_file_option("patch-compress",
+                                  dest="patch_compress")
 
     return parser
 
@@ -482,7 +506,10 @@ def parse_args(argv):
     parser = build_parser(argv[0])
     if not parser:
         return None, None
-    return parser.parse_args(argv)
+
+    options, args = parser.parse_args(argv)
+    options.patch_compress = string_to_int(options.patch_compress)
+    return options, args
 
 
 def main(argv):