Skip to content

Commit

Permalink
configure.py: Use colors for terminal output
Browse files Browse the repository at this point in the history
- The control characters for the colored text will only be used if
the output is being sent to a terminal
- Error messages have been consolidated into a single function
  • Loading branch information
robxnano authored and galad87 committed Jul 20, 2023
1 parent c7946c4 commit e3c0c19
Showing 1 changed file with 51 additions and 36 deletions.
87 changes: 51 additions & 36 deletions make/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ def _final_dir( self, chdir, dir ):
return dir

## output functions
def errln( self, format, *args ):
s = (format % args)
if re.match( '^.*[!?:;.]$', s ):
stderr.write( 'ERROR: %s configure stop.\n' % (s) )
else:
stderr.write( 'ERROR: %s; configure stop.\n' % (s) )
self.record_log()
sys.exit( 1 )
def infof( self, format, *args ):
line = format % args
self._log_verbose.append( line )
Expand Down Expand Up @@ -107,12 +99,12 @@ def doc_ready( self ):
## perform chdir and enable log recording
def chdir( self ):
if os.path.abspath( self.build_dir ) == os.path.abspath( self.src_dir ):
self.errln( 'build (scratch) directory must not be the same as top-level source root!' )
raise AbortError( 'build (scratch) directory must not be the same as top-level source root!' )

if self.build_dir != os.curdir:
if os.path.exists( self.build_dir ):
if not options.force:
self.errln( 'build directory already exists: %s (use --force to overwrite)', self.build_dir )
raise AbortError( 'build directory already exists: %s (use --force to overwrite)', self.build_dir )
else:
self.mkdirs( self.build_dir )
self.infof( 'chdir: %s\n', self.build_dir )
Expand All @@ -139,19 +131,22 @@ def open( self, *args ):
try:
return open( *args )
except Exception as x:
self.errln( 'open failure: %s', x )
raise AbortError( 'open failure: %s', x )

def record_log( self ):
if not self._record:
return
regex = re.compile( r'\x1b\[[0-9A-Fa-f]*m' )
self._record = False
self.verbose = Configure.OUT_QUIET
log_info_file = self.open( 'log/config.info.txt', 'w' )
for line in self._log_info:
line = regex.sub( '', line )
log_info_file.write( line )
log_info_file.close()
log_verbose_file = self.open( 'log/config.verbose.txt', 'w' )
for line in self._log_verbose:
line = regex.sub( '', line )
log_verbose_file.write( line )
log_verbose_file.close()

Expand Down Expand Up @@ -239,8 +234,8 @@ def __init__( self, category, pretext='unknown', abort=False, head=False ):

self.run_done = False
self.fail = True
self.msg_fail = 'fail'
self.msg_pass = 'pass'
self.msg_fail = print_red('fail')
self.msg_pass = print_green('pass')
self.msg_end = 'end'

def _actionBegin( self ):
Expand All @@ -251,7 +246,7 @@ def _actionEnd( self ):
cfg.infof( '(%s) %s\n', self.msg_fail, self.msg_end )
if self.abort:
self._dumpSession( cfg.infof )
cfg.errln( 'unable to continue' )
raise AbortError( 'configure is unable to continue.' )
self._dumpSession( cfg.verbosef )
self._failSession()
else:
Expand Down Expand Up @@ -823,11 +818,10 @@ def __init__( self ):
# Find script that creates repo info
try:
repo_info = os.path.join( cfg.src_dir, 'scripts', 'repo-info.sh' )
if not os.path.isfile( repo_info ):
cfg.errln( 'Missing required script %s\n', repo_info )
sys.exit( 1 )
except:
sys.exit( 1 )
raise AbortError( 'Missing required script repo-info.sh')
if not os.path.isfile( repo_info ):
raise AbortError( 'Missing required script %s', repo_info )

super( RepoProbe, self ).__init__( 'repo info', '%s %s' %
(repo_info, cfg.src_dir) )
Expand Down Expand Up @@ -919,12 +913,12 @@ def _failSession( self ):
if self.session:
self._parseSession()
if self.hash and self.hash != 'deadbeaf':
cfg.infof( '(pass)\n' )
cfg.infof( '(%s)\n' % print_green('pass'))
else:
cfg.infof( '(fail)\n' )
cfg.infof( '(%s)\n' % print_red('fail'))

except:
cfg.infof( '(fail)\n' )
cfg.infof( '(%s)\n' % print_red('fail'))

###############################################################################
##
Expand Down Expand Up @@ -964,14 +958,12 @@ def _action( self ):
url_arch = ''

if repo.date is None:
cfg.errln( '%s is missing version information it needs to build properly.\nClone the official git repository at %s\nor download an official source archive from %s\n', self.name, self.url_repo, self.url_website )
sys.exit( 1 )
raise AbortError( '%s is missing version information it needs to build properly.\nClone the official git repository at %s\nor download an official source archive from %s\n', self.name, self.url_repo, self.url_website )

