Skip to content

Commit eb81f25

Browse files
albu-dikujonasbardino
authored andcommitted
Sweep the tree for everything conditional on python 2.
The only thing explicitly left out is the walk relted code in fileio which is rather weedy and being tackled in a separate change.
1 parent 8d74a98 commit eb81f25

22 files changed

+38
-159
lines changed

mig/server/grid_events.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def __update_rule_monitor(
538538
# Fire 'modified' events for all dirs and files in subpath
539539
# to ensure that all rule files are loaded
540540

541-
for ent in scandir(src_path):
541+
for ent in os.scandir(src_path):
542542
if ent.is_dir(follow_symlinks=True):
543543

544544
# logger.debug('(%s) Dispatch DirCreatedEvent for: %s'
@@ -1139,7 +1139,7 @@ def __update_file_monitor(self, event):
11391139
# For create this occurs by eg. mkdir -p 'path/subpath/subpath2'
11401140
# or 'cp -rf'
11411141

1142-
for ent in scandir(src_path):
1142+
for ent in os.scandir(src_path):
11431143
if ent.is_dir(follow_symlinks=True):
11441144
vgrid_sub_path = strip_base_dirs(ent.path)
11451145

@@ -1491,7 +1491,7 @@ def add_vgrid_file_monitor(configuration, vgrid_name, path):
14911491

14921492
# Traverse dirs for subdirs created since last run
14931493

1494-
for ent in scandir(vgrid_files_path):
1494+
for ent in os.scandir(vgrid_files_path):
14951495
if ent.is_dir(follow_symlinks=True):
14961496
vgrid_sub_path = strip_base_dirs(ent.path)
14971497
# Force utf8 everywhere to avoid encoding issues
@@ -1839,7 +1839,7 @@ def monitor(configuration, vgrid_name):
18391839
configuration.vgrid_triggers)
18401840
all_trigger_rules.append(rule_path)
18411841
else:
1842-
for ent in scandir(vgrid_home):
1842+
for ent in os.scandir(vgrid_home):
18431843
if configuration.vgrid_triggers in ent.name:
18441844
rule_path = ent.path
18451845
all_trigger_rules.append(rule_path)
@@ -1981,7 +1981,7 @@ def monitor(configuration, vgrid_name):
19811981

19821982
# Each top vgrid gets is own process
19831983

1984-
for ent in scandir(configuration.vgrid_home):
1984+
for ent in os.scandir(configuration.vgrid_home):
19851985
vgrid_files_path = os.path.join(configuration.vgrid_files_home,
19861986
ent.name)
19871987
if os.path.isdir(ent.path) and os.path.isdir(vgrid_files_path):

mig/shared/base.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -736,23 +736,13 @@ def native_args(argv):
736736
https://docs.python.org/3/library/sys.html#sys.argv
737737
for further details.
738738
"""
739-
if sys.version_info[0] >= 3:
740-
return [os.fsencode(arg) for arg in argv]
741-
else:
742-
return argv
739+
return [os.fsencode(arg) for arg in argv]
743740

744741

745742
def NativeStringIO(initial_value=''):
746-
"""Mock StringIO pseudo-class to create a StringIO matching the native
747-
string coding form. That is a BytesIO with utf8 on python 2 and unicode
748-
StringIO otherwise. Optional string helpers are automatically converted
749-
accordingly.
743+
"""Pseudo-class wrapper to return a StringIO. This is a unicode StringIO.
750744
"""
751-
if sys.version_info[0] >= 3:
752-
return io.StringIO(initial_value)
753-
else:
754-
from StringIO import StringIO
755-
return StringIO(initial_value)
745+
return io.StringIO(initial_value)
756746

757747

758748
def DefaultStringIO(initial_value=''):

