Skip to content

Commit

Permalink
Manual fixes to imports to work with python2 and 3
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
tmadlener committed Jan 18, 2022
1 parent b47c6a2 commit 1040fc6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 26 deletions.
10 changes: 6 additions & 4 deletions ilcsoft-install
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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:]

Expand Down
13 changes: 10 additions & 3 deletions ilcsoft/baseilc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!!]")
Expand Down
19 changes: 15 additions & 4 deletions ilcsoft/ilcsoft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 )
Expand Down
42 changes: 27 additions & 15 deletions ilcsoft/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 1040fc6

Please sign in to comment.