diff mbox series

[ovs-dev,5/9] tests: Convert soexpand build tool from Perl to Python.

Message ID 20171115185318.26841-5-blp@ovn.org
State Superseded
Headers show
Series [ovs-dev,1/9] tests: Convert uuidfilt utility from Perl to Python. | expand

Commit Message

Ben Pfaff Nov. 15, 2017, 6:53 p.m. UTC
Perl is unfashionable and Python is more widely available and understood,
so this commit converts one of the OVS uses of Perl into Python.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 Makefile.am           |  4 +--
 build-aux/automake.mk |  3 +-
 build-aux/soexpand.pl | 40 ---------------------
 build-aux/soexpand.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 101 insertions(+), 43 deletions(-)
 delete mode 100644 build-aux/soexpand.pl
 create mode 100755 build-aux/soexpand.py

Comments

Aaron Conole Nov. 16, 2017, 2:40 p.m. UTC | #1
Ben Pfaff <blp@ovn.org> writes:

> Perl is unfashionable and Python is more widely available and understood,
> so this commit converts one of the OVS uses of Perl into Python.
>
> Signed-off-by: Ben Pfaff <blp@ovn.org>
> ---
>  Makefile.am           |  4 +--
>  build-aux/automake.mk |  3 +-
>  build-aux/soexpand.pl | 40 ---------------------
>  build-aux/soexpand.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 101 insertions(+), 43 deletions(-)
>  delete mode 100644 build-aux/soexpand.pl
>  create mode 100755 build-aux/soexpand.py
>
> diff --git a/Makefile.am b/Makefile.am
> index c82a9e21ec36..11e2e6d21005 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -88,7 +88,7 @@ EXTRA_DIST = \
>  	build-aux/dist-docs \
>  	build-aux/dpdkstrip.py \
>  	build-aux/sodepends.pl \
> -	build-aux/soexpand.pl \
> +	build-aux/soexpand.py \
>  	build-aux/xml2nroff \
>  	$(MAN_FRAGMENTS) \
>  	$(MAN_ROOTS) \
> @@ -144,7 +144,7 @@ ro_shell = printf '\043 Generated automatically -- do not modify!    -*- buffer-
>  
>  SUFFIXES += .in
>  .in:
> -	$(AM_V_GEN)$(PERL) $(srcdir)/build-aux/soexpand.pl -I$(srcdir) < $< | \
> +	$(AM_V_GEN)$(PYTHON) $(srcdir)/build-aux/soexpand.py -I$(srcdir) < $< | \
>  	  $(PYTHON) $(srcdir)/build-aux/dpdkstrip.py $(DPDKSTRIP_FLAGS) | \
>  	  sed \
>  	    -e 's,[@]PKIDIR[@],$(PKIDIR),g' \
> diff --git a/build-aux/automake.mk b/build-aux/automake.mk
> index c0553e6edffb..1003144fd664 100644
> --- a/build-aux/automake.mk
> +++ b/build-aux/automake.mk
> @@ -1,4 +1,5 @@
>  # This file is purely used for checking the style of the python build tools.
>  FLAKE8_PYFILES += \
>      $(srcdir)/build-aux/xml2nroff \
> -    build-aux/dpdkstrip.py
> +    build-aux/dpdkstrip.py \
> +    build-aux/soexpand.py
> diff --git a/build-aux/soexpand.pl b/build-aux/soexpand.pl
> deleted file mode 100644
> index 216256451a4d..000000000000
> --- a/build-aux/soexpand.pl
> +++ /dev/null
> @@ -1,40 +0,0 @@
> -# Copyright (c) 2008 Nicira, Inc.
> -#
> -# Licensed under the Apache License, Version 2.0 (the "License");
> -# you may not use this file except in compliance with the License.
> -# You may obtain a copy of the License at:
> -#
> -#     http://www.apache.org/licenses/LICENSE-2.0
> -#
> -# Unless required by applicable law or agreed to in writing, software
> -# distributed under the License is distributed on an "AS IS" BASIS,
> -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> -# See the License for the specific language governing permissions and
> -# limitations under the License.
> -
> -use strict;
> -use warnings;
> -use Getopt::Long;
> -
> -my ($exit_code) = 0;
> -my (@include_dirs);
> -Getopt::Long::Configure ("bundling");
> -GetOptions("I|include=s" => \@include_dirs) or exit(1);
> -@include_dirs = ('.') if !@include_dirs;
> -OUTER: while (<STDIN>) {
> -    if (my ($name) = /^\.so (\S+)$/) {
> -	foreach my $dir (@include_dirs, '.') {
> -	    if (open(INNER, "$dir/$name")) {
> -		while (<INNER>) {
> -		    print $_;
> -		}
> -		close(INNER);
> -		next OUTER;
> -	    }
> -	}
> -	print STDERR "$name not found in: ", join(' ', @include_dirs), "\n";
> -	$exit_code = 1;
> -    }
> -    print $_;
> -}
> -exit $exit_code;
> diff --git a/build-aux/soexpand.py b/build-aux/soexpand.py
> new file mode 100755
> index 000000000000..fe99b461f980
> --- /dev/null
> +++ b/build-aux/soexpand.py
> @@ -0,0 +1,97 @@
> +#! /usr/bin/env python
> +
> +# Copyright (c) 2008, 2017 Nicira, Inc.
> +#
> +# Licensed under the Apache License, Version 2.0 (the "License");
> +# you may not use this file except in compliance with the License.
> +# You may obtain a copy of the License at:
> +#
> +#     http://www.apache.org/licenses/LICENSE-2.0
> +#
> +# Unless required by applicable law or agreed to in writing, software
> +# distributed under the License is distributed on an "AS IS" BASIS,
> +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> +# See the License for the specific language governing permissions and
> +# limitations under the License.
> +
> +import getopt
> +import os
> +import re
> +import sys
> +
> +
> +def parse_include_dirs():
> +    include_dirs = []
> +    options, args = getopt.gnu_getopt(sys.argv[1:], 'I:', ['include='])
> +    for key, value in options:
> +        if key in ['-I', '--include']:
> +            include_dirs.append(value)
> +        else:
> +            assert False
> +
> +    include_dirs.append('.')
> +    return include_dirs, args
> +
> +
> +def find_file(include_dirs, name):
> +    for dir in include_dirs:
> +        file = "%s/%s" % (dir, name)
> +        try:
> +            os.stat(file)
> +            return file
> +        except IOError as e:

