diff mbox series

[Unstable,v2] UBUNTU: [Packaging] Rewrite debian/scripts/misc/insert-changes.pl in Python

Message ID 20211103165922.86762-1-juergh@canonical.com
State New
Headers show
Series [Unstable,v2] UBUNTU: [Packaging] Rewrite debian/scripts/misc/insert-changes.pl in Python | expand

Commit Message

Juerg Haefliger Nov. 3, 2021, 4:59 p.m. UTC
Rewrite the insert-changes.pl script in Python to get us one step closer
to dropping Perl as an Ubuntu kernel build dependency.

Signed-off-by: Juerg Haefliger <juergh@canonical.com>
---

v1 -> v2:
 - Fix default value of 'debian' variable
 - Add missing 'skip_newline' flag

 debian/rules.d/1-maintainer.mk        |  2 +-
 debian/scripts/misc/insert-changes    | 42 ++++++++++++++++++++++++++
 debian/scripts/misc/insert-changes.pl | 43 ---------------------------
 3 files changed, 43 insertions(+), 44 deletions(-)
 create mode 100755 debian/scripts/misc/insert-changes
 delete mode 100755 debian/scripts/misc/insert-changes.pl

Comments

Andrea Righi Nov. 4, 2021, 8:13 a.m. UTC | #1
On Wed, Nov 03, 2021 at 05:59:22PM +0100, Juerg Haefliger wrote:
> Rewrite the insert-changes.pl script in Python to get us one step closer
> to dropping Perl as an Ubuntu kernel build dependency.
> 
> Signed-off-by: Juerg Haefliger <juergh@canonical.com>
> ---

Looks good to me. Applied to unstable/5.15.

Thanks!
-Andrea
diff mbox series

Patch

diff --git a/debian/rules.d/1-maintainer.mk b/debian/rules.d/1-maintainer.mk
index 1b5800a031a3..9f59d5087cf2 100644
--- a/debian/rules.d/1-maintainer.mk
+++ b/debian/rules.d/1-maintainer.mk
@@ -113,7 +113,7 @@  printchanges:
 	$(DROOT)/scripts/misc/git-ubuntu-log $(ubuntu_log_opts)
 
 insertchanges: autoreconstruct finalchecks
-	@perl -w -f $(DROOT)/scripts/misc/insert-changes.pl $(DROOT) $(DEBIAN) 
+	$(DROOT)/scripts/misc/insert-changes $(DROOT) $(DEBIAN)
 
 autoreconstruct:
 	# No need for reconstruct for -rc kernels since we don't upload an
diff --git a/debian/scripts/misc/insert-changes b/debian/scripts/misc/insert-changes
new file mode 100755
index 000000000000..e21aaa409884
--- /dev/null
+++ b/debian/scripts/misc/insert-changes
@@ -0,0 +1,42 @@ 
+#!/usr/bin/python3
+
+import os
+import sys
+
+from subprocess import check_output
+
+droot = 'debian'
+if len(sys.argv) > 1:
+    droot = sys.argv[1]
+
+debian = 'debian.master'
+if len(sys.argv) > 2:
+    debian = sys.argv[2]
+
+rules = os.path.join(droot, 'rules')
+changelog = os.path.join(debian, 'changelog')
+changelog_new = os.path.join(debian, 'changelog.new')
+
+# Generate the list of new changes
+changes = check_output(['make', '-s', '-f', rules, 'printchanges']).decode('UTF-8')
+
+# Insert the new changes into the changelog
+with open(changelog) as orig, open(changelog_new, 'w') as new:
+    printed = False
+    skip_newline = False
+    for line in orig:
+        if line.startswith('  CHANGELOG: '):
+            if not printed:
+                printed = True
+                if changes == '':
+                    skip_newline = True
+                    continue
+                new.write(changes)
+        else:
+            if skip_newline and line.strip() == '':
+                skip_newline = False
+                continue
+            new.write(line)
+
+# Replace the original changelog with the new one
+os.rename(changelog_new, changelog)
diff --git a/debian/scripts/misc/insert-changes.pl b/debian/scripts/misc/insert-changes.pl
deleted file mode 100755
index 4eed4e28f9d3..000000000000
--- a/debian/scripts/misc/insert-changes.pl
+++ /dev/null
@@ -1,43 +0,0 @@ 
-#!/usr/bin/perl -w
-
-my $debian;
-$droot = $ARGV[0] if (defined $ARGV[0]);
-$droot = 'debian' if (!defined $droot);
-$debian = $ARGV[1] if (defined $ARGV[1]);
-$debian = 'debian.master' if (!defined $debian);
-
-system("make -s -f $droot/rules printchanges > $debian/changes");
-
-open(CHANGELOG, "< $debian/changelog") or die "Cannot open changelog";
-open(CHANGES, "< $debian/changes") or die "Cannot open new changes";
-open(NEW, "> $debian/changelog.new") or die "Cannot open new changelog";
-
-$printed = 0;
-my $skip_newline = 0;
-
-while (<CHANGELOG>) {
-	if (/^  CHANGELOG: /) {
-		next if $printed;
-
-		$skip_newline = 1;
-		while (<CHANGES>) {
-			$skip_newline = 0;
-			print NEW;
-		}
-
-		$printed = 1;
-	} else {
-		if (/^$/ && $skip_newline == 1) {
-			$skip_newline = 0;
-			next;
-		}
-		print NEW;
-	}
-}
-
-close(NEW);
-close(CHANGES);
-close(CHANGELOG);
-
-rename("$debian/changelog.new", "$debian/changelog");
-unlink("$debian/changes");