This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch main
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=1a06485a77983e2471ec761a0f9bf02a921a8761

commit 1a06485a77983e2471ec761a0f9bf02a921a8761
Author: Guillem Jover <[email protected]>
AuthorDate: Sat Aug 31 19:40:17 2024 +0200

    dpkg-buildtree: Add new command is-rootless
    
    This new command can be used to query whether the build tree requires
    root to build or can be built with no root privileges.
    
    This is useful for build frontends that might need to know whether they
    have to install additional gain-root-command packages to be able to
    build the sources.
    
    Suggested-by: Niels Thykier <[email protected]>
---
 man/dpkg-buildtree.pod                             |  9 +++
 scripts/Dpkg/BuildTree.pm                          | 18 ++++++
 scripts/Makefile.am                                |  6 ++
 scripts/dpkg-buildtree.pl                          | 12 +++-
 scripts/t/dpkg_buildtree.t                         | 71 ++++++++++++++++++++++
 .../src-build-api-v0}/debian/control               |  2 +-
 .../src-build-api-v1/debian/control}               |  4 +-
 .../src-rrr-binary-targets/debian/control          |  5 ++
 .../dpkg_buildtree/src-rrr-missing/debian/control  |  4 ++
 scripts/t/dpkg_buildtree/src-rrr-no/debian/control |  5 ++
 10 files changed, 131 insertions(+), 5 deletions(-)

diff --git a/man/dpkg-buildtree.pod b/man/dpkg-buildtree.pod
index 0e03cc3e8..a251a3ead 100644
--- a/man/dpkg-buildtree.pod
+++ b/man/dpkg-buildtree.pod
@@ -45,6 +45,15 @@ and are in many cases internal implementation details the 
packager should
 not be concerned about,
 or are staging directories containing build artifacts.
 
+=item B<is-rootless>
+
+Checks whether the build tree can be built with no root privileges
+(since dpkg 1.22.12).
+
+This command can be used safely even with versions where the command was not
+yet available as the default will then be the historic one where root was
+required.
+
 =item B<--help>
 
 Show the usage message and exit.
diff --git a/scripts/Dpkg/BuildTree.pm b/scripts/Dpkg/BuildTree.pm
index f7717e16e..862a92968 100644
--- a/scripts/Dpkg/BuildTree.pm
+++ b/scripts/Dpkg/BuildTree.pm
@@ -103,6 +103,24 @@ sub clean {
     return;
 }
 
+=item $bt->needs_root()
+
+Check whether the build tree needs root to build.
+
+=cut
+
+sub needs_root {
+    my $self = shift;
+
+    require Dpkg::Control::Info;
+    require Dpkg::BuildDriver;
+
+    my $ctrl = Dpkg::Control::Info->new();
+    my $bd = Dpkg::BuildDriver->new(ctrl => $ctrl);
+
+    return $bd->need_build_task('build', 'binary');
+}
+
 =back
 
 =head1 CHANGES
diff --git a/scripts/Makefile.am b/scripts/Makefile.am
index 9cfdc1020..64aa047dd 100644
--- a/scripts/Makefile.am
+++ b/scripts/Makefile.am
@@ -274,6 +274,7 @@ test_scripts = \
        t/Dpkg_Dist_Files.t \
        t/dpkg_source.t \
        t/dpkg_buildpackage.t \
+       t/dpkg_buildtree.t \
        t/dpkg_mergechangelogs.t \
        t/mk.t \
        # EOL
@@ -388,6 +389,11 @@ test_data = \
        t/dpkg_buildpackage/test-source_0_full.hook \
        t/dpkg_buildpackage/test-source_0_source.changes \
        t/dpkg_buildpackage/test-source_0_source.hook \
+       t/dpkg_buildtree/src-build-api-v0/debian/control \
+       t/dpkg_buildtree/src-build-api-v1/debian/control \
+       t/dpkg_buildtree/src-rrr-binary-targets/debian/control \
+       t/dpkg_buildtree/src-rrr-missing/debian/control \
+       t/dpkg_buildtree/src-rrr-no/debian/control \
        t/dpkg_mergechangelogs/ch-a \
        t/dpkg_mergechangelogs/ch-b \
        t/dpkg_mergechangelogs/ch-badver-a \
diff --git a/scripts/dpkg-buildtree.pl b/scripts/dpkg-buildtree.pl
index f25059813..cc96e9e36 100755
--- a/scripts/dpkg-buildtree.pl
+++ b/scripts/dpkg-buildtree.pl
@@ -2,7 +2,7 @@
 #
 # dpkg-buildtree
 #
-# Copyright © 2023 Guillem Jover <[email protected]>
+# Copyright © 2023-2024 Guillem Jover <[email protected]>
 #
 # 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