This should just be 'except IOError:'

> +            pass
> +    sys.stderr.write("%s not found in: %s\n" % (name, ' '.join(include_dirs)))
> +    return None
> +
> +
> +so_re = re.compile(r'^\.so (\S+)$')
> +
> +
> +def extract_include_directive(line):
> +    m = so_re.match(line)
> +    if m:
> +        return m.group(1)
> +    else:
> +        return None
> +
> +
> +def soexpand(include_dirs, src, dst):
> +    ok = True
> +    while True:
> +        line = src.readline()
> +        if not line:
> +            break
> +
> +        name = extract_include_directive(line)
> +        if name:
> +            fn = find_file(include_dirs, name)
> +            if fn:
> +                try:
> +                    f = open(fn)
> +                    while True:
> +                        inner = f.readline()
> +                        if not inner:
> +                            break
> +                        dst.write(inner)
> +                except IOError as e:
> +                    sys.stderr.write("%s: open: %s\n" % (fn, e.strerror))
> +                    ok = False
> +                continue
> +            else:
> +                ok = False
> +
> +        dst.write(line)
> +    return ok
> +
> +
> +if __name__ == '__main__':
> +    include_dirs, args = parse_include_dirs()
> +    if args:
> +        error = False
> +        for arg in args:
> +            if not soexpand(include_dirs, open(arg), sys.stdout):
> +                error = True
> +    else:
> +        error = not soexpand(include_dirs, sys.stdin, sys.stdout)
> +    sys.exit(1 if error else 0)
Ben Pfaff Nov. 20, 2017, 6 p.m. UTC | #2
On Thu, Nov 16, 2017 at 09:40:06AM -0500, Aaron Conole wrote:
> Ben Pfaff <blp@ovn.org> writes:
> 
> > Perl is unfashionable and Python is more widely available and understood,
> > so this commit converts one of the OVS uses of Perl into Python.
> >
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> > +def find_file(include_dirs, name):
> > +    for dir in include_dirs:
> > +        file = "%s/%s" % (dir, name)
> > +        try:
> > +            os.stat(file)
> > +            return file
> > +        except IOError as e:
> 
> This should just be 'except IOError:'

Thanks, fixed.
diff mbox series

Patch

diff --git a/Makefile.am b/Makefile.am
index c82a9e21ec36..11e2e6d21005 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -88,7 +88,7 @@  EXTRA_DIST = \
 	build-aux/dist-docs \
 	build-aux/dpdkstrip.py \
 	build-aux/sodepends.pl \
-	build-aux/soexpand.pl \
+	build-aux/soexpand.py \
 	build-aux/xml2nroff \
 	$(MAN_FRAGMENTS) \
 	$(MAN_ROOTS) \
