Beispiel #1
0
 def test_directory(self):
     """Upstream source is a directory"""
     source = UpstreamSource(self.upstream_dir)
     self.assertEqual(source.is_orig(), False)
     self.assertEqual(source.path, self.upstream_dir)
     self.assertEqual(source.unpacked, self.upstream_dir)
     self.assertEqual(source.guess_version(), ('test', '1.0'))
Beispiel #2
0
 def test_unpack(self):
     source = UpstreamSource(self.zipfile)
     self.assertEqual(source.is_orig(), False)
     self.assertEqual(source.is_dir(), False)
     self.assertEqual(source.unpacked, None)
     self.assertEqual(source.guess_version(), ('gbp', '0.1'))
     source.unpack(str(self.tmpdir))
     self.assertNotEqual(source.unpacked, None)
Beispiel #3
0
 def test_unpack(self):
     source = UpstreamSource(self.zipfile)
     self.assertEqual(source.is_orig(), False)
     self.assertEqual(source.is_tarball(), False)
     self.assertEqual(source.is_dir(), False)
     self.assertEqual(source.unpacked, None)
     self.assertEqual(source.guess_version(), ('gbp', '0.1'))
     self.assertEqual(source.archive_fmt, 'zip')
     self.assertEqual(source.compression, None)
     self.assertEqual(source.prefix, 'gbp')
     source.unpack(str(self.tmpdir))
     self.assertNotEqual(source.unpacked, None)
