From 62fce580600a14a0e7f79fc1c81b80b2fd489ca5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Br=C3=BCns?= <stefan.bruens@rwth-aachen.de>
Date: Tue, 29 May 2018 20:34:33 +0200
Subject: [PATCH 2/5] SConstruct: Replace deprecated syntax/modules with
 forwards compatible ones

The subprocess module is available since python 2.4 and replaces the
commands module, which is deprecated since 2.6 and has been removed in 3.0.
Remove unused popen2 and StringIO imports.
Use 'except ... as e:' syntax, which is supported since python 2.6.
dict().has_key is deprecated and can be replace by 'key in dict' or,
for lookup, with get(key, default).
The pickle load/dump methos need a binary file in python3, use the binary
flag for opening.
split no longer is a method of the string module, but of the str() class.
---
 SConstruct                          | 19 ++++++++-------
 sys/gllog/logfunc.py                |  6 ++---
 sys/gllog/read.py                   |  4 ++--
 sys/scons/scons_utils.py            | 36 +++++++++++++++--------------
 tdm_update/SConscript.libtdm_update |  2 +-
 tdm_update/SConscript.minizip       |  2 +-
 tdm_update/SConscript.tdm_update    |  2 +-
 tdm_update/SConstruct               |  9 ++++----
 8 files changed, 40 insertions(+), 40 deletions(-)

diff --git a/SConstruct b/SConstruct
index b244cb6..27823dd 100644
--- a/SConstruct
+++ b/SConstruct
@@ -3,7 +3,7 @@
 # TTimo <ttimo@idsoftware.com>
 # http://scons.sourceforge.net
 
-import sys, os, time, commands, re, pickle, StringIO, popen2, commands, pdb, zipfile, string
+import sys, os, time, re, pickle, subprocess, pdb, zipfile, string
 import SCons
 
 sys.path.append( 'sys/scons' )
@@ -104,13 +104,12 @@ EnsureSConsVersion( 0, 96 )
 # system detection -------------------------------
 
 # CPU type
-cpu = commands.getoutput('uname -m')
-exp = re.compile('.*i?86.*')
-if exp.match(cpu):
+cpu = subprocess.check_output(["uname", "-m"])
+if re.match(b'.*i?86.*', cpu):
 	cpu = 'x86'
 else:
-	cpu = commands.getoutput('uname -p')
-	if ( cpu == 'powerpc' ):
+	cpu = subprocess.check_output(["uname", "-p"])
+	if ( cpu == b'powerpc' ):
 		cpu = 'ppc'
 	else:
 		cpu = 'cpu'
@@ -141,10 +140,10 @@ TARGET_ARCH = 'x86'
 
 # site settings ----------------------------------
 
-if ( not ARGUMENTS.has_key( 'NOCONF' ) or ARGUMENTS['NOCONF'] != '1' ):
+if ( ARGUMENTS.get('NOCONF', 0) != '1' ):
 	site_dict = {}
 	if (os.path.exists(conf_filename)):
-		site_file = open(conf_filename, 'r')
+		site_file = open(conf_filename, 'rb')
 		p = pickle.Unpickler(site_file)
 		site_dict = p.load()
 		print('Loading build configuration from ' + conf_filename + ':')
@@ -173,12 +172,12 @@ if TARGET_ARCH == 'x32':
 
 # save site configuration ----------------------
 
-if ( not ARGUMENTS.has_key( 'NOCONF' ) or ARGUMENTS['NOCONF'] != '1' ):
+if ( ARGUMENTS.get('NOCONF', 0) != '1' ):
 	for k in serialized:
 		exec_cmd = 'site_dict[\'' + k + '\'] = ' + k
 		exec(exec_cmd)
 
-	site_file = open(conf_filename, 'w')
+	site_file = open(conf_filename, 'wb')
 	p = pickle.Pickler(site_file)
 	p.dump(site_dict)
 	site_file.close()
diff --git a/sys/gllog/logfunc.py b/sys/gllog/logfunc.py
index 3a9e46f..e38de5d 100644
--- a/sys/gllog/logfunc.py
+++ b/sys/gllog/logfunc.py
@@ -12,10 +12,10 @@ def do_logfunc(f_in, f_out):
 	for l in (gl, glX):
 		for t in l:
 			# process ret type to strip trailing spaces