@@ -144,7 +144,7 @@  ro_shell = printf '\043 Generated automatically -- do not modify!    -*- buffer-
 
 SUFFIXES += .in
 .in:
-	$(AM_V_GEN)$(PERL) $(srcdir)/build-aux/soexpand.pl -I$(srcdir) < $< | \
+	$(AM_V_GEN)$(PYTHON) $(srcdir)/build-aux/soexpand.py -I$(srcdir) < $< | \
 	  $(PYTHON) $(srcdir)/build-aux/dpdkstrip.py $(DPDKSTRIP_FLAGS) | \
 	  sed \
 	    -e 's,[@]PKIDIR[@],$(PKIDIR),g' \
diff --git a/build-aux/automake.mk b/build-aux/automake.mk
index c0553e6edffb..1003144fd664 100644
--- a/build-aux/automake.mk
+++ b/build-aux/automake.mk
@@ -1,4 +1,5 @@ 
 # This file is purely used for checking the style of the python build tools.
 FLAKE8_PYFILES += \
     $(srcdir)/build-aux/xml2nroff \
-    build-aux/dpdkstrip.py
+    build-aux/dpdkstrip.py \
+    build-aux/soexpand.py
diff --git a/build-aux/soexpand.pl b/build-aux/soexpand.pl
deleted file mode 100644
index 216256451a4d..000000000000
--- a/build-aux/soexpand.pl
+++ /dev/null
@@ -1,40 +0,0 @@ 
-# Copyright (c) 2008 Nicira, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at:
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-use strict;
-use warnings;
-use Getopt::Long;
-
-my ($exit_code) = 0;
-my (@include_dirs);
-Getopt::Long::Configure ("bundling");
-GetOptions("I|include=s" => \@include_dirs) or exit(1);
-@include_dirs = ('.') if !@include_dirs;
-OUTER: while (<STDIN>) {
-    if (my ($name) = /^\.so (\S+)$/) {
-	foreach my $dir (@include_dirs, '.') {
-	    if (open(INNER, "$dir/$name")) {
-		while (<INNER>) {
-		    print $_;
-		}
-		close(INNER);
-		next OUTER;
-	    }
-	}
-	print STDERR "$name not found in: ", join(' ', @include_dirs), "\n";
-	$exit_code = 1;
-    }
-    print $_;
-}
-exit $exit_code;
diff --git a/build-aux/soexpand.py b/build-aux/soexpand.py
new file mode 100755
index 000000000000..fe99b461f980
--- /dev/null
+++ b/build-aux/soexpand.py
@@ -0,0 +1,97 @@ 
+#! /usr/bin/env python
+
+# Copyright (c) 2008, 2017 Nicira, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import getopt
+import os
+import re
+import sys
+
+
+def parse_include_dirs():
+    include_dirs = []
+    options, args = getopt.gnu_getopt(sys.argv[1:], 'I:', ['include='])
+    for key, value in options:
+        if key in ['-I', '--include']:
+            include_dirs.append(value)
+        else:
+            assert False
+
+    include_dirs.append('.')
+    return include_dirs, args
+
+
+def find_file(include_dirs, name):
+    for dir in include_dirs:
+        file = "%s/%s" % (dir, name)
+        try:
+            os.stat(file)
+            return file
+        except IOError as e:
+            pass
+    sys.stderr.write("%s not found in: %s\n" % (name, ' '.join(include_dirs)))
+    return None
+
+
+so_re = re.compile(r'^\.so (\S+)$')
+
+
+def extract_include_directive(line):
+    m = so_re.match(line)
+    if m:
+        return m.group(1)
+    else:
+        return None
+
+
+def soexpand(include_dirs, src, dst):
+    ok = True
+    while True:
+        line = src.readline()
+        if not line:
+            break
+
+        name = extract_include_directive(line)
+        if name:
+            fn = find_file(include_dirs, name)
+            if fn:
+                try:
+                    f = open(fn)
+                    while True:
+                        inner = f.readline()
+                        if not inner:
+                            break
+                        dst.write(inner)
+                except IOError as e:
+                    sys.stderr.write("%s: open: %s\n" % (fn, e.strerror))
+                    ok = False
+                continue
+            else:
+                ok = False
+
+        dst.write(line)
+    return ok
+
+
+if __name__ == '__main__':
+    include_dirs, args = parse_include_dirs()
+    if args:
+        error = False
+        for arg in args:
+            if not soexpand(include_dirs, open(arg), sys.stdout):
+                error = True
+    else:
+        error = not soexpand(include_dirs, sys.stdin, sys.stdout)
+    sys.exit(1 if error else 0)