Esempio n. 1
0
def extract_orig(orig_tarball, dest_dir):
    """extract orig tarball to export dir before exporting from git"""
    gbp.log.info("Extracting %s to '%s'" %
                 (os.path.basename(orig_tarball), dest_dir))

    move_old_export(dest_dir)
    upstream = DebianUpstreamSource(orig_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)
def extract_orig(orig_tarball, dest_dir):
    """extract orig tarball to export dir before exporting from git"""
    gbp.log.info("Extracting %s to '%s'" % (os.path.basename(orig_tarball), dest_dir))

    move_old_export(dest_dir)
    upstream = DebianUpstreamSource(orig_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)
Esempio n. 3
0
def find_source(use_uscan, args):
    """Find the tarball to import - either via uscan or via command line argument
    @return: upstream source filename or None if nothing to import
    @rtype: string
    @raise GbpError: raised on all detected errors
    """
    if use_uscan:
        if args:
            raise GbpError("you can't pass both --uscan and a filename.")

        uscan = Uscan()
        gbp.log.info("Launching uscan...")
        try:
            uscan.scan()
        except UscanError as e:
            raise GbpError("%s" % e)

        if not uscan.uptodate:
            if uscan.tarball:
                gbp.log.info("using %s" % uscan.tarball)
                args.append(uscan.tarball)
            else:
                raise GbpError(
                    "uscan didn't download anything, and no source was found in ../"
                )
        else:
            gbp.log.info("package is up to date, nothing to do.")
            return None
    if len(args) > 1:  # source specified
        raise GbpError("More than one archive specified. Try --help.")
    elif len(args) == 0:
        raise GbpError("No archive to import specified. Try --help.")
    else:
        archive = DebianUpstreamSource(args[0])
        return archive
Esempio n. 4
0
def extract_orig(orig_tarball, dest_dir):
    """extract orig tarball to export dir before exporting from git"""
    gbp.log.info("Extracting %s to '%s'" % (os.path.basename(orig_tarball), dest_dir))

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

    # Check if tarball extracts into a single folder or not:
    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)
Esempio n. 5
0
def extract_orig(orig_tarball, dest_dir):
    """extract orig tarball to export dir before exporting from git"""
    gbp.log.info("Extracting %s to '%s'" %
                 (os.path.basename(orig_tarball), dest_dir))

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

    # Check if tarball extracts into a single folder or not:
    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)
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, [])
Esempio n. 7
0
def find_upstream(use_uscan, args, version=None):
    """Find the main tarball to import - either via uscan or via command line argument
    @return: upstream source filename or None if nothing to import
    @rtype: string
    @raise GbpError: raised on all detected errors

    >>> find_upstream(False, ['too', 'many'])
    Traceback (most recent call last):
    ...
    gbp.errors.GbpError: More than one archive specified. Try --help.
    >>> find_upstream(False, [])
    Traceback (most recent call last):
    ...
    gbp.errors.GbpError: No archive to import specified. Try --help.
    >>> find_upstream(True, ['tarball'])
    Traceback (most recent call last):
    ...
    gbp.errors.GbpError: you can't pass both --uscan and a filename.
    >>> find_upstream(False, ['tarball']).path
    'tarball'
    """
    if use_uscan:
        if args:
            raise GbpError("you can't pass both --uscan and a filename.")

        uscan = Uscan()
        gbp.log.info("Launching uscan...")
        try:
            if not uscan.scan(download_version=version):
                gbp.log.info("package is up to date, nothing to do.")
                return None
        except UscanError as e:
            raise GbpError("%s" % e)

        if uscan.tarball:
            gbp.log.info("Using uscan downloaded tarball %s" % uscan.tarball)
            args.append(uscan.tarball)
        else:
            raise GbpError(
                "uscan didn't download anything, and no source was found in ../"
            )
    if len(args) > 1:  # source specified
        raise GbpError("More than one archive specified. Try --help.")
    elif len(args) == 0:
        raise GbpError("No archive to import specified. Try --help.")
    else:
        sig = '{}.asc'.format(args[0])
        if os.path.exists(sig):
            gbp.log.debug("Signature {} found for {}".format(args[0], sig))
        else:
            sig = None
        return DebianUpstreamSource(args[0], sig=sig)
Esempio n. 8
0
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 = os.path.join(tarball_dir, source.upstream_tarball_name(
            comp_type, component=c))
        gbp.log.info("Extracting '%s' to '%s/%s'" % (os.path.basename(tarball), dest_dir, c))
        unpack_component_tarball(dest_dir, c, tarball, [])
