Universal configurable base tmpdir for all gbp rpm tools
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 12 Jul 2012 06:07:47 +0000 (09:07 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Fri, 14 Nov 2014 12:45:07 +0000 (14:45 +0200)
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/scripts/buildpackage_rpm.py
gbp/scripts/import_orig_rpm.py
gbp/scripts/pq_rpm.py

index a84c378..22f1eea 100755 (executable)
@@ -22,12 +22,12 @@ import ConfigParser
 import errno
 import os, os.path
 import sys
-import tempfile
 import shutil
 import re
 from datetime import datetime
 import gzip
 
+import gbp.tmpfile as tempfile
 import gbp.rpm as rpm
 from gbp.rpm.policy import RpmPkgPolicy
 from gbp.command_wrappers import (Command,
@@ -46,7 +46,8 @@ from gbp.pkg import (compressor_opts, compressor_aliases)
 from gbp.scripts.pq_rpm import update_patch_series
 
 
-def git_archive(repo, spec, output_dir, treeish, prefix, comp_level, with_submodules):
+def git_archive(repo, spec, output_dir, tmpdir_base, treeish, prefix,
+                comp_level, with_submodules):
     "create a compressed orig tarball in output_dir using git_archive"
     comp_opts = ''
     if spec.orig_src['compression']:
@@ -59,8 +60,9 @@ def git_archive(repo, spec, output_dir, treeish, prefix, comp_level, with_submod
     try:
         if repo.has_submodules() and with_submodules:
             repo.update_submodules()
-            git_archive_submodules(repo, treeish, output, prefix,
-                                   spec.orig_src['compression'], comp_level, comp_opts,
+            git_archive_submodules(repo, treeish, output, tmpdir_base,
+                                   prefix, spec.orig_src['compression'],
+                                   comp_level, comp_opts,
                                    spec.orig_src['archive_fmt'])
 
         else:
@@ -184,7 +186,7 @@ def git_archive_build_orig(repo, spec, output_dir, options):
     if spec.orig_src['compression']:
         gbp.log.debug("Building upstream source archive with compression '%s -%s'" %
                       (spec.orig_src['compression'], options.comp_level))
-    if not git_archive(repo, spec, output_dir, upstream_tree,
+    if not git_archive(repo, spec, output_dir, options.tmp_dir, upstream_tree,
                        options.orig_prefix,
                        options.comp_level,
                        options.with_submodules):
@@ -303,6 +305,7 @@ def parse_args(argv, prefix):
     parser.add_boolean_config_file_option(option_name = "ignore-new", dest="ignore_new")
     parser.add_option("--git-verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
+    parser.add_config_file_option(option_name="tmp-dir", dest="tmp_dir")
     parser.add_config_file_option(option_name="color", dest="color", type='tristate')
     parser.add_config_file_option(option_name="notify", dest="notify", type='tristate')
     parser.add_config_file_option(option_name="vendor", action="store", dest="vendor")
@@ -387,7 +390,6 @@ def main(argv):
     retval = 0
     prefix = "git-"
     spec = None
-    dump_dir = None
 
     options, gbp_args, builder_args = parse_args(argv, prefix)
     if not options:
@@ -402,6 +404,14 @@ def main(argv):
         repo_dir = os.path.abspath(os.path.curdir)
 
     try:
+        # Create base temporary directory for this run
+        options.tmp_dir = tempfile.mkdtemp(dir=options.tmp_dir,
+                                           prefix='buildpackage-rpm_')
+    except GbpError, err:
+        gbp.log.err(err)
+        return 1
+
+    try:
         branch = repo.get_branch()
     except GitRepositoryError:
         branch = None
@@ -436,7 +446,8 @@ def main(argv):
             raise GbpError('Invalid treeish object %s' % tree)
 
         # Dump from git to a temporary directory:
-        dump_dir = tempfile.mkdtemp(dir=".")
+        dump_dir = tempfile.mkdtemp(dir=options.tmp_dir,
+                                    prefix='dump_tree_')
         gbp.log.debug("Dumping tree '%s' to '%s'" % (options.export, dump_dir))
         if not dump_tree(repo, dump_dir, tree, options.with_submodules):
             raise GbpError
@@ -501,8 +512,10 @@ def main(argv):
                     gbp.log.info("Creating (native) source archive %s from '%s'" % (spec.orig_src['filename'], tree))
                     if spec.orig_src['compression']:
                         gbp.log.debug("Building source archive with compression '%s -%s'" % (spec.orig_src['compression'], options.comp_level))
-                    if not git_archive(repo, spec, source_dir, tree, options.orig_prefix,
-                                       options.comp_level, options.with_submodules):
+                    if not git_archive(repo, spec, source_dir, options.tmp_dir,
+                                       tree, options.orig_prefix,
+                                       options.comp_level,
+                                       options.with_submodules):
                         raise GbpError, "Cannot create source tarball at '%s'" % export_dir
             # Non-native packages: create orig tarball from upstream
             elif spec.orig_src:
@@ -563,10 +576,7 @@ def main(argv):
         retval = 1
     finally:
         drop_index(repo)
-
-    # Clean temporary dumpdir
-    if dump_dir and retval == 0:
-        shutil.rmtree(dump_dir)
+        shutil.rmtree(options.tmp_dir)
 
     if not options.tag_only:
         if spec and options.notify:
index 1653fa6..d1ec57a 100755 (executable)
@@ -21,7 +21,7 @@
 import ConfigParser
 import os
 import sys
-import tempfile
+import gbp.tmpfile as tempfile
 import gbp.command_wrappers as gbpc
 import string
 from gbp.pkg import parse_archive_filename
@@ -185,6 +185,7 @@ def parse_args(argv):
     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
     parser.add_config_file_option(option_name="color", dest="color", type='tristate')
+    parser.add_config_file_option(option_name="tmp-dir", dest="tmp_dir")
 
     (options, args) = parser.parse_args(argv[1:])
     gbp.log.setup(options.color, options.verbose)
@@ -199,7 +200,7 @@ def main(argv):
     if not options:
         return 1
 
-    tmpdir = tempfile.mkdtemp(dir='../')
+    tmpdir = tempfile.mkdtemp(dir=options.tmp_dir, prefix='import-orig-rpm_')
     try:
         source = find_source(options, args)
         try:
index 377ae9f..9c1493b 100755 (executable)
@@ -23,10 +23,10 @@ import errno
 import os
 import shutil
 import sys
-import tempfile
 import re
 import gzip
 import subprocess
+import gbp.tmpfile as tempfile
 from gbp.config import (GbpOptionParserRpm, GbpOptionGroup)
 from gbp.rpm.git import (GitRepositoryError, RpmGitRepository)
 from gbp.git.modifier import GitModifier, GitTz
@@ -218,7 +218,7 @@ def safe_patches(queue, tmpdir_base):
     @rtype: tuple
     """
 
-    tmpdir = tempfile.mkdtemp(dir=tmpdir_base, prefix='gbp-pq')
+    tmpdir = tempfile.mkdtemp(dir=tmpdir_base, prefix='patchimport_')
     safequeue=PatchSeries()
 
     if len(queue) > 0:
@@ -309,7 +309,7 @@ def import_spec_patches(repo, branch, options):
     queue = spec.patchseries()
     packager = get_packager(spec)
     # Put patches in a safe place
-    tmpdir, queue = safe_patches(queue, repo.path)
+    tmpdir, queue = safe_patches(queue, options.tmp_dir)
     for commit in commits:
         try:
             gbp.log.info("Trying to apply patches at '%s'" % commit)
@@ -332,10 +332,6 @@ def import_spec_patches(repo, branch, options):
     else:
         raise GbpError, "Couldn't apply patches"
 
-    if tmpdir:
-        gbp.log.debug("Remove temporary patch safe '%s'" % tmpdir)
-        shutil.rmtree(tmpdir)
-
     repo.set_branch(branch)
 
     return os.path.basename(spec.specfile)
@@ -409,6 +405,7 @@ def main(argv):
                       help="In case of import even import if the branch already exists")
     parser.add_config_file_option(option_name="vendor", action="store", dest="vendor")
     parser.add_config_file_option(option_name="color", dest="color", type='tristate')
+    parser.add_config_file_option(option_name="tmp-dir", dest="tmp_dir")
     parser.add_config_file_option(option_name="upstream-tag", dest="upstream_tag")
     parser.add_config_file_option(option_name="spec-file", dest="spec_file")
     parser.add_config_file_option(option_name="packaging-dir", dest="packaging_dir")
@@ -456,6 +453,9 @@ def main(argv):
         os.chdir(repo.path)
 
     try:
+        # Create base temporary directory for this run
+        options.tmp_dir = tempfile.mkdtemp(dir=options.tmp_dir,
+                                           prefix='gbp-pq-rpm_')
         current = repo.get_branch()
         if action == "export":
             export_patches(repo, current, options)
@@ -482,6 +482,8 @@ def main(argv):
         if len(err.__str__()):
             gbp.log.err(err)
         retval = 1
+    finally:
+        shutil.rmtree(options.tmp_dir, ignore_errors=True)
 
     return retval