From 57bcf840587e524527374ee1f8863fe539c21a39 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 13 Feb 2014 12:41:39 +0200 Subject: [PATCH] import-orig-rpm: get archive from spec file Try to get archive path/filename from spec file if no file name is given on the command line. This should make version bumps more straightforward: just update version number in the spec file and run 'git-import-orig-rpm'. Signed-off-by: Markus Lehtonen --- gbp/scripts/import_orig_rpm.py | 66 ++++++++++++++++++----------- tests/component/rpm/test_import_orig_rpm.py | 47 ++++++++++++++++++-- 2 files changed, 84 insertions(+), 29 deletions(-) diff --git a/gbp/scripts/import_orig_rpm.py b/gbp/scripts/import_orig_rpm.py index 8a6df9e..1d3972a 100755 --- a/gbp/scripts/import_orig_rpm.py +++ b/gbp/scripts/import_orig_rpm.py @@ -42,36 +42,41 @@ from gbp.scripts.import_srpm import download_file def upstream_import_commit_msg(options, version): return options.import_msg % dict(version=version) - -def detect_name_and_version(repo, source, options): - # Guess defaults for the package name and version from the - # original tarball. - (guessed_package, guessed_version) = source.guess_version() or ('', '') - - # Try to find the source package name +def find_spec(repo, options): + """Find spec in the working tree or repository""" try: preferred_fn = os.path.basename(repo.path) + '.spec' spec = guess_spec(os.path.join(repo.path, options.packaging_dir), True, preferred_fn) - sourcepackage = spec.name except NoSpecError: try: - # Check the spec file from the repository, in case - # we're not on the packaging-branch (but upstream, for - # example). + # Check the spec file from the repository, in case we're not on the + # packaging-branch (but upstream, for example). spec = guess_spec_repo(repo, options.packaging_branch, options.packaging_dir, True, preferred_fn) - sourcepackage = spec.name except NoSpecError: - if options.interactive: - sourcepackage = ask_package_name(guessed_package, - RpmPkgPolicy.is_valid_packagename, - RpmPkgPolicy.packagename_msg) + spec = None + return spec + +def detect_name_and_version(repo, source, spec, options): + """Determine name and version of the upstream project""" + # Guess defaults for the package name and version from the + # original tarball. + (guessed_package, guessed_version) = source.guess_version() or ('', '') + + # Try to find the source package name + if spec: + sourcepackage = spec.name + else: + if options.interactive: + sourcepackage = ask_package_name(guessed_package, + RpmPkgPolicy.is_valid_packagename, + RpmPkgPolicy.packagename_msg) + else: + if guessed_package: + sourcepackage = guessed_package else: - if guessed_package: - sourcepackage = guessed_package - else: - raise GbpError, "Couldn't determine upstream package name. Use --interactive." + raise GbpError, "Couldn't determine upstream package name. Use --interactive." # Try to find the version. if options.version: @@ -90,16 +95,24 @@ def detect_name_and_version(repo, source, options): return (sourcepackage, version) -def find_source(options, args): +def find_source(spec, options, args): """Find the tarball to import @return: upstream source filename or None if nothing to import @rtype: string @raise GbpError: raised on all detected errors """ if len(args) > 1: # source specified - raise GbpError, "More than one archive specified. Try --help." + raise GbpError("More than one archive specified. Try --help.") elif len(args) == 0: - raise GbpError, "No archive to import specified. Try --help." + if spec and spec.orig_src: + path = spec.orig_src['uri'] + gbp.log.info("Archive file path from spec is used ('%s')" % path) + elif spec: + raise GbpError("No archive to import specified and unable to " + "determine source from spec. Try --help.") + else: + raise GbpError("No archive to import specified and no spec file " + "found. Try --help.") else: path = args[0] if re.match(r'[a-z]{1,5}://', path): @@ -212,12 +225,14 @@ def main(argv): tmpdir = tempfile.mkdtemp(dir=options.tmp_dir, prefix='import-orig-rpm_') try: - source = find_source(options, args) try: repo = RpmGitRepository('.') except GitRepositoryError: raise GbpError, "%s is not a git repository" % (os.path.abspath('.')) + spec = find_spec(repo, options) + source = find_source(spec, options, args) + # an empty repo has now branches: initial_branch = repo.get_branch() is_empty = False if initial_branch else True @@ -231,7 +246,8 @@ def main(argv): else: raise GbpError(no_upstream_branch_msg % options.upstream_branch) - (sourcepackage, version) = detect_name_and_version(repo, source, options) + sourcepackage, version = detect_name_and_version(repo, source, spec, + options) (clean, out) = repo.is_clean() if not clean and not is_empty: diff --git a/tests/component/rpm/test_import_orig_rpm.py b/tests/component/rpm/test_import_orig_rpm.py index d26f5eb..465c5fa 100644 --- a/tests/component/rpm/test_import_orig_rpm.py +++ b/tests/component/rpm/test_import_orig_rpm.py @@ -39,6 +39,18 @@ from tests.component.rpm import RPM_TEST_DATA_DIR DATA_DIR = os.path.join(RPM_TEST_DATA_DIR, 'orig') +DUMMY_SPEC = """ +Name: dummy +Version: 1.0 +Release: 0 +License: GPL-2.0 +Summary: Dummy package +Source: %(source)s + +%%description +Dummy package generated by unit tests +""" + def mock_import(args, stdin_data="\n\n", cwd=None): """Wrapper for import-orig-rpm for feeding mock stdin data to it""" old_cwd = os.path.abspath(os.path.curdir) @@ -111,6 +123,13 @@ class TestImportOrig(ImportOrigTestBase): repo.commit_all('First commit') return repo + @staticmethod + def _create_dummy_spec(path, **kwargs): + """Create a dummy spec file""" + with open(path, 'w') as fobj: + print kwargs + fobj.write(DUMMY_SPEC % kwargs) + def test_invalid_args(self): """ See that import-orig-rpm fails gracefully when called with invalid args @@ -118,10 +137,6 @@ class TestImportOrig(ImportOrigTestBase): repo = GitRepository.create('.') origs = [os.path.join(DATA_DIR, 'gbp-test-1.0.tar.bz2'), os.path.join(DATA_DIR, 'gbp-test-1.1.tar.bz2')] - # Test empty args - eq_(mock_import([]), 1) - self._clear_log() - # Test multiple archives eq_(mock_import([] + origs), 1) self._check_log(0, 'gbp:error: More than one archive specified') @@ -383,6 +398,30 @@ class TestImportOrig(ImportOrigTestBase): # Other parts of the import should've succeeded self._check_repo_state(repo, 'master', ['master', 'upstream']) + def test_archive_from_spec(self): + """Test taking archive file path from spec file""" + repo = GitRepository.create('.') + orig = os.path.join(DATA_DIR, 'gbp-test-1.0.tar.bz2') + + # Test non-existent spec file + eq_(mock_import([]), 1) + self._check_log(0, '.*No archive to import specified and no spec file') + + # Test non-existent archive + self._create_dummy_spec('dummy.spec', source='non-existent.tar.gz') + eq_(mock_import([]), 1) + self._check_log(-1, '.*unable to find \S+non-existent.tar.gz') + + # Test failing download + self._create_dummy_spec('dummy.spec', source='foo://bar.tar.gz') + eq_(mock_import([]), 1) + self._check_log(-1, '.*Download failed') + + # Test existing archive + self._create_dummy_spec('dummy.spec', source=orig) + eq_(mock_import([]), 0) + self._check_repo_state(repo, None, ['upstream'], ['dummy.spec']) + class TestPristineTar(ImportOrigTestBase): """ -- 2.7.4