Procházet zdrojové kódy

Support fallback to python2 on some distributions

Turns out that even distro as new as RHEL7 does not still support
python3 properly.  This commit enables fallback to python2 on older
distros.

RPM part could be solved by rpm macros; Debian part is left to require
Python 3.4 (as per current stable); we can fix that later if need be.
Alois Mahdal před 6 roky
rodič
revize
796653c7d8
2 změnil soubory, kde provedl 29 přidání a 8 odebrání
  1. 11
    4
      packaging/template.spec
  2. 18
    4
      src/uripecker.sh.skel

+ 11
- 4
packaging/template.spec Zobrazit soubor

@@ -1,6 +1,13 @@
1 1
 %global sfincb %{_datadir}/shellfu/include-bash
2 2
 %global sfmodn __SHELLFU_MODNAME__
3 3
 %global shellfu_req shellfu >= 0.10.4, shellfu < 0.11
4
+%if 0%{?rhel} <= 7
5
+%global py_eq     python
6
+%global pylibs_req python-libs
7
+%else
8
+%global py_eq     python3
9
+%global pylibs_req python3-libs
10
+%endif
4 11
 
5 12
 Name:       __MKIT_PROJ_PKGNAME__
6 13
 Version:    __MKIT_PROJ_VERSION__
@@ -12,14 +19,14 @@ Source0:    %{name}-%{version}.tar.gz
12 19
 BuildArch:  noarch
13 20
 BuildRequires: %shellfu_req
14 21
 BuildRequires: perl
15
-BuildRequires: python3
16
-BuildRequires: python3-libs
22
+BuildRequires: %py_eq
23
+BuildRequires: %pylibs_req
17 24
 BuildRequires: shellfu-bash-pretty
18 25
 
19 26
 Requires: %shellfu_req
20 27
 Requires: perl
21
-Requires: python3
22
-Requires: python3-libs
28
+Requires: %py_eq
29
+Requires: %pylibs_req
23 30
 Requires: shellfu-bash
24 31
 Requires: shellfu-bash-pretty
25 32
 %description

+ 18
- 4
src/uripecker.sh.skel Zobrazit soubor

@@ -241,7 +241,7 @@ __uripecker__flt_uris() {
241 241
         echo 'for uri in re.findall(os.environ["URI_RE"], sys.stdin.read()):'
242 242
         echo '    print(uri)'
243 243
     )
244
-    URI_RE=$uri_re python3 -c "$py_code"
244
+    URI_RE=$uri_re $__URIPECKER_PYBIN -c "$py_code"
245 245
 }
246 246
 
247 247
 __uripecker__minimize() {
@@ -263,12 +263,26 @@ __uripecker__urlquote() {
263 263
     local query=$1
264 264
     debug -v query
265 265
     #shellcheck disable=SC2028
266
-    LC_ALL=en_US.UTF-8 python3 -c "$(
267
-        echo 'import urllib.parse'
266
+    LC_ALL=en_US.UTF-8 $__URIPECKER_PYBIN -c "$(
267
+        echo "import $__URIPECKER_PYMOD"
268 268
         echo 'import sys'
269
-        echo 'print(urllib.parse.quote_plus(sys.argv[1]))'
269
+        echo "print($__URIPECKER_PYMOD.quote_plus(sys.argv[1]))"
270 270
     )" "$query"
271 271
     #FIXME: There should be proper way w/o touching LC_ALL
272 272
 }
273 273
 
274
+__shellfu_uripecker__init() {
275
+    #
276
+    # See what python version is here
277
+    #
278
+    if python3 --version 2>/dev/null \
279
+        | grep -qF 'Python 3.'; then
280
+        __URIPECKER_PYBIN=python3
281
+        __URIPECKER_PYMOD=urllib.parse
282
+    else
283
+        __URIPECKER_PYBIN=python
284
+        __URIPECKER_PYMOD=urllib
285
+    fi
286
+}
287
+
274 288
 #shellfu module-version=__MKIT_PROJ_VERSION__