[lintian] 01/02: dplint: Support tools with .pm extension
This is an automated email from the git hooks/post-receive script.
nthykier pushed a commit to branch master
in repository lintian.
commit 9e78af74093572c1da56c09e5242c15a4be98a11
Author: Niels Thykier <niels@thykier.net>
Date: Fri Apr 3 18:53:20 2015 +0200
dplint: Support tools with .pm extension
This provides a trivial way to avoid the standard boilerplate code in
new commands written in Perl.
Signed-off-by: Niels Thykier <niels@thykier.net>
---
frontend/dplint | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/frontend/dplint b/frontend/dplint
index 33dd29b..c771027 100755
--- a/frontend/dplint
+++ b/frontend/dplint
@@ -22,6 +22,36 @@
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
+# In main scope, need a unnamed sub to avoid tainting the "main" namespace
+# in case we need to run a ".pm" tool.
+use strict;
+use warnings;
+
+my $TOOL_RUNNER = sub {
+ my ($tool_pm_path) = @_;
+ my $main;
+ eval {require $tool_pm_path;};
+ if (my $err = $@) {
+ dplint::error("Could not load ${tool_pm_path}: $err");
+ }
+ {
+ no strict 'refs';
+ $main = \&{'::main'};
+ }
+ if (not defined($main)) {
+ dplint::error("${tool_pm_path} does not define a main sub");
+ }
+ eval {$main->();};
+ if (my $err = "$@") {
+ # main threw an exception
+ $err .= "\n" if ($err !~ m/\n\Z/);
+ print STDERR $err;
+ exit(255);
+ }
+ exit(0);
+};
+
+package dplint;
use strict;
use warnings;
@@ -53,6 +83,7 @@ sub run_tool {
my ($truename, $tool, @args) = @_;
for my $include_dir (@INCLUDE_DIRS) {
my $tool_path = "$include_dir/commands/${tool}";
+ my $tool_pm_path = "${tool_path}.pm";
if (-f $tool_path) {
if (!-x $tool_path) {
error("$tool ($tool_path) is present but not executable!");
@@ -68,6 +99,16 @@ sub run_tool {
" Error from exec: $!"
);
}
+ if (-f $tool_pm_path) {
+ # Re-configure Getopt::Long - to avoid surprises in the tool
+ Getopt::Long::config('default');
+ # Update @INC before running the tool
+ require lib;
+ import lib grep { -d } map { "$_/lib" } @INCLUDE_DIRS;
+
+ $TOOL_RUNNER->($tool_pm_path);
+ error("TOOL_RUNNER returned unexpectedly!?");
+ }
}
error("$tool is not available");
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/lintian/lintian.git
Reply to: