ubuntu-dev-tools/pull-debian-debdiff
Benjamin Drung aa556af89d Use f-strings
pylint complains about C0209: Formatting a regular string which could be
a f-string (consider-using-f-string)

Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
2023-01-31 19:32:58 +01:00

129 lines
4.3 KiB
Python
Executable File

#!/usr/bin/python3
# pull-debian-debdiff - find and download a specific version of a Debian
# package and its immediate parent to generate a debdiff.
#
# Copyright (C) 2010-2011, Stefano Rivera <stefanor@ubuntu.com>
# Inspired by a tool of the same name by Kees Cook.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
# pylint: disable=invalid-name
# pylint: enable=invalid-name
import argparse
import sys
import debian.changelog
from ubuntutools import getLogger
from ubuntutools.archive import DebianSourcePackage, DownloadError
from ubuntutools.config import UDTConfig
from ubuntutools.version import Version
Logger = getLogger()
def previous_version(package, version, distance):
"Given an (extracted) package, determine the version distance versions ago"
upver = Version(version).upstream_version
filename = f"{package}-{upver}/debian/changelog"
changelog_file = open(filename, "r", encoding="utf-8")
changelog = debian.changelog.Changelog(changelog_file.read())
changelog_file.close()
seen = 0
for entry in changelog:
if entry.distributions == "UNRELEASED":
continue
if seen == distance:
return entry.version.full_version
seen += 1
return False
def main():
parser = argparse.ArgumentParser(usage="%(prog)s [options] <package> <version> [distance]")
parser.add_argument(
"-f",
"--fetch",
dest="fetch_only",
default=False,
action="store_true",
help="Only fetch the source packages, don't diff.",
)
parser.add_argument(
"-d",
"--debian-mirror",
metavar="DEBIAN_MIRROR",
dest="debian_mirror",
help="Preferred Debian mirror (default: http://deb.debian.org/debian)",
)
parser.add_argument(
"-s",
"--debsec-mirror",
metavar="DEBSEC_MIRROR",
dest="debsec_mirror",
help="Preferred Debian Security mirror (default: http://security.debian.org)",
)
parser.add_argument(
"--no-conf",
dest="no_conf",
default=False,
action="store_true",
help="Don't read config files or environment variables",
)
parser.add_argument("package", help=argparse.SUPPRESS)
parser.add_argument("version", help=argparse.SUPPRESS)
parser.add_argument("distance", default=1, type=int, nargs="?", help=argparse.SUPPRESS)
args = parser.parse_args()
config = UDTConfig(args.no_conf)
if args.debian_mirror is None:
args.debian_mirror = config.get_value("DEBIAN_MIRROR")
if args.debsec_mirror is None:
args.debsec_mirror = config.get_value("DEBSEC_MIRROR")
mirrors = [args.debsec_mirror, args.debian_mirror]
Logger.info("Downloading %s %s", args.package, args.version)
newpkg = DebianSourcePackage(args.package, args.version, mirrors=mirrors)
try:
newpkg.pull()
except DownloadError as e:
Logger.error("Failed to download: %s", str(e))
sys.exit(1)
newpkg.unpack()
if args.fetch_only:
sys.exit(0)
oldversion = previous_version(args.package, args.version, args.distance)
if not oldversion:
Logger.error("No previous version could be found")
sys.exit(1)
Logger.info("Downloading %s %s", args.package, oldversion)
oldpkg = DebianSourcePackage(args.package, oldversion, mirrors=mirrors)
try:
oldpkg.pull()
except DownloadError as e:
Logger.error("Failed to download: %s", str(e))
sys.exit(1)
Logger.info("file://%s", oldpkg.debdiff(newpkg, diffstat=True))
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
Logger.info("User abort.")