mig/shared/cgiscriptstub.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ def finish_cgi_script(configuration, backend, output_format, ret_code, ret_msg,
117117
#logger.debug("flush stdout")
118118
sys.stdout.flush()
119119
#logger.debug("write content: %s" % [output[:64], '..', output[-64:]])
120-
# NOTE: always output native strings to stdout but use raw buffer
121-
# for byte output on py3 as explained above.
122-
if sys.version_info[0] < 3 or is_default_str_coding(output):
120+
# NOTE: use raw buffer for byte output as explained above
121+
if is_default_str_coding(output):
123122
sys.stdout.write(output)
124123
else:
125124
sys.stdout.buffer.write(output)

mig/shared/compat.py

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,10 @@
3636
import codecs
3737
import io
3838
import sys
39-
# NOTE: StringIO is only available in python2
40-
try:
41-
import StringIO
42-
except ImportError:
43-
StringIO = None
4439

45-
PY2 = sys.version_info[0] < 3
4640
_TYPE_UNICODE = type(u"")
4741

4842

49-
if PY2:
50-
class SimpleNamespace(dict):
51-
"""Bare minimum SimpleNamespace for Python 2."""
52-
53-
def __getattribute__(self, name):
54-
if name == '__dict__':
55-
return dict(**self)
56-
57-
return self[name]
58-
else:
59-
from types import SimpleNamespace
60-
61-
6243
def _is_unicode(val):
6344
"""Return boolean indicating if the value is a unicode string.
6445
@@ -72,34 +53,9 @@ def ensure_native_string(string_or_bytes):
7253
"""Given a supplied input which can be either a string or bytes
7354
return a representation providing string operations while ensuring that
7455
its contents represent a valid series of textual characters.
75-
76-
Arrange identical operation across python 2 and 3 - specifically,
77-
the presence of invalid UTF-8 bytes (thus the input not being a
78-
valid textual string) will trigger a UnicodeDecodeError on PY3.
79-
Force the same to occur on PY2.
8056
"""
81-
if PY2:
82-
# Simulate decoding done by PY3 to trigger identical exceptions
83-
# note the use of a forced "utf8" encoding value: this function
84-
# is generally used to wrap, for example, substitutions of values
85-
# into strings that are defined in the source code. In Python 3
86-
# these are mandated to be UTF-8, and thus decoding as "utf8" is
87-
# what will be attempted on supplied input. Match it.
88-
textual_output = codecs.encode(string_or_bytes, 'utf8')
89-
elif not _is_unicode(string_or_bytes):
57+
if not _is_unicode(string_or_bytes):
9058
textual_output = str(string_or_bytes, 'utf8')
9159
else:
9260
textual_output = string_or_bytes
9361
return textual_output
94-
95-
96-
def NativeStringIO(initial_value=''):
97-
"""Mock StringIO pseudo-class to create a StringIO matching the native
98-
string coding form. That is a BytesIO with utf8 on python 2 and unicode
99-
StringIO otherwise. Optional string helpers are automatically converted
100-
accordingly.
101-
"""
102-
if PY2 and StringIO is not None:
103-
return StringIO.StringIO(initial_value)
104-
else:
105-
return io.StringIO(initial_value)

mig/shared/configuration.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from builtins import object
3535

3636
import base64
37+
from configparser import ConfigParser
3738
import copy
3839
import datetime
3940
import functools
@@ -50,13 +51,6 @@
5051
standard_library.install_aliases()
5152

5253

53-
# NOTE: should be handled by future aliases but fails in PAM C extension
54-
if sys.version_info[0] < 3:
55-
# NOTE: always use native py2 version here - new one causes unicode mess
56-
from ConfigParser import ConfigParser
57-
else:
58-
from configparser import ConfigParser
59-
6054
# NOTE: protect migrid import from autopep8 reordering
6155
try:
6256
from mig.shared.base import force_native_str

mig/shared/defaults.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,8 @@
3737
MIG_BASE = os.path.realpath(os.path.join(os.path.dirname(__file__), '../..'))
3838
MIG_ENV = os.getenv('MIG_ENV', 'default')
3939

40-
# NOTE: python3 switched strings to use unicode by default in contrast to bytes
41-
# in python2. File systems remain with utf8 however so we need to
42-
# carefully handle a lot of cases of either encoding to utf8 or decoding
43-
# to unicode depending on the python used.
44-
# Please refer to the helpers in shared.base for actual handling of it.
45-
if sys.version_info[0] >= 3:
46-
default_str_coding = 'unicode'
47-
default_fs_coding = 'utf8'
48-
else:
49-
default_str_coding = 'utf8'
50-
default_fs_coding = 'utf8'
40+
default_str_coding = 'unicode'
41+
default_fs_coding = 'utf8'
5142

5243
CODING_KINDS = (STR_KIND, FS_KIND) = ('__STR__', '__FS__')
5344

mig/shared/output.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def txt_format(configuration, ret_val, ret_msg, out_obj):
651651
lines = [status_line] + lines
652652

653653
# NOTE: careful handling required for binary on python3+
654-
if sys.version_info[0] > 2 and binary_output:
654+
if binary_output:
655655
return b''.join(lines)
656656
else:
657657
return ''.join(lines)
@@ -2646,7 +2646,7 @@ def html_format(configuration, ret_val, ret_msg, out_obj):
26462646
user_widgets))
26472647

26482648
# NOTE: careful handling required for binary on python3+
2649-
if sys.version_info[0] > 2 and binary_output:
2649+
if binary_output:
26502650
return b''.join(lines)
26512651
else:
26522652
return '\n'.join(lines)

mig/shared/ssh.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@
4444
# Paramiko not available - imported fom griddaemons so fail gracefully
4545
paramiko = None
4646

47-
from mig.shared.base import client_id_dir, force_utf8
47+
from mig.shared.base import client_id_dir, force_utf8, NativeStringIO
4848
from mig.shared.conf import get_resource_exe, get_configuration_object
49-
from mig.shared.compat import NativeStringIO
5049
from mig.shared.defaults import ssh_conf_dir
5150
from mig.shared.safeeval import subprocess_popen, subprocess_pipe
5251

mig/shared/url.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,9 @@
4242
import base64
4343
import os
4444
import sys
45-
46-
# NOTE: moved to urllib.parse in python3 and are re-exposed with future.
47-
# Other modules should import helpers from here for consistency.
48-
# TODO: handle the unicode returned by python3 and future versions!
49-
# Perhaps switch to suggested "easiest option" from
50-
# http://python-future.org/compatible_idioms.html#urllib-module
51-
# once we have unicode/bytecode mix-up sorted out.
52-
if sys.version_info[0] >= 3:
53-
from urllib.parse import quote, unquote, urlencode, parse_qs, parse_qsl, \
54-
urlsplit, urlparse, urljoin
55-
from urllib.request import urlopen
56-
else:
57-
from urllib import quote, unquote, urlencode, urlopen
58-
from urlparse import parse_qs, parse_qsl, urlsplit, urlparse, urljoin
45+
from urllib.parse import quote, unquote, urlencode, parse_qs, parse_qsl, \
46+
urlsplit, urlparse, urljoin
47+
from urllib.request import urlopen
5948

6049
try:
6150
from mig.shared.base import force_utf8, force_native_str

mig/unittest/testcore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
sys.path.append(os.path.realpath(
4040
os.path.join(os.path.dirname(__file__), "../..")))
41-
from tests.support import MIG_BASE, PY2, is_path_within
41+
from tests.support import MIG_BASE, is_path_within
4242

4343
from mig.shared.base import client_id_dir, client_dir_id, get_short_id, \
4444
invisible_path, allow_script, brief_list

0 commit comments

Comments
 (0)