aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xgprintify181
-rwxr-xr-xgprintify.py89
2 files changed, 159 insertions, 111 deletions
diff --git a/gprintify b/gprintify
index dd40de9..99da1b0 100755
--- a/gprintify
+++ b/gprintify
@@ -1,24 +1,161 @@
-#!/bin/sh
+#!/usr/bin/perl
# $Id$
+# convert end of line patterns from DOS to UNIX
-if [ -z "$RPM_BUILD_ROOT" ]; then
- echo "No build root defined" >&2
- exit 1
-fi
-
-if [ ! -d "$RPM_BUILD_ROOT" ]; then
- echo "Invalid build root" >&2
- exit 1
-fi
-
-scripts=
-for f in `ls $RPM_BUILD_ROOT/etc/rc.d/init.d/* $RPM_BUILD_ROOT/etc/init.d/* 2> /dev/null`; do
- test -f $f || continue
- egrep -q '[[:space:]]*\.[[:space:]]+.*/functions' $f || continue
- scripts="$scripts $f"
-done
-if [ -n "$scripts" ]; then
- echo -n "Gprintifying init scripts..."
- /usr/share/spec-helper/gprintify.py $scripts
- echo "done"
-fi
+use strict;
+use warnings;
+use File::Find;
+use File::Temp;
+use File::Slurp;
+use List::MoreUtils qw(none);
+
+my $string_pattern = qr/
+ ^
+ ([^"]*)
+ \$?
+ "
+ (
+ (?:
+ \\. # an escaped character
+ | # or
+ [^\\"] # anything but a backslash or a quote
+ )*
+ )
+ "
+ ([^>|\[\]]*|.*\|\|.*)
+ $
+ /x;
+my $echo_pattern = qr/
+ ^
+ (.*)
+ echo
+ \s+
+ (-[en]+)?
+ /x;
+my $initfunc_pattern = qr/
+ (
+ .*
+ (?:action|success|failure|passed)
+ \s*
+ .*
+ )
+ /x;
+
+my $var_pattern = qr/
+ (?<!\\) # don't catch escaped variables
+ (
+ (?:
+ \$[a-zA-Z0-9_]+ # simple variable name
+ |
+ \${ # protected variable name
+ [a-zA-Z0-9_]+
+ (?:[#%]{1,2}[^}]+)? # with optional expansion pattern
+ }
+ )
+ )
+ /x;
+
+main(@ARGV) unless caller();
+
+sub main {
+ my $buildroot = $ENV{RPM_BUILD_ROOT};
+ die "No build root defined" unless $buildroot;
+ die "Invalid build root" unless -d $buildroot;
+ # normalize build root
+ $buildroot =~ s|/$||;
+
+ my $exclude_pattern = $ENV{EXCLUDE_FROM_GPRINTIFICATION} ?
+ qr/$ENV{EXCLUDE_FROM_GPRINTIFICATION}/ : undef;
+
+ my $initrddir1 = "$buildroot/etc/rc.d/init.d";
+ my $initrddir2 = "$buildroot/etc/init.d";
+
+ foreach my $file (<$initrddir1/*>, <$initrddir2/*>) {
+ # skip everything but files
+ next unless -f $file;
+
+ # skip excluded files
+ next if $exclude_pattern && $file =~ $exclude_pattern;
+
+ my @lines = read_file($file);
+
+ # skip scripts not sourcing /etc/init.d/functions
+ next if none { m{\s* \. \s+ /etc/(rc.d/)?init.d/functions}x } @lines;
+
+ # convert lines
+ @lines = map { process_line($_) } @lines;
+
+ # rewrite script
+ open(my $out, '>', $file) or die "Unable to open file $file: $!";
+ print $out @lines;
+ close($out);
+ }
+}
+
+sub process_line {
+ my ($line) = @_;
+
+ if ($line =~ /$string_pattern/) {
+ my $start = $1;
+ my $string = $2;
+ my $end = $3;
+
+ my ($new_start, $string_end) = process_start($start);
+ return $line if !$new_start;
+
+ my ($new_string, $variables) = process_string($string);
+ chomp $end;
+ my $final = $new_start . $new_string . $string_end . $variables;
+
+ if ($end =~ /$string_pattern/) {
+ my $start2 = $1;
+ my $string2 = $2;
+ my $end2 = $2;
+ my ($new_start2, $string_end2) = process_start($start2);
+ return $final . $end . "\n"
+ if !$new_start2;
+
+ my ($new_string2, $variables2) = process_string($string2);
+ my $final2 = $new_start2 . $new_string2 . $string_end2 . $variables2;
+ return $final . $final2 . "\n";
+ } else {
+ return $final . $end . "\n";
+ }
+
+ } else {
+ return $line;
+ }
+
+}
+
+sub process_start {
+ my ($start) = @_;
+
+ if ($start =~ /$echo_pattern/) {
+ my $before = $1;
+ my $option = $2;
+ my $new_start = $before . 'gprintf "';
+ my $string_end = ($option && $option eq '-n') ? '"' : '\\n"';
+ return ($new_start, $string_end);
+ }
+
+ if ($start =~ /$initfunc_pattern/) {
+ return ($1 . '"', '"');
+ }
+
+ return ();
+}
+
+sub process_string {
+ my ($string) = @_;
+
+ my $variables = '';
+ while ($string =~ m/$var_pattern/g) {
+ $variables .= ' "' . $1 . '"';
+ }
+ $string =~ s/$var_pattern/\%s/g;
+
+ return ($string, $variables);
+}
+
+1;
diff --git a/gprintify.py b/gprintify.py
deleted file mode 100755
index abdbbf1..0000000
--- a/gprintify.py
+++ /dev/null
@@ -1,89 +0,0 @@
-#!/usr/bin/python
-#---------------------------------------------------------------
-# Project : Mandrake Linux
-# Module : spec-helper
-# File : gprintify.py
-# Version : $Id$
-# Author : Frederic Lepied
-# Created On : Tue Feb 6 18:39:14 2001
-# Purpose : rewrite "bla $TOTO bla" in "bla %s bla" $TOTO
-# in echo/failure/success/passed/daemon/action
-# and replqce echo by gprintf.
-#---------------------------------------------------------------
-
-import sys
-import re
-
-echo_regex=re.compile('^(.*)echo +(-[en]+)?')
-string_regex=re.compile('^([^"]*?)\$?"([^"]*[^\\\\])"([^>\|\[\]]*|.*\|\|.*)$')
-var_regex=re.compile('(?<!\\\)(\$[a-zA-Z0-9_{}]+(?:\[\$[a-zA-Z0-9_{}]+\])?}?)')
-init_func_regex=re.compile('(.*(action|success|failure|passed)\s*.*)')
-
-def process_start(start):
- res=echo_regex.findall(start)
- if res:
- if 'n' in res[0][1]:
- return [res[0][0] + 'gprintf "', '"', '']
- else:
- return [res[0][0] + 'gprintf "', '\\n"', '']
- res=init_func_regex.search(start)
- if res:
- return [res.group(0) + '"', '"', '']
- return None
-
-def process_vars(str, trail):
- var_res=var_regex.findall(str)
- if var_res:
- ret=var_regex.sub('%s', str) + trail
- for v in var_res:
- ret = ret + ' "' + v + '"'
- return ret
- else:
- return str + trail
-
-def process_line(line):
- res=string_regex.findall(line)
- if res:
- res=res[0]
- start=process_start(res[0])
- if not start:
- return line
- str=process_vars(res[1], start[1])
- if res[2][-1] == "\n":
- end = res[2][:-1]
- else:
- end=res[2]
- final=start[0] + str + start[2]
-
- res=string_regex.findall(end)
- if res:
- res=res[0]
- start=process_start(res[0])
- if not start:
- return final + end + '\n'
- str=process_vars(res[1], start[1])
- end=res[2]
- return final + start[0] + str + start[2] + end + '\n'
- else:
- return final + end + '\n'
- else:
- return line
-
-def process_file(filename):
- fd=open(filename, 'r')
- lines=fd.readlines()
- fd.close()
-
- fd=open(filename, 'w')
- for l in lines:
- fd.write(process_line(l))
- fd.close()
-
-def main(args):
- for f in args:
- process_file(f)
-
-if __name__ == '__main__':
- main(sys.argv[1:])
-
-# gprintify.py ends here