Proposed fix for https://bugs.gnunet.org/view.php?id=5611
authorng0 <ng0@n0.is>
Tue, 26 Feb 2019 08:35:45 +0000 (08:35 +0000)
committerng0 <ng0@n0.is>
Tue, 26 Feb 2019 08:35:45 +0000 (08:35 +0000)
src/util/Makefile.am
src/util/gnunet-qr.in [new file with mode: 0755]
src/util/gnunet-qr.py [new file with mode: 0755]
src/util/gnunet-qr.py.in [deleted file]

index d1aa8aa9b7390bd83fa022c0834afe2511d673d6..7041004ace41376e26187ddfec72d0632939e0e2 100644 (file)
@@ -204,8 +204,15 @@ libexec_PROGRAMS = \
  gnunet-timeout \
  $(W32CONSOLEHELPER)
 
+do_subst = $(SED) -e 's,[@]PREFIX[@],$(PREFIX),g'
+
+gnunet-qr: gnunet-qr.in Makefile
+       $(do_subst) < $(srcdir)/gnunet-qr.in > gnunet-qr
+       chmod +x gnunet-qr
+
 bin_SCRIPTS =\
- gnunet-qr
+ gnunet-qr \
+ gnunet-qr.py
 
 bin_PROGRAMS = \
  gnunet-resolver \
@@ -236,21 +243,6 @@ gnunet_timeout_SOURCES = \
  gnunet-timeout-w32.c
 endif
 
-# This is horrible, but compared to the alternatives and the solution
-# which preceded this it is a good compromise and good enough for one
-# file. Everyone else is invited to patch it locally.
-# In case someone reads this file and is wondering about the
-# assignment operator below, it's explained here:
-# https://lists.gnu.org/archive/html/make-w32/2013-10/msg00021.html
-
-xENV != which env
-
-do_subst = $(SED) -e 's,[@]ENV[@],${xENV},g'
-
-gnunet-qr: gnunet-qr.py.in Makefile
-       $(do_subst) < $(top_srcdir)/src/util/gnunet-qr.py.in > gnunet-qr
-       chmod +x gnunet-qr
-
 gnunet_service_resolver_SOURCES = \
  gnunet-service-resolver.c
 gnunet_service_resolver_LDADD = \
@@ -675,4 +667,5 @@ EXTRA_DIST = \
   test_resolver_api_data.conf \
   test_service_data.conf \
   test_speedup_data.conf \