@@ -42,16 +42,21 @@ sub usage {
     . "\n\n" . g_(
 'Commands:
   clean              clean dpkg generated artifacts from the build tree.
+  is-rootless        checks whether the build tree needs root to build.
   --help             show this help message.
   --version          show the version.
 '), $Dpkg::PROGNAME;
 }
 
+my %known_actions = map { $_ => 1 } qw(
+    clean
+    is-rootless
+);
 my $action;
 
 while (@ARGV) {
     my $arg = shift @ARGV;
-    if ($arg eq 'clean') {
+    if (exists $known_actions{$arg}) {
         usageerr(g_('two commands specified: %s and %s'), $1, $action)
             if defined $action;
         $action = $arg;
@@ -72,4 +77,7 @@ my $bt = Dpkg::BuildTree->new();
 
 if ($action eq 'clean') {
     $bt->clean();
+} elsif ($action eq 'is-rootless') {
+    exit 1 if $bt->needs_root();
+    exit 0;
 }
diff --git a/scripts/t/dpkg_buildtree.t b/scripts/t/dpkg_buildtree.t
new file mode 100644
index 000000000..35cb273db
--- /dev/null
+++ b/scripts/t/dpkg_buildtree.t
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+#
+# 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 2 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 <https://www.gnu.org/licenses/>.
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Dpkg qw(:paths);
+
+use File::Spec::Functions qw(rel2abs);
+
+use Dpkg::IPC;
+use Dpkg::BuildTree;
+
+plan tests => 5;
+
+my $srcdir = rel2abs($ENV{srcdir} || '.');
+my $datadir = "$srcdir/t/dpkg_buildtree";
+
+$ENV{$_} = rel2abs($ENV{$_}) foreach qw(DPKG_DATADIR DPKG_ORIGINS_DIR);
+
+my %is_rootless = (
+    'src-build-api-v0' => 0,
+    'src-build-api-v1' => 1,
+    'src-rrr-binary-targets' => 0,
+    'src-rrr-missing' => 0,
+    'src-rrr-no' => 1,
+);
+
+sub test_is_rootless
+{
+    my $test = shift;
+    my $dirname = "$datadir/$test";
+
+    my $stderr;
+    my $res;
+
+    chdir $dirname;
+    spawn(
+        exec => [
+            $ENV{PERL}, "$srcdir/dpkg-buildtree.pl",
+            'is-rootless'
+        ],
+        error_to_string => \$stderr,
+        wait_child => 1,
+        nocheck => 1,
+    );
+    $res = $? == 0 ? 1 : 0;
+    chdir $datadir;
+
+    return $res;
+}
+
+foreach my $test (sort keys %is_rootless) {
+    my $exp = $is_rootless{$test};
+    my $res = test_is_rootless($test);
+
+    is($res, $exp, "dpkg-buildtree is-rootless on $test not $exp");
+}
diff --git a/scripts/t/mk/debian/control 
b/scripts/t/dpkg_buildtree/src-build-api-v0/debian/control
similarity index 72%
copy from scripts/t/mk/debian/control
copy to scripts/t/dpkg_buildtree/src-build-api-v0/debian/control
index 43b35d30d..18d04a613 100644
--- a/scripts/t/mk/debian/control
+++ b/scripts/t/dpkg_buildtree/src-build-api-v0/debian/control
@@ -1,4 +1,4 @@
-Source: source
+Source: source-build-api-v0
 Build-Depends:
  dpkg-build-api (= 0),
 
diff --git a/scripts/t/Dpkg_BuildAPI/ctrl-api-explicit 
b/scripts/t/dpkg_buildtree/src-build-api-v1/debian/control
similarity index 56%
copy from scripts/t/Dpkg_BuildAPI/ctrl-api-explicit
copy to scripts/t/dpkg_buildtree/src-build-api-v1/debian/control
index cb168f877..f070a7c45 100644
--- a/scripts/t/Dpkg_BuildAPI/ctrl-api-explicit
+++ b/scripts/t/dpkg_buildtree/src-build-api-v1/debian/control
@@ -1,6 +1,6 @@
-Source: pkg-source
+Source: source-build-api-v1
 Build-Depends:
  dpkg-build-api (= 1),
 
-Package: pkg-binary
+Package: binary
 Architecture: all
diff --git a/scripts/t/dpkg_buildtree/src-rrr-binary-targets/debian/control 
b/scripts/t/dpkg_buildtree/src-rrr-binary-targets/debian/control
new file mode 100644
index 000000000..792b47812
--- /dev/null
+++ b/scripts/t/dpkg_buildtree/src-rrr-binary-targets/debian/control
@@ -0,0 +1,5 @@
+Source: source-rrr-binary-targets
+Rules-Requires-Root: binary-targets
+
+Package: binary
+Architecture: all
diff --git a/scripts/t/dpkg_buildtree/src-rrr-missing/debian/control 
b/scripts/t/dpkg_buildtree/src-rrr-missing/debian/control
new file mode 100644
index 000000000..5989f71b5
--- /dev/null
+++ b/scripts/t/dpkg_buildtree/src-rrr-missing/debian/control
@@ -0,0 +1,4 @@
+Source: source-rrr-missing
+
+Package: binary
+Architecture: all
diff --git a/scripts/t/dpkg_buildtree/src-rrr-no/debian/control 
b/scripts/t/dpkg_buildtree/src-rrr-no/debian/control
new file mode 100644
index 000000000..c31f8d754
--- /dev/null
+++ b/scripts/t/dpkg_buildtree/src-rrr-no/debian/control
@@ -0,0 +1,5 @@
+Source: source-rrr-no
+Rules-Requires-Root: no
+
+Package: binary
+Architecture: all

-- 
Dpkg.Org's dpkg

Reply via email to