From 1040fc6baee68f89a9d2b6d9b481fb72d8c3ea42 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Wed, 24 Mar 2021 19:27:13 +0100 Subject: [PATCH] Manual fixes to imports to work with python2 and 3 - Import getoutput from the appropriate place in util.py. This has changed locations between python2 and python3 and would ideally be replaced by a more modern check_output approach, but it is used so widely throughout that it is easier for now to just do it this way. - Use urrlib and fallback to urllib2 if necessary - Remove blanket imports in util and rather import them in the few places where it is actually necessary --- ilcsoft-install | 10 ++++++---- ilcsoft/baseilc.py | 13 ++++++++++--- ilcsoft/ilcsoft.py | 19 +++++++++++++++---- ilcsoft/util.py | 42 +++++++++++++++++++++++++++--------------- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/ilcsoft-install b/ilcsoft-install index cd0d7c52..f4a9e572 100755 --- a/ilcsoft-install +++ b/ilcsoft-install @@ -1,9 +1,11 @@ #!/usr/bin/env python + +from __future__ import print_function + import os, sys import logging -import subprocess -# from ilcsoft import * -import ilcsoft as ilcsoft +from ilcsoft import * +from util import getoutput _version = "v01-17-07" @@ -52,7 +54,7 @@ if( not os.path.exists(config_file) ): parser.error( 'config file %s does not exist' % config_file ) # some settings needed for reading nightly build cfg files -date_iso8601 = subprocess.getoutput( "date +%F" ) +date_iso8601 = getoutput( "date +%F" ) config_file_basename = config_file[config_file.rfind( '/' )+1:config_file.rfind(".")] config_file_extension = config_file[config_file.rfind(".")+1:] diff --git a/ilcsoft/baseilc.py b/ilcsoft/baseilc.py index 39491d5e..a07b6cb6 100644 --- a/ilcsoft/baseilc.py +++ b/ilcsoft/baseilc.py @@ -7,11 +7,18 @@ # ################################################## +from __future__ import print_function + # custom imports -from .util import * +from util import * import logging -import urllib2 as urllib +import re +import time +try: + from urllib.request import urlopen +except ImportError: + from urllib2 import urlopen try: import simplejson as json @@ -757,7 +764,7 @@ def downloadSources(self): print("Cloning of repository %s/%s into directory %s sucessfully finished" % (self.download.gituser, self.download.gitrepo, self.version)) - elif 'message' not in list(json.loads(urllib.request.urlopen('https://api.github.com/repos/%s/%s/git/refs/tags/%s' % (self.download.gituser, self.download.gitrepo, self.version)).read()).keys()): + elif 'message' not in list(json.loads(urlopen('https://api.github.com/repos/%s/%s/git/refs/tags/%s' % (self.download.gituser, self.download.gitrepo, self.version)).read()).keys()): cmd = "mkdir -p %s" % (self.version) if os_system( cmd ) != 0: self.abort( "Could not create folder" + self.version + " [!!ERROR!!]") diff --git a/ilcsoft/ilcsoft.py b/ilcsoft/ilcsoft.py index 757a5eaf..f5ad2b12 100644 --- a/ilcsoft/ilcsoft.py +++ b/ilcsoft/ilcsoft.py @@ -7,12 +7,22 @@ # ################################################## +from __future__ import print_function + # custom imports from .util import * from .java import Java from .qt import QT from .cmake import CMake -import subprocess +import time +import re +import shutil + +try: + from subprocess import DEVNULL +except ImportError: + from os import devnull + DEVNULL = open(devnull, 'wb') class ILCSoft: """ Container class for the ILC software modules. @@ -372,7 +382,8 @@ def makeinstall(self): # copy config file try: shutil.copyfile( self.config_file, self.logsdir + self.config_file_prefix + "-" + self.time + ".cfg") - except: + # Should be fine for python2 and python3 to signal that there is a permission problem + except IOError: print("*** FATAL ERROR: you don't have write permissions in " + self.installPath + "!!!\n") sys.exit(1) @@ -402,10 +413,10 @@ def makeinstall(self): #------- prepend the pathes for the compiler and python used in this installation ------- compiler = self.env["CXX"] - status, cxx_path = subprocess.getstatusoutput( "which "+compiler ) + status, cxx_path = getstatusoutput( "which "+compiler ) cxx_path = os.path.dirname( os.path.dirname( cxx_path ) ) # remove '/bin/g++' - status, py_path = subprocess.getstatusoutput( "which python" ) + status, py_path = getstatusoutput( "which python" ) py_path = os.path.dirname( os.path.dirname( py_path ) ) # remove '/bin/python' f.write( os.linesep + '# -------------------------------------------------------------------- ---' + os.linesep ) diff --git a/ilcsoft/util.py b/ilcsoft/util.py index f6f2180b..2eb4dc88 100644 --- a/ilcsoft/util.py +++ b/ilcsoft/util.py @@ -7,20 +7,28 @@ # ################################################## -from .version import Version -from subprocess import getstatusoutput -from subprocess import getoutput +from version import Version +import subprocess import os import os.path import sys -import shutil -import glob -import time -import string -import re import fnmatch import platform +from six.moves import input + +# A lot of packages depend on getoutput (and an implicit import from here) +# Luckily those have been "de-deprecated" and put into the subprocess module so +# we have to see where we can find them +# +# NOTE: This should probably be fixed to use check_output and friends where +# possible for proper python2 and python3 support. See also: +# https://docs.python.org/3/library/subprocess.html#legacy-shell-invocation-functions +try: + from subprocess import getoutput, getstatusoutput +except ImportError: + from commands import getoutput, getstatusoutput + #-------------------------------------------------------------------------------- def os_system( cmd ): """ forces os.system calls wto use bash """ @@ -54,14 +62,18 @@ def __init__(self): if( self.type == "Linux" ): # description - out=getstatusoutput("lsb_release -d") - if( out[0] == 0 ): - self.ver=out[1].split("Description:")[1].strip() + try: + version = subprocess.check_output("lsb_release -b", shell=True, + stderr=subprocess.STDOUT) + self.ver = version.split("Description:")[1].strip() + except subprocess.CalledProcessError: + pass # hardware platform - out=getstatusoutput("uname -i") - if( out[0] == 0 ): - self.platform=out[1].strip() - + try: + self.platform = subprocess.check_output("uname -i", shell=True, + stderr=subprocess.STDOUT).strip() + except subprocess.CalledProcessError: + pass try: sizeofint = platform.architecture()[0]