-  gnunet-qr.py.in
+  gnunet-qr.py \
+  gnunet-qr.in
diff --git a/src/util/gnunet-qr.in b/src/util/gnunet-qr.in
new file mode 100755 (executable)
index 0000000..5e7a285
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/sh
+#
+# From curl's buildconf, making this script subject to the
+# curl license: https://curl.haxx.se/docs/copyright.html
+# Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
+# Copyright (C) 2019 GNUnet e.V.
+
+# findtool works like which without relying on which (which is a problem
+# for some limited shells.
+findtool(){
+  file="$1"
+
+  if { echo "$file" | grep "/" >/dev/null 2>&1; } then
+    # when file is given with a path check it first
+    if test -f "$file"; then
+      echo "$file"
+      return
+    fi
+  fi
+
+  old_IFS=$IFS; IFS=':'
+  for path in $PATH
+  do
+    IFS=$old_IFS
+    # echo "checks for $file in $path" >&2
+    if test "$path" -a "$path" != '.' -a -f "$path/$file"; then
+      echo "$path/$file"
+      return
+    fi
+  done
+  IFS=$old_IFS
+}
+
+# end curl licensed code
+pythonize=`findtool python2.7 2>/dev/null`
+if test ! -x "$pythonize"; then
+       pythonize=`findtool ${PYTHON2:-python2.7}`
+fi
+
+if test -z "$pythonize"; then
+  echo "ERROR: python2.7 not found."
+  echo "  You need python2.7 installed."
+  exit 1
+fi
+
+${pythonize} @PREFIX@/bin/gnunet-qr.py || echo "ERROR: python2.7 or future not found" && exit 1
diff --git a/src/util/gnunet-qr.py b/src/util/gnunet-qr.py
new file mode 100755 (executable)
index 0000000..bf35b3a
--- /dev/null
@@ -0,0 +1,110 @@
+from __future__ import print_function
+from builtins import str
+import sys
+import getopt
+import subprocess
+from sys import argv
+try:
+    import zbar
+except ImportError as e:
+    print('Cannot run gnunet-qr, please install zbar-python')
+    sys.exit(1)
+
+
+def help():
+    print('gnunet-qr\n\
+Scan a QR code using a video device and import\n\
+Arguments mandatory for long options are also mandatory for short options.\n\
+  -c, --config=FILENAME      use configuration file FILENAME\n\
+  -d, --device=DEVICE        use device DEVICE\n\
+  -s, --silent               do not show preview windows\n\
+  -h, --help                 print this help\n\
+  -v, --verbose              be verbose\n\
+Report bugs to gnunet-developers@gnu.org.\n\
+GNUnet home page: http://www.gnu.org/software/gnunet/\n\
+General help using GNU software: http://www.gnu.org/gethelp/')
+
+
+if __name__ == '__main__':
+        configuration = ''
+        device = '/dev/video0'
+        url = ''
+        verbose = False
+        silent = False
+        # Parse arguments
+        try:
+                opts, args = getopt.gnu_getopt(sys.argv[1:], "c:hd:sv", ["config", "help", "device", "silent", "verbose"])
+        except getopt.GetoptError as e:
+                help()
+                print(str(e))
+                exit(1)
+        for o, a in opts:
+                if o in ("-h", "--help"):
+                        help()
+                        sys.exit(0)
+                elif o in ("-c", "--config"):
+                        configuration = a
+                elif o in ("-d", "--device"):
+                        device = a
+                elif o in ("-s", "--silent"):
+                        silent = True
+                elif o in ("-v", "--verbose"):
+                        verbose = True
+        if (True == verbose):
+                print('Initializing')
+        # create a Processor
+        proc = zbar.Processor()
+
+        # configure the Processor
+        proc.parse_config('enable')
+
+        # initialize the Processor
+        try:
+                if (True == verbose):
+                        print('Opening video device ' + device)
+                proc.init(device)
+        except Exception as e:
+                print('Failed to open device ' + device)
+                exit(1)
+
+        # enable the preview window
+        # if (True == silent):
+        #       proc.visible = True
+        # else:
+        #               proc.visible = False
+
+        proc.visible = True
+        # read at least one barcode (or until window closed)
+        try:
+                if (True == verbose):
+                        print('Capturing')
+                proc.process_one()
+        except Exception as e:
+                # Window was closed without finding code
+                exit(1)
+
+        # hide the preview window
+        proc.visible = False
+
+        # extract results
+        for symbol in proc.results:
+                # do something useful with results
+                if (True == verbose):
+                        print('Found ', symbol.type, ' symbol ', '"%s"' % symbol.data)
+                args = list()
+                args.append("gnunet-uri")
+                if (configuration != ''):
+                        args.append(str("-c " + str(configuration)))
+                args.append(str(symbol.data))
+                cmd = ''
+                for a in args:
+                        cmd += " " + str(a)
+                if (verbose):
+                        print('Running `' + cmd +'`')
+                res = subprocess.call(args)
+                if (0 != res):
+                        print('Failed to add URI ' + str(symbol.data))
+                else:
+                        print('Added URI ' + str(symbol.data))
+                exit(res)
+        exit(1)
diff --git a/src/util/gnunet-qr.py.in b/src/util/gnunet-qr.py.in
deleted file mode 100755 (executable)
index ceed8bd..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-#!@ENV@ python2.7
-from __future__ import print_function
-from builtins import str
-import sys
-import getopt
-import subprocess
-from sys import argv
-try:
-    import zbar
-except ImportError as e:
-    print('Cannot run gnunet-qr, please install zbar-python')
-    sys.exit(1)
-
-
-def help():
-    print('gnunet-qr\n\
-Scan a QR code using a video device and import\n\
-Arguments mandatory for long options are also mandatory for short options.\n\
-  -c, --config=FILENAME      use configuration file FILENAME\n\
-  -d, --device=DEVICE        use device DEVICE\n\
-  -s, --silent               do not show preview windows\n\
-  -h, --help                 print this help\n\
-  -v, --verbose              be verbose\n\
-Report bugs to gnunet-developers@gnu.org.\n\
-GNUnet home page: http://www.gnu.org/software/gnunet/\n\
-General help using GNU software: http://www.gnu.org/gethelp/')
-
-
-if __name__ == '__main__':
-        configuration = ''
-        device = '/dev/video0'
-        url = ''
-        verbose = False
-        silent = False
-        # Parse arguments
-        try:
-                opts, args = getopt.gnu_getopt(sys.argv[1:], "c:hd:sv", ["config", "help", "device", "silent", "verbose"])
-        except getopt.GetoptError as e:
-                help()
-                print(str(e))
-                exit(1)
-        for o, a in opts:
-                if o in ("-h", "--help"):
-                        help()
-                        sys.exit(0)
-                elif o in ("-c", "--config"):
-                        configuration = a
-                elif o in ("-d", "--device"):
-                        device = a
-                elif o in ("-s", "--silent"):
-                        silent = True
-                elif o in ("-v", "--verbose"):
-                        verbose = True
-        if (True == verbose):
-                print('Initializing')
-        # create a Processor
-        proc = zbar.Processor()
-
-        # configure the Processor
-        proc.parse_config('enable')
-
-        # initialize the Processor
-        try:
-                if (True == verbose):
-                        print('Opening video device ' + device)
-                proc.init(device)
-        except Exception as e:
-                print('Failed to open device ' + device)
-                exit(1)
-
-        # enable the preview window
-        # if (True == silent):
-        #       proc.visible = True
-        # else:
-        #               proc.visible = False
-
-        proc.visible = True
-        # read at least one barcode (or until window closed)
-        try:
-                if (True == verbose):
-                        print('Capturing')
-                proc.process_one()
-        except Exception as e:
-                # Window was closed without finding code
-                exit(1)
-
-        # hide the preview window
-        proc.visible = False
-
-        # extract results
-        for symbol in proc.results:
-                # do something useful with results
-                if (True == verbose):
-                        print('Found ', symbol.type, ' symbol ', '"%s"' % symbol.data)
-                args = list()
-                args.append("gnunet-uri")
-                if (configuration != ''):
-                        args.append(str("-c " + str(configuration)))
-                args.append(str(symbol.data))
-                cmd = ''
-                for a in args:
-                        cmd += " " + str(a)
-                if (verbose):
-                        print('Running `' + cmd +'`')
-                res = subprocess.call(args)
-                if (0 != res):
-                        print('Failed to add URI ' + str(symbol.data))
-                else:
-                        print('Added URI ' + str(symbol.data))
-                exit(res)
-        exit(1)