Esempio n. 9
0
def main(argv):
    ret = 1
    repo = None

    (options, args) = parse_args(argv)
    if not options:
        return ExitCodes.parse_error

    if len(args) != 2 or args[0] not in ['commit']:
        gbp.log.err("No action given")
        return 1
    else:
        tarball = args[1]

    try:
        try:
            repo = DebianGitRepository('.')
        except GitRepositoryError:
            raise GbpError("%s is not a git repository" %
                           (os.path.abspath('.')))

        debsource = DebianSource('.')
        # FIXME: this should be a single call
        sources = [DebianUpstreamSource(tarball)]
        sources += get_component_tarballs(debsource.sourcepkg,
                                          debsource.upstream_version,
                                          sources[0].path, options.components)
        upstream_tag = repo.version_to_tag(options.upstream_tag,
                                           debsource.upstream_version)
        repo.create_pristine_tar_commits(upstream_tag, sources)
        ret = 0
    except (GitRepositoryError, GbpError, CommandExecFailed) as err:
        if str(err):
            gbp.log.err(err)
    except KeyboardInterrupt:
        gbp.log.err("Interrupted. Aborting.")

    if not ret:
        comp_msg = (' with additional tarballs for %s' %
                    ", ".join([os.path.basename(t.path)
                               for t in sources[1:]])) if sources[1:] else ''
        gbp.log.info(
            "Successfully committed pristine-tar data for version %s of %s%s" %
            (debsource.version, tarball, comp_msg))
    return ret
Esempio n. 10
0
def download_orig(url):
    """
    Download orig tarball from given URL
    @param url: the download URL
    @type url: C{str}
    @returns: The upstream source tarball
    @rtype: DebianUpstreamSource
    @raises GbpError: on all errors
    """
    CHUNK_SIZE = 4096

    try:
        import requests
    except ImportError:
        requests = None

    if requests is None:
        raise GbpError("python3-requests not installed")

    tarball = os.path.basename(url)
    target = os.path.join('..', tarball)

    if os.path.exists(target):
        raise GbpError("Failed to download %s: %s already exists" %
                       (url, target))

    try:
        with contextlib.closing(requests.get(url, verify=True,
                                             stream=True)) as r:
            r.raise_for_status()
            with open(target, 'wb', CHUNK_SIZE) as target_fd:
                for d in r.iter_content(CHUNK_SIZE):
                    target_fd.write(d)
    except Exception as e:
        if os.path.exists(target):
            os.unlink(target)
        raise GbpError("Failed to download %s: %s" % (url, e))

    sig = '{}.asc'.format(target)
    if os.path.exists(sig):
        gbp.log.debug("Signature {} found for {}".format(target, sig))
    else:
        sig = None
    return DebianUpstreamSource(target, sig=sig)
Esempio n. 11
0
def download_orig(url):
    """
    Download orig tarball from given URL
    @param url: the download URL
    @type url: C{str}
    @returns: The upstream source tarball
    @rtype: DebianUpstreamSource
    @raises GbpError: on all errors
    """
    CHUNK_SIZE = 4096

    try:
        import requests
    except ImportError:
        requests = None

    if requests is None:
        raise GbpError("python-requests not installed")

    tarball = os.path.basename(url)
    target = os.path.join('..', tarball)

    if os.path.exists(target):
        raise GbpError("Failed to download %s: %s already exists" %
                       (url, target))

    try:
        with contextlib.closing(requests.get(url, verify=True,
                                             stream=True)) as r:
            with contextlib.closing(open(target, 'w',
                                         CHUNK_SIZE)) as target_fd:
                for d in r.iter_content(CHUNK_SIZE):
                    target_fd.write(d)
    except Exception as e:
        raise GbpError("Failed to download %s: %s" % (url, e))
        if os.path.exists(target):
            os.unlink(target)

    return DebianUpstreamSource(target)