-			t[0] = string.strip(t[0])
+			t[0] = t[0].strip()
 			f_out.write('static %s APIENTRY log%s(%s) {\n' % ( t[0], t[2], t[3] ))
 			# work on parameters
-			base_params = string.split(t[3], ',')
+			base_params = t[3].split(',')
 			#f_out.write('// %s\n' % repr(base_params))
 			# init format string and parameter list
 			params = []
@@ -26,7 +26,7 @@ def do_logfunc(f_in, f_out):
 			for i in base_params:
 				regex = re.compile('([a-zA-Z0-9]*)$')
 				name = regex.search(i).group(1)
-				type = string.strip(i[0:len(i)-len(name)])				
+				type = i[0:len(i)-len(name)].strip()
 				# catch type with no name
 				if (len(type) == 0):
 					type = name
diff --git a/sys/gllog/read.py b/sys/gllog/read.py
index 502322d..04b8d50 100644
--- a/sys/gllog/read.py
+++ b/sys/gllog/read.py
@@ -4,7 +4,7 @@ import sys, string
 
 def read_gl(f_in):
 	buffer = f_in.read()
-	lines = string.split(buffer, '\n')
+	lines = buffer.split('\n')
 
 	gl = []
 	wgl = []
@@ -12,7 +12,7 @@ def read_gl(f_in):
 
 	for line in lines:
 		if ( len(line) ): # drop empty lines
-			tokens = string.split(line, ';')
+			tokens = line.split(';')
 			if ( tokens[1] == 'qgl' ):
 				gl.append(tokens)
 			elif ( tokens[1] == 'qwgl' ):
diff --git a/sys/scons/scons_utils.py b/sys/scons/scons_utils.py
index c2050e1..9af5926 100644
--- a/sys/scons/scons_utils.py
+++ b/sys/scons/scons_utils.py
@@ -1,5 +1,5 @@
 # -*- mode: python -*-
-import sys, os, string, time, commands, re, pickle, StringIO, popen2, commands, pdb, zipfile, tempfile
+import sys, os, subprocess, string, time, re, pickle, pdb, zipfile, tempfile
 import SCons
 
 # need an Environment and a matching buffered_spawn API .. encapsulate
@@ -16,7 +16,7 @@ class idBuffering:
 			command_string += i
 		try:
 			retval = self.env['PSPAWN']( sh, escape, cmd, args, env, stdout, stderr )
-		except OSError, x:
+		except OSError as x:
 			if x.errno != 10:
 				raise x
 			print('OSError ignored on command: {0}'.format(command_string))
@@ -31,18 +31,19 @@ class idSetupBase:
 	
 	def SimpleCommand( self, cmd ):
 		print(cmd)
-		ret = commands.getstatusoutput( cmd )
-		if ( len( ret[ 1 ] ) ):
-			sys.stdout.write( ret[ 1 ] )
+		ret = subprocess.check_output( cmd )
+		if ( len( ret ) ):
+			sys.stdout.write( ret.decode('utf-8') )
 			sys.stdout.write( '\n' )
-		if ( ret[ 0 ] != 0 ):
-			raise 'command failed'
-		return ret[ 1 ]
+		return ret.decode('utf-8')
 
 	def TrySimpleCommand( self, cmd ):
 		print(cmd)
-		ret = commands.getstatusoutput( cmd )
-		sys.stdout.write( ret[ 1 ] )
+		try:
+			ret = subprocess.check_output( cmd )
+		except:
+			pass
+		sys.stdout.write( ret.decode('utf-8') )
 
 	def M4Processing( self, file, d ):
 		file_out = file[:-3]
@@ -95,20 +96,21 @@ class idSetupBase:
 		f = open( 'framework/BuildVersion.h' )
 		l = f.readlines()[ 4 ]
 		f.close()
-		pat = re.compile( '.* = (.*);\n' )
-		return pat.split( l )[ 1 ]
+		return re.split( '.* = (.*);\n', l )[ 1 ]
 
 def checkLDD( target, source, env ):
 	file = target[0]
 	if (not os.path.isfile(file.abspath)):
 		print('ERROR: CheckLDD: target {0} not found\n'.format(target[0]))
 		Exit(1)
