Skip to content

Commit 21a10d3

Browse files
Federico Ressiyoctozepto
authored andcommitted
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
1 parent 2dcbc28 commit 21a10d3

8 files changed

Lines changed: 56 additions & 29 deletions

File tree

inc/python

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,31 @@ function python3_enabled {
450450
fi
451451
}
452452

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+
453478
# Install python3 packages
454479
function install_python3 {
455480
if is_ubuntu; then

stack.sh

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,8 @@ fi
415415

416416
# Ensure python is installed
417417
# --------------------------
418-
install_python3
418+
install_python
419419

420-
if ! python3_enabled; then
421-
is_package_installed python || install_package python
422-
fi
423420

424421
# Configure Logging
425422
# -----------------
@@ -497,14 +494,14 @@ if [[ -n "$LOGFILE" ]]; then
497494
_of_args="$_of_args --no-timestamp"
498495
fi
499496
# Set fd 1 and 2 to write the log file
500-
exec 1> >( $TOP_DIR/tools/outfilter.py $_of_args -o "${LOGFILE}" ) 2>&1
497+
exec 1> >( $PYTHON $TOP_DIR/tools/outfilter.py $_of_args -o "${LOGFILE}" ) 2>&1
501498
# Set fd 6 to summary log file
502-
exec 6> >( $TOP_DIR/tools/outfilter.py -o "${SUMFILE}" )
499+
exec 6> >( $PYTHON $TOP_DIR/tools/outfilter.py -o "${SUMFILE}" )
503500
else
504501
# Set fd 1 and 2 to primary logfile
505-
exec 1> >( $TOP_DIR/tools/outfilter.py -o "${LOGFILE}" ) 2>&1
502+
exec 1> >( $PYTHON $TOP_DIR/tools/outfilter.py -o "${LOGFILE}" ) 2>&1
506503
# Set fd 6 to summary logfile and stdout
507-
exec 6> >( $TOP_DIR/tools/outfilter.py -v -o "${SUMFILE}" >&3 )
504+
exec 6> >( $PYTHON $TOP_DIR/tools/outfilter.py -v -o "${SUMFILE}" >&3 )
508505
fi
509506

510507
echo_summary "stack.sh log $LOGFILE"
@@ -521,7 +518,7 @@ else
521518
exec 1>/dev/null 2>&1
522519
fi
523520
# Always send summary fd to original stdout
524-
exec 6> >( $TOP_DIR/tools/outfilter.py -v >&3 )
521+
exec 6> >( $PYTHON $TOP_DIR/tools/outfilter.py -v >&3 )
525522
fi
526523

527524
# Basic test for ``$DEST`` path permissions (fatal on error unless skipped)
@@ -557,9 +554,9 @@ function exit_trap {
557554
generate-subunit $DEVSTACK_START_TIME $SECONDS 'fail' >> ${SUBUNIT_OUTPUT}
558555
fi
559556
if [[ -z $LOGDIR ]]; then
560-
$TOP_DIR/tools/worlddump.py
557+
${PYTHON} $TOP_DIR/tools/worlddump.py
561558
else
562-
$TOP_DIR/tools/worlddump.py -d $LOGDIR
559+
${PYTHON} $TOP_DIR/tools/worlddump.py -d $LOGDIR
563560
fi
564561
else
565562
# If we error before we've installed os-testr, this will fail.

tests/test_worlddump.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ source $TOP/tests/unittest.sh
88

99
OUT_DIR=$(mktemp -d)
1010

11-
$TOP/tools/worlddump.py -d $OUT_DIR
11+
${PYTHON} $TOP/tools/worlddump.py -d $OUT_DIR
1212

1313
if [[ $? -ne 0 ]]; then
1414
fail "worlddump failed"

tools/generate-devstack-plugins-list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/env python3
22

33
# Copyright 2016 Hewlett Packard Enterprise Development Company, L.P.
44
#

tools/install_prereqs.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ if [[ -n "$SYSLOG" && "$SYSLOG" != "False" ]]; then
8181
fi
8282
fi
8383

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
9084

9185
# Mark end of run
9286
# ---------------

tools/outfilter.py

100755100644
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/usr/bin/env python
2-
#
1+
#!/usr/bin/env python3
2+
33
# Copyright 2014 Hewlett-Packard Development Company, L.P.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License"); you may

tools/update_clouds_yaml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
# Licensed under the Apache License, Version 2.0 (the "License"); you may
44
# not use this file except in compliance with the License. You may obtain

tools/worlddump.py

100755100644
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
#
33
# Copyright 2014 Hewlett-Packard Development Company, L.P.
44
#
@@ -23,8 +23,8 @@
2323
import datetime
2424
from distutils import spawn
2525
import fnmatch
26+
import io
2627
import os
27-
import os.path
2828
import shutil
2929
import subprocess
3030
import sys
@@ -109,9 +109,10 @@ def _bridge_list():
109109
# This method gets max version searching 'OpenFlow versions 0x1:0x'.
110110
# And return a version value converted to an integer type.
111111
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)
113114
stdout, _ = process.communicate()
114-
find_str = 'OpenFlow versions 0x1:0x'
115+
find_str = b'OpenFlow versions 0x1:0x'
115116
offset = stdout.find(find_str)
116117
return int(stdout[offset + len(find_str):-1]) - 1
117118

@@ -206,7 +207,7 @@ def process_list():
206207

207208
def compute_consoles():
208209
_header("Compute consoles")
209-
for root, dirnames, filenames in os.walk('/opt/stack'):
210+
for root, _, filenames in os.walk('/opt/stack'):
210211
for filename in fnmatch.filter(filenames, 'console.log'):
211212
fullpath = os.path.join(root, filename)
212213
_dump_cmd("sudo cat %s" % fullpath)
@@ -234,12 +235,22 @@ def var_core():
234235
# tools out there that can do that sort of thing though.
235236
_dump_cmd("ls -ltrah /var/core")
236237

238+
239+
def disable_stdio_buffering():
240+
# re-open STDOUT as binary, then wrap it in a
241+
# TextIOWrapper, and write through everything.
242+
binary_stdout = io.open(sys.stdout.fileno(), 'wb', 0)
243+
sys.stdout = io.TextIOWrapper(binary_stdout, write_through=True)
244+
245+
237246
def main():
238247
opts = get_options()
239248
fname = filename(opts.dir, opts.name)
240249
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:
250+
251+
disable_stdio_buffering()
252+
253+
with io.open(fname, 'w') as f:
243254
os.dup2(f.fileno(), sys.stdout.fileno())
244255
disk_space()
245256
process_list()

0 commit comments

Comments
 (0)