Esempio n. 12
0
def main(argv):
    dirs = dict(top=os.path.abspath(os.curdir))
    needs_repo = False
    ret = 0
    skipped = False

    options, args = parse_args(argv)
    if not options:
        return 1

    try:
        if len(args) != 1:
            gbp.log.err(
                "Need to give exactly one package to import. Try --help.")
            raise GbpError
        else:
            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

            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)

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

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

            dirs['tmp'] = os.path.abspath(tempfile.mkdtemp(dir='..'))
            upstream = DebianUpstreamSource(src.tgz)
            upstream.unpack(dirs['tmp'], options.filters)
            for tarball in src.additional_tarballs.values():
                gbp.log.info("Found component tarball '%s'" %
                             os.path.basename(tarball))
                subtarball = DebianUpstreamSource(tarball)
                subtarball.unpack(upstream.unpacked, 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)

            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

            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)

                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:
                        generate_pristine_tarballs(repo, src,
                                                   options.upstream_branch)
                if (not repo.has_branch(options.debian_branch)
                        and (is_empty or options.create_missing_branches)):
                    repo.create_branch(options.debian_branch, commit)
            if not src.native:
                if src.diff or src.deb_tgz:
                    apply_debian_patch(repo, upstream.unpacked, src, options,
                                       tag)
                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 str(err):
            gbp.log.err(err)
        ret = 1
    except SkipImport:
        skipped = True
    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'" %
                     (src.version, src.pkg))
    return ret
Esempio n. 13
0
def main(argv):
    dirs = dict(top=os.path.abspath(os.curdir))
    needs_repo = False
    ret = 0
    skipped = False

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

    try:
        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 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)

        if needs_repo:
            target = target or src.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)
            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")

        dirs['tmp'] = os.path.abspath(tempfile.mkdtemp(dir='..'))
        upstream = DebianUpstreamSource(src.tgz)
        upstream.unpack(dirs['tmp'], options.filters)
        for (component, tarball) in src.additional_tarballs.items():
            gbp.log.info("Found component tarball '%s'" % os.path.basename(tarball))
            unpack_component_tarball(upstream.unpacked, component, tarball, 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)

        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

        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)
                commit_msg = "Import %s\n%s" % (msg, get_changes(upstream.unpacked,
                                                                 repo,
                                                                 is_empty,
                                                                 options.debian_branch))
            else:
                author = committer = {}
                commit_msg = "Import %s" % msg

            commit = repo.commit_dir(upstream.unpacked,
                                     commit_msg,
                                     branch,
                                     author=author,
                                     committer=committer)

            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.create_pristinetar_commits(options.upstream_branch,
                                                    src.tgz,
                                                    src.additional_tarballs.items())
            if (not repo.has_branch(options.debian_branch) and
                    (is_empty or options.create_missing_branches)):
                repo.create_branch(options.debian_branch, commit)
        if not src.native:
            if src.diff or src.deb_tgz:
                apply_debian_patch(repo, upstream.unpacked, src, options,
                                   tag, is_empty)
            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 str(err):
            gbp.log.err(err)
        ret = 1
    except SkipImport:
        skipped = True
    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'" % (src.version, repo.path))
    return ret
