#!/usr/bin/perl # Copyright (C) 2010 Modestas Vainius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # 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 =head1 NAME pkgkde-override-sc-dev-latest - loosen kde-sc-dev-latest Break restrictions =head1 SYNOPSIS B [B<-b>I] [B<-v>I] =head1 DESCRIPTION B is a helper utility which can be used to generate a dummy I package without I field that is present in the original version. Original I package is used to force KDE Software Compilation modules to be built against the latest version of the KDE Development Platform modules without bumping versions of a bunch build dependencies for each KDE SC module. However, while original I does not cause problems when building in clean chroots, the restrictions imposed by its I field might be undesirable on the maintainer system when: =over 4 =item * the maintainer wants to (test)build KDE module version X.Y.A against KDE Development Platform X.Y.B where (A > B); =item * the maintainer builds a new upstream version of the some KDE Development Platform module on the system that has an old version of some KDE Development Platform packages installed. For example, even if kdepimlibs does not build depend on kdebase-workspace-dev, original I may prevent kdepimlibs X.Y.A to be built on the system which has kdebase-workspace X.Y.B (where A > B) installed. =back By default, B generates a dummy I package in the current working directory that is based on the current C version of the original I (as per apt-cache policy). You may specify a base version with the I<-b> option. The resulting dummy I override package will have C+override> as its version unless another one is specified with I<-v> option. Once the package is generated, you can install it like: # dpkg --install kde-sc-dev_+override_all.deb =head1 OPTIONS =over 4 =item B<-b>I, B<--basever>=I The version of the original I package to base an override package on. It must be available in the APT database on the system. You may also pass special value C to select the latest available version in the APT database. If you specify C, the latest version with the highest priority will be selection. By default (if this option is not specified), candidate version is selected =item B<-v>I, B<--version>=I Generate an override package with the specified C+override> rather than default C+override> version. =back =head1 AUTHOR Modestas Vainius =cut use warnings; use strict; use File::Temp; use File::Basename qw(basename); use Getopt::Long; use Cwd qw(cwd); my $APT_CACHE = `which apt-cache`; chomp $APT_CACHE; my $DPKG_GENCONTROL = `which dpkg-gencontrol`; chomp $DPKG_GENCONTROL; my $DPKG_DEB = `which dpkg-deb`; chomp $DPKG_DEB; my $DPKG_NAME = `which dpkg-name`; chomp $DPKG_NAME; sub error { my $format = shift; print STDERR sprintf(basename($0) . ": error ". $format, @_), "\n"; exit 1; } sub info { my $format = shift; print STDERR sprintf(basename($0) . ": info ". $format, @_), "\n"; } sub syserr { my $format = shift; error("$format: $!", @_); } sub check_environment { unless (-x $APT_CACHE) { error('`apt-cache` from the apt package is needed'); } unless (-x $DPKG_GENCONTROL) { error('`dpkg-gencontrol` from the dpkg-dev package is needed'); } unless (-x $DPKG_DEB) { error('`dpkg-deb` from the dpkg-dev package is needed'); } unless (-x $DPKG_NAME) { error('`dpkg-name` from the dpkg-dev package is needed'); } } sub get_apt_policy { my $package = shift; my %result; my $versions; my $ver; open (APT_CACHE, "LC_ALL=C $APT_CACHE policy $package |") or syserr("unable to run apt-cache"); while () { chop; if (m/^ Candidate: (\S+)$/) { $result{candidate} = $1 unless $1 =~ /\(none\)/; } elsif (m/^ Version table/) { $versions = {}; } elsif (defined $versions) { if (/^ [ *]{3} (\S+)/) { $ver = $1; push @{$result{byversion}}, $ver; } elsif (defined $ver && /^ \s\s*(\d+)/) { if (!exists $versions->{$ver} || $1 > $versions->{$ver}) { $versions->{$ver} = $1; } } else { $ver = undef; } } } close APT_CACHE; # By priority use sort 'stable'; $result{bypriority} = [ sort { -($versions->{$a} <=> $versions->{$b}) } keys %$versions ]; no sort 'stable'; $result{versions} = $versions; return \%result; } sub get_apt_show { my ($package, $version) = @_; open (APT_CACHE, "LC_ALL=C $APT_CACHE show $package |") or syserr("unable to run apt-cache"); my %fields; my $field; while () { chop; if (m/^(\S+):\s*(.*)$/) { $field = $1; if ($field eq "Package") { if (exists $fields{Version} && $fields{Version} eq $version) { return \%fields; } else { %fields = (); } } $fields{$field} = $2; } elsif (defined $field && m/^(\s.*)$/) { $fields{$field} .= "\n" . $_; } elsif (! m/^\s*$/) { error("problem while parsing apt-cache show output, line $."); } } close APT_CACHE; if (exists $fields{Version} && $fields{Version} eq $version) { return \%fields; } else { return (); } } sub get_maintainer { my $maintlogin; my $maintname = $ENV{DEBFULLNAME} || $ENV{NAME}; my $maintemail = $ENV{DEBEMAIL}; my ($login, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwuid($<); $maintlogin = $login || $uid || "unknown"; unless ($maintname) { my @maintname = split(/,/, $comment); $maintname = $maintname[0] || "Unknown name"; } unless ($maintemail) { $maintemail = "$maintlogin\@localhost"; } return "$maintname <$maintemail>"; } check_environment(); my $opt_basever; my $opt_version; if (!GetOptions("b|basever=s" => \$opt_basever, "v|version=s" => \$opt_version)) { exit 2; } my $policy = get_apt_policy("kde-sc-dev-latest"); error("there no versions of kde-sc-dev-latest available in the APT database") unless %{$policy->{versions}}; if (! $opt_basever) { $opt_basever = $policy->{candidate}; $opt_basever = $policy->{bypriority}->[0] unless $opt_basever; } elsif ($opt_basever eq "latest") { $opt_basever = $policy->{byversion}->[0]; } elsif ($opt_basever eq "priority") { $opt_basever = $policy->{bypriority}->[0]; } if ($opt_basever && ! exists $policy->{versions}{$opt_basever}) { error("kde-sc-dev-latest version $opt_basever is not available in the APT database"); } unless ($opt_version) { $opt_version = $opt_basever; } $opt_version .= "+override"; my $fields = get_apt_show("kde-sc-dev-latest", $opt_basever); unless (%$fields) { error("no kde-sc-dev-latest version $opt_basever in the APT database"); } info "Basing custom kde-sc-dev-latest package on the version %s ...", $opt_basever; my $maintainer = get_maintainer(); my $date = `date -R`; chop $date; my $curdir = cwd(); my $dir = File::Temp->newdir("kde-sc-dev-latest-override.XXXXXX", CLEANUP => 1); chdir $dir or syserr("unable to change directory to ". $dir->filename); mkdir "debian" or syserr("unable to create debian subdirectory"); # Write control open(CONTROL, ">", "debian/control") or syserr("unable to create control file"); print CONTROL <{$field}) { print CONTROL $field, ": ", $fields->{$field}, "\n"; } } print CONTROL <", "debian/changelog") or syserr("unable to create changelog file"); print CHANGELOG "kde-sc-dev-latest-override (", $opt_version, ") dummy; urgency=low", "\n"; print CHANGELOG "\n"; print CHANGELOG " * kde-sc-dev-latest override (without Breaks) for version ", $opt_basever, ".", "\n"; print CHANGELOG "\n"; print CHANGELOG " -- ", $maintainer, " ", $date, "\n"; close CHANGELOG; # Generate binary control and create deb mkdir "debian/tmp" or syserr("unable to create debian/tmp subdirectory"); mkdir "debian/tmp/DEBIAN" or syserr("unable to create debian/tmp/DEBIAN subdirectory"); system($DPKG_GENCONTROL, "-pkde-sc-dev-latest", "-Pdebian/tmp") and error("dpkg-gencontrol FAILED!"); system($DPKG_DEB, "--build", "debian/tmp", "kde-sc-dev-latest.deb") and error("dpkg-deb --build FAILED!"); system($DPKG_NAME, "kde-sc-dev-latest.deb") and error("dpkg-name FAILED!"); # Move my @deb = glob('*.deb'); system("mv", $deb[0], $curdir) and error("unable to move debian package to the current directory"); chdir $curdir; info "%s created sucessfully.", $deb[0]; info 'You may install it with `dpkg -i %s`', $deb[0];