diff options
author | Markus Lehtonen <markus.lehtonen@linux.intel.com> | 2012-07-12 09:07:47 +0300 |
---|---|---|
committer | Markus Lehtonen <markus.lehtonen@linux.intel.com> | 2013-04-03 10:10:29 +0300 |
commit | ae29a6e47a28de0d0cef266b5d862dc860bf95cb (patch) | |
tree | 3de63e7516efab88d9a9e84c66ea932337ce999d | |
parent | f5d507165c918930affc35289480e12c2ada4dff (diff) | |
download | git-buildpackage-ae29a6e47a28de0d0cef266b5d862dc860bf95cb.tar.gz git-buildpackage-ae29a6e47a28de0d0cef266b5d862dc860bf95cb.tar.bz2 git-buildpackage-ae29a6e47a28de0d0cef266b5d862dc860bf95cb.zip |
Universal configurable base tmpdir for all gbp rpm tools
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
-rwxr-xr-x | gbp/scripts/buildpackage_rpm.py | 36 | ||||
-rwxr-xr-x | gbp/scripts/import_orig_rpm.py | 5 | ||||
-rwxr-xr-x | gbp/scripts/import_srpm.py | 50 | ||||
-rwxr-xr-x | gbp/scripts/pq_rpm.py | 72 |
4 files changed, 87 insertions, 76 deletions
diff --git a/gbp/scripts/buildpackage_rpm.py b/gbp/scripts/buildpackage_rpm.py index b6493cd1..e78c368f 100755 --- a/gbp/scripts/buildpackage_rpm.py +++ b/gbp/scripts/buildpackage_rpm.py @@ -22,11 +22,11 @@ import ConfigParser import errno import os, os.path import sys -import tempfile import shutil import re 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, @@ -44,7 +44,8 @@ from gbp.scripts.common.buildpackage import (index_name, wc_names, 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']: @@ -57,8 +58,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: @@ -182,7 +184,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): @@ -291,6 +293,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") @@ -378,7 +381,6 @@ def main(argv): retval = 0 prefix = "git-" cp = None - dump_dir = None options, gbp_args, builder_args = parse_args(argv, prefix) if not options: @@ -393,6 +395,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 @@ -427,7 +437,8 @@ def main(argv): raise GbpError # git-ls-tree printed an error message already # 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 @@ -492,8 +503,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: @@ -556,10 +569,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 options.purge and not retval and not options.export_only: diff --git a/gbp/scripts/import_orig_rpm.py b/gbp/scripts/import_orig_rpm.py index d9092a59..4b7fb481 100755 --- a/gbp/scripts/import_orig_rpm.py +++ b/gbp/scripts/import_orig_rpm.py @@ -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 import shutil @@ -218,6 +218,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) @@ -227,10 +228,10 @@ def parse_args(argv): def main(argv): ret = 0 - tmpdir = tempfile.mkdtemp(dir='../') pristine_orig = None (options, args) = parse_args(argv) + tmpdir = tempfile.mkdtemp(dir=options.tmp_dir, prefix='import-orig-rpm_') try: source = find_source(options, args) try: diff --git a/gbp/scripts/import_srpm.py b/gbp/scripts/import_srpm.py index 7c7dfa0f..b48f835c 100755 --- a/gbp/scripts/import_srpm.py +++ b/gbp/scripts/import_srpm.py @@ -21,13 +21,13 @@ import ConfigParser import sys import re import os -import tempfile import glob import pipes import time import shutil import errno import urllib2 +import gbp.tmpfile as tempfile import gbp.command_wrappers as gbpc from gbp.rpm import (parse_srpm, SrcRpmFile, SpecFile, guess_spec, NoSpecError, parse_spec, RpmUpstreamSource) @@ -63,23 +63,23 @@ def download_source(pkg, dirs): else: mode = 'yumdownloader' - dirs['download'] = tempfile.mkdtemp(prefix='download', dir=dirs['tmp_base']) + tmpdir = tempfile.mkdtemp(dir=dirs['tmp_base'], prefix='download_') gbp.log.info("Downloading '%s' using '%s'..." % (pkg, mode)) if mode == 'yumdownloader': gbpc.RunAtCommand('yumdownloader', ['--source', '--destdir=', '.', pkg], - shell=False)(dir=dirs['download']) + shell=False)(dir=tmpdir) else: try: url = urllib2.urlopen(pkg) - local_fn = os.path.join(dirs['download'], os.path.basename(pkg)) + local_fn = os.path.join(tmpdir, os.path.basename(pkg)) with open(local_fn, "wb") as local_file: local_file.write(url.read()) except urllib2.HTTPError as err: raise GbpError("Download failed: %s" % err) except urllib2.URLError as err: raise GbpError("Download failed: %s" % err.reason) - srpm = glob.glob(os.path.join(dirs['download'], '*.src.rpm'))[0] + srpm = glob.glob(os.path.join(tmpdir, '*.src.rpm'))[0] return srpm @@ -110,7 +110,7 @@ def set_bare_repo_options(options): options.patch_import = False -def import_spec_patches(repo, spec): +def import_spec_patches(repo, spec, dirs): """ Import patches from a spec file to the current branch """ @@ -119,22 +119,19 @@ def import_spec_patches(repo, spec): return gbp.log.info("Importing patches to '%s' branch" % repo.get_branch()) - tmpdir = tempfile.mkdtemp() + tmpdir = tempfile.mkdtemp(dir=dirs['tmp_base'], prefix='import_') orig_head = repo.rev_parse("HEAD") packager = get_packager(spec) - try: - # Put patches in a safe place - safedir, queue = safe_patches(queue, tmpdir) - for patch in queue: - gbp.log.debug("Applying %s" % patch.path) - try: - apply_and_commit_patch(repo, patch, packager) - except (GbpError, GitRepositoryError): - repo.force_head(orig_head, hard=True) - raise GbpError, "Couldn't import patches, you need to apply and commit manually" - finally: - shutil.rmtree(tmpdir) + # Put patches in a safe place + safedir, queue = safe_patches(queue, tmpdir) + for patch in queue: + gbp.log.debug("Applying %s" % patch.path) + try: + apply_and_commit_patch(repo, patch, packager) + except (GbpError, GitRepositoryError): + repo.force_head(orig_head, hard=True) + raise GbpError, "Couldn't import patches, you need to apply and commit manually" # Remove patches from spec and packaging directory gbp.log.info("Removing imported patch files from spec and packaging dir") @@ -179,6 +176,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") parser.add_option("--download", action="store_true", dest="download", default=False, help="download source package") parser.add_config_file_option(option_name="vendor", action="store", dest="vendor") @@ -235,7 +233,8 @@ def main(argv): gbp.log.err("Need to give exactly one package to import. Try --help.") return 1 try: - dirs['tmp_base'] = os.path.abspath(tempfile.mkdtemp()) + dirs['tmp_base'] = tempfile.mkdtemp(dir=options.tmp_dir, + prefix='import-srpm') except GbpError as err: gbp.log.err(err) return 1 @@ -247,7 +246,8 @@ def main(argv): # Real srpm, we need to unpack, first if not os.path.isdir(srpm) and not srpm.endswith(".spec"): src = parse_srpm(srpm) - dirs['pkgextract'] = tempfile.mkdtemp(prefix='pkgextract', dir=dirs['tmp_base']) + dirs['pkgextract'] = tempfile.mkdtemp(dir=dirs['tmp_base'], + prefix='pkgextract_') gbp.log.info("Extracting src rpm to '%s'" % dirs['pkgextract']) src.unpack(dirs['pkgextract']) srpm = dirs['pkgextract'] @@ -282,8 +282,10 @@ def main(argv): set_bare_repo_options(options) # Create more tempdirs - dirs['origsrc'] = tempfile.mkdtemp(prefix='origsrc', dir=dirs['tmp_base']) - dirs['packaging_base'] = tempfile.mkdtemp(prefix='packaging', dir=dirs['tmp_base']) + dirs['origsrc'] = tempfile.mkdtemp(dir=dirs['tmp_base'], + prefix='origsrc_') + dirs['packaging_base'] = tempfile.mkdtemp(dir=dirs['tmp_base'], + prefix='packaging_') dirs['packaging'] = os.path.join(dirs['packaging_base'], options.packaging_dir) try: os.mkdir(dirs['packaging']) @@ -426,7 +428,7 @@ def main(argv): spec = parse_spec(os.path.join(repo.path, options.packaging_dir, os.path.basename(spec.specfile))) - import_spec_patches(repo, spec) + import_spec_patches(repo, spec, dirs) commit = options.packaging_branch # Create packaging tag diff --git a/gbp/scripts/pq_rpm.py b/gbp/scripts/pq_rpm.py index d3ac247d..2c16e347 100755 --- a/gbp/scripts/pq_rpm.py +++ b/gbp/scripts/pq_rpm.py @@ -22,10 +22,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 import GitModifier @@ -169,36 +169,32 @@ def update_patch_series(repo, spec, start, end, options): """ Export patches to packaging directory and update spec file accordingly. """ - tmpdir = tempfile.mkdtemp(prefix='gbp-') - try: - # Create "vanilla" patches - patches = generate_git_patches(repo, - start, - options.patch_export_squash_until, - end, - tmpdir) - - # Unlink old patch files and generate new patches - rm_patch_files(spec) - - # Filter "vanilla" patches through write_patch() - filenames = [] - if patches: - gbp.log.debug("Regenerating patch series in '%s'." % spec.specdir) - for patch in patches: - patch_file = write_patch(patch, - spec.specdir, - options.patch_numbers, - options.patch_export_compress, - options.patch_export_ignore_regex) - if patch_file != None: - filenames.append(os.path.basename(patch_file)) - - spec.update_patches(filenames) - spec.write_spec_file() - - finally: - shutil.rmtree(tmpdir) + tmpdir = tempfile.mkdtemp(dir=options.tmp_dir, prefix='patchexport_') + # Create "vanilla" patches + patches = generate_git_patches(repo, + start, + options.patch_export_squash_until, + end, + tmpdir) + + # Unlink old patch files and generate new patches + rm_patch_files(spec) + + # Filter "vanilla" patches through write_patch() + filenames = [] + if patches: + gbp.log.debug("Regenerating patch series in '%s'." % spec.specdir) + for patch in patches: + patch_file = write_patch(patch, + spec.specdir, + options.patch_numbers, + options.patch_export_compress, + options.patch_export_ignore_regex) + if patch_file != None: + filenames.append(os.path.basename(patch_file)) + + spec.update_patches(filenames) + spec.write_spec_file() def export_patches(repo, branch, options): @@ -253,7 +249,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: @@ -347,7 +343,7 @@ def import_spec_patches(repo, branch, tries, 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) @@ -370,10 +366,6 @@ def import_spec_patches(repo, branch, tries, 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) @@ -442,6 +434,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") @@ -488,6 +481,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) @@ -516,6 +512,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 |