if repo.tag != '':
m = re.match( '^([0-9]+)\.([0-9]+)\.([0-9]+)-?(.+)?$', repo.tag )
if not m:
cfg.errln( 'Invalid repo tag format %s\n', repo.tag )
sys.exit( 1 )
raise AbortError( 'Invalid repo tag format %s\n', repo.tag )
(vmajor, vminor, vpoint, suffix) = m.groups()
self.vmajor = int(vmajor)
self.vminor = int(vminor)
Expand Down Expand Up @@ -1275,12 +1267,12 @@ def write( self, type ):
os.remove( ftmp )
except Exception as x:
pass
cfg.errln( 'failed writing to %s\n%s', ftmp, x )
raise AbortError( 'failed writing to %s\n%s', ftmp, x )

try:
os.rename( ftmp, fname )
except Exception as x:
cfg.errln( 'failed writing to %s\n%s', fname, x )
raise AbortError( 'failed writing to %s\n%s', fname, x )

###############################################################################

Expand Down Expand Up @@ -1310,12 +1302,12 @@ def encodeDistfileConfig():
os.remove( ftmp )
except Exception as x:
pass
cfg.errln( 'failed writing to %s\n%s', ftmp, x )
raise AbortError( 'failed writing to %s\n%s', ftmp, x )

try:
os.rename( ftmp, fname )
except Exception as x:
cfg.errln( 'failed writing to %s\n%s', fname, x )
raise AbortError( 'failed writing to %s\n%s', fname, x )

###############################################################################
##
Expand Down Expand Up @@ -1508,7 +1500,7 @@ def __init__( self, targets ):
try:
pipe = subprocess.Popen( cmd, shell=True, bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
except Exception as x:
cfg.errln( 'launch failure: %s', x )
raise AbortError( 'launch failure: %s', x )
for line in pipe.stdout:
if not isinstance(line, str):
line = line.decode()
Expand All @@ -1520,9 +1512,9 @@ def __init__( self, targets ):
elapsed = timeEnd - timeBegin

if pipe.returncode:
result = 'FAILURE (code %d)' % pipe.returncode
result = '%s (code %d)' % (print_red('FAILURE'), pipe.returncode)
else:
result = 'SUCCESS'
result = print_green('SUCCESS')

## present duration in decent format
seconds = elapsed
Expand Down Expand Up @@ -1570,6 +1562,30 @@ def infof( self, format, *args ):
self._file.write( line )
cfg.infof( '%s', line )

###############################################################################
##
## Functions for color terminal output
##
def print_color(text: string, color: int) -> string:
if os.environ.get('CLICOLOR_FORCE') or \
(os.isatty(sys.stdout.fileno()) and os.isatty(sys.stderr.fileno()) and os.environ.get('TERM') != 'dumb'):
output = ('\x1b[%xm\x1b[1m%s\x1b[0m' % (color, text))
else:
output = text
return output

def print_bold(text: string) -> string:
return print_color(text, 0)

def print_red(text: string) -> string:
return print_color(text, 0x31)

def print_green(text: string) -> string:
return print_color(text, 0x32)

def print_blue(text: string) -> string:
return print_color(text, 0x34)

###############################################################################
##
## main program
Expand Down Expand Up @@ -2210,7 +2226,6 @@ class Tools:
stdout.write( ' (%s)\n' % note_unsupported ) if not (host_tuple.system == 'linux' or host_tuple.match( 'x86_64-w64-mingw32*' )) else stdout.write( '\n' )
stdout.write( 'Enable libdovi: %s\n' % options.enable_libdovi )


if options.launch:
stdout.write( '%s\n' % ('-' * 79) )
Launcher( targets )
Expand All @@ -2224,20 +2239,20 @@ class Tools:

stdout.write( '%s\n' % ('-' * 79) )
if options.launch:
stdout.write( 'Build is finished!\n' )
stdout.write( print_bold( 'Build is finished!\n' ) )
if nocd:
stdout.write( 'You may now examine the output.\n' )
else:
stdout.write( 'You may now cd into %s and examine the output.\n' % (cfg.build_dir) )
else:
stdout.write( 'Build is configured!\n' )
stdout.write( print_bold( 'Build is configured!\n' ) )
if nocd:
stdout.write( 'You may now run make (%s).\n' % (Tools.gmake.pathname) )
else:
stdout.write( 'You may now cd into %s and run make (%s).\n' % (cfg.build_dir,Tools.gmake.pathname) )

except AbortError as x:
stderr.write( 'ERROR: %s\n' % (x) )
stderr.write( '\n%s\n\n' % print_red( 'ERROR: %s' % x ) )
try:
cfg.record_log()
except:
Expand Down

0 comments on commit e3c0c19

Please sign in to comment.