Remove Python2 references from the source code.

refs #5443

Initial patch: Neel Patel
This commit is contained in:
Akshay Joshi
2020-04-30 17:22:48 +05:30
parent 7dd00a1494
commit ad80217593
48 changed files with 143 additions and 537 deletions

View File

@@ -40,7 +40,6 @@ from threading import Thread
import signal
_IS_WIN = (os.name == 'nt')
_IS_PY2 = (sys.version_info[0] == 2)
_ZERO = timedelta(0)
_sys_encoding = None
_fs_encoding = None
@@ -48,16 +47,12 @@ _u = None
_out_dir = None
_log_file = None
if _IS_PY2:
def _log(msg):
with open(_log_file, 'a') as fp:
fp.write(('INFO:: %s\n' % str(msg)))
else:
def _log(msg):
with open(_log_file, 'a') as fp:
fp.write(
('INFO:: %s\n' % msg.encode('ascii', 'xmlcharrefreplace'))
)
def _log(msg):
with open(_log_file, 'a') as fp:
fp.write(
('INFO:: %s\n' % msg.encode('ascii', 'xmlcharrefreplace'))
)
def unescape_dquotes_process_arg(arg):
@@ -193,61 +188,32 @@ class ProcessLogger(Thread):
self.process = process
self.stream = stream
if not _IS_PY2:
def log(self, msg):
"""
This function will update log file
def log(self, msg):
"""
This function will update log file
Args:
msg: message
Args:
msg: message
Returns:
None
"""
# Write into log file
if self.logger:
if msg:
self.logger.write(
get_current_time(
format='%y%m%d%H%M%S%f'
).encode('utf-8')
)
self.logger.write(b',')
self.logger.write(
msg.lstrip(b'\r\n' if _IS_WIN else b'\n')
)
self.logger.write(os.linesep.encode('utf-8'))
Returns:
None
"""
# Write into log file
if self.logger:
if msg:
self.logger.write(
get_current_time(
format='%y%m%d%H%M%S%f'
).encode('utf-8')
)
self.logger.write(b',')
self.logger.write(
msg.lstrip(b'\r\n' if _IS_WIN else b'\n')
)
self.logger.write(os.linesep.encode('utf-8'))
return True
return False
else:
def log(self, msg):
"""
This function will update log file
Args:
msg: message
Returns:
None
"""
# Write into log file
if self.logger:
if msg:
self.logger.write(
b'{0},{1}{2}'.format(
get_current_time(
format='%y%m%d%H%M%S%f'
),
msg.lstrip(
b'\r\n' if _IS_WIN else b'\n'
),
os.linesep
)
)
return True
return False
return True
return False
def run(self):
if self.process and self.stream:
@@ -327,11 +293,7 @@ def execute(argv):
kwargs['shell'] = True if _IS_WIN else False
# We need environment variables & values in string
if _IS_PY2:
_log('Converting the environment variable in the bytes format...')
kwargs['env'] = convert_environment_variables(os.environ.copy())
else:
kwargs['env'] = os.environ.copy()
kwargs['env'] = os.environ.copy()
_log('Starting the command execution...')
process = Popen(
@@ -454,9 +416,6 @@ if __name__ == '__main__':
_fs_encoding = 'utf-8'
def u(_s, _encoding=_sys_encoding):
if _IS_PY2:
if isinstance(_s, str):
return unicode(_s, _encoding)
return _s
_u = u

View File

@@ -21,7 +21,7 @@ from pickle import dumps, loads
from subprocess import Popen, PIPE
import logging
from pgadmin.utils import IS_PY2, u, file_quote, fs_encoding, \
from pgadmin.utils import u, file_quote, fs_encoding, \
get_complete_file_path
import pytz
@@ -32,10 +32,7 @@ from flask_security import current_user
import config
from pgadmin.model import Process, db
if IS_PY2:
from StringIO import StringIO
else:
from io import StringIO
from io import StringIO
PROCESS_NOT_STARTED = 0
PROCESS_STARTED = 1
@@ -87,15 +84,7 @@ class BatchProcess(object):
_("Could not find a process with the specified ID.")
)
try:
tmp_desc = loads(p.desc.encode('latin-1')) if \
IS_PY2 and hasattr(p.desc, 'encode') else loads(p.desc)
except UnicodeDecodeError:
tmp_desc = loads(p.desc.encode('utf-8')) if \
IS_PY2 and hasattr(p.desc, 'encode') else loads(p.desc)
except Exception as e:
tmp_desc = loads(p.desc.encode('utf-8', 'ignore')) if \
IS_PY2 and hasattr(p.desc, 'encode') else loads(p.desc)
tmp_desc = loads(p.desc)
# ID
self.id = _id
@@ -183,33 +172,15 @@ class BatchProcess(object):
csv_writer = csv.writer(
args_csv_io, delimiter=str(','), quoting=csv.QUOTE_MINIMAL
)
if sys.version_info[0] == 2:
csv_writer.writerow(
[
a.encode('utf-8')
if isinstance(a, unicode) else a for a in _args
]
)
else:
csv_writer.writerow(_args)
csv_writer.writerow(_args)
args_val = args_csv_io.getvalue().strip(str('\r\n'))
tmp_desc = dumps(self.desc)
try:
tmp_desc = tmp_desc.decode('utf-8') if \
IS_PY2 and hasattr(tmp_desc, 'decode') else tmp_desc
except UnicodeDecodeError:
tmp_desc = tmp_desc.decode('latin-1') if \
IS_PY2 and hasattr(tmp_desc, 'decode') else tmp_desc
except Exception:
tmp_desc = tmp_desc.decode('utf-8', 'ignore') if \
IS_PY2 and hasattr(tmp_desc, 'decode') else tmp_desc
j = Process(
pid=int(id),
command=_cmd,
arguments=args_val.decode('utf-8', 'replace')
if IS_PY2 and hasattr(args_val, 'decode') else args_val,
arguments=args_val,
logdir=log_dir,
desc=tmp_desc,
user_id=current_user.id
@@ -318,24 +289,10 @@ class BatchProcess(object):
]
cmd.extend(self.args)
if os.name == 'nt' and IS_PY2:
command = []
for c in cmd:
command.append(
c.encode('utf-8') if isinstance(c, unicode) else str(c)
)
current_app.logger.info(
u"Executing the process executor with the arguments: %s",
''.join(command)
)
cmd = command
else:
current_app.logger.info(
u"Executing the process executor with the arguments: %s",
str(cmd)
)
current_app.logger.info(
u"Executing the process executor with the arguments: %s",
str(cmd)
)
# Make a copy of environment, and add new variables to support
env = os.environ.copy()
@@ -349,10 +306,6 @@ class BatchProcess(object):
if cb is not None:
cb(env)
if IS_PY2:
# We need environment variables & values in string
env = convert_environment_variables(env)
if os.name == 'nt':
DETACHED_PROCESS = 0x00000008
from subprocess import CREATE_NEW_PROCESS_GROUP
@@ -602,17 +555,7 @@ class BatchProcess(object):
etime = parser.parse(p.end_time or get_current_time())
execution_time = BatchProcess.total_seconds(etime - stime)
desc = ""
try:
desc = loads(p.desc.encode('latin-1')) if \
IS_PY2 and hasattr(p.desc, 'encode') else loads(p.desc)
except UnicodeDecodeError:
desc = loads(p.desc.encode('utf-8')) if \
IS_PY2 and hasattr(p.desc, 'encode') else loads(p.desc)
except Exception:
desc = loads(p.desc.encode('utf-8', 'ignore')) if \
IS_PY2 and hasattr(p.desc, 'encode') else loads(p.desc)
desc = loads(p.desc)
details = desc
if isinstance(desc, IProcessDesc):

View File

@@ -98,9 +98,7 @@ def splitext(path):
def is_folder_hidden(filepath):
if _platform == "win32":
try:
attrs = ctypes.windll.kernel32.GetFileAttributesW(
unicode(filepath) if sys.version_info[0] < 3 else filepath
)
attrs = ctypes.windll.kernel32.GetFileAttributesW(filepath)
assert attrs != -1
result = bool(attrs & 2)
except (AttributeError, AssertionError):