-	( status, output ) = commands.getstatusoutput( 'ldd -r %s' % file )
-	if ( status != 0 ):
-		print('ERROR: ldd command returned with exit code {0}'.format(ldd_ret))
+	try:
+		output = subprocess.check_output( 'ldd -r %s' % file )
+		output = output.decode('utf-8')
+	except CalledProcessError as e:
+		print('ERROR: ldd command returned with exit code {0}: {1}'.format(e.returncode, e.output))
 		os.system( 'rm %s' % target[ 0 ] )
 		sys.exit(1)
-	lines = string.split( output, '\n' )
+	lines = output.split( '\n' )
 	have_undef = 0
 	for i_line in lines:
 		#print(repr(i_line))
@@ -171,7 +173,7 @@ def SetupUtils( env ):
 	env.BuildSetup = NotImplementedStub
 
 def BuildList( s_prefix, s_string ):
-	s_list = string.split( s_string )
+	s_list = s_string.split( )
 	for i in range( len( s_list ) ):
 		s_list[i] = os.path.join(s_prefix, s_list[i])
 	return s_list
diff --git a/tdm_update/SConscript.libtdm_update b/tdm_update/SConscript.libtdm_update
index 8143c41..8a038fd 100644
--- a/tdm_update/SConscript.libtdm_update
+++ b/tdm_update/SConscript.libtdm_update
@@ -19,7 +19,7 @@
 import string
 
 def BuildList( s_prefix, s_string ):
-	s_list = string.split( s_string )
+	s_list = s_string.split( )
 	for i in range( len( s_list ) ):
 		s_list[ i ] = s_prefix + '/' + s_list[ i ]
 	return s_list
diff --git a/tdm_update/SConscript.minizip b/tdm_update/SConscript.minizip
index 9a0cdb3..04f95f5 100644
--- a/tdm_update/SConscript.minizip
+++ b/tdm_update/SConscript.minizip
@@ -19,7 +19,7 @@
 import string
 
 def BuildList( s_prefix, s_string ):
-	s_list = string.split( s_string )
+	s_list = s_string.split( )
 	for i in range( len( s_list ) ):
 		s_list[ i ] = s_prefix + '/' + s_list[ i ]
 	return s_list
diff --git a/tdm_update/SConscript.tdm_update b/tdm_update/SConscript.tdm_update
index 92ffbdd..3c39073 100644
--- a/tdm_update/SConscript.tdm_update
+++ b/tdm_update/SConscript.tdm_update
@@ -31,7 +31,7 @@ if ( g_os == 'Linux' ):
 	g_env.Append( CPPFLAGS = '-D_GLIBCXX_USE_CXX11_ABI=0' )
 
 def BuildList( s_prefix, s_string ):
-	s_list = string.split( s_string )
+	s_list = s_string.split( )
 	for i in range( len( s_list ) ):
 		s_list[ i ] = s_prefix + '/' + s_list[ i ]
 	return s_list
diff --git a/tdm_update/SConstruct b/tdm_update/SConstruct
index 48ca68d..853af53 100644
--- a/tdm_update/SConstruct
+++ b/tdm_update/SConstruct
@@ -20,7 +20,7 @@
 # Based on id's game sconscript
 # Author: greebo
 
-import sys, os, time, commands, re, pickle, StringIO, commands, pdb, string
+import sys, os, time, re, pickle, subprocess, pdb, string
 import SCons
 
 # choose configuration variables which should be saved between runs
@@ -72,12 +72,11 @@ EnsureSConsVersion( 0, 98 )
 # system detection -------------------------------
 
 # CPU type
-cpu = commands.getoutput('uname -m')
-exp = re.compile('.*i?86.*')
-if exp.match(cpu):
+cpu = subprocess.check_output(["uname", "-m"])
+if re.match(b'.*i?86.*', cpu):
 	cpu = 'x86'
 else:
-	cpu = commands.getoutput('uname -p')
+	cpu = subprocess.check_output(["uname", "-p"])
 	if ( cpu == 'powerpc' ):
 		cpu = 'ppc'
 	else:
-- 
2.17.0

