22# -*- python -*-
33#BEGIN_LEGAL
44#
5- #Copyright (c) 2022 Intel Corporation
5+ #Copyright (c) 2024 Intel Corporation
66#
77# Licensed under the Apache License, Version 2.0 (the "License");
88# you may not use this file except in compliance with the License.
2525import locale
2626
2727_MBUILD_ENCODING = locale .getpreferredencoding ()
28- def unicode_encoding ():
28+ def unicode_encoding () -> str :
2929 return _MBUILD_ENCODING
3030
3131PY3 = sys .version_info > (3 ,)
3232def is_python3 ():
33- global PY3
3433 return PY3
3534
3635_mbuild_verbose_level = 1
37- def verbose (level = 0 ):
36+ def verbose (level = 0 ) -> bool :
3837 """Return True if the configured message level supplied is >= the
3938 level arguement
4039 @param level: int
@@ -57,30 +56,26 @@ def get_verbosity():
5756 global _mbuild_verbose_level
5857 return _mbuild_verbose_level
5958
60- def bracket (s ,m = '' ):
59+ def bracket (s ,m = '' ) -> str :
6160 """add a bracket around s and append m.
6261 @rtype: string
6362 @return: a bracketed string s and a suffixed message m
6463 """
65- n = convert2unicode (m )
66- return u'[{}] {}' .format (s ,n )
64+ return u'[{}] {}' .format (s ,m )
6765
6866def error_msg (s ,t ):
6967 """Emit '[s] t' to stderr with a newline"""
70- sys . stderr . write ( u2output ( bracket (s ,t ) + " \n " ))
68+ print ( bracket (s ,t ))
7169
7270def msg (s , pad = '' ):
7371 """Emit s to stdout with a newline"""
74- # someone could pass unicode as pad...
75- sys .stdout .write (u2output (pad ))
76- sys .stdout .write (u2output (s ))
77- sys .stdout .write ("\n " )
72+ print (pad , end = '' )
73+ print (s )
7874
7975def msgn (s , pad = '' ):
8076 """Emit s to stdout without a newline"""
81- # someone could pass unicode as pad...
82- sys .stdout .write (u2output (pad ))
83- sys .stdout .write (u2output (s ))
77+ print (pad , end = '' )
78+ print (s , end = '' )
8479
8580def msgb (s ,t = '' ,pad = '' ):
8681 """a bracketed string s sent to stdout, followed by a string t"""
@@ -170,12 +165,12 @@ def check_python_version(maj,minor,fix=0):
170165
171166
172167try :
173- if check_python_version (2 , 7 ) == False :
174- die ("MBUILD error: Need Python version 2.7 or later." )
168+ if check_python_version (3 , 8 ) == False :
169+ die ("MBUILD error: Need Python version 3.8 or later." )
175170except :
176- die ("MBUILD error: Need Python version 2.7 or later." )
171+ die ("MBUILD error: Need Python version 3.8 or later." )
177172
178- import platform # requires python 2.3
173+ import platform
179174_on_mac = False
180175_on_native_windows = False
181176_on_windows = False # cygwin or native windows
@@ -227,28 +222,9 @@ def on_mac():
227222
228223######
229224
230- # UNICODE SUPPORT FEATURES for PY2/PY3 co-existence
231-
232-
233- # unicode string constructors
234- if PY3 :
235- ustr = str
236- else :
237- ustr = unicode # converts its argument to a unicode object
238-
239- # binary data strings constructors
240- if PY3 :
241- bstr = bytes
242- else :
243- bstr = str
244-
245- def unicode2bytes (us ):
246- """convert a unicode object (unicode type in python2 or string type in
247- python3) to bytes suitable for writing to a file."""
248- return us .encode (unicode_encoding ())
249225
250226def bytes2unicode (bs ):
251- """Convert a bytes object or a python2 string to unicode"""
227+ """Convert a bytes object to unicode"""
252228 return bs .decode (unicode_encoding ())
253229
254230def ensure_string (x ):
@@ -269,42 +245,8 @@ def ensure_string(x):
269245def uappend (lst ,s ):
270246 """Make sure s is unicode before adding it to the list lst"""
271247 lst .append (ensure_string (s ))
272-
273- def u2output (s ):
274- """encode unicode string for output to stderr/stdout, but leave bytes
275- strings as is. Python3 can print unicode to stdout/stderr if
276- the locale (LANG env var, etc.) supports it. """
277- # we don't want to call encode for non-unicode (bytes) strings
278- # because that can generate *decode* errors. In python3 we are set
279- # since all strings are unicode and thus it is always safe to call
280- # encode on them. In python2 we must see if the string is unicode
281- # or bytes.
282-
283- # python3 does not allow bytes objects as arguments to
284- # sys.stdout.write() so we just leave stuff as unicode strings in
285- # python3. If LANG is not C, that works. If LANG is C, wait for
286- # python 3.7 in mid June 2018. Or just do not use LANG = C!
287- global PY3
288- if not PY3 :
289- if isinstance (s ,unicode ):
290- return unicode2bytes (s )
291- return s
292-
293- def uprint (s ):
294- """encode unicode for output and print"""
295- t = u2output (s )
296- print (t )
297248
298249def is_stringish (x ):
299- global PY3
300250 if isinstance (x ,bytes ) or isinstance (x ,str ):
301251 return True
302- # python2 has a type unicode, which does not exist by default in
303- # python3.
304- if not PY3 :
305- return isinstance (x ,unicode )
306252 return False
307-
308- def convert2unicode (x ):
309- """convert an arbitrary x to a unicode string"""
310- return ustr (x )
0 commit comments