Browse code

Use python3 as default python command

After Python 2 is getting unsupported, new distros
like CentOS 8 and RHEL8 have stopped providing 'python'
package forcing user to decide which alternative to
use by installing 'python2' or 'python3.x' package
and then setting python alternative.

This change is intended to make using python3 command as
much as possible and use it as default 'python' alternative
where needed.

The final goals motivating this change are:
- stop using python2 as much as possible
- help adding support for CentOS 8 and RHEL8

Change-Id: I1e90db987c0bfa6206c211e066be03ea8738ad3f

Federico Ressi authored on 2020/01/31 15:43:30
Showing 8 changed files
... ...
@@ -450,6 +450,31 @@ function python3_enabled {
450 450
     fi
451 451
 }
452 452
 
453
+# Provide requested python version and sets PYTHON variable
454
+function install_python {
455
+    # NOTE: install_python function should finally just do what install_python3
456
+    # does as soon Python 2 support has been dropped
457
+    if python3_enabled; then
458
+        install_python3
459
+        export PYTHON=$(which python${PYTHON3_VERSION} 2>/dev/null ||
460
+                        which python3 2>/dev/null)
461
+        if [[ "${DISTRO}" =~ (rhel8) ]]; then
462
+            # Use Python 3 as default python command so that we have only one
463
+            # python alternative to use on the system for either python and
464
+            # python3
465
+            sudo alternatives --set python "${PYTHON}"
466
+        else
467
+            # Install anyway Python 2 for legacy scripts that still requires
468
+            # python instead of python3 command
469
+            install_package python
470
+        fi
471
+    else
472
+        echo "WARNING - Python 2 support has been deprecated in favor of Python 3"
473
+        install_package python
474
+        export PYTHON=$(which python 2>/dev/null)
475
+    fi
476
+}
477
+
453 478
 # Install python3 packages
