Skip to content

Commit 807db73

Browse files
committed
feat: removed python2 support
1 parent ff53464 commit 807db73

File tree

9 files changed

+48
-173
lines changed

9 files changed

+48
-173
lines changed

.github/workflows/basic.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ jobs:
2424
fail-fast: false
2525
matrix:
2626
python:
27-
- 2.7.18
2827
- 3.6.15
2928
- 3.9.17
3029

@@ -55,7 +54,6 @@ jobs:
5554
fail-fast: false
5655
matrix:
5756
python:
58-
- 2.7.18
5957
- 3.6.15
6058
- 3.9.17
6159

@@ -82,7 +80,6 @@ jobs:
8280
fail-fast: false
8381
matrix:
8482
python:
85-
- 2.7.18
8683
- 3.6.15
8784
- 3.9.17
8885

.pylintrc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,3 @@ dummy-variables=_
1818
disable=
1919
invalid-name,
2020
line-too-long, # would be nice to remove this one
21-
consider-using-f-string, # python2/3 support
22-
unspecified-encoding, # python2/3 support
23-
super-with-arguments, # python2/3 support
24-
redefined-builtin, # python2/3 support

Pilot/dirac-pilot.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,10 @@
1919
But, as said, all the actions are actually configurable.
2020
"""
2121

22-
from __future__ import absolute_import, division, print_function
23-
2422
import os
2523
import sys
2624
import time
27-
28-
############################
29-
# python 2 -> 3 "hacks"
30-
31-
try:
32-
from cStringIO import StringIO
33-
except ImportError:
34-
from io import StringIO
25+
from io import StringIO
3526

3627
try:
3728
from Pilot.pilotTools import (
@@ -41,14 +32,15 @@
4132
getCommand,
4233
pythonPathCheck,
4334
)
44-
except ImportError:
35+
except ModuleNotFoundError:
4536
from pilotTools import (
4637
Logger,
4738
PilotParams,
4839
RemoteLogger,
4940
getCommand,
5041
pythonPathCheck,
5142
)
43+
5244
############################
5345

5446
if __name__ == "__main__":

Pilot/pilotCommands.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ def __init__(self, pilotParams):
1717
execution.
1818
"""
1919

20-
from __future__ import absolute_import, division, print_function
21-
2220
import filecmp
2321
import os
2422
import platform
@@ -28,22 +26,9 @@ def __init__(self, pilotParams):
2826
import sys
2927
import time
3028
import traceback
31-
import subprocess
3229
from collections import Counter
33-
34-
############################
35-
# python 2 -> 3 "hacks"
36-
try:
37-
# For Python 3.0 and later
38-
from http.client import HTTPSConnection
39-
except ImportError:
40-
# Fall back to Python 2
41-
from httplib import HTTPSConnection
42-
43-
try:
44-
from shlex import quote
45-
except ImportError:
46-
from pipes import quote
30+
from http.client import HTTPSConnection
31+
from shlex import quote
4732

4833
try:
4934
from Pilot.pilotTools import (
@@ -53,14 +38,15 @@ def __init__(self, pilotParams):
5338
safe_listdir,
5439
sendMessage,
5540
)
56-
except ImportError:
41+
except ModuleNotFoundError:
5742
from pilotTools import (
5843
CommandBase,
5944
getSubmitterInfo,
6045
retrieveUrlTimeout,
6146
safe_listdir,
6247
sendMessage,
6348
)
49+
6450
############################
6551

6652

@@ -296,10 +282,7 @@ def _localInstallDIRAC(self):
296282

297283
# 1. Get the DIRACOS installer name
298284
# curl -O -L https://github.com/DIRACGrid/DIRACOS2/releases/latest/download/DIRACOS-Linux-$(uname -m).sh
299-
try:
300-
machine = os.uname().machine # py3
301-
except AttributeError:
302-
machine = os.uname()[4] # py2
285+
machine = os.uname().machine
303286

