File: merge-changelog

package info (click to toggle)
ubuntu-dev-tools 0.206
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,172 kB
  • sloc: python: 9,120; sh: 1,304; perl: 135; makefile: 10
file content (94 lines) | stat: -rwxr-xr-x 2,815 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright © 2008 Canonical Ltd.
# Author: Scott James Remnant <scott at ubuntu.com>.
# Hacked up by: Bryce Harrington <bryce at ubuntu.com>
# Change merge_changelog to merge-changelog: Ryan Kavanagh
#                                            <ryanakca@kubuntu.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of version 3 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=invalid-name
# pylint: enable=invalid-name

import sys

from debian.changelog import Changelog

from ubuntutools import getLogger

Logger = getLogger()


def usage(exit_code=1):
    Logger.info(
        """Usage: merge-changelog <left changelog> <right changelog>

merge-changelog takes two changelogs that once shared a common source,
merges them back together, and prints the merged result to stdout.  This
is useful if you need to manually merge a ubuntu package with a new
Debian release of the package.
"""
    )
    sys.exit(exit_code)


########################################################################
# Changelog Management
########################################################################


def merge_changelog(left_changelog, right_changelog):
    """Merge a changelog file."""

    with open(left_changelog, encoding="utf-8") as f:
        left_cl = Changelog(f)
    with open(right_changelog, encoding="utf-8") as f:
        right_cl = Changelog(f)

    left_versions = set(left_cl.versions)
    right_versions = set(right_cl.versions)
    left_blocks = iter(left_cl)
    right_blocks = iter(right_cl)

    clist = sorted(left_versions | right_versions, reverse=True)
    remaining = len(clist)
    for version in clist:
        remaining -= 1
        if version in left_versions:
            block = next(left_blocks)
            if version in right_versions:
                next(right_blocks)
        else:
            block = next(right_blocks)

        assert block.version == version

        Logger.info("%s%s", str(block).strip(), "\n" if remaining else "")


def main():
    if len(sys.argv) > 1 and sys.argv[1] in ("-h", "--help"):
        usage(0)
    if len(sys.argv) != 3:
        usage(1)

    left_changelog = sys.argv[1]
    right_changelog = sys.argv[2]

    merge_changelog(left_changelog, right_changelog)
    sys.exit(0)


if __name__ == "__main__":
    main()