454 479
 function install_python3 {
455 480
     if is_ubuntu; then
... ...
@@ -415,11 +415,8 @@ fi
415 415
 
416 416
 # Ensure python is installed
417 417
 # --------------------------
418
-install_python3
418
+install_python
419 419
 
420
-if ! python3_enabled; then
421
-    is_package_installed python || install_package python
422
-fi
423 420
 
424 421
 # Configure Logging
425 422
 # -----------------
... ...
@@ -497,14 +494,14 @@ if [[ -n "$LOGFILE" ]]; then
497 497
             _of_args="$_of_args --no-timestamp"
498 498
         fi
499 499
         # Set fd 1 and 2 to write the log file
500
-        exec 1> >( $TOP_DIR/tools/outfilter.py $_of_args -o "${LOGFILE}" ) 2>&1
500
+        exec 1> >( $PYTHON $TOP_DIR/tools/outfilter.py $_of_args -o "${LOGFILE}" ) 2>&1
501 501
         # Set fd 6 to summary log file
502
-        exec 6> >( $TOP_DIR/tools/outfilter.py -o "${SUMFILE}" )
502
+        exec 6> >( $PYTHON $TOP_DIR/tools/outfilter.py -o "${SUMFILE}" )
503 503
     else
504 504
         # Set fd 1 and 2 to primary logfile
505
-        exec 1> >( $TOP_DIR/tools/outfilter.py -o "${LOGFILE}" ) 2>&1
505
+        exec 1> >( $PYTHON $TOP_DIR/tools/outfilter.py -o "${LOGFILE}" ) 2>&1
506 506
         # Set fd 6 to summary logfile and stdout
507
-        exec 6> >( $TOP_DIR/tools/outfilter.py -v -o "${SUMFILE}" >&3 )
507
+        exec 6> >( $PYTHON $TOP_DIR/tools/outfilter.py -v -o "${SUMFILE}" >&3 )
508 508
     fi
509 509
 
510 510
     echo_summary "stack.sh log $LOGFILE"
... ...
@@ -521,7 +518,7 @@ else
521 521
         exec 1>/dev/null 2>&1
522 522
     fi
523 523
     # Always send summary fd to original stdout
524
-    exec 6> >( $TOP_DIR/tools/outfilter.py -v >&3 )
524
+    exec 6> >( $PYTHON $TOP_DIR/tools/outfilter.py -v >&3 )
525 525
 fi
526 526
 
527 527
 # Basic test for ``$DEST`` path permissions (fatal on error unless skipped)
... ...
@@ -557,9 +554,9 @@ function exit_trap {
557 557
             generate-subunit $DEVSTACK_START_TIME $SECONDS 'fail' >> ${SUBUNIT_OUTPUT}
558 558
         fi
559 559
         if [[ -z $LOGDIR ]]; then
560
-            $TOP_DIR/tools/worlddump.py
560
+            ${PYTHON} $TOP_DIR/tools/worlddump.py
561 561
         else
562
-            $TOP_DIR/tools/worlddump.py -d $LOGDIR
562
+            ${PYTHON} $TOP_DIR/tools/worlddump.py -d $LOGDIR
563 563
         fi
564 564
     else
565 565
         # If we error before we've installed os-testr, this will fail.
... ...
@@ -8,7 +8,7 @@ source $TOP/tests/unittest.sh
8 8
 
9 9
 OUT_DIR=$(mktemp -d)
10 10
 
11
-$TOP/tools/worlddump.py -d $OUT_DIR
11
+${PYTHON} $TOP/tools/worlddump.py -d $OUT_DIR
12 12
 
13 13
 if [[ $? -ne 0 ]]; then
14 14
     fail "worlddump failed"
... ...
@@ -1,4 +1,4 @@
1
-#! /usr/bin/env python
1
+#! /usr/bin/env python3
2 2
 
3 3
 # Copyright 2016 Hewlett Packard Enterprise Development Company, L.P.
4 4
 #
... ...
@@ -81,12 +81,6 @@ if [[ -n "$SYSLOG" && "$SYSLOG" != "False" ]]; then
81 81
     fi
82 82
 fi
83 83
 
84
-if python3_enabled; then
85
-    install_python3
86
-    export PYTHON=$(which python${PYTHON3_VERSION} 2>/dev/null || which python3 2>/dev/null)
87
-else
88
-    export PYTHON=$(which python 2>/dev/null)
89
-fi
90 84
 
91 85
 # Mark end of run
92 86
 # ---------------
93 87
old mode 100755
94 88
new mode 100644
... ...
@@ -1,5 +1,5 @@
1
-#!/usr/bin/env python
2
-#
1
+#!/usr/bin/env python3
2
+
3 3
 # Copyright 2014 Hewlett-Packard Development Company, L.P.
4 4
 #
5 5
 # Licensed under the Apache License, Version 2.0 (the "License"); you may
... ...
@@ -1,4 +1,4 @@
1
-#!/usr/bin/env python
1
+#!/usr/bin/env python3
2 2
 
3 3
 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 4
 # not use this file except in compliance with the License. You may obtain
5 5
old mode 100755
6 6
new mode 100644
... ...
@@ -1,4 +1,4 @@
1
-#!/usr/bin/env python
1
+#!/usr/bin/env python3
2 2
 #
3 3
 # Copyright 2014 Hewlett-Packard Development Company, L.P.
4 4
 #
... ...
@@ -23,8 +23,8 @@ import argparse
23 23
 import datetime
24 24
 from distutils import spawn
25 25
 import fnmatch
26
+import io
26 27
 import os
27
-import os.path
28 28
 import shutil
29 29
 import subprocess
30 30
 import sys
... ...
@@ -109,9 +109,10 @@ def _bridge_list():
109 109
 # This method gets max version searching 'OpenFlow versions 0x1:0x'.
110 110
 # And return a version value converted to an integer type.
111 111
 def _get_ofp_version():
112
-    process = subprocess.Popen(['ovs-ofctl', '--version'], stdout=subprocess.PIPE)
112
+    process = subprocess.Popen(['ovs-ofctl', '--version'],
113
+                               stdout=subprocess.PIPE)
113 114
     stdout, _ = process.communicate()
114
-    find_str = 'OpenFlow versions 0x1:0x'
115
+    find_str = b'OpenFlow versions 0x1:0x'
115 116
     offset = stdout.find(find_str)
116 117
     return int(stdout[offset + len(find_str):-1]) - 1
117 118
 
... ...
@@ -206,7 +207,7 @@ def process_list():
206 206
 
207 207
 def compute_consoles():
208 208
     _header("Compute consoles")
209
-    for root, dirnames, filenames in os.walk('/opt/stack'):
209
+    for root, _, filenames in os.walk('/opt/stack'):
210 210
         for filename in fnmatch.filter(filenames, 'console.log'):
211 211
             fullpath = os.path.join(root, filename)
212 212
             _dump_cmd("sudo cat %s" % fullpath)
... ...
@@ -234,12 +235,22 @@ def var_core():
234 234
         # tools out there that can do that sort of thing though.
235 235
         _dump_cmd("ls -ltrah /var/core")
236 236
 
237
+
238
+def disable_stdio_buffering():
239
+    # re-open STDOUT as binary, then wrap it in a
240
+    # TextIOWrapper, and write through everything.
241
+    binary_stdout = io.open(sys.stdout.fileno(), 'wb', 0)
242
+    sys.stdout = io.TextIOWrapper(binary_stdout, write_through=True)
243
+
244
+
237 245
 def main():
238 246
     opts = get_options()
239 247
     fname = filename(opts.dir, opts.name)
240 248
     print("World dumping... see %s for details" % fname)
241
-    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
242
-    with open(fname, 'w') as f:
249
+
250
+    disable_stdio_buffering()
251
+
252
+    with io.open(fname, 'w') as f:
243 253
         os.dup2(f.fileno(), sys.stdout.fileno())
244 254
         disk_space()
245 255
         process_list()