def overlay_extract_origs(source, tarball_dir, dest_dir, options):
    """Overlay extract orig tarballs to export dir before exporting debian dir from git"""

    comp_type = guess_comp_type(options.comp_type,
                                source,
                                repo=None,
                                tarball_dir=tarball_dir)
    tarball = os.path.join(tarball_dir,
                           source.upstream_tarball_name(comp_type))
    gbp.log.info("Extracting '%s' to '%s'" %
                 (os.path.basename(tarball), dest_dir))

    move_old_export(dest_dir)
    upstream = DebianUpstreamSource(tarball)
    upstream.unpack(dest_dir)

    # Check if tarball extracts into a single folder:
    if upstream.unpacked != dest_dir:
        # If it extracts a single folder, move its contents to dest_dir:
        gbp.log.debug("Moving %s to %s" % (upstream.unpacked, dest_dir))
        tmpdir = dest_dir + '.new'
        os.rename(upstream.unpacked, tmpdir)
        os.rmdir(dest_dir)
        os.rename(tmpdir, dest_dir)

    # Remove debian/ from unpacked upstream tarball in case of non 1.0 format
    underlay_debian_dir = os.path.join(dest_dir, 'debian')
    format_file = os.path.join('debian', 'source', 'format')
    if os.path.exists(underlay_debian_dir) and os.path.exists(format_file):
        format = DebianSourceFormat.parse_file(format_file)
        if format.version in ['2.0', '3.0']:
            gbp.log.info("Removing debian/ from unpacked upstream "
                         "source at %s" % underlay_debian_dir)
            shutil.rmtree(underlay_debian_dir)

    # Unpack additional tarballs
    for c in options.components:
        tarball = DebianAdditionalTarball(os.path.join(
            tarball_dir, source.upstream_tarball_name(comp_type, component=c)),
                                          component=c)
        gbp.log.info("Extracting '%s' to '%s/%s'" %
                     (os.path.basename(tarball.path), dest_dir, c))
        tarball.unpack(dest_dir, [])
示例#2
0
def get_component_tarballs(name, version, tarball, components):
    """
    Figure out the paths to the component tarballs based on the main
    tarball.
    """
    tarballs = []
    (_, _, comp_type) = Archive.parse_filename(tarball)
    for component in components:
        cname = DebianPkgPolicy.build_tarball_name(name, version, comp_type,
                                                   os.path.dirname(tarball),
                                                   component)
        tarballs.append(DebianAdditionalTarball(cname, component))
        if not os.path.exists(cname):
            raise GbpError("Can not find component tarball %s" % cname)
    return tarballs
def main(argv):
    dirs = dict(top=os.path.abspath(os.curdir))
    needs_repo = False
    ret = 1
    skipped = False

    options, pkg, target = parse_all(argv)
    if not options:
        return ExitCodes.parse_error

    try:
        try:
            repo = DebianGitRepository('.')
            # remember if the was repo initially empty
            repo.empty = repo.is_empty()

            (clean, out) = repo.is_clean()
            if not clean and not repo.empty:
                raise GbpError(
                    "Repository has uncommitted changes, commit these first: %s"
                    % out)
        except GitRepositoryError:
            # no repo found, create one
            needs_repo = True

        if options.download:
            dscfile = download_source(pkg,
                                      dirs=dirs,
                                      unauth=options.allow_unauthenticated)
        else:
            dscfile = pkg

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

        if needs_repo:
            target = target or dsc.pkg
            if os.path.exists(target):
                raise GbpError(
                    "Directory '%s' already exists. If you want to import into it, "
                    "please change into this directory otherwise move it away first."
                    % target)
            gbp.log.info("No git repository found, creating one.")
            repo = DebianGitRepository.create(target)
            repo.empty = True
            os.chdir(repo.path)
            repo_setup.set_user_name_and_email(options.repo_user,
                                               options.repo_email, repo)

        if repo.bare:
            disable_pristine_tar(options, "Bare repository")

        # unpack
        dirs['tmp'] = os.path.abspath(tempfile.mkdtemp(dir='..'))
        sigfile = '{}.asc'.format(dsc.tgz)
        sigfile = sigfile if sigfile in dsc.sigs else None
        sources = [DebianUpstreamSource(dsc.tgz, sig=sigfile)]
        for component, tarball in dsc.additional_tarballs.items():
            sigfile = '{}.asc'.format(tarball)
            sigfile = sigfile if sigfile in dsc.sigs else None
            sources.append(DebianAdditionalTarball(tarball, component))
        sources[0].unpack(dirs['tmp'], options.filters)
        for tarball in sources[1:]:
            gbp.log.info("Found component tarball '%s'" %
                         os.path.basename(tarball.path))
            tarball.unpack(sources[0].unpacked, options.filters)

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

        # import
        if dsc.native:
            import_native(repo, sources[0], dsc, options)
        else:
            imported = False
            commit = repo.find_version(options.upstream_tag,
                                       dsc.upstream_version)
            if not repo.find_version(options.upstream_tag,
                                     dsc.upstream_version):
                commit = import_upstream(repo, sources[0], dsc, options)
                imported = True

            if not repo.has_branch(options.debian_branch):
                if options.create_missing_branches:
                    repo.create_branch(options.debian_branch, commit)
                else:
                    raise GbpError(
                        "Branch %s does not exist, use --create-missing-branches"
                        % options.debian_branch)

            if dsc.diff or dsc.deb_tgz:
                apply_debian_patch(repo, sources[0], dsc, commit, options)
            else:
                gbp.log.warn("Didn't find a diff to apply.")

            if imported and options.pristine_tar:
                repo.create_pristine_tar_commits(commit, sources)
        if repo.get_branch() == options.debian_branch or repo.empty:
            # Update HEAD if we modified the checked out branch
            repo.force_head(options.debian_branch, hard=True)
        ret = 0
    except KeyboardInterrupt:
        gbp.log.err("Interrupted. Aborting.")
    except gbpc.CommandExecFailed:
        pass  # command itself printed an error
    except GitRepositoryError as msg:
        gbp.log.err("Git command failed: %s" % msg)
        debug_exc(options)
    except GbpError as err:
        if str(err):
            gbp.log.err(err)
        debug_exc(options)
    except SkipImport:
        skipped = True
        ret = 0
    finally:
        os.chdir(dirs['top'])
        for d in ['tmp', 'download']:
            if d in dirs:
                gbpc.RemoveTree(dirs[d])()

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