11"""A set of common tools to be used in pilot commands"""
22
3- from __future__ import absolute_import , division , print_function
4-
53import fcntl
64import getopt
5+ import importlib .util
76import json
87import os
98import re
1615import warnings
1716from datetime import datetime
1817from 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 :
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
4324
44- def import_module (module ):
45- import imp
25+ from .proxyTools import getVO
4626
47- impData = imp .find_module (module )
48- return imp .load_module (module , * impData )
27+ # Utilities functions
4928
50- def load_module_from_path (module_name , path_to_module ):
51- import imp
5229
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
70-
71- try :
72- from Pilot .proxyTools import getVO
73- except ImportError :
74- from proxyTools import getVO
75-
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
86-
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
92-
93- # Utilities functions
30+ def load_module_from_path (module_name , path_to_module ):
31+ spec = importlib .util .spec_from_file_location (module_name , path_to_module ) # pylint: disable=no-member
32+ module = importlib .util .module_from_spec (spec ) # pylint: disable=no-member
33+ spec .loader .exec_module (module )
34+ return module
9435
9536
9637def parseVersion (releaseVersion ):
@@ -399,7 +340,7 @@ def loadModule(self, modName, hideExceptions=False):
399340
400341 def __recurseImport (self , modName , parentModule = None , hideExceptions = False ):
401342 """Internal function to load modules"""
402- if isinstance (modName , basestring ):
343+ if isinstance (modName , str ):
403344 modName = modName .split ("." )
404345 try :
405346 if parentModule :
@@ -713,11 +654,7 @@ def sendMessage(url, pilotUUID, wnVO, method, rawMessage):
713654 context .load_cert_chain (os .path .join (cert , "hostcert.pem" ), os .path .join (cert , "hostkey.pem" ))
714655 raw_data = {"method" : method , "args" : message , "extraCredentials" : '"hosts"' }
715656
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 )
657+ data = urlencode (raw_data ).encode ("utf-8" ) # encode to bytes
721658
722659 res = urlopen (url , data , context = context )
723660 res .close ()
@@ -787,17 +724,7 @@ def executeAndGetOutput(self, cmd, environDict=None):
787724 if not outChunk :
788725 continue
789726 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
727+ outChunk = str (outChunk .replace ("\ufffd " , "" )) # Ensure it's a string
801728
802729 if stream == _p .stderr :
803730 sys .stderr .write (outChunk )
@@ -1455,7 +1382,7 @@ def __initJSON(self):
14551382 # Commands first
14561383 # FIXME: pilotSynchronizer() should publish these as comma-separated lists. We are ready for that.
14571384 try :
1458- if isinstance (self .pilotJSON ["Setups" ][self .setup ]["Commands" ][self .gridCEType ], basestring ):
1385+ if isinstance (self .pilotJSON ["Setups" ][self .setup ]["Commands" ][self .gridCEType ], str ):
14591386 self .commands = [
14601387 str (pv ).strip ()
14611388 for pv in self .pilotJSON ["Setups" ][self .setup ]["Commands" ][self .gridCEType ].split ("," )
@@ -1466,7 +1393,7 @@ def __initJSON(self):
14661393 ]
14671394 except KeyError :
14681395 try :
1469- if isinstance (self .pilotJSON ["Setups" ][self .setup ]["Commands" ]["Defaults" ], basestring ):
1396+ if isinstance (self .pilotJSON ["Setups" ][self .setup ]["Commands" ]["Defaults" ], str ):
14701397 self .commands = [
14711398 str (pv ).strip ()
14721399 for pv in self .pilotJSON ["Setups" ][self .setup ]["Commands" ]["Defaults" ].split ("," )
@@ -1477,7 +1404,7 @@ def __initJSON(self):
14771404 ]
14781405 except KeyError :
14791406 try :
1480- if isinstance (self .pilotJSON ["Setups" ]["Defaults" ]["Commands" ][self .gridCEType ], basestring ):
1407+ if isinstance (self .pilotJSON ["Setups" ]["Defaults" ]["Commands" ][self .gridCEType ], str ):
14811408 self .commands = [
14821409 str (pv ).strip ()
14831410 for pv in self .pilotJSON ["Setups" ]["Defaults" ]["Commands" ][self .gridCEType ].split ("," )
@@ -1488,7 +1415,7 @@ def __initJSON(self):
14881415 ]
14891416 except KeyError :
14901417 try :
1491- if isinstance (self .pilotJSON ["Defaults" ]["Commands" ]["Defaults" ], basestring ):
1418+ if isinstance (self .pilotJSON ["Defaults" ]["Commands" ]["Defaults" ], str ):
14921419 self .commands = [
14931420 str (pv ).strip () for pv in self .pilotJSON ["Defaults" ]["Commands" ]["Defaults" ].split ("," )
14941421 ]
@@ -1504,7 +1431,7 @@ def __initJSON(self):
15041431 # pilotSynchronizer() can publish this as a comma separated list. We are ready for that.
15051432 try :
15061433 if isinstance (
1507- self .pilotJSON ["Setups" ][self .setup ]["CommandExtensions" ], basestring
1434+ self .pilotJSON ["Setups" ][self .setup ]["CommandExtensions" ], str
15081435 ): # In the specific setup?
15091436 self .commandExtensions = [
15101437 str (pv ).strip () for pv in self .pilotJSON ["Setups" ][self .setup ]["CommandExtensions" ].split ("," )
@@ -1516,7 +1443,7 @@ def __initJSON(self):
15161443 except KeyError :
15171444 try :
15181445 if isinstance (
1519- self .pilotJSON ["Setups" ]["Defaults" ]["CommandExtensions" ], basestring
1446+ self .pilotJSON ["Setups" ]["Defaults" ]["CommandExtensions" ], str
15201447 ): # Or in the defaults section?
15211448 self .commandExtensions = [
15221449 str (pv ).strip () for pv in self .pilotJSON ["Setups" ]["Defaults" ]["CommandExtensions" ].split ("," )
@@ -1533,7 +1460,7 @@ def __initJSON(self):
15331460 # pilotSynchronizer() can publish this as a comma separated list. We are ready for that
15341461 try :
15351462 if isinstance (
1536- self .pilotJSON ["ConfigurationServers" ], basestring
1463+ self .pilotJSON ["ConfigurationServers" ], str
15371464 ): # Generic, there may also be setup-specific ones
15381465 self .configServer = "," .join (
15391466 [str (pv ).strip () for pv in self .pilotJSON ["ConfigurationServers" ].split ("," )]
@@ -1544,7 +1471,7 @@ def __initJSON(self):
15441471 pass
15451472 try : # now trying to see if there is setup-specific ones
15461473 if isinstance (
1547- self .pilotJSON ["Setups" ][self .setup ]["ConfigurationServer" ], basestring
1474+ self .pilotJSON ["Setups" ][self .setup ]["ConfigurationServer" ], str
15481475 ): # In the specific setup?
15491476 self .configServer = "," .join (
15501477 [str (pv ).strip () for pv in self .pilotJSON ["Setups" ][self .setup ]["ConfigurationServer" ].split ("," )]
@@ -1556,7 +1483,7 @@ def __initJSON(self):
15561483 except KeyError : # and if it doesn't exist
15571484 try :
15581485 if isinstance (
1559- self .pilotJSON ["Setups" ]["Defaults" ]["ConfigurationServer" ], basestring
1486+ self .pilotJSON ["Setups" ]["Defaults" ]["ConfigurationServer" ], str
15601487 ): # Is there one in the defaults section?
15611488 self .configServer = "," .join (
15621489 [
0 commit comments