Esempio n. 14
0
class DscFile(object):
    """Keeps data read from a dscfile"""
    compressions = r"(%s)" % '|'.join(
        DebianUpstreamSource.known_compressions())
    pkg_re = re.compile(r'Source:\s+(?P<pkg>.+)\s*')
    version_re = re.compile(r'Version:\s((?P<epoch>\d+)\:)?'
                            r'(?P<version>[%s]+)\s*$' %
                            DebianPkgPolicy.debianversion_chars)
    tar_re = re.compile(r'^\s\w+\s\d+\s+(?P<tar>[^_]+_[^_]+'
                        r'(\.orig)?\.tar\.%s)$' % compressions)
    add_tar_re = re.compile(r'^\s\w+\s\d+\s+(?P<tar>[^_]+_[^_]+'
                            r'\.orig-(?P<dir>[a-zA-Z0-9-]+)\.tar\.%s)$' %
                            compressions)
    diff_re = re.compile(r'^\s\w+\s\d+\s+(?P<diff>[^_]+_[^_]+'
                         r'\.diff.(gz|bz2))$')
    deb_tgz_re = re.compile(r'^\s\w+\s\d+\s+(?P<deb_tgz>[^_]+_[^_]+'
                            r'\.debian.tar.%s)$' % compressions)
    format_re = re.compile(r'Format:\s+(?P<format>[0-9.]+)\s*')
    sig_re = re.compile(r'^\s\w+\s\d+\s+(?P<sig>[^_]+_[^_]+'
                        r'\.orig(-[a-z0-9-]+)?\.tar\.%s.asc)$' % compressions)

    def __init__(self, dscfile):
        self.pkg = ""
        self.tgz = ""
        self.diff = ""
        self.deb_tgz = ""
        self.pkgformat = "1.0"
        self.debian_version = ""
        self.upstream_version = ""
        self.native = False
        self.dscfile = os.path.abspath(dscfile)
        sigs = []
        add_tars = []

        f = open(self.dscfile, encoding='utf-8')
        fromdir = os.path.dirname(os.path.abspath(dscfile))
        for line in f:
            m = self.version_re.match(line)
            if m and not self.upstream_version:
                if '-' in m.group('version'):
                    self.debian_version = m.group('version').split("-")[-1]
                    self.upstream_version = "-".join(
                        m.group('version').split("-")[0:-1])
                    self.native = False
                else:
                    self.native = True  # Debian native package
                    self.upstream_version = m.group('version')
                if m.group('epoch'):
                    self.epoch = m.group('epoch')
                else:
                    self.epoch = ""
                continue
            m = self.pkg_re.match(line)
            if m:
                self.pkg = m.group('pkg')
                continue
            m = self.deb_tgz_re.match(line)
            if m:
                self.deb_tgz = os.path.join(fromdir, m.group('deb_tgz'))
                continue
            m = self.add_tar_re.match(line)
            if m:
                add_tars.append(
                    (m.group('dir'), os.path.join(fromdir, m.group('tar'))))
                continue
            m = self.tar_re.match(line)
            if m:
                self.tgz = os.path.join(fromdir, m.group('tar'))
                continue
            m = self.sig_re.match(line)
            if m:
                sigs.append(os.path.join(fromdir, m.group('sig')))
                continue
            m = self.diff_re.match(line)
            if m:
                self.diff = os.path.join(fromdir, m.group('diff'))
                continue
            m = self.format_re.match(line)
            if m:
                self.pkgformat = m.group('format')
                continue
        f.close()

        # Source format 1.0 can have non-native packages without a Debian revision:
        # e.g. http://snapshot.debian.org/archive/debian/20090801T192339Z/pool/main/l/latencytop/latencytop_0.5.dsc
        if self.pkgformat == "1.0" and self.diff:
            self.native = False
        elif not self.native and not self.debian_version:
            raise GbpError("Cannot parse Debian version number from '%s'" %
                           self.dscfile)

        if not self.pkg:
            raise GbpError("Cannot parse package name from '%s'" %
                           self.dscfile)
        elif not self.tgz:
            raise GbpError("Cannot parse archive name from '%s'" %
                           self.dscfile)
        if not self.upstream_version:
            raise GbpError("Cannot parse version number from '%s'" %
                           self.dscfile)
        self.additional_tarballs = dict(add_tars)
        self.sigs = list(set(sigs))

    @property
    def version(self):
        version = ["", self.epoch + ":"][len(self.epoch) > 0]
        if self.native:
            version += self.upstream_version
        else:
            if self.debian_version != '':
                version += "%s-%s" % (self.upstream_version,
                                      self.debian_version)
            else:  # possible in 1.0
                version += "%s" % self.upstream_version
        return version

    def __str__(self):
        return "<%s object %s>" % (self.__class__.__name__, self.dscfile)

    @classmethod
    def parse(cls, filename):
        try:
            dsc = cls(filename)
        except IOError as err:
            raise GbpError("Error reading dsc file: %s" % err)
        return dsc
Esempio n. 15
0
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='..'))
        source = DebianUpstreamSource(dsc.tgz)
        source.unpack(dirs['tmp'], options.filters)
        for (component, tarball) in dsc.additional_tarballs.items():
            gbp.log.info("Found component tarball '%s'" % os.path.basename(tarball))
            unpack_component_tarball(source.unpacked, component, tarball, 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, source, 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, source, 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, source, 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,
                                                 dsc.tgz,
                                                 dsc.additional_tarballs.items())

        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
