From 5826e9c2f5ae4fad2954ba02a43849ce237050a2 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 12 Jan 2012 15:44:23 +0200 Subject: Refactor git-import-orig as preparation for rpm support Separate some functions of git-import-orig into a baselib, intended to be re-used by the upcoming rpm variant of the tool. --- gbp/scripts/common/import_orig.py | 125 ++++++++++++++++++++++++++++++++++++++ gbp/scripts/import_orig.py | 113 ++-------------------------------- 2 files changed, 131 insertions(+), 107 deletions(-) create mode 100644 gbp/scripts/common/import_orig.py diff --git a/gbp/scripts/common/import_orig.py b/gbp/scripts/common/import_orig.py new file mode 100644 index 0000000..62daab8 --- /dev/null +++ b/gbp/scripts/common/import_orig.py @@ -0,0 +1,125 @@ +# vim: set fileencoding=utf-8 : +# +# (C) 2006, 2007, 2009, 2011 Guido Guenther +# (C) 2012 Intel Corporation +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""Common functionality for import-orig scripts""" +import os +import tempfile +import gbp.command_wrappers as gbpc +from gbp.deb import UpstreamSource +from gbp.errors import GbpError +import gbp.log + +class OrigUpstreamSource(UpstreamSource): + """Upstream source that will be imported""" + + def needs_repack(self, options): + """ + Determine if the upstream sources needs to be repacked + + We repack if + 1. we want to filter out files and use pristine tar since we want + to make a filtered tarball available to pristine-tar + 2. when we don't have a suitable upstream tarball (e.g. zip archive or unpacked dir) + and want to use filters + 3. when we don't have a suitable upstream tarball (e.g. zip archive or unpacked dir) + and want to use pristine-tar + """ + if ((options.pristine_tar and options.filter_pristine_tar and len(options.filters) > 0)): + return True + elif not self.is_orig(): + if len(options.filters): + return True + elif options.pristine_tar: + return True + return False + + +def cleanup_tmp_tree(tree): + """remove a tree of temporary files""" + try: + gbpc.RemoveTree(tree)() + except gbpc.CommandExecFailed: + gbp.log.err("Removal of tmptree %s failed." % tree) + + +def ask_package_name(default, name_validator_func, err_msg): + """ + Ask the user for the source package name. + @param default: The default package name to suggest to the user. + """ + while True: + sourcepackage = raw_input("What will be the source package name? [%s] " % default) + if not sourcepackage: # No input, use the default. + sourcepackage = default + # Valid package name, return it. + if name_validator_func(sourcepackage): + return sourcepackage + + # Not a valid package name. Print an extra + # newline before the error to make the output a + # bit clearer. + gbp.log.warn("\nNot a valid package name: '%s'.\n%s" % (sourcepackage, err_msg)) + + +def ask_package_version(default, ver_validator_func, err_msg): + """ + Ask the user for the upstream package version. + @param default: The default package version to suggest to the user. + """ + while True: + version = raw_input("What is the upstream version? [%s] " % default) + if not version: # No input, use the default. + version = default + # Valid version, return it. + if ver_validator_func(version): + return version + + # Not a valid upstream version. Print an extra + # newline before the error to make the output a + # bit clearer. + gbp.log.warn("\nNot a valid upstream version: '%s'.\n%s" % (version, err_msg)) + + +def repacked_tarball_name(source, name, version): + if source.is_orig(): + # Repacked orig tarball needs a different name since there's already + # one with that name + name = os.path.join( + os.path.dirname(source.path), + os.path.basename(source.path).replace(".tar", ".gbp.tar")) + else: + # Repacked sources or other archives get canonical name + name = os.path.join( + os.path.dirname(source.path), + "%s_%s.orig.tar.bz2" % (name, version)) + return name + + +def repack_source(source, name, version, tmpdir, filters): + """Repack the source tree""" + name = repacked_tarball_name(source, name, version) + repacked = source.pack(name, filters) + if source.is_orig(): # the tarball was filtered on unpack + repacked.unpacked = source.unpacked + else: # otherwise unpack the generated tarball get a filtered tree + if tmpdir: + cleanup_tmp_tree(tmpdir) + tmpdir = tempfile.mkdtemp(dir='../') + repacked.unpack(tmpdir, filters) + return (repacked, tmpdir) + diff --git a/gbp/scripts/import_orig.py b/gbp/scripts/import_orig.py index 2e4947e..8559a25 100644 --- a/gbp/scripts/import_orig.py +++ b/gbp/scripts/import_orig.py @@ -18,23 +18,22 @@ """Import a new upstream version into a git repository""" import ConfigParser -import glob import os import sys import re -import subprocess import tempfile import gbp.command_wrappers as gbpc -from gbp.deb import (UpstreamSource, - do_uscan, - parse_changelog_repo, is_valid_packagename, - packagename_msg, is_valid_upstreamversion, - upstreamversion_msg) +from gbp.deb import (do_uscan, parse_changelog_repo, + is_valid_packagename, packagename_msg, + is_valid_upstreamversion, upstreamversion_msg) from gbp.deb.changelog import ChangeLog, NoChangeLogError from gbp.deb.git import (GitRepositoryError, DebianGitRepository) from gbp.config import GbpOptionParserDebian, GbpOptionGroup, no_upstream_branch_msg from gbp.errors import (GbpError, GbpNothingImported) import gbp.log +from gbp.scripts.common.import_orig import (OrigUpstreamSource, cleanup_tmp_tree, + ask_package_name, ask_package_version, + repacked_tarball_name, repack_source) # Try to import readline, since that will cause raw_input to get fancy # line editing and history capabilities. However, if readline is not @@ -45,39 +44,6 @@ except ImportError: pass -class OrigUpstreamSource(UpstreamSource): - """Upstream source that will be imported""" - - def needs_repack(self, options): - """ - Determine if the upstream sources needs to be repacked - - We repack if - 1. we want to filter out files and use pristine tar since we want - to make a filtered tarball available to pristine-tar - 2. when we don't have a suitable upstream tarball (e.g. zip archive or unpacked dir) - and want to use filters - 3. when we don't have a suitable upstream tarball (e.g. zip archive or unpacked dir) - and want to use pristine-tar - """ - if ((options.pristine_tar and options.filter_pristine_tar and len(options.filters) > 0)): - return True - elif not self.is_orig(): - if len(options.filters): - return True - elif options.pristine_tar: - return True - return False - - -def cleanup_tmp_tree(tree): - """remove a tree of temporary files""" - try: - gbpc.RemoveTree(tree)() - except gbpc.CommandExecFailed: - gbp.log.err("Removal of tmptree %s failed." % tree) - - def is_link_target(target, link): """does symlink link already point to target?""" if os.path.exists(link): @@ -160,44 +126,6 @@ def detect_name_and_version(repo, source, options): return (sourcepackage, version) -def ask_package_name(default, name_validator_func, err_msg): - """ - Ask the user for the source package name. - @param default: The default package name to suggest to the user. - """ - while True: - sourcepackage = raw_input("What will be the source package name? [%s] " % default) - if not sourcepackage: # No input, use the default. - sourcepackage = default - # Valid package name, return it. - if name_validator_func(sourcepackage): - return sourcepackage - - # Not a valid package name. Print an extra - # newline before the error to make the output a - # bit clearer. - gbp.log.warn("\nNot a valid package name: '%s'.\n%s" % (sourcepackage, err_msg)) - - -def ask_package_version(default, ver_validator_func, err_msg): - """ - Ask the user for the upstream package version. - @param default: The default package version to suggest to the user. - """ - while True: - version = raw_input("What is the upstream version? [%s] " % default) - if not version: # No input, use the default. - version = default - # Valid version, return it. - if ver_validator_func(version): - return version - - # Not a valid upstream version. Print an extra - # newline before the error to make the output a - # bit clearer. - gbp.log.warn("\nNot a valid upstream version: '%s'.\n%s" % (version, err_msg)) - - def find_source(options, args): """Find the tarball to import - either via uscan or via command line argument @return: upstream source filename or None if nothing to import @@ -232,35 +160,6 @@ def find_source(options, args): return archive -def repacked_tarball_name(source, name, version): - if source.is_orig(): - # Repacked orig tarball needs a different name since there's already - # one with that name - name = os.path.join( - os.path.dirname(source.path), - os.path.basename(source.path).replace(".tar", ".gbp.tar")) - else: - # Repacked sources or other archives get canonical name - name = os.path.join( - os.path.dirname(source.path), - "%s_%s.orig.tar.bz2" % (name, version)) - return name - - -def repack_source(source, name, version, tmpdir, filters): - """Repack the source tree""" - name = repacked_tarball_name(source, name, version) - repacked = source.pack(name, filters) - if source.is_orig(): # the tarball was filtered on unpack - repacked.unpacked = source.unpacked - else: # otherwise unpack the generated tarball get a filtered tree - if tmpdir: - cleanup_tmp_tree(tmpdir) - tmpdir = tempfile.mkdtemp(dir='../') - repacked.unpack(tmpdir, filters) - return (repacked, tmpdir) - - def set_bare_repo_options(options): """Modify options for import into a bare repository""" if options.pristine_tar or options.merge: -- cgit v1.2.3