304287
installerName = "DIRACOS-Linux-%s.sh" % machine
305288

Pilot/pilotTools.py

Lines changed: 26 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""A set of common tools to be used in pilot commands"""
22

3-
from __future__ import absolute_import, division, print_function
4-
53
import fcntl
64
import getopt
5+
import importlib.util
76
import json
87
import os
98
import re
@@ -16,81 +15,26 @@
1615
import warnings
1716
from datetime import datetime
1817
from functools import partial, wraps
19-
from threading import RLock
20-
21-
############################
22-
# python 2 -> 3 "hacks"
23-
try:
24-
from urllib.error import HTTPError, URLError
25-
from urllib.parse import urlencode
26-
from urllib.request import urlopen
27-
except ImportError:
28-
from urllib import urlencode
29-
30-
from urllib2 import HTTPError, URLError, urlopen
31-
32-
try:
33-
import importlib.util
34-
from importlib import import_module
35-
36-
def load_module_from_path(module_name, path_to_module):
37-
spec = importlib.util.spec_from_file_location(module_name, path_to_module) # pylint: disable=no-member
38-
module = importlib.util.module_from_spec(spec) # pylint: disable=no-member
39-
spec.loader.exec_module(module)
40-
return module
41-
42-
except ImportError:
43-
44-
def import_module(module):
45-
import imp
46-
47-
impData = imp.find_module(module)
48-
return imp.load_module(module, *impData)
49-
50-
def load_module_from_path(module_name, path_to_module):
51-
import imp
52-
53-
fp, pathname, description = imp.find_module(module_name, [path_to_module])
54-
try:
55-
return imp.load_module(module_name, fp, pathname, description)
56-
finally:
57-
if fp:
58-
fp.close()
59-
60-
61-
try:
62-
from cStringIO import StringIO
63-
except ImportError:
64-
from io import StringIO
65-
66-
try:
67-
basestring # pylint: disable=used-before-assignment
68-
except NameError:
69-
basestring = str
18+
from importlib import import_module
19+
from io import StringIO
20+
from threading import RLock, Timer
21+
from urllib.error import HTTPError, URLError
22+
from urllib.parse import urlencode
23+
from urllib.request import urlopen
7024

7125
try:
7226
from Pilot.proxyTools import getVO
73-
except ImportError:
27+
except ModuleNotFoundError:
7428
from proxyTools import getVO
7529

76-
try:
77-
FileNotFoundError # pylint: disable=used-before-assignment
78-
# because of https://github.com/PyCQA/pylint/issues/6748
79-
except NameError:
80-
FileNotFoundError = OSError
81-
82-
try:
83-
IsADirectoryError # pylint: disable=used-before-assignment
84-
except NameError:
85-
IsADirectoryError = IOError
30+
# Utilities functions
8631

87-
# Timer 2.7 and < 3.3 versions issue where Timer is a function
88-
if sys.version_info.major == 2 or sys.version_info.major == 3 and sys.version_info.minor < 3:
89-
from threading import _Timer as Timer # pylint: disable=no-name-in-module
90-
else:
91-
from threading import Timer
9232

93-
# Utilities functions
33+
def load_module_from_path(module_name, path_to_module):
34+
spec = importlib.util.spec_from_file_location(module_name, path_to_module) # pylint: disable=no-member
35+
module = importlib.util.module_from_spec(spec) # pylint: disable=no-member
36+
spec.loader.exec_module(module)
37+
return module
9438

9539

9640
def parseVersion(releaseVersion):
@@ -399,7 +343,7 @@ def loadModule(self, modName, hideExceptions=False):
399343

400344
def __recurseImport(self, modName, parentModule=None, hideExceptions=False):
401345
"""Internal function to load modules"""
402-
if isinstance(modName, basestring):
346+
if isinstance(modName, str):
403347
modName = modName.split(".")
404348
try:
405349
if parentModule:
@@ -713,11 +657,7 @@ def sendMessage(url, pilotUUID, wnVO, method, rawMessage):
713657
context.load_cert_chain(os.path.join(cert, "hostcert.pem"), os.path.join(cert, "hostkey.pem"))
714658
raw_data = {"method": method, "args": message, "extraCredentials": '"hosts"'}
715659

716-
if sys.version_info.major == 3:
717-
data = urlencode(raw_data).encode("utf-8") # encode to bytes ! for python3
718-
else:
719-
# Python2
720-
data = urlencode(raw_data)
660+
data = urlencode(raw_data).encode("utf-8") # encode to bytes
721661

722662
res = urlopen(url, data, context=context)
723663
res.close()
@@ -787,17 +727,7 @@ def executeAndGetOutput(self, cmd, environDict=None):
787727
if not outChunk:
788728
continue
789729
dataWasRead = True
790-
if sys.version_info.major == 2:
791-
# Ensure outChunk is unicode in Python 2
792-
if isinstance(outChunk, str):
793-
outChunk = outChunk.decode("utf-8")
794-
# Strip unicode replacement characters
795-
# Ensure correct type conversion in Python 2
796-
outChunk = str(outChunk.replace(u"\ufffd", ""))
797-
# Avoid potential str() issues in Py2
798-
outChunk = unicode(outChunk) # pylint: disable=undefined-variable
799-
else:
800-
outChunk = str(outChunk.replace("\ufffd", "")) # Python 3: Ensure it's a string
730+
outChunk = str(outChunk.replace("\ufffd", "")) # Python 3: Ensure it's a string
801731

802732
if stream == _p.stderr:
803733
sys.stderr.write(outChunk)
@@ -1455,7 +1385,7 @@ def __initJSON(self):
14551385
# Commands first
14561386
# FIXME: pilotSynchronizer() should publish these as comma-separated lists. We are ready for that.
14571387
try:
1458-
if isinstance(self.pilotJSON["Setups"][self.setup]["Commands"][self.gridCEType], basestring):
1388+
if isinstance(self.pilotJSON["Setups"][self.setup]["Commands"][self.gridCEType], str):
14591389
self.commands = [
14601390
str(pv).strip()
14611391
for pv in self.pilotJSON["Setups"][self.setup]["Commands"][self.gridCEType].split(",")
@@ -1466,7 +1396,7 @@ def __initJSON(self):
14661396
]
14671397
except KeyError:
14681398
try:
1469-
if isinstance(self.pilotJSON["Setups"][self.setup]["Commands"]["Defaults"], basestring):
1399+
if isinstance(self.pilotJSON["Setups"][self.setup]["Commands"]["Defaults"], str):
14701400
self.commands = [
14711401
str(pv).strip()
14721402
for pv in self.pilotJSON["Setups"][self.setup]["Commands"]["Defaults"].split(",")
@@ -1477,7 +1407,7 @@ def __initJSON(self):
14771407
]
14781408
except KeyError:
14791409
try:
1480-
if isinstance(self.pilotJSON["Setups"]["Defaults"]["Commands"][self.gridCEType], basestring):
1410+
if isinstance(self.pilotJSON["Setups"]["Defaults"]["Commands"][self.gridCEType], str):
14811411
self.commands = [
14821412
str(pv).strip()
14831413
for pv in self.pilotJSON["Setups"]["Defaults"]["Commands"][self.gridCEType].split(",")
@@ -1488,7 +1418,7 @@ def __initJSON(self):
14881418
]
14891419
except KeyError:
14901420
try:
1491-
if isinstance(self.pilotJSON["Defaults"]["Commands"]["Defaults"], basestring):
1421+
if isinstance(self.pilotJSON["Defaults"]["Commands"]["Defaults"], str):
14921422
self.commands = [
14931423
str(pv).strip() for pv in self.pilotJSON["Defaults"]["Commands"]["Defaults"].split(",")
14941424
]
@@ -1504,7 +1434,7 @@ def __initJSON(self):
15041434
# pilotSynchronizer() can publish this as a comma separated list. We are ready for that.
15051435
try:
15061436
if isinstance(
1507-
self.pilotJSON["Setups"][self.setup]["CommandExtensions"], basestring
1437+
self.pilotJSON["Setups"][self.setup]["CommandExtensions"], str
15081438
): # In the specific setup?
15091439
self.commandExtensions = [
15101440
str(pv).strip() for pv in self.pilotJSON["Setups"][self.setup]["CommandExtensions"].split(",")
@@ -1516,7 +1446,7 @@ def __initJSON(self):
15161446
except KeyError:
15171447
try:
15181448
if isinstance(
1519-
self.pilotJSON["Setups"]["Defaults"]["CommandExtensions"], basestring
1449+
self.pilotJSON["Setups"]["Defaults"]["CommandExtensions"], str
15201450
): # Or in the defaults section?
15211451
self.commandExtensions = [
15221452
str(pv).strip() for pv in self.pilotJSON["Setups"]["Defaults"]["CommandExtensions"].split(",")
@@ -1533,7 +1463,7 @@ def __initJSON(self):
15331463
# pilotSynchronizer() can publish this as a comma separated list. We are ready for that
15341464
try:
15351465
if isinstance(
1536-
self.pilotJSON["ConfigurationServers"], basestring
1466+
self.pilotJSON["ConfigurationServers"], str
15371467
): # Generic, there may also be setup-specific ones
15381468
self.configServer = ",".join(
15391469
[str(pv).strip() for pv in self.pilotJSON["ConfigurationServers"].split(",")]
@@ -1544,7 +1474,7 @@ def __initJSON(self):
15441474
pass
15451475
try: # now trying to see if there is setup-specific ones
15461476
if isinstance(
1547-
self.pilotJSON["Setups"][self.setup]["ConfigurationServer"], basestring
1477+
self.pilotJSON["Setups"][self.setup]["ConfigurationServer"], str
15481478
): # In the specific setup?
15491479
self.configServer = ",".join(
15501480
[str(pv).strip() for pv in self.pilotJSON["Setups"][self.setup]["ConfigurationServer"].split(",")]
@@ -1556,7 +1486,7 @@ def __initJSON(self):
15561486
except KeyError: # and if it doesn't exist
15571487
try:
15581488
if isinstance(
1559-
self.pilotJSON["Setups"]["Defaults"]["ConfigurationServer"], basestring
1489+
self.pilotJSON["Setups"]["Defaults"]["ConfigurationServer"], str
15601490
): # Is there one in the defaults section?
15611491
self.configServer = ",".join(
15621492
[

Pilot/proxyTools.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""few functions for dealing with proxies"""
22

3-
from __future__ import absolute_import, division, print_function
4-
53
import re
64
from base64 import b16decode
75
from subprocess import PIPE, Popen

Pilot/tests/Test_Pilot.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Test class for Pilot"""
22

3-
from __future__ import absolute_import, division, print_function
4-
53
import json
64
import os
75
import shutil
@@ -12,8 +10,12 @@
1210
# imports
1311
import unittest
1412

15-
from Pilot.pilotCommands import CheckWorkerNode, ConfigureSite, NagiosProbes
16-
from Pilot.pilotTools import PilotParams
13+
try:
14+
from Pilot.pilotCommands import CheckWorkerNode, ConfigureSite, NagiosProbes
15+
from Pilot.pilotTools import PilotParams
16+
except ModuleNotFoundError:
17+
from pilotCommands import CheckWorkerNode, ConfigureSite, NagiosProbes
18+
from pilotTools import PilotParams
1719

1820

1921
class PilotTestCase(unittest.TestCase):

0 commit comments

Comments
 (0)