From d0812e2c928457f7048fa57d58fa5eafcf25d018 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 12 Jul 2012 09:07:47 +0300 Subject: [PATCH] Universal configurable base tmpdir for all gbp tools Adds support for a configurable tmpdir under which all gbp tools now create their temporary directories and files. NOTE: an exception is git-import-dscs which doesn't use the common configuration system. Signed-off-by: Markus Lehtonen --- gbp/config.py | 4 ++++ gbp/scripts/buildpackage.py | 10 ++++++---- gbp/scripts/common/buildpackage.py | 8 ++++---- gbp/scripts/import_dsc.py | 14 +++++++++----- gbp/scripts/import_orig.py | 6 ++++-- gbp/scripts/pq.py | 9 +++++---- tests/04_test_submodules.py | 8 ++++---- 7 files changed, 36 insertions(+), 23 deletions(-) diff --git a/gbp/config.py b/gbp/config.py index 228c67c..b275ab5 100644 --- a/gbp/config.py +++ b/gbp/config.py @@ -162,6 +162,7 @@ class GbpOptionParser(OptionParser): 'purge': 'True', 'drop': 'False', 'commit': 'False', + 'tmp-dir' : '/var/tmp/gbp/', } help = { 'debian-branch': @@ -310,6 +311,9 @@ class GbpOptionParser(OptionParser): "after export. Default is '%(drop)s'"), 'commit': "commit changes after export, Default is '%(commit)s'", + 'tmp-dir': + ("Base directory under which temporary directories are " + "created, default is '%(tmp-dir)s'"), } def_config_files = [ '/etc/git-buildpackage/gbp.conf', diff --git a/gbp/scripts/buildpackage.py b/gbp/scripts/buildpackage.py index 415ba03..b65f375 100755 --- a/gbp/scripts/buildpackage.py +++ b/gbp/scripts/buildpackage.py @@ -41,7 +41,8 @@ from gbp.scripts.common.buildpackage import (index_name, wc_names, write_wc, drop_index) from gbp.pkg import compressor_opts, compressor_aliases, parse_archive_filename -def git_archive(repo, cp, output_dir, treeish, comp_type, comp_level, with_submodules): +def git_archive(repo, cp, output_dir, tmpdir_base, treeish, comp_type, + comp_level, with_submodules): "create a compressed orig tarball in output_dir using git_archive" try: comp_opts = compressor_opts[comp_type][0] @@ -54,8 +55,8 @@ def git_archive(repo, cp, output_dir, treeish, comp_type, comp_level, with_submo try: if repo.has_submodules() and with_submodules: repo.update_submodules() - git_archive_submodules(repo, treeish, output, prefix, - comp_type, comp_level, comp_opts) + git_archive_submodules(repo, treeish, output, tmpdir_base, + prefix, comp_type, comp_level, comp_opts) else: git_archive_single(repo, treeish, output, prefix, @@ -279,7 +280,7 @@ def git_archive_build_orig(repo, cp, output_dir, options): upstream_tree)) gbp.log.debug("Building upstream tarball with compression '%s -%s'" % (options.comp_type, options.comp_level)) - if not git_archive(repo, cp, output_dir, upstream_tree, + if not git_archive(repo, cp, output_dir, options.tmp_dir, upstream_tree, options.comp_type, options.comp_level, options.with_submodules): @@ -405,6 +406,7 @@ def build_parser(name, prefix=None): parser.add_config_file_option(option_name="color-scheme", dest="color_scheme") parser.add_config_file_option(option_name="notify", dest="notify", type='tristate') + parser.add_config_file_option(option_name="tmp-dir", dest="tmp_dir") tag_group.add_option("--git-tag", action="store_true", dest="tag", default=False, help="create a tag after a successful build") tag_group.add_option("--git-tag-only", action="store_true", dest="tag_only", default=False, diff --git a/gbp/scripts/common/buildpackage.py b/gbp/scripts/common/buildpackage.py index 303adae..670f682 100644 --- a/gbp/scripts/common/buildpackage.py +++ b/gbp/scripts/common/buildpackage.py @@ -19,10 +19,10 @@ """Common functionality for Debian and RPM buildpackage scripts""" import os, os.path -import tempfile import subprocess import shutil +import gbp.tmpfile as tempfile from gbp.command_wrappers import (CatenateTarArchive, CatenateZipArchive) from gbp.git.repository import GitRepository, GitRepositoryError from gbp.errors import GbpError @@ -74,8 +74,8 @@ def compress(cmd, options, output, input_data=None): raise GbpError("Error creating %s: %s" % (output, err)) -def git_archive_submodules(repo, treeish, output, prefix, comp_type, comp_level, - comp_opts, format='tar'): +def git_archive_submodules(repo, treeish, output, tmpdir_base, prefix, + comp_type, comp_level, comp_opts, format='tar'): """ Create a source tree archive with submodules. @@ -85,7 +85,7 @@ def git_archive_submodules(repo, treeish, output, prefix, comp_type, comp_level, Exception handling is left to the caller. """ prefix = sanitize_prefix(prefix) - tempdir = tempfile.mkdtemp() + tempdir = tempfile.mkdtemp(dir=tmpdir_base, prefix='git-archive_') main_archive = os.path.join(tempdir, "main.%s" % format) submodule_archive = os.path.join(tempdir, "submodule.%s" % format) try: diff --git a/gbp/scripts/import_dsc.py b/gbp/scripts/import_dsc.py index f1416d9..304494f 100644 --- a/gbp/scripts/import_dsc.py +++ b/gbp/scripts/import_dsc.py @@ -21,10 +21,10 @@ import sys import re import os import shutil -import tempfile import glob import pipes import time +import gbp.tmpfile as tempfile import gbp.command_wrappers as gbpc from gbp.deb.dscfile import DscFile from gbp.deb.upstreamsource import DebianUpstreamSource @@ -41,7 +41,7 @@ class SkipImport(Exception): pass -def download_source(pkg, dirs, unauth): +def download_source(pkg, dirs, unauth, tmpdir_base): opts = [ '--download-only' ] if unauth: opts.append('--allow-unauthenticated') @@ -53,7 +53,8 @@ def download_source(pkg, dirs, unauth): cmd = 'apt-get' opts += ['-qq', 'source', pkg] - dirs['download'] = os.path.abspath(tempfile.mkdtemp()) + dirs['download'] = tempfile.mkdtemp(dir=tmpdir_base, + prefix='import-dsc_download_') gbp.log.info("Downloading '%s' using '%s'..." % (pkg, cmd)) gbpc.RunAtCommand(cmd, opts, shell=False)(dir=dirs['download']) @@ -227,6 +228,7 @@ def build_parser(name): parser.add_config_file_option(option_name="color", dest="color", type='tristate') parser.add_config_file_option(option_name="color-scheme", dest="color_scheme") + parser.add_config_file_option(option_name="tmp-dir", dest="tmp_dir") parser.add_option("--download", action="store_true", dest="download", default=False, help="download source package") branch_group.add_config_file_option(option_name="debian-branch", @@ -295,7 +297,8 @@ def main(argv): if options.download: dsc = download_source(pkg, dirs=dirs, - unauth=options.allow_unauthenticated) + unauth=options.allow_unauthenticated, + tmpdir_base=options.tmp_dir) else: dsc = pkg @@ -327,7 +330,8 @@ def main(argv): if repo.bare: set_bare_repo_options(options) - dirs['tmp'] = os.path.abspath(tempfile.mkdtemp(dir='..')) + dirs['tmp'] = tempfile.mkdtemp(dir=options.tmp_dir, + prefix='import-dsc_') upstream = DebianUpstreamSource(src.tgz) upstream = upstream.unpack(dirs['tmp'], options.filters) diff --git a/gbp/scripts/import_orig.py b/gbp/scripts/import_orig.py index 12134b7..901a523 100644 --- a/gbp/scripts/import_orig.py +++ b/gbp/scripts/import_orig.py @@ -20,7 +20,7 @@ import ConfigParser import os import sys -import tempfile +import gbp.tmpfile as tempfile import gbp.command_wrappers as gbpc from gbp.deb import (DebianPkgPolicy, parse_changelog_repo) from gbp.deb.upstreamsource import DebianUpstreamSource @@ -200,6 +200,7 @@ def build_parser(name): parser.add_config_file_option(option_name="color", dest="color", type='tristate') parser.add_config_file_option(option_name="color-scheme", dest="color_scheme") + parser.add_config_file_option(option_name="tmp-dir", dest="tmp_dir") # Accepted for compatibility parser.add_option("--no-dch", dest='no_dch', action="store_true", @@ -225,12 +226,13 @@ def parse_args(argv): def main(argv): ret = 0 - tmpdir = tempfile.mkdtemp(dir='../') (options, args) = parse_args(argv) if not options: return 1 + tmpdir = tempfile.mkdtemp(dir=options.tmp_dir, prefix='import-orig_') + try: source = find_source(options.uscan, args) if not source: diff --git a/gbp/scripts/pq.py b/gbp/scripts/pq.py index 515c48a..1421431 100755 --- a/gbp/scripts/pq.py +++ b/gbp/scripts/pq.py @@ -22,8 +22,8 @@ import errno import os import shutil import sys -import tempfile import re +import gbp.tmpfile as tempfile from gbp.config import GbpOptionParserDebian from gbp.git import (GitRepositoryError, GitRepository) from gbp.command_wrappers import (GitCommand, CommandExecFailed) @@ -196,7 +196,7 @@ def export_patches(repo, branch, options): drop_pq(repo, branch) -def safe_patches(series): +def safe_patches(series, tmpdir_base): """ Safe the current patches in a temporary directory below .git/ @@ -209,7 +209,7 @@ def safe_patches(series): src = os.path.dirname(series) name = os.path.basename(series) - tmpdir = tempfile.mkdtemp(dir='.git/', prefix='gbp-pq') + tmpdir = tempfile.mkdtemp(dir=tmpdir_base, prefix='gbp-pq_') patches = os.path.join(tmpdir, 'patches') series = os.path.join(patches, name) @@ -256,7 +256,7 @@ def import_quilt_patches(repo, branch, series, tries, force): # If we go back in history we have to safe our pq so we always try to apply # the latest one if len(commits) > 1: - tmpdir, series = safe_patches(series) + tmpdir, series = safe_patches(series, options.tmp_dir) queue = PatchSeries.read_series_file(series) @@ -343,6 +343,7 @@ def build_parser(name): parser.add_config_file_option(option_name="color-scheme", dest="color_scheme") parser.add_config_file_option(option_name="meta-closes", dest="meta_closes") + parser.add_config_file_option(option_name="tmp-dir", dest="tmp_dir") return parser diff --git a/tests/04_test_submodules.py b/tests/04_test_submodules.py index 4b07220..1b45c86 100644 --- a/tests/04_test_submodules.py +++ b/tests/04_test_submodules.py @@ -107,12 +107,12 @@ def test_create_tarballs(): """Create an upstream tarball""" # Tarball with submodules changelog = { "Source": "test", "Upstream-Version": "0.1" } - ok_(buildpackage.git_archive(REPO, changelog, str(TMPDIR), "HEAD", "bzip2", - "9", True)) + ok_(buildpackage.git_archive(REPO, changelog, str(TMPDIR), str(TMPDIR), + "HEAD", "bzip2", "9", True)) # Tarball without submodules changelog = { "Source": "test", "Upstream-Version": "0.2" } - ok_(buildpackage.git_archive(REPO, changelog, str(TMPDIR), "HEAD", "bzip2", - "9", False)) + ok_(buildpackage.git_archive(REPO, changelog, str(TMPDIR), str(TMPDIR), + "HEAD", "bzip2", "9", False)) def test_check_tarfiles(): """Check the contents of the created tarfile""" -- 2.7.4