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 :
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
7125try :
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
9640def 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,6 +727,7 @@ def executeAndGetOutput(self, cmd, environDict=None):
787727 if not outChunk :
788728 continue
789729 dataWasRead = True
730+ < << << << HEAD
790731 if sys .version_info .major == 2 :
791732 # Ensure outChunk is unicode in Python 2
792733 if isinstance (outChunk , str ):
@@ -798,6 +739,9 @@ def executeAndGetOutput(self, cmd, environDict=None):
798739 outChunk = unicode (outChunk ) # pylint: disable=undefined-variable
799740 else :
800741 outChunk = str (outChunk .replace ("\ufffd " , "" )) # Python 3: Ensure it's a string
742+ == == == =
743+ outChunk = str (outChunk .replace ("\ufffd " , "" )) # Ensure it's a string
744+ >> >> >> > 7 eb2ebf (feat : removed python2 support )
801745
802746 if stream == _p .stderr :
803747 sys .stderr .write (outChunk )
@@ -1458,7 +1402,7 @@ def __initJSON(self):
14581402 # Commands first
14591403 # FIXME: pilotSynchronizer() should publish these as comma-separated lists. We are ready for that.
14601404 try :
1461- if isinstance (self .pilotJSON ["Setups" ][self .setup ]["Commands" ][self .gridCEType ], basestring ):
1405+ if isinstance (self .pilotJSON ["Setups" ][self .setup ]["Commands" ][self .gridCEType ], str ):
14621406 self .commands = [
14631407 str (pv ).strip ()
14641408 for pv in self .pilotJSON ["Setups" ][self .setup ]["Commands" ][self .gridCEType ].split ("," )
@@ -1469,7 +1413,7 @@ def __initJSON(self):
14691413 ]
14701414 except KeyError :
14711415 try :
1472- if isinstance (self .pilotJSON ["Setups" ][self .setup ]["Commands" ]["Defaults" ], basestring ):
1416+ if isinstance (self .pilotJSON ["Setups" ][self .setup ]["Commands" ]["Defaults" ], str ):
14731417 self .commands = [
14741418 str (pv ).strip ()
14751419 for pv in self .pilotJSON ["Setups" ][self .setup ]["Commands" ]["Defaults" ].split ("," )
@@ -1480,7 +1424,7 @@ def __initJSON(self):
14801424 ]
14811425 except KeyError :
14821426 try :
1483- if isinstance (self .pilotJSON ["Setups" ]["Defaults" ]["Commands" ][self .gridCEType ], basestring ):
1427+ if isinstance (self .pilotJSON ["Setups" ]["Defaults" ]["Commands" ][self .gridCEType ], str ):
14841428 self .commands = [
14851429 str (pv ).strip ()
14861430 for pv in self .pilotJSON ["Setups" ]["Defaults" ]["Commands" ][self .gridCEType ].split ("," )
@@ -1491,7 +1435,7 @@ def __initJSON(self):
14911435 ]
14921436 except KeyError :
14931437 try :
1494- if isinstance (self .pilotJSON ["Defaults" ]["Commands" ]["Defaults" ], basestring ):
1438+ if isinstance (self .pilotJSON ["Defaults" ]["Commands" ]["Defaults" ], str ):
14951439 self .commands = [
14961440 str (pv ).strip () for pv in self .pilotJSON ["Defaults" ]["Commands" ]["Defaults" ].split ("," )
14971441 ]
@@ -1507,7 +1451,7 @@ def __initJSON(self):
15071451 # pilotSynchronizer() can publish this as a comma separated list. We are ready for that.
15081452 try :
15091453 if isinstance (
1510- self .pilotJSON ["Setups" ][self .setup ]["CommandExtensions" ], basestring
1454+ self .pilotJSON ["Setups" ][self .setup ]["CommandExtensions" ], str
15111455 ): # In the specific setup?
15121456 self .commandExtensions = [
15131457 str (pv ).strip () for pv in self .pilotJSON ["Setups" ][self .setup ]["CommandExtensions" ].split ("," )
@@ -1519,7 +1463,7 @@ def __initJSON(self):
15191463 except KeyError :
15201464 try :
15211465 if isinstance (
1522- self .pilotJSON ["Setups" ]["Defaults" ]["CommandExtensions" ], basestring
1466+ self .pilotJSON ["Setups" ]["Defaults" ]["CommandExtensions" ], str
15231467 ): # Or in the defaults section?
15241468 self .commandExtensions = [
15251469 str (pv ).strip () for pv in self .pilotJSON ["Setups" ]["Defaults" ]["CommandExtensions" ].split ("," )
@@ -1536,7 +1480,7 @@ def __initJSON(self):
15361480 # pilotSynchronizer() can publish this as a comma separated list. We are ready for that
15371481 try :
15381482 if isinstance (
1539- self .pilotJSON ["ConfigurationServers" ], basestring
1483+ self .pilotJSON ["ConfigurationServers" ], str
15401484 ): # Generic, there may also be setup-specific ones
15411485 self .configServer = "," .join (
15421486 [str (pv ).strip () for pv in self .pilotJSON ["ConfigurationServers" ].split ("," )]
@@ -1547,7 +1491,7 @@ def __initJSON(self):
15471491 pass
15481492 try : # now trying to see if there is setup-specific ones
15491493 if isinstance (
1550- self .pilotJSON ["Setups" ][self .setup ]["ConfigurationServer" ], basestring
1494+ self .pilotJSON ["Setups" ][self .setup ]["ConfigurationServer" ], str
15511495 ): # In the specific setup?
15521496 self .configServer = "," .join (
15531497 [str (pv ).strip () for pv in self .pilotJSON ["Setups" ][self .setup ]["ConfigurationServer" ].split ("," )]
@@ -1559,7 +1503,7 @@ def __initJSON(self):
15591503 except KeyError : # and if it doesn't exist
15601504 try :
15611505 if isinstance (
1562- self .pilotJSON ["Setups" ]["Defaults" ]["ConfigurationServer" ], basestring
1506+ self .pilotJSON ["Setups" ]["Defaults" ]["ConfigurationServer" ], str
15631507 ): # Is there one in the defaults section?
15641508 self .configServer = "," .join (
15651509 [
0 commit comments