def main(argv):
    dirs = dict(top=os.path.abspath(os.curdir))
    needs_repo = False
    ret = 0
    skipped = False
    parents = None

    options, args = parse_args(argv)

    try:
        if len(args) != 1:
            gbp.log.err("Need to give exactly one package to import. Try --help.")
            raise GbpError
        else:
            pkg = args[0]
            if options.download:
                dsc = download_source(pkg,
                                      dirs=dirs,
                                      unauth=options.allow_unauthenticated)
            else:
                dsc = pkg

            src = DscFile.parse(dsc)
            if src.pkgformat not in [ '1.0', '3.0' ]:
                raise GbpError("Importing %s source format not yet supported." % src.pkgformat)
            if options.verbose:
                print_dsc(src)

            try:
                repo = DebianGitRepository('.')
                is_empty = repo.is_empty()

                (clean, out) = repo.is_clean()
                if not clean and not is_empty:
                    gbp.log.err("Repository has uncommitted changes, commit these first: ")
                    raise GbpError(out)

            except GitRepositoryError:
                # no repo found, create one
                needs_repo = True
                is_empty = True

            if needs_repo:
                gbp.log.info("No git repository found, creating one.")
                repo = DebianGitRepository.create(src.pkg)
                os.chdir(repo.path)

            if repo.bare:
                set_bare_repo_options(options)

            if repo.find_version(options.debian_tag, src.version):
                 gbp.log.warn("Version %s already imported." % src.version)
                 if options.allow_same_version:
                    gbp.log.info("Moving tag of version '%s' since import forced" % src.version)
                    move_tag_stamp(repo, options.debian_tag, src.version)
                 else:
                    raise SkipImport

            dirs['tmp'] = os.path.abspath(tempfile.mkdtemp(dir='..'))
            upstream = UpstreamSource(src.tgz)
            upstream.unpack(dirs['tmp'], options.filters)

            format = [(options.upstream_tag, "Upstream"), (options.debian_tag, "Debian")][src.native]
            tag = repo.version_to_tag(format[0], src.upstream_version)
            msg = "%s version %s" % (format[1], src.upstream_version)

            parents = []

            if not repo.find_version(format[0], src.upstream_version):
                gbp.log.info("Tag %s not found, importing %s tarball" % (tag, format[1]))
                if is_empty:
                    branch = None
                else:
                    branch = [options.upstream_branch,
                              options.debian_branch][src.native]
                    if not repo.has_branch(branch):
                        if options.create_missing_branches:
                            gbp.log.info("Creating missing branch '%s'" % branch)
                            repo.create_branch(branch)
                        else:
                            gbp.log.err(no_upstream_branch_msg % branch +
                                        "\nAlso check the --create-missing-branches option.")
                            raise GbpError

                if src.native:
                    author = get_author_from_changelog(upstream.unpacked)
                    committer = get_committer_from_author(author, options)
                else:
                    author = committer = {}

                commit = repo.commit_dir(upstream.unpacked,
                                         "Imported %s" % msg,
                                         branch,
                                         author=author,
                                         committer=committer)
                parents.append(( tag, commit ))

                if not (src.native and options.skip_debian_tag):
                    repo.create_tag(name=tag,
                                    msg=msg,
                                    commit=commit,
                                    sign=options.sign_tags,
                                    keyid=options.keyid)
                if not src.native:
                    if is_empty:
                        repo.create_branch(options.upstream_branch, commit)
                    if options.pristine_tar:
                        repo.pristine_tar.commit(src.tgz, options.upstream_branch)
                if is_empty and not repo.has_branch(options.debian_branch):
                    repo.create_branch(options.debian_branch, commit)

            if not src.native and src.extra_tgz:
                for tgz in src.extra_tgz:
                    tmp = os.path.abspath(tempfile.mkdtemp(dir='..'))
                    try:
                        extra = UpstreamSource(tgz)
                        extra.unpack(tmp, options.filters, False)

                        _, _, component = extra.guess_version()
                        tagcomponent = '-' + component
                        tag = repo.version_to_tag(format[0], src.upstream_version, tagcomponent)

                        if not repo.find_version(format[0], src.upstream_version, tagcomponent):
                            gbp.log.info("Tag %s not found, importing %s tarball" % (tag, format[1]))
                            upstream_branch = options.upstream_branch + '-' + component
                            if is_empty:
                                branch = None
                            else:
                                branch = upstream_branch
                                if not repo.has_branch(branch):
                                    if options.create_missing_branches:
                                        gbp.log.info("Creating missing branch '%s'" % branch)
                                        repo.create_branch(branch)
                                    else:
                                        gbp.log.err(no_upstream_branch_msg % branch +
                                                    "\nAlso check the --create-missing-branches option.")
                                        raise GbpError

                            commit = repo.commit_dir(extra.unpacked,
                                                     "Imported %s" % msg,
                                                     branch,
                                                     author)
                            parents.append(( tag, commit ))

                            repo.create_tag(name=tag,
                                            msg=msg,
                                            commit=commit,
                                            sign=options.sign_tags,
                                            keyid=options.keyid)

                            if is_empty:
                                repo.create_branch(upstream_branch, commit)
                            if options.pristine_tar:
                                repo.pristine_tar.commit(tgz, upstream_branch)

                        # Move unpacked source components into base tree
                        for item in os.listdir(extra.unpacked):
                            shutil.move(os.path.join(extra.unpacked, item),
                                        os.path.join(upstream.unpacked, item))
                    finally:
                        gbpc.RemoveTree(tmp)()

            if not src.native:
                if src.diff or src.deb_tgz:
                    apply_debian_patch(repo, upstream.unpacked, src, options, parents)
                else:
                    gbp.log.warn("Didn't find a diff to apply.")
            if repo.get_branch() == options.debian_branch or is_empty:
                # Update HEAD if we modified the checked out branch
                repo.force_head(options.debian_branch, hard=True)
    except KeyboardInterrupt:
        ret = 1
        gbp.log.err("Interrupted. Aborting.")
    except gbpc.CommandExecFailed:
        ret = 1
    except GitRepositoryError as msg:
        gbp.log.err("Git command failed: %s" % msg)
        ret = 1
    except GbpError as err:
        if len(err.__str__()):
            gbp.log.err(err)
        ret = 1
    except SkipImport:
        skipped = True
    finally:
        os.chdir(dirs['top'])

    for d in [ 'tmp', 'download' ]:
        if dirs.has_key(d):
            gbpc.RemoveTree(dirs[d])()

    if not ret and not skipped:
        gbp.log.info("Version '%s' imported under '%s'" % (src.version, src.pkg))
    return ret