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:
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):
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
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:
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)
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
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')
# 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):
"""