Esempio n. 16
0
class DscFile(object):
    """Keeps all needed data read from a dscfile"""
    compressions = r"(%s)" % '|'.join(DebianUpstreamSource.known_compressions())
    pkg_re = re.compile(r'Source:\s+(?P<pkg>.+)\s*')
    version_re = re.compile(r'Version:\s((?P<epoch>\d+)\:)?'
                             '(?P<version>[%s]+)\s*$'
                            % DebianPkgPolicy.debianversion_chars)
    tar_re = re.compile(r'^\s\w+\s\d+\s+(?P<tar>[^_]+_[^_]+'
                         '(\.orig)?\.tar\.%s)' % compressions)
    add_tar_re = re.compile(r'^\s\w+\s\d+\s+(?P<tar>[^_]+_[^_]+'
                            '(?P<add_tar>\.orig-[a-z0-9-]+)\.tar\.%s)' % compressions)
    diff_re = re.compile(r'^\s\w+\s\d+\s+(?P<diff>[^_]+_[^_]+'
                          '\.diff.(gz|bz2))')
    deb_tgz_re = re.compile(r'^\s\w+\s\d+\s+(?P<deb_tgz>[^_]+_[^_]+'
                             '\.debian.tar.%s)' % compressions)
    format_re = re.compile(r'Format:\s+(?P<format>[0-9.]+)\s*')

    def __init__(self, dscfile):
        self.pkg = ""
        self.tgz = ""
        self.diff = ""
        self.deb_tgz = ""
        self.pkgformat = "1.0"
        self.debian_version = ""
        self.upstream_version = ""
        self.native = False
        self.dscfile = os.path.abspath(dscfile)
        add_tars = []

        f = open(self.dscfile)
        fromdir = os.path.dirname(os.path.abspath(dscfile))
        for line in f:
            m = self.version_re.match(line)
            if m and not self.upstream_version:
                if '-' in m.group('version'):
                    self.debian_version = m.group('version').split("-")[-1]
                    self.upstream_version = "-".join(m.group('version').split("-")[0:-1])
                    self.native = False
                else:
                    self.native = True # Debian native package
                    self.upstream_version = m.group('version')
                if m.group('epoch'):
                    self.epoch = m.group('epoch')
                else:
                    self.epoch = ""
                continue
            m = self.pkg_re.match(line)
            if m:
                self.pkg = m.group('pkg')
                continue
            m = self.deb_tgz_re.match(line)
            if m:
                self.deb_tgz = os.path.join(fromdir, m.group('deb_tgz'))
                continue
            m = self.add_tar_re.match(line)
            if m:
                add_tars.append(os.path.join(fromdir, m.group('tar')))
                continue
            m = self.tar_re.match(line)
            if m:
                self.tgz = os.path.join(fromdir, m.group('tar'))
                continue
            m = self.diff_re.match(line)
            if m:
                self.diff = os.path.join(fromdir, m.group('diff'))
                continue
            m = self.format_re.match(line)
            if m:
                self.pkgformat = m.group('format')
                continue
        f.close()

        if not self.pkg:
            raise GbpError("Cannot parse package name from '%s'" % self.dscfile)
        elif not self.tgz:
            raise GbpError("Cannot parse archive name from '%s'" % self.dscfile)
        if not self.upstream_version:
            raise GbpError("Cannot parse version number from '%s'" % self.dscfile)
        if not self.native and not self.debian_version:
            raise GbpError("Cannot parse Debian version number from '%s'" % self.dscfile)
        self.additional_tarballs = list(set(add_tars))

    def _get_version(self):
        version = [ "", self.epoch + ":" ][len(self.epoch) > 0]
        if self.native:
            version += self.upstream_version
        else:
            version += "%s-%s" % (self.upstream_version, self.debian_version)
        return version

    version = property(_get_version)

    def __str__(self):
        return "<%s object %s>" % (self.__class__.__name__, self.dscfile)

    @classmethod
    def parse(cls, filename):
        try:
            dsc = cls(filename)
        except IOError as err:
            raise GbpError("Error reading dsc file: %s" % err)
        return dsc
Esempio n. 17
0
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='..'))
        source = DebianUpstreamSource(dsc.tgz)
        source.unpack(dirs['tmp'], options.filters)
        for (component, tarball) in dsc.additional_tarballs.items():
            gbp.log.info("Found component tarball '%s'" %
                         os.path.basename(tarball))
            unpack_component_tarball(source.unpacked, component, tarball,
                                     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, source, 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, source, 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, source, 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, dsc.tgz, dsc.additional_tarballs.items())

        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)
    except GbpError as err:
        if str(err):
            gbp.log.err(err)
    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
Esempio n. 18
0
def main(argv):
    dirs = dict(top=os.path.abspath(os.curdir))
    needs_repo = False
    ret = 0
    skipped = False

    options, args = parse_args(argv)
    if not options:
        return 1

    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,
                                      no_default_keyrings=options.no_default_keyrings,
                                      keyrings=options.keyring)
            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)

            dirs['tmp'] = os.path.abspath(tempfile.mkdtemp(dir='..'))
            upstream = DebianUpstreamSource(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)

            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

            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)

                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 (not repo.has_branch(options.debian_branch)
                    and (is_empty or options.create_missing_branches)):
                    repo.create_branch(options.debian_branch, commit)
            if not src.native:
                if src.diff or src.deb_tgz:
                    apply_debian_patch(repo, upstream.unpacked, src, options,
                                       tag)
                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 str(err):
            gbp.log.err(err)
        ret = 1
    except SkipImport:
        skipped = True
    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'" % (src.version, src.pkg))
    return ret